bglibs
gstack.h
1 #ifndef BGLIBS__GENERIC_STACK__H__
2 #define BGLIBS__GENERIC_STACK__H__
3 
4 #include "adt_common.h"
5 
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
void adt_free_fn(void *)
Definition: adt_common.h:12
void * gstack_top(const struct gstack *s)
Definition: gstack_top.c:5
void gstack_pop(struct gstack *s, adt_free_fn *fn)
Definition: gstack_pop.c:9
struct gstack_node * head
Definition: gstack.h:32
Definition: gstack.h:29
int gstack_push(struct gstack *d, unsigned datasize, const void *data, adt_copy_fn *fn)
Definition: gstack_push.c:8
int adt_copy_fn(void *, const void *)
Definition: adt_common.h:16
char data[0]
Definition: gstack.h:25
struct gstack_node * next
Definition: gstack.h:23
Definition: gstack.h:20
unsigned count
Definition: gstack.h:34