Resolved memory haemorrhage in perl replay implementation.
[svn-fe.git] / obj_pool.h
blobe26089128d59709cc322c3f6ffefe49578403876
1 #ifndef OBJ_POOL_H_
2 #define OBJ_POOL_H_
4 #include <stdint.h>
5 #include <stdlib.h>
7 #define obj_pool_gen(pre, obj_t, initial_capacity) \
8 static struct { \
9 uint32_t size; \
10 uint32_t capacity; \
11 obj_t *base; \
12 } pre##_pool = { 0, 0, NULL}; \
13 static uint32_t pre##_alloc(uint32_t count) \
14 { \
15 uint32_t offset; \
16 while (pre##_pool.size + count > pre##_pool.capacity) { \
17 if (pre##_pool.capacity) { \
18 pre##_pool.capacity *= 2; \
19 } else { \
20 pre##_pool.capacity = initial_capacity; \
21 } \
22 pre##_pool.base = \
23 realloc(pre##_pool.base, pre##_pool.capacity * sizeof(obj_t)); \
24 } \
25 offset = pre##_pool.size; \
26 pre##_pool.size += count; \
27 return offset; \
28 } \
29 static void pre##_free(uint32_t count) \
30 { \
31 pre##_pool.size -= count; \
32 } \
33 static uint32_t pre##_offset(obj_t * obj) \
34 { \
35 return obj == NULL ? ~0 : obj - pre##_pool.base; \
36 } \
37 static obj_t *pre##_pointer(uint32_t offset) \
38 { \
39 return offset >= pre##_pool.size ? NULL : &pre##_pool.base[offset]; \
40 } \
42 #endif