forked from cloudwu/mread
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestringbuffer.c
80 lines (67 loc) · 1.48 KB
/
testringbuffer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "ringbuffer.h"
#include <stdio.h>
static void
init(struct ringbuffer_block * blk, int n) {
char * ptr = (char *)(blk+1);
int i;
for (i=0;i<n;i++) {
ptr[i] = i + 1;
}
}
static void
dump(struct ringbuffer * rb, struct ringbuffer_block *blk, int size) {
void * buffer;
int sz = ringbuffer_data(rb, blk, size, 0, &buffer);
char * data = buffer;
if (data) {
int i;
for (i=0;i<sz;i++) {
printf("%d ",data[i]);
}
printf("\n");
} else {
printf("size = %d\n",sz);
}
}
static void
test(struct ringbuffer *rb) {
struct ringbuffer_block * blk;
blk = ringbuffer_alloc(rb,80);
blk->id = 0;
ringbuffer_free(rb,blk);
blk = ringbuffer_alloc(rb,50);
blk->id = 1;
struct ringbuffer_block * next = ringbuffer_alloc(rb, 40);
next->id = 1;
ringbuffer_link(rb, blk, next);
ringbuffer_dump(rb);
blk = ringbuffer_alloc(rb,4);
printf("%p\n",blk);
int id = ringbuffer_collect(rb);
printf("collect %d\n",id);
blk = ringbuffer_alloc(rb,4);
blk->id = 2;
init(blk,4);
next = ringbuffer_alloc(rb,5);
init(next,5);
ringbuffer_link(rb, blk, next);
next = ringbuffer_alloc(rb,6);
init(next,6);
ringbuffer_link(rb, blk , next);
dump(rb, blk , 3);
dump(rb, blk , 6);
dump(rb, blk , 16);
blk = ringbuffer_yield(rb, blk, 5);
next = ringbuffer_alloc(rb, 7);
ringbuffer_copy(rb, blk, 1, next);
dump(rb, next, 7);
blk = ringbuffer_yield(rb, blk , 5);
ringbuffer_dump(rb);
}
int
main() {
struct ringbuffer * rb = ringbuffer_new(128);
test(rb);
ringbuffer_delete(rb);
return 0;
}