bglibs: gstack.h Source File

bglibs

gstack.h
1 #ifndef BGLIBS__GENERIC_STACK__H__
2 #define BGLIBS__GENERIC_STACK__H__
3
4 #include "adt_common.h"
5
20 struct gstack_node
21 {
23 struct gstack_node* next;
25 char data[0];
26 };
27
29 struct gstack
30 {
32 struct gstack_node* head;
34 unsigned count;
35 };
36
37 int gstack_push(struct gstack* d, unsigned datasize, const void* data,
38 adt_copy_fn* fn);
39 void* gstack_top(const struct gstack* s);
40 void gstack_pop(struct gstack* s, adt_free_fn* fn);
41
43 #define GSTACK_DECL(PREFIX,TYPE) \
44 extern int PREFIX##_push(struct gstack* s, TYPE const* data); \
45 extern TYPE* PREFIX##_top(struct gstack* s); \
46 extern void PREFIX##_pop(struct gstack* s);
47
49 #define GSTACK_PUSH_DEFN(PREFIX,TYPE,COPY) \
50 int PREFIX##_push(struct gstack* s, TYPE const* data) { \
51 return gstack_push(s, sizeof *data, data, (adt_copy_fn*)COPY); \
52 }
53
55 #define GSTACK_TOP_DEFN(PREFIX,TYPE) \
56 TYPE* PREFIX##_top(struct gstack* s) { \
57 return (s->head == 0) ? 0 : (TYPE*)s->head->data; \
58 }
59
61 #define GSTACK_POP_DEFN(PREFIX,FREE) \
62 void PREFIX##_pop(struct gstack* s) { \
63 gstack_pop(s, (adt_free_fn*)(FREE)); \
64 }
65
69 #define GSTACK_DEFN(PREFIX,TYPE,COPY,FREE) \
70 GSTACK_PUSH_DEFN(PREFIX,TYPE,COPY) \
71 GSTACK_TOP_DEFN(PREFIX,TYPE) \
72 GSTACK_POP_DEFN(PREFIX,FREE)
73
76 #endif
adt_free_fn
void adt_free_fn(void *)
Definition: adt_common.h:12
gstack_top
void * gstack_top(const struct gstack *s)
Definition: gstack_top.c:5
gstack_pop
void gstack_pop(struct gstack *s, adt_free_fn *fn)
Definition: gstack_pop.c:9
gstack::head
struct gstack_node * head
Definition: gstack.h:32
gstack
Definition: gstack.h:29
gstack_push
int gstack_push(struct gstack *d, unsigned datasize, const void *data, adt_copy_fn *fn)
Definition: gstack_push.c:8
adt_copy_fn
int adt_copy_fn(void *, const void *)
Definition: adt_common.h:16
gstack_node::data
char data[0]
Definition: gstack.h:25
gstack_node::next
struct gstack_node * next
Definition: gstack.h:23
gstack_node
Definition: gstack.h:20
gstack::count
unsigned count
Definition: gstack.h:34