一个简单高效的循环缓冲区的管理代码

一个简单高效的循环缓冲区的管理代码,与平台无关,可以方便在各种场合使用。

-A simple and efficient management of the cycle of the buffer zone code, and platform-independent, it will be convenient to use on various occasions.


#ifndef LOOP_INCLUDED 
#define LOOP_INCLUDED 
 
#ifdef __cplusplus 
extern "C" { 
#endif 
 
struct LoopBuffCtl{ 
    unsigned char * buffer; 
    unsigned int size; 
    unsigned int reader; 
    unsigned int writer; 
}; 
 
void LoopBuffInit( struct LoopBuffCtl * pLoopCtl, unsigned int size, unsigned char * buffer ); 
unsigned short LoopBuffDataLength( struct LoopBuffCtl * pLoopCtl ); 
unsigned short LoopBuffFreeSpace( struct LoopBuffCtl * pLoopCtl ); 
unsigned short LoopBuffWrite( struct LoopBuffCtl *, unsigned char * , unsigned short ); 
unsigned short LoopBuffRead( struct LoopBuffCtl *, unsigned char *, unsigned short ); 
 
#ifdef __cplusplus 
} 
#endif 
#endif 

#include "LoopBuff.h"   
   
#define min(a,b)  ((a)>(b)) ? (b) : (a)   
   
unsigned short LoopBuffFreeSpace( struct LoopBuffCtl * pLoopCtl )   
{   
    return ( pLoopCtl->reader + pLoopCtl->size - pLoopCtl->writer - 1 ) & ( pLoopCtl->size - 1 );   
}   
   
unsigned short LoopBuffDataLength( struct LoopBuffCtl * pLoopCtl )   
{   
    return ( pLoopCtl->writer + pLoopCtl->size - pLoopCtl->reader ) & ( pLoopCtl->size - 1 );   
}   
   
void LoopBuffInit( struct LoopBuffCtl * pLoopCtl, unsigned int size, unsigned char * buffer )   
{   
    pLoopCtl->reader = pLoopCtl->writer = 0;   
    pLoopCtl->size = size;   
    pLoopCtl->buffer = buffer;   
}   
   
unsigned short LoopBuffWrite( struct LoopBuffCtl * pLoopCtl, unsigned char * buffer , unsigned short len )   
{   
    unsigned short l;   
   
    len = min( len, pLoopCtl->size - ( pLoopCtl->writer - pLoopCtl->reader ));   
    l = min( len, pLoopCtl->size - ( pLoopCtl->writer & ( pLoopCtl->size - 1 )));   
    memcpy( pLoopCtl->buffer + ( pLoopCtl->writer & ( pLoopCtl->size - 1 )), buffer, l );   
    memcpy( pLoopCtl->buffer, buffer + l, len - l );   
    pLoopCtl->writer += len;   
    return len;   
}   
   
unsigned short LoopBuffRead( struct LoopBuffCtl * pLoopCtl, unsigned char * buffer , unsigned short len )   
{   
    unsigned short l;   
   
    len = min( len, pLoopCtl->reader - pLoopCtl->writer );   
   
    l = min( len, pLoopCtl->size - ( pLoopCtl->reader & ( pLoopCtl->size - 1 )));   
    if( buffer != 0 )   
    {   
        memcpy( buffer, pLoopCtl->buffer + ( pLoopCtl->reader & ( pLoopCtl->size - 1 )), l );   
        memcpy( buffer + l, pLoopCtl->buffer, len - l );       
    }   
    pLoopCtl->reader += len;       
    return len;   
}   


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
缓冲区代码 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "stream.h" int quit; #define DELAY_MAX 5000 //最大延时5000毫秒 #define IN_NUM (2) #define MAIN_STREAM (0) #define SLAVE_STREAM (1) void Init_Interface(int Msg_id) { int qid; if(ApproDrvInit(Msg_id)) exit(1); if (func_get_mem(&qid)) { ApproDrvExit(); exit(1); } } void Clean_Interface(void) { ApproInterfaceExit(); } static int alarm_rec_enable; static unsigned int deltime; static unsigned int GetTimeStamp(void) { struct timeval timeval; gettimeofday(&timeval, NULL); return (timeval.tv_sec * 1000) + (timeval.tv_usec + 500) / 1000;; } void *get_frame(void *arg) { FILE *filefd=NULL; char filename[80]; int ch=0; stream_rpos_t rpos; frame_head_t frame_head; int first_frame_type=0; int first_frame=0; char *frame_pos; char *data; unsigned int cur_time; printf("get ch %d stream\n", ch); memset(&rpos, 0, sizeof(rpos)); st_stream_getStartPos(ch, MAIN_STREAM, &rpos); st_stream_getOneFrame(ch, MAIN_STREAM, 10, &rpos); while(!quit) { if(alarm_rec_enable) { if(st_stream_getOneFrame(ch, MAIN_STREAM, 200, &rpos) <= 0) { usleep(10*1000); continue; } frame_pos = &rpos.p_buf_data[rpos.data_start_pos]; memcpy(&frame_head,&rpos.p_buf_data[rpos.data_start_pos],sizeof(frame_head)); data=frame_pos+sizeof(frame_head_t

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值