-
Notifications
You must be signed in to change notification settings - Fork 0
/
mrbuf_test.c
122 lines (98 loc) · 3.67 KB
/
mrbuf_test.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* mrbuffer.c magic ring buffer
*
* Copyright (C) 2016 Seongmyun Cho <highsky@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <string.h>
#include "mrbuffer.h"
#define LEN (1024*3-256)
char data[LEN];
int main(int argc, char *argv[]) {
struct mrbuffer *mrbuf;
size_t len;
int i;
for (i = 0; i < LEN; i++)
data[i] = i & 0xff ;
mrbuf = mrbuffer_alloc(MRBUF_FLAG_SHMAT, 0);
//mrbuf = mrbuffer_alloc(MRBUF_FLAG_MMAP, 0);
len = mrbuffer_write(mrbuf, LEN, data);
printf("h:%04lu t:%04lu %04lu\n", \
MRBUF_WRITE_OFFSET(mrbuf), \
MRBUF_READ_OFFSET(mrbuf), \
MRBUF_FREE_LEN(mrbuf));
len = mrbuffer_read(mrbuf, LEN, data);
len = mrbuffer_write(mrbuf, LEN, data);
printf("h:%04lu t:%04lu %04lu\n", \
MRBUF_WRITE_OFFSET(mrbuf), \
MRBUF_READ_OFFSET(mrbuf), \
MRBUF_FREE_LEN(mrbuf));
len = mrbuffer_read(mrbuf, LEN, data);
len = mrbuffer_write(mrbuf, LEN, data);
printf("h:%04lu t:%04lu %04lu\n", \
MRBUF_WRITE_OFFSET(mrbuf), \
MRBUF_READ_OFFSET(mrbuf), \
MRBUF_FREE_LEN(mrbuf));
len = mrbuffer_read(mrbuf, LEN, data);
len = mrbuffer_write(mrbuf, LEN, data);
printf("h:%04lu t:%04lu %04lu\n", \
MRBUF_WRITE_OFFSET(mrbuf), \
MRBUF_READ_OFFSET(mrbuf), \
MRBUF_FREE_LEN(mrbuf));
len = mrbuffer_read(mrbuf, LEN, data);
len = mrbuffer_write(mrbuf, LEN, data);
printf("h:%04lu t:%04lu %04lu\n", \
MRBUF_WRITE_OFFSET(mrbuf), \
MRBUF_READ_OFFSET(mrbuf), \
MRBUF_FREE_LEN(mrbuf));
len = mrbuffer_write(mrbuf, LEN, data);
printf("h:%04lu t:%04lu %04lu\n", \
MRBUF_WRITE_OFFSET(mrbuf), \
MRBUF_READ_OFFSET(mrbuf), \
MRBUF_FREE_LEN(mrbuf));
len = mrbuffer_write(mrbuf, LEN, data);
printf("h:%04lu t:%04lu %04lu\n", \
MRBUF_WRITE_OFFSET(mrbuf), \
MRBUF_READ_OFFSET(mrbuf), \
MRBUF_FREE_LEN(mrbuf));
if (MRBUF_FULL(mrbuf))
printf("buffer full(data len): %lu\n", MRBUF_DATA_LEN(mrbuf));
len = mrbuffer_read(mrbuf, LEN, data);
printf("h:%04lu t:%04lu %04lu\n", \
MRBUF_WRITE_OFFSET(mrbuf), MRBUF_READ_OFFSET(mrbuf), len);
len = mrbuffer_read(mrbuf, LEN, data);
printf("h:%04lu t:%04lu %04lu\n", \
MRBUF_WRITE_OFFSET(mrbuf), MRBUF_READ_OFFSET(mrbuf), len);
len = mrbuffer_read(mrbuf, LEN, data);
printf("h:%04lu t:%04lu %04lu\n", \
MRBUF_WRITE_OFFSET(mrbuf), MRBUF_READ_OFFSET(mrbuf), len);
if (MRBUF_EMPTY(mrbuf))
printf("buffer empty(free len): %lu\n", MRBUF_FREE_LEN(mrbuf));
if (MRBUF_FREE_LEN(mrbuf) >= LEN) {
printf("write mrbuf using memcpy\n");
memcpy(MRBUF_WRITE_ADDR(mrbuf), data, LEN);
/* this must be done to update write pointer after every writes */
len = mrbuffer_give(mrbuf, LEN);
printf("h:%04lu t:%04lu %04lu\n", \
MRBUF_WRITE_OFFSET(mrbuf), \
MRBUF_READ_OFFSET(mrbuf), \
len);
}
if (!memcmp(mrbuf->vaddr, mrbuf->vaddr + mrbuf->size, mrbuf->size))
printf("mrbuffer is working\n");
mrbuffer_free(mrbuf);
return 0;
}