1 #ifndef COMMIT_SLAB_IMPL_H
2 #define COMMIT_SLAB_IMPL_H
4 #define MAYBE_UNUSED __attribute__((__unused__))
6 #define implement_static_commit_slab(slabname, elemtype) \
7 implement_commit_slab(slabname, elemtype, static MAYBE_UNUSED)
9 #define implement_shared_commit_slab(slabname, elemtype) \
10 implement_commit_slab(slabname, elemtype, )
12 #define implement_commit_slab(slabname, elemtype, scope) \
14 static int stat_ ##slabname## realloc; \
16 scope void init_ ##slabname## _with_stride(struct slabname *s, \
19 unsigned int elem_size; \
23 elem_size = sizeof(elemtype) * stride; \
24 s->slab_size = COMMIT_SLAB_SIZE / elem_size; \
29 scope void init_ ##slabname(struct slabname *s) \
31 init_ ##slabname## _with_stride(s, 1); \
34 scope void clear_ ##slabname(struct slabname *s) \
37 for (i = 0; i < s->slab_count; i++) \
40 FREE_AND_NULL(s->slab); \
43 scope elemtype *slabname## _at_peek(struct slabname *s, \
44 const struct commit *c, \
47 unsigned int nth_slab, nth_slot; \
49 nth_slab = c->index / s->slab_size; \
50 nth_slot = c->index % s->slab_size; \
52 if (s->slab_count <= nth_slab) { \
54 if (!add_if_missing) \
56 REALLOC_ARRAY(s->slab, nth_slab + 1); \
57 stat_ ##slabname## realloc++; \
58 for (i = s->slab_count; i <= nth_slab; i++) \
60 s->slab_count = nth_slab + 1; \
62 if (!s->slab[nth_slab]) { \
63 if (!add_if_missing) \
65 s->slab[nth_slab] = xcalloc(s->slab_size, \
66 sizeof(**s->slab) * s->stride); \
68 return &s->slab[nth_slab][nth_slot * s->stride]; \
71 scope elemtype *slabname## _at(struct slabname *s, \
72 const struct commit *c) \
74 return slabname##_at_peek(s, c, 1); \
77 scope elemtype *slabname## _peek(struct slabname *s, \
78 const struct commit *c) \
80 return slabname##_at_peek(s, c, 0); \
86 * Note that this redundant forward declaration is required
87 * to allow a terminating semicolon, which makes instantiations look
88 * like function declarations. I.e., the expansion of
90 * implement_commit_slab(indegree, int, static);
92 * ends in 'struct indegree;'. This would otherwise
93 * be a syntax error according (at least) to ISO C. It's hard to
94 * catch because GCC silently parses it by default.
97 #endif /* COMMIT_SLAB_IMPL_H */