diff options
Diffstat (limited to 'util/queue.h')
| -rw-r--r-- | util/queue.h | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/util/queue.h b/util/queue.h new file mode 100644 index 0000000..31236f6 --- /dev/null +++ b/util/queue.h @@ -0,0 +1,46 @@ +#ifndef QUEUE_H +#define QUEUE_H + +#define QUEUE_GENERATE(id, type, cap) \ + static struct \ + { type buf[cap]; size_t start; size_t end; } _##id##_queue; \ + \ + static int id##_enqueue(type *m) \ + { \ + if(_##id##_queue.end >= _##id##_queue.start + cap) { \ + fprintf(stderr, \ + "ERROR: Queue capacity of %d reached\n", cap); \ + return 1; \ + } \ + \ + _##id##_queue.buf[_##id##_queue.end++ % cap] = *m; \ + return 0; \ + } \ + \ + static int id##_dequeue(type *m) \ + { \ + if(_##id##_queue.start >= _##id##_queue.end) { \ + fprintf(stderr, "ERROR: Trying to dequeue empty queue\n"); \ + return 1; \ + } \ + \ + *m = _##id##_queue.buf[_##id##_queue.start++ % cap]; \ + return 0; \ + } \ + \ + static int id##_empty() \ + { return _##id##_queue.start == _##id##_queue.end; } \ + \ + static int id##_peek(type *m) \ + { \ + if(id##_empty()) { \ + fprintf(stderr, \ + "ERROR: Trying to peek into empty queue\n"); \ + return 1; \ + } \ + \ + *m = _##id##_queue.buf[_##id##_queue.start % cap]; \ + return 0; \ + } + +#endif |
