📄 ring.c
字号:
/* Copyright 1998,1999, Mark Whitis <whitis@dbd.com> *//* http://www.freelabs.com/~whitis/ */#include "ring.h"int ring_debug=1;#ifdef TEST#define printk printf#include <stdlib.h>#include <stdio.h>#include <unistd.h>#endifvoid ring_buffer_init(ring_buffer_t *ring){ ring->signature=RING_SIGNATURE; ring->head_p=ring->buffer; ring->tail_p=ring->buffer; ring->begin_p=ring->buffer; ring->end_p=&ring->buffer[sizeof(ring->buffer)]; #if 0 strcpy(ring->buffer,"This is a test.\n"); ring->head_p +=16; #endif}/* returns number of bytes read *//* will not block (blocking will be handled by calling routine if *//* necessary). If you request to read more bytes than are currently *//* availible, it will return a count less than the value you passed in */int ring_buffer_read(ring_buffer_t *ring, unsigned char *buf, int count) { #ifdef PARANOID if(ring_debug>5) printk("das1600: ring_buffer_read(%08X,%08X,%d)\n",ring,buf,count); if(ring->signature != RING_SIGNATURE) { printk("ring_buffer_read: signature corrupt\n"); return(0); } if(ring->tail_p < ring->begin_p) { printk("ring_buffer_read: tail corrupt\n"); return(0); } if(ring->tail_p > ring->end_p) { printk("ring_buffer_read: tail corrupt\n"); return(0); } if(count != 1) { printk("ring_buffer_read: count must currently be 1\n"); return(0); } #endif; if(ring->tail_p == ring->end_p) { ring->tail_p = ring->begin_p; } if(ring->tail_p == ring->head_p) { if(ring_debug>5) printk("ring_buffer_read: buffer underflow\n"); return(0); } *buf = *ring->tail_p++; return(1);}/* returns number of bytes written *//* will not block (blocking will be handled by calling routine if *//* necessary). If you request to read more bytes than are currently *//* availible, it will return a count less than the value you passed in */int ring_buffer_write(ring_buffer_t *ring, unsigned char *buf, int count) { unsigned char *tail_p; #ifdef PARANOID if(ring->signature != RING_SIGNATURE) { printk("ring_buffer_write: signature corrupt\n"); return(0); } if(ring->head_p < ring->begin_p) { printk("ring_buffer_write: head corrupt\n"); return(0); } if(ring->head_p > ring->end_p) { printk("ring_buffer_write: head corrupt\n"); return(0); } if(count != 1) { printk("ring_buffer_write: count must currently be 1\n"); return(0); } #endif /* Copy tail_p to a local variable in case it changes between comparisons */ tail_p = ring->tail_p; if( (ring->head_p == (tail_p - 1) ) || ((ring->head_p == (ring->end_p - 1)) && (tail_p==ring->begin_p)) ) { if(ring_debug>5) printk("ring_buffer_write: buffer overflow\n"); return(0); } *ring->head_p++ = *buf; if(ring->head_p == ring->end_p ) { ring->head_p = ring->begin_p; } return(1);}#ifdef TESTring_buffer_t buffer;main(){ char c; char c2; int child; int rc; int i; int j; char lastread; int errors; int reads; int writes; ring_buffer_init(&buffer); c=0; lastread=-1; errors=0; reads=0; writes=0; for(j=0; j<50000; j++) { for(i=0; i<31; i++) { rc=ring_buffer_write(&buffer, &c, 1); writes++; if(ring_debug>2) printf("ring_buffer_write returned %d, was passed %d\n",rc,c); if(rc==1) c++; } for(i=0; i<47; i++) { rc=ring_buffer_read(&buffer, &c2, 1); reads++; if(ring_debug>2) printf("ring_buffer_read returned: rc=%d,c2=%d\n",rc,c2); if(rc==1) { if(c2!=(char)(lastread+1)) { printf("ERROR: expected %d, got %d\n",(char)(lastread+1),c2); errors++; } lastread=c2; } } } printf("number of errors=%d\n",errors); printf("number of reads=%d\n",reads); printf("number of writes=%d\n",writes);}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -