-
2007-10-11
Congratulations..milestone
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://rockliao.blogbus.com/logs/10240925.html
Why I update my blog at this special time? That's because I just finished a very important work which can be called as a milestone. Although there was much unhappiness yestoday night, my temper is much better now. I find that working is one of the best regulator for the bad temper. What's the others ? Ask yourself...
OK, in this chapter, let's go over the "data structure". I happened to find a very good use of "ring buffer" in our code, so just take out to share with you.
First, ring buffer is used to cache the data and can be reused. Second, ring buffer can not be loaded completely full, it must leave one node as a symbol of full. Very foundational..
In our code, we use a malloc ring buffer between two tasks to help the data transfer. We use the mssage queue to send the "pointer" which point to the buffer address and the receiver can just use the pointer to read the data from the ring buffer.That's very efficient !
However,I have to list the code of the ring buffer:
int InitRingQueue(SqQueue_t *Q)
{
Q->base = (QElemType *)malloc(sizeof(QElemType) * MAXRINGQSIZE);
if(!Q->base)
{
printf("malloc failed\r\n");
}
Q->next = Q->prior = 0;
return 0;
}int WriteRingQueue(SqQueue_t *Q,QElemType e)
{
if((Q->prior +1)%MAXRINGQSIZE == Q->next)
{
//printf("out of queue: %d\r\n",Q.prior);
return -1;
}
Q->base[Q->prior] = e;
Q->prior = (Q->prior + 1) % MAXRINGQSIZE;
return 0;
}int ReadRingQueue(SqQueue_t *Q,QElemType **e)
{
if(Q->next == Q->prior)
{
/*printf("write is equal to read\r\n");*/
return -1;
}
*e = &Q->base[Q->next];
Q->next = (Q->next + 1) % MAXRINGQSIZE;
return 0;
}
收藏到:Del.icio.us








评论