Chinaunix首页 | 论坛 | 博客
  • 博客访问: 73706
  • 博文数量: 35
  • 博客积分: 1420
  • 博客等级: 上尉
  • 技术积分: 306
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-15 13:33
个人简介

自强不息!

文章分类

全部博文(35)

文章存档

2020年(1)

2017年(1)

2011年(3)

2010年(15)

2009年(2)

2008年(2)

2007年(1)

2006年(10)

我的朋友

分类: C/C++

2007-11-19 13:08:42

这是一个ring buffer 测试程序,希望对需要的人有帮助,这个程序在VC下测试通过。
以下是程序的源代码:
ring.c
-------------------------------------------------------------------------------

#include "ring.h"
int ring_debug=6;
#ifdef TEST
#define printk printf
#include
#include
//#include
#endif
void 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 the calling routine if necessary).
* If you request to read more bytes than are currently
* available, 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 the calling routine if necessary).
* If you request to write more bytes than are currently
* available, 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 TEST

ring_buffer_t buffer;
main()
{
char c;
char c2;
int child;
int rc;
int i;
int j;
int h;
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++) {
    for (j=0;j<46;j++)
    {
rc=ring_buffer_write(&buffer, &c, 1);
c++;
    }
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




头文件ring.h
------------------------------------------------------------------------------
#define RING_SIGNATURE 0x175DE210
#define TEST
#define PARANOID
typedef struct {
long signature;
unsigned char *head_p;
unsigned char *tail_p;
unsigned char *end_p; /* end + 1 */
unsigned char *begin_p;
unsigned char buffer[32768];
} ring_buffer_t;
extern void ring_buffer_init(ring_buffer_t *ring);
extern int ring_buffer_read(ring_buffer_t *ring,
unsigned char *buf, int count);
extern int ring_buffer_write(ring_buffer_t *ring,
unsigned char *buf, int count);
阅读(712) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~