commit-slab: support shared commit-slab

define_shared_commit_slab() could be used in a header file to define a commit-slab. One of these C files must include commit-slab-impl.h and "call" implement_shared_commit_slab(). Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed May 19, 2018 at 07:28 UTC 878f0bb81990170cde5dba934fcd1ae4c2e698ee
3 files changed +28 -9
commit-slab-decl.h
+13
@@ -27,4 +27,17 @@ struct slabname { \
27 (stride), 0, NULL \
28 }
29
30 +#define declare_commit_slab_prototypes(slabname, elemtype) \
31 + \
32 +void init_ ##slabname## _with_stride(struct slabname *s, unsigned stride); \
33 +void init_ ##slabname(struct slabname *s); \
34 +void clear_ ##slabname(struct slabname *s); \
35 +elemtype *slabname## _at_peek(struct slabname *s, const struct commit *c, int add_if_missing); \
36 +elemtype *slabname## _at(struct slabname *s, const struct commit *c); \
37 +elemtype *slabname## _peek(struct slabname *s, const struct commit *c)
38 +
39 +#define define_shared_commit_slab(slabname, elemtype) \
40 + declare_commit_slab(slabname, elemtype); \
41 + declare_commit_slab_prototypes(slabname, elemtype)
42 +
43 #endif /* COMMIT_SLAB_HDR_H */
commit-slab-impl.h
+14 -8
@@ -3,11 +3,17 @@
3
4 #define MAYBE_UNUSED __attribute__((__unused__))
5
6 -#define implement_commit_slab(slabname, elemtype) \
6 +#define implement_static_commit_slab(slabname, elemtype) \
7 + implement_commit_slab(slabname, elemtype, static MAYBE_UNUSED)
8 +
9 +#define implement_shared_commit_slab(slabname, elemtype) \
10 + implement_commit_slab(slabname, elemtype, )
11 +
12 +#define implement_commit_slab(slabname, elemtype, scope) \
13 \
14 static int stat_ ##slabname## realloc; \
15 \
10 -static MAYBE_UNUSED void init_ ##slabname## _with_stride(struct slabname *s, \
16 +scope void init_ ##slabname## _with_stride(struct slabname *s, \
17 unsigned stride) \
18 { \
19 unsigned int elem_size; \
@@ -20,12 +26,12 @@ static MAYBE_UNUSED void init_ ##slabname## _with_stride(struct slabname *s, \
26 s->slab = NULL; \
27 } \
28 \
23 -static MAYBE_UNUSED void init_ ##slabname(struct slabname *s) \
29 +scope void init_ ##slabname(struct slabname *s) \
30 { \
31 init_ ##slabname## _with_stride(s, 1); \
32 } \
33 \
28 -static MAYBE_UNUSED void clear_ ##slabname(struct slabname *s) \
34 +scope void clear_ ##slabname(struct slabname *s) \
35 { \
36 unsigned int i; \
37 for (i = 0; i < s->slab_count; i++) \
@@ -34,7 +40,7 @@ static MAYBE_UNUSED void clear_ ##slabname(struct slabname *s) \
40 FREE_AND_NULL(s->slab); \
41 } \
42 \
37 -static MAYBE_UNUSED elemtype *slabname## _at_peek(struct slabname *s, \
43 +scope elemtype *slabname## _at_peek(struct slabname *s, \
44 const struct commit *c, \
45 int add_if_missing) \
46 { \
@@ -62,13 +68,13 @@ static MAYBE_UNUSED elemtype *slabname## _at_peek(struct slabname *s, \
68 return &s->slab[nth_slab][nth_slot * s->stride]; \
69 } \
70 \
65 -static MAYBE_UNUSED elemtype *slabname## _at(struct slabname *s, \
71 +scope elemtype *slabname## _at(struct slabname *s, \
72 const struct commit *c) \
73 { \
74 return slabname##_at_peek(s, c, 1); \
75 } \
76 \
71 -static MAYBE_UNUSED elemtype *slabname## _peek(struct slabname *s, \
77 +scope elemtype *slabname## _peek(struct slabname *s, \
78 const struct commit *c) \
79 { \
80 return slabname##_at_peek(s, c, 0); \
@@ -81,7 +87,7 @@ struct slabname
87 * to allow a terminating semicolon, which makes instantiations look
88 * like function declarations. I.e., the expansion of
89 *
84 - * implement_commit_slab(indegree, int);
90 + * implement_commit_slab(indegree, int, static);
91 *
92 * ends in 'struct indegree;'. This would otherwise
93 * be a syntax error according (at least) to ISO C. It's hard to
commit-slab.h
+1 -1
@@ -46,6 +46,6 @@
46
47 #define define_commit_slab(slabname, elemtype) \
48 declare_commit_slab(slabname, elemtype); \
49 - implement_commit_slab(slabname, elemtype)
49 + implement_static_commit_slab(slabname, elemtype)
50
51 #endif /* COMMIT_SLAB_H */