00001 #ifndef BGLIBS__GENERIC_QUEUE__H__ 00002 #define BGLIBS__GENERIC_QUEUE__H__ 00003 00004 #include <adt/common.h> 00005 00020 struct gqueue_node 00021 { 00023 struct gqueue_node* next; 00025 char data[0]; 00026 }; 00027 00029 struct gqueue 00030 { 00032 struct gqueue_node* head; 00034 struct gqueue_node* tail; 00036 unsigned count; 00037 }; 00038 00039 int gqueue_push(struct gqueue* d, unsigned datasize, const void* data, 00040 adt_copy_fn* fn); 00041 void* gqueue_top(const struct gqueue* q); 00042 void gqueue_pop(struct gqueue* q, adt_free_fn* fn); 00043 00045 #define GQUEUE_DECL(PREFIX,TYPE) \ 00046 extern int PREFIX##_push(struct gqueue* q, TYPE const* data); \ 00047 extern TYPE* PREFIX##_top(struct gqueue* q); \ 00048 extern void PREFIX##_pop(struct gqueue* q); 00049 00051 #define GQUEUE_PUSH_DEFN(PREFIX,TYPE,COPY) \ 00052 int PREFIX##_push(struct gqueue* q, TYPE const* data) { \ 00053 return gqueue_push(q, sizeof *data, data, (adt_copy_fn*)COPY); \ 00054 } 00055 00057 #define GQUEUE_TOP_DEFN(PREFIX,TYPE) \ 00058 TYPE* PREFIX##_top(struct gqueue* q) { \ 00059 return (q->head == 0) ? 0 : (TYPE*)q->head->data; \ 00060 } 00061 00063 #define GQUEUE_POP_DEFN(PREFIX,FREE) \ 00064 void PREFIX##_pop(struct gqueue* q) { \ 00065 gqueue_pop(q, (adt_free_fn*)(FREE)); \ 00066 } 00067 00071 #define GQUEUE_DEFN(PREFIX,TYPE,COPY,FREE) \ 00072 GQUEUE_PUSH_DEFN(PREFIX,TYPE,COPY) \ 00073 GQUEUE_TOP_DEFN(PREFIX,TYPE) \ 00074 GQUEUE_POP_DEFN(PREFIX,FREE) 00075 00078 #endif