Go to the documentation of this file.
31 #define KMEMPOOL_INIT(name, kmptype_t, kmpfree_f) \
36 static inline kmp_##name##_t *kmp_init_##name(void) { \
37 return calloc(1, sizeof(kmp_##name##_t)); \
39 static inline void kmp_destroy_##name(kmp_##name##_t *mp) { \
41 for (k = 0; k < mp->n; ++k) { \
42 kmpfree_f(mp->buf[k]); free(mp->buf[k]); \
44 free(mp->buf); free(mp); \
46 static inline kmptype_t *kmp_alloc_##name(kmp_##name##_t *mp) { \
48 if (mp->n == 0) return calloc(1, sizeof(kmptype_t)); \
49 return mp->buf[--mp->n]; \
51 static inline void kmp_free_##name(kmp_##name##_t *mp, kmptype_t *p) { \
53 if (mp->n == mp->max) { \
54 mp->max = mp->max? mp->max<<1 : 16; \
55 mp->buf = realloc(mp->buf, sizeof(kmptype_t *) * mp->max); \
57 mp->buf[mp->n++] = p; \
60 #define kmempool_t(name) kmp_##name##_t
61 #define kmp_init(name) kmp_init_##name()
62 #define kmp_destroy(name, mp) kmp_destroy_##name(mp)
63 #define kmp_alloc(name, mp) kmp_alloc_##name(mp)
64 #define kmp_free(name, mp, p) kmp_free_##name(mp, p)
66 #define KLIST_INIT(name, kltype_t, kmpfree_t) \
67 struct __kl1_##name { \
69 struct __kl1_##name *next; \
71 typedef struct __kl1_##name kl1_##name; \
72 KMEMPOOL_INIT(name, kl1_##name, kmpfree_t) \
74 kl1_##name *head, *tail; \
78 static inline kl_##name##_t *kl_init_##name(void) { \
79 kl_##name##_t *kl = calloc(1, sizeof(kl_##name##_t)); \
80 kl->mp = kmp_init(name); \
81 kl->head = kl->tail = kmp_alloc(name, kl->mp); \
85 static inline void kl_destroy_##name(kl_##name##_t *kl) { \
87 for (p = kl->head; p != kl->tail; p = p->next) \
88 kmp_free(name, kl->mp, p); \
89 kmp_free(name, kl->mp, p); \
90 kmp_destroy(name, kl->mp); \
93 static inline kltype_t *kl_pushp_##name(kl_##name##_t *kl) { \
94 kl1_##name *q, *p = kmp_alloc(name, kl->mp); \
95 q = kl->tail; p->next = 0; kl->tail->next = p; kl->tail = p; \
99 static inline int kl_shift_##name(kl_##name##_t *kl, kltype_t *d) { \
101 if (kl->head->next == 0) return -1; \
103 p = kl->head; kl->head = kl->head->next; \
104 if (d) *d = p->data; \
105 kmp_free(name, kl->mp, p); \
109 #define kliter_t(name) kl1_##name
110 #define klist_t(name) kl_##name##_t
111 #define kl_val(iter) ((iter)->data)
112 #define kl_next(iter) ((iter)->next)
113 #define kl_begin(kl) ((kl)->head)
114 #define kl_end(kl) ((kl)->tail)
116 #define kl_init(name) kl_init_##name()
117 #define kl_destroy(name, kl) kl_destroy_##name(kl)
118 #define kl_pushp(name, kl) kl_pushp_##name(kl)
119 #define kl_shift(name, kl, d) kl_shift_##name(kl, d)