aboutsummaryrefslogtreecommitdiff
path: root/ringbuff.c
diff options
context:
space:
mode:
Diffstat (limited to 'ringbuff.c')
-rw-r--r--ringbuff.c23
1 files changed, 13 insertions, 10 deletions
diff --git a/ringbuff.c b/ringbuff.c
index 99dc0d8..e101073 100644
--- a/ringbuff.c
+++ b/ringbuff.c
@@ -1,13 +1,14 @@
1#include <stdbool.h>
1#include <stdio.h> 2#include <stdio.h>
2#include <stdlib.h> 3#include <stdlib.h>
3#include <stdbool.h> 4
4#include "util.h" 5#include "util.h"
5 6
6typedef int T; 7typedef int T;
7 8
8struct ring_buffer { 9struct ring_buffer {
9 int head, tail; 10 size_t head, tail;
10 T array[8]; /* fit for your needs... */ 11 T array[8]; /* fit for your needs... */
11}; 12};
12 13
13void 14void
@@ -19,13 +20,13 @@ ring_init(struct ring_buffer *rb)
19bool 20bool
20ring_put(struct ring_buffer *rb, T data) 21ring_put(struct ring_buffer *rb, T data)
21{ 22{
22 const int next = (rb->head + 1) % NELEM(rb->array); 23 const size_t next = (rb->head + 1) % NELEM(rb->array);
23 24
24 if ( next == rb->tail ) 25 if ( next == rb->tail )
25 return false; 26 return false;
26 27
27 rb->array[rb->head] = data; 28 rb->array[rb->head] = data;
28 rb->head = next; 29 rb->head = next;
29 30
30 return true; 31 return true;
31} 32}
@@ -36,15 +37,19 @@ ring_get(struct ring_buffer *rb, T *data)
36 if ( rb->head == rb->tail ) 37 if ( rb->head == rb->tail )
37 return false; 38 return false;
38 39
39 const int next = (rb->tail + 1) % NELEM(rb->array); 40 const size_t next = (rb->tail + 1) % NELEM(rb->array);
40 41
41 *data = rb->array[rb->tail]; 42 *data = rb->array[rb->tail];
42 rb->tail = next; 43 rb->tail = next;
43 44
44 return true; 45 return true;
45} 46}
46 47
47void f() { ERROR(""); } 48void
49f()
50{
51 ERROR("");
52}
48 53
49int 54int
50main(void) 55main(void)
@@ -68,5 +73,3 @@ main(void)
68 73
69 return EXIT_SUCCESS; 74 return EXIT_SUCCESS;
70} 75}
71
72