1 #ifndef COMMIT_SLAB_IMPL_H
2 #define COMMIT_SLAB_IMPL_H
4 #define MAYBE_UNUSED __attribute__((__unused__))
6 #define implement_commit_slab(slabname, elemtype) \
8 static int stat_ ##slabname## realloc; \
10 static MAYBE_UNUSED void init_ ##slabname## _with_stride(struct slabname *s, \
13 unsigned int elem_size; \
17 elem_size = sizeof(elemtype) * stride; \
18 s->slab_size = COMMIT_SLAB_SIZE / elem_size; \
23 static MAYBE_UNUSED void init_ ##slabname(struct slabname *s) \
25 init_ ##slabname## _with_stride(s, 1); \
28 static MAYBE_UNUSED void clear_ ##slabname(struct slabname *s) \
31 for (i = 0; i < s->slab_count; i++) \
34 FREE_AND_NULL(s->slab); \
37 static MAYBE_UNUSED elemtype *slabname## _at_peek(struct slabname *s, \
38 const struct commit *c, \
41 unsigned int nth_slab, nth_slot; \
43 nth_slab = c->index / s->slab_size; \
44 nth_slot = c->index % s->slab_size; \
46 if (s->slab_count <= nth_slab) { \
48 if (!add_if_missing) \
50 REALLOC_ARRAY(s->slab, nth_slab + 1); \
51 stat_ ##slabname## realloc++; \
52 for (i = s->slab_count; i <= nth_slab; i++) \
54 s->slab_count = nth_slab + 1; \
56 if (!s->slab[nth_slab]) { \
57 if (!add_if_missing) \
59 s->slab[nth_slab] = xcalloc(s->slab_size, \
60 sizeof(**s->slab) * s->stride); \
62 return &s->slab[nth_slab][nth_slot * s->stride]; \
65 static MAYBE_UNUSED elemtype *slabname## _at(struct slabname *s, \
66 const struct commit *c) \
68 return slabname##_at_peek(s, c, 1); \
71 static MAYBE_UNUSED elemtype *slabname## _peek(struct slabname *s, \
72 const struct commit *c) \
74 return slabname##_at_peek(s, c, 0); \
80 * Note that this redundant forward declaration is required
81 * to allow a terminating semicolon, which makes instantiations look
82 * like function declarations. I.e., the expansion of
84 * implement_commit_slab(indegree, int);
86 * ends in 'struct indegree;'. This would otherwise
87 * be a syntax error according (at least) to ISO C. It's hard to
88 * catch because GCC silently parses it by default.
91 #endif /* COMMIT_SLAB_IMPL_H */