-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspsc_ringbuff.c
259 lines (209 loc) · 4.57 KB
/
spsc_ringbuff.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
Thread safe, lock free SPSC (Single Producer, Single Consumer) ring buffer.
It must be used with only one producer and only one consumer thread.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdatomic.h>
#include "spsc_ringbuff.h"
/* Opaque type representing a ring buffer. */
struct spsc_ringbuff_impl_t {
char *buffer;
int capacity;
int elemsize;
char full;
/* these shall be atomic */
atomic_int head;
atomic_int tail;
};
SPSC_RINGBUFF_STS spsc_ringbuff_create(SPSC_RINGBUFF_ID *id, int capacity, int elemsize)
{
/* Allocate the ring buffer object, set all elements to zero. */
if( (*id = (struct spsc_ringbuff_impl_t *)calloc(1, sizeof(struct spsc_ringbuff_impl_t))) == NULL)
{
return SPSC_RINGBUFF_MEM_ERROR;
}
(*id)->capacity = capacity;
(*id)->elemsize = elemsize;
/* Allocate data buffer. */
if( ((*id)->buffer = calloc(capacity, elemsize)) == NULL)
{
return SPSC_RINGBUFF_MEM_ERROR;
}
return SPSC_RINGBUFF_OK;
}
SPSC_RINGBUFF_STS spsc_ringbuff_reset(SPSC_RINGBUFF_ID id)
{
if(id == NULL)
{
return SPSC_RINGBUFF_INVALID;
}
id->full = (char)0;
id->head = 0;
id->tail = 0;
return SPSC_RINGBUFF_OK;
}
SPSC_RINGBUFF_STS spsc_ringbuff_isfull(SPSC_RINGBUFF_ID id, int *isfull)
{
if(id == NULL)
{
return SPSC_RINGBUFF_INVALID;
}
*isfull = id->full;
return SPSC_RINGBUFF_OK;
}
SPSC_RINGBUFF_STS spsc_ringbuff_isempty(SPSC_RINGBUFF_ID id, int *isempty)
{
if(id == NULL)
{
return SPSC_RINGBUFF_INVALID;
}
*isempty = (!id->full && (id->head == id->tail));
return SPSC_RINGBUFF_OK;
}
SPSC_RINGBUFF_STS spsc_ringbuff_capacity(SPSC_RINGBUFF_ID id, int *capacity)
{
if(id == NULL)
{
return SPSC_RINGBUFF_INVALID;
}
*capacity = id->capacity;
return SPSC_RINGBUFF_OK;
}
SPSC_RINGBUFF_STS spsc_ringbuff_element_size(SPSC_RINGBUFF_ID id, int *elemsize)
{
if(id == NULL)
{
return SPSC_RINGBUFF_INVALID;
}
*elemsize = id->elemsize;
return SPSC_RINGBUFF_OK;
}
SPSC_RINGBUFF_STS spsc_ringbuff_num_elements(SPSC_RINGBUFF_ID id, int *num)
{
if(id == NULL)
{
return SPSC_RINGBUFF_INVALID;
}
if(id->full)
{
*num = id->capacity;
}
else
{
int head = id->head;
int tail = id->tail;
if(head >= tail)
{
*num = (head - tail);
}
else
{
*num = (id->capacity - tail + head);
}
}
return SPSC_RINGBUFF_OK;
}
char *spsc_ringbuff_get_write_buffer(SPSC_RINGBUFF_ID id, int overwrite, int *overwritten)
{
int isfull;
if(id == NULL)
{
return NULL;
}
isfull = id->full;
if(!overwrite && isfull)
{
if( overwritten != NULL )
{
*overwritten = 0;
}
return NULL;
}
else if( overwritten != NULL )
{
*overwritten = isfull;
}
/* Return the buffer at head position. */
return id->buffer + (id->head*id->elemsize);
}
SPSC_RINGBUFF_STS spsc_ringbuff_add_element(SPSC_RINGBUFF_ID id)
{
if(id == NULL)
{
return SPSC_RINGBUFF_INVALID;
}
/* Adding an element means to increase the head index but if the buffer
is full also the tail index shall be increased. */
if(id->full)
{
if(++(id->tail) == id->capacity)
{
id->tail = 0;
}
}
/* Increase the head index. */
if(++(id->head) == id->capacity)
{
id->head = 0;
}
/* Recompute full flag. */
id->full = (id->head == id->tail);
return SPSC_RINGBUFF_OK;
}
char *spsc_ringbuff_get_read_buffer(SPSC_RINGBUFF_ID id)
{
int empty;
if(id == NULL)
{
return NULL;
}
if( spsc_ringbuff_isempty(id, &empty) != SPSC_RINGBUFF_OK)
{
return NULL;
}
if(empty)
{
return NULL;
}
/* Return the buffer at tail position. */
return id->buffer + (id->tail*id->elemsize);
}
SPSC_RINGBUFF_STS spsc_ringbuff_remove_element(SPSC_RINGBUFF_ID id)
{
int empty;
SPSC_RINGBUFF_STS ret;
if(id == NULL)
{
return SPSC_RINGBUFF_INVALID;
}
if( (ret = spsc_ringbuff_isempty(id, &empty)) != SPSC_RINGBUFF_OK)
{
return ret;
}
if(!empty)
{
/* Reset full flag. */
id->full = 0;
/* Increase the tail index. */
if(++(id->tail) == id->capacity)
{
id->tail = 0;
}
}
return SPSC_RINGBUFF_OK;
}
SPSC_RINGBUFF_STS spsc_ringbuff_destroy(SPSC_RINGBUFF_ID id)
{
if(id == NULL)
{
return SPSC_RINGBUFF_INVALID;
}
/* Free memory. */
if(id->buffer != NULL)
{
free(id->buffer);
}
free(id);
return SPSC_RINGBUFF_OK;
}