bglibs: gqueue.h Source File

bglibs

gqueue.h
1 #ifndef BGLIBS__GENERIC_QUEUE__H__
2 #define BGLIBS__GENERIC_QUEUE__H__
3
4 #include "adt_common.h"
5
20 struct gqueue_node
21 {
23 struct gqueue_node* next;
25 char data[0];
26 };
27
29 struct gqueue
30 {
32 struct gqueue_node* head;
34 struct gqueue_node* tail;
36 unsigned count;
37 };
38
39 int gqueue_push(struct gqueue* d, unsigned datasize, const void* data,
40 adt_copy_fn* fn);
41 void* gqueue_top(const struct gqueue* q);
42 void gqueue_pop(struct gqueue* q, adt_free_fn* fn);
43
45 #define GQUEUE_DECL(PREFIX,TYPE) \
46 extern int PREFIX##_push(struct gqueue* q, TYPE const* data); \
47 extern TYPE* PREFIX##_top(struct gqueue* q); \
48 extern void PREFIX##_pop(struct gqueue* q);
49
51 #define GQUEUE_PUSH_DEFN(PREFIX,TYPE,COPY) \
52 int PREFIX##_push(struct gqueue* q, TYPE const* data) { \
53 return gqueue_push(q, sizeof *data, data, (adt_copy_fn*)COPY); \
54 }
55
57 #define GQUEUE_TOP_DEFN(PREFIX,TYPE) \
58 TYPE* PREFIX##_top(struct gqueue* q) { \
59 return (q->head == 0) ? 0 : (TYPE*)q->head->data; \
60 }
61
63 #define GQUEUE_POP_DEFN(PREFIX,FREE) \
64 void PREFIX##_pop(struct gqueue* q) { \
65 gqueue_pop(q, (adt_free_fn*)(FREE)); \
66 }
67
71 #define GQUEUE_DEFN(PREFIX,TYPE,COPY,FREE) \
72 GQUEUE_PUSH_DEFN(PREFIX,TYPE,COPY) \
73 GQUEUE_TOP_DEFN(PREFIX,TYPE) \
74 GQUEUE_POP_DEFN(PREFIX,FREE)
75
78 #endif
gqueue_push
int gqueue_push(struct gqueue *d, unsigned datasize, const void *data, adt_copy_fn *fn)
Definition: gqueue_push.c:8
adt_free_fn
void adt_free_fn(void *)
Definition: adt_common.h:12
gqueue_pop
void gqueue_pop(struct gqueue *q, adt_free_fn *fn)
Definition: gqueue_pop.c:9
gqueue_node::next
struct gqueue_node * next
Definition: gqueue.h:23
gqueue_node
Definition: gqueue.h:20
gqueue_node::data
char data[0]
Definition: gqueue.h:25
gqueue
Definition: gqueue.h:29
gqueue_top
void * gqueue_top(const struct gqueue *q)
Definition: gqueue_top.c:5
gqueue::count
unsigned count
Definition: gqueue.h:36
gqueue::tail
struct gqueue_node * tail
Definition: gqueue.h:34
gqueue::head
struct gqueue_node * head
Definition: gqueue.h:32
adt_copy_fn
int adt_copy_fn(void *, const void *)
Definition: adt_common.h:16