22
22
*
23
23
* Initializes the indegree slab that associates an array of integers
24
24
* to each commit. 'stride' specifies how big each array is. The slab
25
- * that id initialied by the variant without "_with_stride" associates
25
+ * that is initialized by the variant without "_with_stride" associates
26
26
* each commit with an array of one integer.
27
+ *
28
+ * - void clear_indegree(struct indegree *);
29
+ *
30
+ * Empties the slab. The slab can be reused with the same stride
31
+ * without calling init_indegree() again or can be reconfigured to a
32
+ * different stride by calling init_indegree_with_stride().
33
+ *
34
+ * Call this function before the slab falls out of scope to avoid
35
+ * leaking memory.
27
36
*/
28
37
29
38
/* allocate ~512kB at once, allowing for malloc overhead */
30
39
#ifndef COMMIT_SLAB_SIZE
31
40
#define COMMIT_SLAB_SIZE (512*1024-32)
32
41
#endif
33
42
43
+ #define MAYBE_UNUSED __attribute__((__unused__))
44
+
34
45
#define define_commit_slab (slabname , elemtype ) \
35
46
\
36
47
struct slabname { \
@@ -41,8 +52,8 @@ struct slabname { \
41
52
}; \
42
53
static int stat_ ##slabname## realloc; \
43
54
\
44
- static void init_ ##slabname## _with_stride(struct slabname *s, \
45
- unsigned stride) \
55
+ static MAYBE_UNUSED void init_ ##slabname## _with_stride(struct slabname *s, \
56
+ unsigned stride) \
46
57
{ \
47
58
unsigned int elem_size; \
48
59
if (!stride) \
@@ -54,12 +65,12 @@ static void init_ ##slabname## _with_stride(struct slabname *s, \
54
65
s->slab = NULL; \
55
66
} \
56
67
\
57
- static void init_ ##slabname(struct slabname *s) \
68
+ static MAYBE_UNUSED void init_ ##slabname(struct slabname *s) \
58
69
{ \
59
70
init_ ##slabname## _with_stride(s, 1); \
60
71
} \
61
72
\
62
- static void clear_ ##slabname(struct slabname *s) \
73
+ static MAYBE_UNUSED void clear_ ##slabname(struct slabname *s) \
63
74
{ \
64
75
int i; \
65
76
for (i = 0; i < s->slab_count; i++) \
@@ -69,8 +80,8 @@ static void clear_ ##slabname(struct slabname *s) \
69
80
s->slab = NULL; \
70
81
} \
71
82
\
72
- static elemtype *slabname## _at(struct slabname *s, \
73
- const struct commit *c) \
83
+ static MAYBE_UNUSED elemtype *slabname## _at(struct slabname *s, \
84
+ const struct commit *c) \
74
85
{ \
75
86
int nth_slab, nth_slot; \
76
87
\
@@ -80,7 +91,7 @@ static elemtype *slabname## _at(struct slabname *s, \
80
91
if (s->slab_count <= nth_slab) { \
81
92
int i; \
82
93
s->slab = xrealloc(s->slab, \
83
- (nth_slab + 1) * sizeof(s->slab)); \
94
+ (nth_slab + 1) * sizeof(* s->slab)); \
84
95
stat_ ##slabname## realloc++; \
85
96
for (i = s->slab_count; i <= nth_slab; i++) \
86
97
s->slab[i] = NULL; \
@@ -94,4 +105,16 @@ static elemtype *slabname## _at(struct slabname *s, \
94
105
\
95
106
static int stat_ ##slabname## realloc
96
107
108
+ /*
109
+ * Note that this seemingly redundant second declaration is required
110
+ * to allow a terminating semicolon, which makes instantiations look
111
+ * like function declarations. I.e., the expansion of
112
+ *
113
+ * define_commit_slab(indegree, int);
114
+ *
115
+ * ends in 'static int stat_indegreerealloc;'. This would otherwise
116
+ * be a syntax error according (at least) to ISO C. It's hard to
117
+ * catch because GCC silently parses it by default.
118
+ */
119
+
97
120
#endif /* COMMIT_SLAB_H */
0 commit comments