-
Notifications
You must be signed in to change notification settings - Fork 27
/
ringbuffer.h
217 lines (186 loc) · 6.58 KB
/
ringbuffer.h
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* The MIT License (MIT)
*
* Copyright (c) 2013 Philip Thrasher
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*
*
* Philip Thrasher's Crazy Awesome Ring Buffer Macros!
*
* Below you will find some naughty macros for easy owning and manipulating
* generic ring buffers. Yes, they are slightly evil in readability, but they
* are really fast, and they work great.
*
* Example usage:
*
* #include <stdio.h>
*
* // So we can use this in any method, this gives us a typedef
* // named 'intBuffer'.
* ringBuffer_typedef(int, intBuffer);
*
* int main() {
* // Declare vars.
* intBuffer myBuffer;
*
* bufferInit(myBuffer,1024,int);
*
* // We must have the pointer. All of the macros deal with the pointer.
* // (except for init.)
* intBuffer* myBuffer_ptr;
* myBuffer_ptr = &myBuffer;
*
* // Write two values.
* bufferWrite(myBuffer_ptr,37);
* bufferWrite(myBuffer_ptr,72);
*
* // Read a value into a local variable.
* int first;
* bufferRead(myBuffer_ptr,first);
* assert(first == 37); // true
*
* // Get reference of the current element to avoid copies
* int* second_ptr = &bufferReadPeek(myBuffer_ptr);
* assert(*second_ptr == 72); // true
* operate_on_reference(second_prt);
*
* // move to next value
* bufferReadSkip(myBuffer_ptr);
*
* return 0;
* }
*
*/
#ifndef _ringbuffer_h
#define _ringbuffer_h
/* Setting RINGBUFFER_USE_STATIC_MEMORY to 1 will use a static memory area
* allocated for the ringbuffer instead of a dynamic one, useful if there is no
* dynamic allocation or the buffer will always be around anyways. There is no
* bufferDestroy() in this case */
#ifndef RINGBUFFER_USE_STATIC_MEMORY
#define RINGBUFFER_USE_STATIC_MEMORY 0
#endif
/* Setting RINGBUFFER_AVOID_MODULO to 1 will replace the modulo operations with
* comparisons, useful if there is no hardware divide operations */
#ifndef RINGBUFFER_AVOID_MODULO
#define RINGBUFFER_AVOID_MODULO 0
#endif
#define ringBuffer_typedef(T, NAME) \
typedef struct { \
int size; \
int start; \
int end; \
T* elems; \
} NAME
/* To allow for S elements to be stored we allocate space for (S + 1) elements,
* otherwise a full buffer would be indistinguishable from an empty buffer.
*
* Also as the elements will be accessed like an array it makes sense to pad
* the type of the elements to the platform wordsize, otherwise you will end up
* with unaligned accesses. */
#if RINGBUFFER_USE_STATIC_MEMORY == 1
#define bufferInit(BUF, S, T) \
{ \
static T StaticBufMemory[S + 1];\
BUF.elems = StaticBufMemory; \
} \
BUF.size = S; \
BUF.start = 0; \
BUF.end = 0;
/* This has only been tested with GCC */
#if defined(__GNUC__)
/* In cases where the developer needs to explicitly arrange memory in linker
* scripts, this macro allows to specify the linker section the buffer will
* be allocated in. */
#define bufferInitWithSection(SECTION, BUF, S, T) \
{ \
__attribute__((section(SECTION))) static T StaticBufMemory[S + 1]; \
BUF.elems = StaticBufMemory; \
} \
BUF.size = S; \
BUF.start = 0; \
BUF.end = 0;
#endif
#else
#define bufferInit(BUF, S, T) \
BUF.size = S; \
BUF.start = 0; \
BUF.end = 0; \
BUF.elems = (T*)calloc(BUF.size + 1, sizeof(T))
#define bufferDestroy(BUF) free((BUF)->elems)
#endif
#if RINGBUFFER_AVOID_MODULO == 1
#define nextStartIndex(BUF) (((BUF)->start != (BUF)->size) ? ((BUF)->start + 1) : 0)
#define nextEndIndex(BUF) (((BUF)->end != (BUF)->size) ? ((BUF)->end + 1) : 0)
#else
#define nextStartIndex(BUF) (((BUF)->start + 1) % ((BUF)->size + 1))
#define nextEndIndex(BUF) (((BUF)->end + 1) % ((BUF)->size + 1))
#endif
#define nextIndex(BUF, IX) ({ ((IX != ((BUF)->size - 1)) ? (IX + 1) : 0); });
#define previousIndex(BUF, IX) ({ ((IX != 0) ? (IX - 1) : ((BUF)->size)); });
#define isBufferEmpty(BUF) ((BUF)->end == (BUF)->start)
#define isBufferFull(BUF) (nextEndIndex(BUF) == (BUF)->start)
#define bufferUsedSpace(BUF) \
(isBufferEmpty(BUF) ? 0 \
: ((BUF)->start < (BUF)->end) \
? (((BUF)->end - (BUF)->start)) \
: ((BUF)->size - ((BUF)->start - (BUF)->end - 1)))
#define bufferFreeSpace(BUF) ((BUF)->size - (bufferUsedSpace(BUF)))
#define bufferWritePeek(BUF) (BUF)->elems[(BUF)->end]
#define bufferWriteSkip(BUF) \
(BUF)->end = nextEndIndex(BUF); \
if (isBufferEmpty(BUF)) { \
(BUF)->start = nextStartIndex(BUF); \
}
#define bufferReadPeek(BUF) (BUF)->elems[(BUF)->start]
#define bufferReadSkip(BUF) \
(BUF)->start = nextStartIndex(BUF);
/* Variable names are weird here to avoid variable shadowing.
* I don't think this is the best way to handle this issue,
* but if you actually want to use "asdfghhgfsa" as a
* variable name, you've got bigger problems. */
#define bufferPeekBack(BUF, STEPS, ELEM) \
({ \
int asdfghhgfsa = previousIndex(BUF, (BUF)->end); \
for (int qpwoeiieowpq = 0; qpwoeiieowpq < STEPS; qpwoeiieowpq++) { \
asdfghhgfsa = previousIndex(BUF, asdfghhgfsa); \
} \
ELEM = (BUF)->elems[asdfghhgfsa]; \
});
#define bufferPeekForward(BUF, STEPS, ELEM) \
({ \
int asdfghhgfsa = (BUF)->start; \
for (int qpwoeiieowpq = 0; qpwoeiieowpq < STEPS; qpwoeiieowpq++) { \
asdfghhgfsa = nextIndex(BUF, asdfghhgfsa);\
} \
ELEM = (BUF)->elems[asdfghhgfsa];\
});
#define bufferWrite(BUF, ELEM) \
bufferWritePeek(BUF) = ELEM; \
bufferWriteSkip(BUF)
#define bufferRead(BUF, ELEM) \
ELEM = bufferReadPeek(BUF); \
bufferReadSkip(BUF)
#define bufferReset(BUF) \
({ \
(BUF)->start = 0; \
(BUF)->end = 0; \
});
#endif