annotate TOOLS/buffer.c @ 2160:81f16b270b8b

Enable discards - now it works
author nick
date Thu, 11 Oct 2001 06:24:17 +0000
parents 064a6422fd42
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
968
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
1
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
2 // General purpose Ring-buffering routines
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
3
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
4 #define BUFFSIZE (1024)
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
5 #define NUM_BUFS (64)
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
6
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
7 static unsigned char *buffer[NUM_BUFS];
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
8
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
9 static unsigned int buf_read=0;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
10 static unsigned int buf_write=0;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
11 static unsigned int buf_read_pos=0;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
12 static unsigned int buf_write_pos=0;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
13
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
14 static int full_buffers=0;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
15 static int buffered_bytes=0;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
16
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
17 static int write_buffer(unsigned char* data,int len){
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
18 int len2=0;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
19 int x;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
20 while(len>0){
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
21 if(full_buffers==NUM_BUFS) break;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
22 x=BUFFSIZE-buf_write_pos;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
23 if(x>len) x=len;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
24 memcpy(buffer[buf_write]+buf_write_pos,data+len2,x);
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
25 len2+=x; len-=x;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
26 buffered_bytes+=x; buf_write_pos+=x;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
27 if(buf_write_pos>=BUFFSIZE){
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
28 // block is full, find next!
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
29 buf_write=(buf_write+1)%NUM_BUFS;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
30 ++full_buffers;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
31 buf_write_pos=0;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
32 }
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
33 }
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
34 return len2;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
35 }
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
36
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
37 static int read_buffer(unsigned char* data,int len){
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
38 int len2=0;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
39 int x;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
40 while(len>0){
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
41 if(full_buffers==0) break; // no more data buffered!
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
42 x=BUFFSIZE-buf_read_pos;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
43 if(x>len) x=len;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
44 memcpy(data+len2,buffer[buf_read]+buf_read_pos,x);
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
45 len2+=x; len-=x;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
46 buffered_bytes-=x; buf_read_pos+=x;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
47 if(buf_read_pos>=BUFFSIZE){
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
48 // block is empty, find next!
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
49 buf_read=(buf_read+1)%NUM_BUFS;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
50 --full_buffers;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
51 buf_read_pos=0;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
52 }
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
53 }
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
54 return len2;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
55 }
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
56
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
57 static int get_space(){
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
58 return (NUM_BUFS-full_buffers)*BUFFSIZE - buf_write_pos;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
59 }
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
60
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
61 static int get_delay(){
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
62 return buffered_bytes;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
63 }
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
64
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
65 int main(int argc,char* argv[]){
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
66
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
67 int i;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
68
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
69 for(i=0;i<NUM_BUFS;i++) buffer[i]=malloc(BUFFSIZE);
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
70
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
71 return 0;
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
72 }
064a6422fd42 ring buffering example
arpi_esp
parents:
diff changeset
73