blob: 557738df271c7dd78c5ddcc65d6fe6e47c665758 [file] [log] [blame]
Nguyễn Thái Ngọc Duya9f1f1f2018-05-19 07:28:17 +02001#ifndef COMMIT_SLAB_IMPL_H
2#define COMMIT_SLAB_IMPL_H
3
Carlo Marcelo Arenas Belónbbd8eb32018-10-23 14:50:19 -07004#include "git-compat-util.h"
Nguyễn Thái Ngọc Duya9f1f1f2018-05-19 07:28:17 +02005
Nguyễn Thái Ngọc Duy878f0bb2018-05-19 07:28:18 +02006#define implement_static_commit_slab(slabname, elemtype) \
Carlo Marcelo Arenas Belónbbd8eb32018-10-23 14:50:19 -07007 implement_commit_slab(slabname, elemtype, MAYBE_UNUSED static)
Nguyễn Thái Ngọc Duy878f0bb2018-05-19 07:28:18 +02008
9#define implement_shared_commit_slab(slabname, elemtype) \
10 implement_commit_slab(slabname, elemtype, )
11
12#define implement_commit_slab(slabname, elemtype, scope) \
Nguyễn Thái Ngọc Duya9f1f1f2018-05-19 07:28:17 +020013 \
Nguyễn Thái Ngọc Duy878f0bb2018-05-19 07:28:18 +020014scope void init_ ##slabname## _with_stride(struct slabname *s, \
Nguyễn Thái Ngọc Duya9f1f1f2018-05-19 07:28:17 +020015 unsigned stride) \
16{ \
17 unsigned int elem_size; \
18 if (!stride) \
19 stride = 1; \
20 s->stride = stride; \
21 elem_size = sizeof(elemtype) * stride; \
22 s->slab_size = COMMIT_SLAB_SIZE / elem_size; \
23 s->slab_count = 0; \
24 s->slab = NULL; \
25} \
26 \
Nguyễn Thái Ngọc Duy878f0bb2018-05-19 07:28:18 +020027scope void init_ ##slabname(struct slabname *s) \
Nguyễn Thái Ngọc Duya9f1f1f2018-05-19 07:28:17 +020028{ \
29 init_ ##slabname## _with_stride(s, 1); \
30} \
31 \
Nguyễn Thái Ngọc Duy878f0bb2018-05-19 07:28:18 +020032scope void clear_ ##slabname(struct slabname *s) \
Nguyễn Thái Ngọc Duya9f1f1f2018-05-19 07:28:17 +020033{ \
34 unsigned int i; \
35 for (i = 0; i < s->slab_count; i++) \
36 free(s->slab[i]); \
37 s->slab_count = 0; \
38 FREE_AND_NULL(s->slab); \
39} \
40 \
SZEDER Gábor1df15f82020-06-05 13:00:26 +000041scope void deep_clear_ ##slabname(struct slabname *s, void (*free_fn)(elemtype *)) \
42{ \
43 unsigned int i; \
44 for (i = 0; i < s->slab_count; i++) { \
45 unsigned int j; \
46 if (!s->slab[i]) \
47 continue; \
48 for (j = 0; j < s->slab_size; j++) \
49 free_fn(&s->slab[i][j * s->stride]); \
50 } \
51 clear_ ##slabname(s); \
52} \
53 \
Nguyễn Thái Ngọc Duy878f0bb2018-05-19 07:28:18 +020054scope elemtype *slabname## _at_peek(struct slabname *s, \
Nguyễn Thái Ngọc Duya9f1f1f2018-05-19 07:28:17 +020055 const struct commit *c, \
56 int add_if_missing) \
57{ \
58 unsigned int nth_slab, nth_slot; \
59 \
60 nth_slab = c->index / s->slab_size; \
61 nth_slot = c->index % s->slab_size; \
62 \
63 if (s->slab_count <= nth_slab) { \
64 unsigned int i; \
65 if (!add_if_missing) \
66 return NULL; \
67 REALLOC_ARRAY(s->slab, nth_slab + 1); \
Nguyễn Thái Ngọc Duya9f1f1f2018-05-19 07:28:17 +020068 for (i = s->slab_count; i <= nth_slab; i++) \
69 s->slab[i] = NULL; \
70 s->slab_count = nth_slab + 1; \
71 } \
72 if (!s->slab[nth_slab]) { \
73 if (!add_if_missing) \
74 return NULL; \
75 s->slab[nth_slab] = xcalloc(s->slab_size, \
76 sizeof(**s->slab) * s->stride); \
77 } \
78 return &s->slab[nth_slab][nth_slot * s->stride]; \
79} \
80 \
Nguyễn Thái Ngọc Duy878f0bb2018-05-19 07:28:18 +020081scope elemtype *slabname## _at(struct slabname *s, \
Nguyễn Thái Ngọc Duya9f1f1f2018-05-19 07:28:17 +020082 const struct commit *c) \
83{ \
84 return slabname##_at_peek(s, c, 1); \
85} \
86 \
Nguyễn Thái Ngọc Duy878f0bb2018-05-19 07:28:18 +020087scope elemtype *slabname## _peek(struct slabname *s, \
Nguyễn Thái Ngọc Duya9f1f1f2018-05-19 07:28:17 +020088 const struct commit *c) \
89{ \
90 return slabname##_at_peek(s, c, 0); \
91} \
92 \
93struct slabname
94
95/*
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
99 *
Nguyễn Thái Ngọc Duy878f0bb2018-05-19 07:28:18 +0200100 * implement_commit_slab(indegree, int, static);
Nguyễn Thái Ngọc Duya9f1f1f2018-05-19 07:28:17 +0200101 *
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.
105 */
106
107#endif /* COMMIT_SLAB_IMPL_H */