Merge branch 'tr/cache-tree' into maint-1.7.8
[git/gitweb.git] / vcs-svn / obj_pool.h
blobdeb6eb8135fb0e01dedc213402a76e4ca75fcfc9
1 /*
2 * Licensed under a two-clause BSD-style license.
3 * See LICENSE for details.
4 */
6 #ifndef OBJ_POOL_H_
7 #define OBJ_POOL_H_
9 #include "git-compat-util.h"
11 #define MAYBE_UNUSED __attribute__((__unused__))
13 #define obj_pool_gen(pre, obj_t, initial_capacity) \
14 static struct { \
15 uint32_t committed; \
16 uint32_t size; \
17 uint32_t capacity; \
18 obj_t *base; \
19 } pre##_pool = {0, 0, 0, NULL}; \
20 static MAYBE_UNUSED uint32_t pre##_alloc(uint32_t count) \
21 { \
22 uint32_t offset; \
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; \
27 else \
28 pre##_pool.capacity = initial_capacity; \
29 pre##_pool.base = realloc(pre##_pool.base, \
30 pre##_pool.capacity * sizeof(obj_t)); \
31 } \
32 offset = pre##_pool.size; \
33 pre##_pool.size += count; \
34 return offset; \
35 } \
36 static MAYBE_UNUSED void pre##_free(uint32_t count) \
37 { \
38 pre##_pool.size -= count; \
39 } \
40 static MAYBE_UNUSED uint32_t pre##_offset(obj_t *obj) \
41 { \
42 return obj == NULL ? ~0 : obj - pre##_pool.base; \
43 } \
44 static MAYBE_UNUSED obj_t *pre##_pointer(uint32_t offset) \
45 { \
46 return offset >= pre##_pool.size ? NULL : &pre##_pool.base[offset]; \
47 } \
48 static MAYBE_UNUSED void pre##_commit(void) \
49 { \
50 pre##_pool.committed = pre##_pool.size; \
51 } \
52 static MAYBE_UNUSED void pre##_reset(void) \
53 { \
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; \
61 #endif