]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Licensed under a two-clause BSD-style license. | |
3 | * See LICENSE for details. | |
4 | */ | |
5 | ||
6 | #ifndef OBJ_POOL_H_ | |
7 | #define OBJ_POOL_H_ | |
8 | ||
9 | #include "git-compat-util.h" | |
10 | ||
11 | #define MAYBE_UNUSED __attribute__((__unused__)) | |
12 | ||
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; \ | |
59 | } | |
60 | ||
61 | #endif |