968
|
1
|
|
2 // General purpose Ring-buffering routines
|
|
3
|
|
4 #define BUFFSIZE (1024)
|
|
5 #define NUM_BUFS (64)
|
|
6
|
|
7 static unsigned char *buffer[NUM_BUFS];
|
|
8
|
|
9 static unsigned int buf_read=0;
|
|
10 static unsigned int buf_write=0;
|
|
11 static unsigned int buf_read_pos=0;
|
|
12 static unsigned int buf_write_pos=0;
|
|
13
|
|
14 static int full_buffers=0;
|
|
15 static int buffered_bytes=0;
|
|
16
|
|
17 static int write_buffer(unsigned char* data,int len){
|
|
18 int len2=0;
|
|
19 int x;
|
|
20 while(len>0){
|
|
21 if(full_buffers==NUM_BUFS) break;
|
|
22 x=BUFFSIZE-buf_write_pos;
|
|
23 if(x>len) x=len;
|
|
24 memcpy(buffer[buf_write]+buf_write_pos,data+len2,x);
|
|
25 len2+=x; len-=x;
|
|
26 buffered_bytes+=x; buf_write_pos+=x;
|
|
27 if(buf_write_pos>=BUFFSIZE){
|
|
28 // block is full, find next!
|
|
29 buf_write=(buf_write+1)%NUM_BUFS;
|
|
30 ++full_buffers;
|
|
31 buf_write_pos=0;
|
|
32 }
|
|
33 }
|
|
34 return len2;
|
|
35 }
|
|
36
|
|
37 static int read_buffer(unsigned char* data,int len){
|
|
38 int len2=0;
|
|
39 int x;
|
|
40 while(len>0){
|
|
41 if(full_buffers==0) break; // no more data buffered!
|
|
42 x=BUFFSIZE-buf_read_pos;
|
|
43 if(x>len) x=len;
|
|
44 memcpy(data+len2,buffer[buf_read]+buf_read_pos,x);
|
|
45 len2+=x; len-=x;
|
|
46 buffered_bytes-=x; buf_read_pos+=x;
|
|
47 if(buf_read_pos>=BUFFSIZE){
|
|
48 // block is empty, find next!
|
|
49 buf_read=(buf_read+1)%NUM_BUFS;
|
|
50 --full_buffers;
|
|
51 buf_read_pos=0;
|
|
52 }
|
|
53 }
|
|
54 return len2;
|
|
55 }
|
|
56
|
|
57 static int get_space(){
|
|
58 return (NUM_BUFS-full_buffers)*BUFFSIZE - buf_write_pos;
|
|
59 }
|
|
60
|
|
61 static int get_delay(){
|
|
62 return buffered_bytes;
|
|
63 }
|
|
64
|
|
65 int main(int argc,char* argv[]){
|
|
66
|
|
67 int i;
|
|
68
|
|
69 for(i=0;i<NUM_BUFS;i++) buffer[i]=malloc(BUFFSIZE);
|
|
70
|
|
71 return 0;
|
|
72 }
|
|
73
|