ios TPCircularBuffer 的使用

来源:互联网 发布:淘宝汽车服务 编辑:程序博客网 时间:2024/05/23 12:43

https://github.com/michaeltyson/TPCircularBuffer

这个环形buffer还不错,就是没有使用的说明:

1. 加入

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

-(BOOL) Init_buffer:(TPCircularBuffer*)buffer_ :size_
{
     return TPCircularBufferInit( buffer_, size_ );
}

-(void) Destory_buffer:(TPCircularBuffer*)buffer_
{
    TPCircularBufferCleanup( buffer_ );
}

-(UInt32)addData:(TPCircularBuffer*)buffer_ :(void *)buf_ :(UInt32)size_
{
    int32_t availableBytes = 0;
    TPCircularBufferHead(buffer_, &availableBytes);
    if (availableBytes <= 0)
          return 0;
    
    UInt32 len =  (availableBytes >= size_ ? size_ : availableBytes);
    TPCircularBufferProduceBytes(buffer_, (void*)buf_, size_);
    return len;
}

-(UInt32)getData:(TPCircularBuffer*)buffer_ :(void *)buf_ :(UInt32)size_
{
    int32_t availableBytes = 0;
    void *bufferTail = TPCircularBufferTail(buffer_, &availableBytes);
    if (availableBytes >= size_)
    {
        UInt32 len = 0;
        len = (size_ > availableBytes ? availableBytes : size_);
        memcpy(buf_, bufferTail, len);
        TPCircularBufferConsume(buffer_, len);
        NSLog(@"len = %ld", len);
        return len;
    }
    return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


2. 定义对象

    // 生成一个环形队列实例
    TPCircularBuffer buffer;
    TPCircularBuffer buffer2;


3.

 // 初始化队列
 [self Init_buffer:&buffer : BUF_SIZE];

4.

    Uint8 pread[4096];
    memset( pread, 0, sizeof( pread ) * sizeof(Uint8) );
    // fill buffer............ to pread.......

    [self addData:&buffer :pread :size];

5.

       Uint8 pw[4096];
        memset( pw, 0, sizeof( pw ) * sizeof(Uint8) );
 // get data from pw.....

UInt32 buf_size = [self getData: &buffer :pw : sizeof( pw )];   


6.

    TPCircularBufferCleanup(&buffer);

结束。。。。

0 0