#include #include #include #include "util.h" typedef int T; struct ring_buffer { size_t head, tail; T array[8]; /* fit for your needs... */ }; void ring_init(struct ring_buffer *rb) { rb->head = rb->tail = 0; } bool ring_put(struct ring_buffer *rb, T data) { const size_t next = (rb->head + 1) % NELEM(rb->array); if ( next == rb->tail ) return false; rb->array[rb->head] = data; rb->head = next; return true; } bool ring_get(struct ring_buffer *rb, T *data) { if ( rb->head == rb->tail ) return false; const size_t next = (rb->tail + 1) % NELEM(rb->array); *data = rb->array[rb->tail]; rb->tail = next; return true; } void f() { ERROR(""); } int main(void) { #if 0 struct ring_buffer rb; ring_init(&rb); #else struct ring_buffer rb = { .head = 0, .tail = 0 }; #endif for ( int i = 0; i != 30; ++i ) { if ( !ring_put(&rb, i) ) break; } int j; while ( ring_get(&rb, &j) ) { printf("%d\n", j); } return EXIT_SUCCESS; }