环形缓冲C代码

在这里插入图片描述

typedef struct _buf_t{
	uint8_t  ready;
	int  len;
	char * buf;
    struct _buf_t * next;
} buf_t;

buf_t * buf_list = NULL;
/*
count块数量,len单块buf长度
*/
void init_buf_list(int count){

    buf_t * t = NULL;

    for(int i=0;i<count;i++){

    	if(t == NULL){
			t = (buf_t *)malloc(sizeof(struct _buf_t));
			buf_list = t;
		}
    	else {
    		t->next = (buf_t *)malloc(sizeof(struct _buf_t));
    		t = t->next;
    	}

    	t->ready = 0;
    	t->len = 256;
    	t->buf = malloc(t->len);

    	t->next = buf_list;
    }
}

static void read_task(void *arg)
{
	buf_t * t = buf_list;

	void(*read_func)(void *, int, size_t *)=arg;

    while(true){

		//wait buffer ready
		while(t->ready == 1){
			printf("drop\n");
		}

    	size_t bytes_read = 0;

    	if(read_func != NULL){
    		read_func(t->buf,
    				  t->len,     // the doc says bytes, but its elements.
    				  &bytes_read);
    	}

		t->ready = 1;

		t = t->next;
    }
}

static void wirte_task(void *arg)
{
	buf_t * t = buf_list;

	void(*write_func)(void *,int)=arg;

    while(true){

    	//wait buffer ready
		while(t->ready == 0);

		if(write_func != NULL)
			write_func(t->buf,t->len);

		t->ready = 0;

		t = t->next;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 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
发出的红包

打赏作者

楠木123456

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值