#define R_SIGNATURE 0x860510 typedef struct{ long signature; int *head; int *tail; int *end; int *begin; int buffer[];
}ring_buf; //定义一个环形队列数据结构
#include int * psudo_head=NULL; void ring_buf_init(ring_buf *round) { round->signature=R_SIGNATURE; round->head=round->buffer; round->tail=round->buffer; round->begin=round->buffer; round->end=&round->buffer[sizeof(round->buffer)-1]; } /** read the user_defined value from the ring_buffer, ** if no value available 0 is returned, on error -1 is ** returned, on success 1 is returned **/ int ring_buf_read(ring_buf *round,int *val) { int* tmp=round->tail;//每次调用该函数时都会将round->tail指针的值赋给tmp,因为round->tail始终指向环形队列中未读数据的地址,这是下面round->tail=tmp语句来实现的 if(round->signature !=R_SIGNATURE){ printk(\"ring_buf_read:signature fake/n\"); return(-1); } if((tmp if(tmp==round->head){ return(0); } *val=*tmp++;//将*tmp指向的值赋给val,实现数据的读出操作 if(tmp>round->end) tmp=round->begin; round->tail=tmp;//保证round->tail始终指向环形队列中未读数据的地址,从而实现循环读取 if(tag){ round->head=psudo_head; tag=0; } return(1); } int ring_buf_write(ring_buf *round,int val) { int* tmp=round->head; if(round->signature !=R_SIGNATURE){ printk(\"ring_buf_write:signature fake/n\"); return(-1); } if((tmp if(tmp>round->end) tmp = round->begin; if(tmp==round->tail){ tag=1; psudo_head=tmp; return(1); } round->head=tmp; return(1); } 因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- efsc.cn 版权所有 赣ICP备2024042792号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务