ios TPCircularBuffer 的使用

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
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值