aboutsummaryrefslogtreecommitdiff
path: root/src/mempool.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/mempool.h')
-rw-r--r--src/mempool.h22
1 files changed, 10 insertions, 12 deletions
diff --git a/src/mempool.h b/src/mempool.h
index 6c25ac9..7d065f5 100644
--- a/src/mempool.h
+++ b/src/mempool.h
@@ -1,13 +1,11 @@
#ifndef MEMPOOL_H
#define MEMPOOL_H
-#ifndef MEMPOOL_OBJ_TYPE
-#define MEMPOOL_OBJ_TYPE int
-#endif
-
-#ifndef MEMPOOL_CAP
-#define MEMPOOL_CAP (1 << 16)
-#endif
+/* To generate use MEMPOOL_GENERATE(id, type, cap), where functions
+ * _mempool_allocate() and _mempool_free(type *obj) will be prefixed with id
+ * and can be used to allocate objects of type <type> with <cap> objects in
+ * each block
+ */
#define for_each_block(id, b, head) \
for(struct id##_block *b = (head), *next = NULL; \
@@ -15,13 +13,13 @@
#define get_last_block(b) \
while((b)->next != NULL) (b) = (b)->next
-#define MEMPOOL_GENERATE(id) \
+#define MEMPOOL_GENERATE(id, type, cap) \
\
struct id##_block { \
union id##_chunk { \
- MEMPOOL_OBJ_TYPE obj; \
+ type obj; \
union id##_chunk *next; \
- } chunks[MEMPOOL_CAP]; \
+ } chunks[cap]; \
\
struct id##_block *next; \
}; \
@@ -33,9 +31,9 @@
static inline void *_##id##_mempool_init_block(struct id##_block *b) \
{ \
b->next = NULL; \
- for(size_t i = 0; i < MEMPOOL_CAP; i++) \
+ for(size_t i = 0; i < cap; i++) \
b->chunks[i].next = &b->chunks[i+1]; \
- b->chunks[MEMPOOL_CAP-1].next = NULL; \
+ b->chunks[cap-1].next = NULL; \
\
return b->chunks; \
} \