2 * Licensed under a two-clause BSD-style license.
3 * See LICENSE for details.
9 #include "git-compat-util.h"
11 #define MAYBE_UNUSED __attribute__((__unused__))
13 #define obj_pool_gen(pre, obj_t, initial_capacity) \
19 } pre##_pool = {0, 0, 0, NULL}; \
20 static MAYBE_UNUSED uint32_t pre##_alloc(uint32_t count) \
23 if (pre##_pool.size + count > pre##_pool.capacity) { \
24 while (pre##_pool.size + count > pre##_pool.capacity) \
25 if (pre##_pool.capacity) \
26 pre##_pool.capacity *= 2; \
28 pre##_pool.capacity = initial_capacity; \
29 pre##_pool.base = realloc(pre##_pool.base, \
30 pre##_pool.capacity * sizeof(obj_t)); \
32 offset = pre##_pool.size; \
33 pre##_pool.size += count; \
36 static MAYBE_UNUSED void pre##_free(uint32_t count) \
38 pre##_pool.size -= count; \
40 static MAYBE_UNUSED uint32_t pre##_offset(obj_t *obj) \
42 return obj == NULL ? ~0 : obj - pre##_pool.base; \
44 static MAYBE_UNUSED obj_t *pre##_pointer(uint32_t offset) \
46 return offset >= pre##_pool.size ? NULL : &pre##_pool.base[offset]; \
48 static MAYBE_UNUSED void pre##_commit(void) \
50 pre##_pool.committed = pre##_pool.size; \
52 static MAYBE_UNUSED void pre##_reset(void) \
54 free(pre##_pool.base); \
55 pre##_pool.base = NULL; \
56 pre##_pool.size = 0; \
57 pre##_pool.capacity = 0; \
58 pre##_pool.committed = 0; \