diff options
Diffstat (limited to 'ringbuff.c')
| -rw-r--r-- | ringbuff.c | 23 |
1 files changed, 13 insertions, 10 deletions
| @@ -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 | ||
| 6 | typedef int T; | 7 | typedef int T; |
| 7 | 8 | ||
| 8 | struct ring_buffer { | 9 | struct 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 | ||
| 13 | void | 14 | void |
| @@ -19,13 +20,13 @@ ring_init(struct ring_buffer *rb) | |||
| 19 | bool | 20 | bool |
| 20 | ring_put(struct ring_buffer *rb, T data) | 21 | ring_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 | ||
| 47 | void f() { ERROR(""); } | 48 | void |
| 49 | f() | ||
| 50 | { | ||
| 51 | ERROR(""); | ||
| 52 | } | ||
| 48 | 53 | ||
| 49 | int | 54 | int |
| 50 | main(void) | 55 | main(void) |
| @@ -68,5 +73,3 @@ main(void) | |||
| 68 | 73 | ||
| 69 | return EXIT_SUCCESS; | 74 | return EXIT_SUCCESS; |
| 70 | } | 75 | } |
| 71 | |||
| 72 | |||
