Commit 3d4b32ceb9ca9f3a824e9ae27bceebb75c6c0397

Thomas de Grivel 2020-03-04T22:27:05

wip queue

diff --git a/queue.h b/queue.h
new file mode 100644
index 0000000..f886dd3
--- /dev/null
+++ b/queue.h
@@ -0,0 +1,60 @@
+#ifndef QUEUE_H
+#define QUEUE_H
+
+/* static queue */
+
+#define QUEUE_TYPE(type, size)  \
+  type ## _queue_ ## size
+
+#define QUEUE(type, size)          \
+  struct QUEUE_TYPE(type, size) {  \
+    unsigned int length;           \
+    unsigned int read_index;       \
+    unsigned int write_index;      \
+    type item[size];               \
+  }
+
+#define S_QUEUE_TYPE(type, size)  \
+  s_ ## QUEUE_TYPE(type, size)
+
+#define QUEUE_METHOD(type, size, method)  \
+  QUEUE_TYPE(type, size) ## _ ## method
+
+#define DEFINE_QUEUE(type, size)                            \
+  typedef QUEUE(type, size) S_QUEUE_TYPE(type, size);       \
+  static void QUEUE_METHOD(type, size, init)                \
+    (S_QUEUE_TYPE(type, size) *q)                           \
+  {                                                         \
+    bzero(q, sizeof(q));                                    \
+  }                                                         \
+  static void QUEUE_METHOD(type, size, free)                \
+    (S_QUEUE_TYPE(type, size) *q)                           \
+  {                                                         \
+    bzero(q, sizeof(q));                                    \
+  }                                                         \
+  static int QUEUE_METHOD(type, size, enqueue)              \
+    (S_QUEUE_TYPE(type, size) *q, type item)                \
+  {                                                         \
+    if (q->length < size) {                                 \
+      unsigned int write_index = q->write_index;            \
+      q->write_index = (q->write_index + 1) % size;         \
+      q->item[write_index] = item;                          \
+      q->length++;                                          \
+      return 0;                                             \
+    }                                                       \
+    return -1;                                              \
+  }                                                         \
+  static int QUEUE_METHOD(type, size, dequeue)              \
+    (S_QUEUE_TYPE(type, size) *q, type *ptr)                \
+  {                                                         \
+    if (q->length > 0) {                                    \
+      unsigned int read_index = q->read_index;              \
+      q->read_index = (read_index + 1) % size;              \
+      *ptr = q->item[read_index];                           \
+      q->length--;                                          \
+      return 0;                                             \
+    }                                                       \
+    return -1;                                              \
+  }
+
+#endif