1 #ifndef COMMIT_SLAB_IMPL_H
2 #define COMMIT_SLAB_IMPL_H
4 #include "git-compat-util.h"
6 #define implement_static_commit_slab(slabname, elemtype) \
7 implement_commit_slab(slabname, elemtype, MAYBE_UNUSED static)
9 #define implement_shared_commit_slab(slabname, elemtype) \
10 implement_commit_slab(slabname, elemtype, )
12 #define implement_commit_slab(slabname, elemtype, scope) \
14 scope void init_ ##slabname## _with_stride(struct slabname *s, \
17 unsigned int elem_size; \
21 elem_size = sizeof(elemtype) * stride; \
22 s->slab_size = COMMIT_SLAB_SIZE / elem_size; \
27 scope void init_ ##slabname(struct slabname *s) \
29 init_ ##slabname## _with_stride(s, 1); \
32 scope void clear_ ##slabname(struct slabname *s) \
35 for (i = 0; i < s->slab_count; i++) \
38 FREE_AND_NULL(s->slab); \
41 scope void deep_clear_ ##slabname(struct slabname *s, void (*free_fn)(elemtype *)) \
44 for (i = 0; i < s->slab_count; i++) { \
48 for (j = 0; j < s->slab_size; j++) \
49 free_fn(&s->slab[i][j * s->stride]); \
51 clear_ ##slabname(s); \
54 scope elemtype *slabname## _at_peek(struct slabname *s, \
55 const struct commit *c, \
58 unsigned int nth_slab, nth_slot; \
60 nth_slab = c->index / s->slab_size; \
61 nth_slot = c->index % s->slab_size; \
63 if (s->slab_count <= nth_slab) { \
65 if (!add_if_missing) \
67 REALLOC_ARRAY(s->slab, nth_slab + 1); \
68 for (i = s->slab_count; i <= nth_slab; i++) \
70 s->slab_count = nth_slab + 1; \
72 if (!s->slab[nth_slab]) { \
73 if (!add_if_missing) \
75 s->slab[nth_slab] = xcalloc(s->slab_size, \
76 sizeof(**s->slab) * s->stride); \
78 return &s->slab[nth_slab][nth_slot * s->stride]; \
81 scope elemtype *slabname## _at(struct slabname *s, \
82 const struct commit *c) \
84 return slabname##_at_peek(s, c, 1); \
87 scope elemtype *slabname## _peek(struct slabname *s, \
88 const struct commit *c) \
90 return slabname##_at_peek(s, c, 0); \
96 * Note that this redundant forward declaration is required
97 * to allow a terminating semicolon, which makes instantiations look
98 * like function declarations. I.e., the expansion of
100 * implement_commit_slab(indegree, int, static);
102 * ends in 'struct indegree;'. This would otherwise
103 * be a syntax error according (at least) to ISO C. It's hard to
104 * catch because GCC silently parses it by default.
107 #endif /* COMMIT_SLAB_IMPL_H */