#include \"ring.h\" int tag=0;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((tmpbegin)||(tmp>round->end)){ printk(\"ring_buf_read:tail bad/n\"); return(-1); }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((tmpbegin)||(tmp>round->end)){ printk(\"ring_buf_write:head bad/n\"); return(-1); } if(tag) return 0; (*tmp)=val; tmp++;if(tmp>round->end) tmp = round->begin; if(tmp==round->tail){ tag=1;
psudo_head=tmp; return(1); }
round->head=tmp; return(1); }