summaryrefslogtreecommitdiff
path: root/toxcore/util.c
diff options
context:
space:
mode:
authormannol <eniz_vukovic@hotmail.com>2015-04-13 01:45:53 +0200
committermannol <eniz_vukovic@hotmail.com>2015-04-13 01:45:53 +0200
commit2465f486acd90ed8395c8a83a13af09ecd024c98 (patch)
tree4abe53d39eb07a45e5ed4d8894b7ae038e2bb705 /toxcore/util.c
parentb2d88a4544a81a217db18b60d91a44d85821db3d (diff)
Started custom RTCP
Diffstat (limited to 'toxcore/util.c')
-rw-r--r--toxcore/util.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/toxcore/util.c b/toxcore/util.c
index 5a72c4a4..d6db946d 100644
--- a/toxcore/util.c
+++ b/toxcore/util.c
@@ -185,3 +185,76 @@ int create_recursive_mutex(pthread_mutex_t *mutex)
185 185
186 return 0; 186 return 0;
187} 187}
188
189
190struct RingBuffer {
191 uint16_t size; /* Max size */
192 uint16_t start;
193 uint16_t end;
194 void **data;
195};
196
197bool rb_full(const RingBuffer *b)
198{
199 return (b->end + 1) % b->size == b->start;
200}
201bool rb_empty(const RingBuffer *b)
202{
203 return b->end == b->start;
204}
205void* rb_write(RingBuffer *b, void *p)
206{
207 void* rc = NULL;
208 if ((b->end + 1) % b->size == b->start) /* full */
209 rc = b->data[b->start];
210
211 b->data[b->end] = p;
212 b->end = (b->end + 1) % b->size;
213
214 if (b->end == b->start)
215 b->start = (b->start + 1) % b->size;
216
217 return rc;
218}
219bool rb_read(RingBuffer *b, void **p)
220{
221 if (b->end == b->start) { /* Empty */
222 *p = NULL;
223 return false;
224 }
225
226 *p = b->data[b->start];
227 b->start = (b->start + 1) % b->size;
228 return true;
229}
230void rb_clear(RingBuffer *b)
231{
232 while (!rb_empty(b)) {
233 void *p;
234 rb_read(b, &p);
235 free(p);
236 }
237}
238RingBuffer *rb_new(int size)
239{
240 RingBuffer *buf = calloc(sizeof(RingBuffer), 1);
241
242 if (!buf) return NULL;
243
244 buf->size = size + 1; /* include empty elem */
245
246 if (!(buf->data = calloc(buf->size, sizeof(void *)))) {
247 free(buf);
248 return NULL;
249 }
250
251 return buf;
252}
253void rb_free(RingBuffer *b)
254{
255 if (b) {
256 rb_clear(b);
257 free(b->data);
258 free(b);
259 }
260} \ No newline at end of file