Skip to content

Commit df5f0ad

Browse files
committed
Merge branch 'tr/commit-slab-cleanup'
* tr/commit-slab-cleanup: commit-slab: sizeof() the right type in xrealloc commit-slab: declare functions "static inline" commit-slab: document clear_$slabname()
2 parents fca26a3 + cde0a05 commit df5f0ad

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

commit-slab.h

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,26 @@
2222
*
2323
* Initializes the indegree slab that associates an array of integers
2424
* 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
2626
* 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.
2736
*/
2837

2938
/* allocate ~512kB at once, allowing for malloc overhead */
3039
#ifndef COMMIT_SLAB_SIZE
3140
#define COMMIT_SLAB_SIZE (512*1024-32)
3241
#endif
3342

43+
#define MAYBE_UNUSED __attribute__((__unused__))
44+
3445
#define define_commit_slab(slabname, elemtype) \
3546
\
3647
struct slabname { \
@@ -41,8 +52,8 @@ struct slabname { \
4152
}; \
4253
static int stat_ ##slabname## realloc; \
4354
\
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) \
4657
{ \
4758
unsigned int elem_size; \
4859
if (!stride) \
@@ -54,12 +65,12 @@ static void init_ ##slabname## _with_stride(struct slabname *s, \
5465
s->slab = NULL; \
5566
} \
5667
\
57-
static void init_ ##slabname(struct slabname *s) \
68+
static MAYBE_UNUSED void init_ ##slabname(struct slabname *s) \
5869
{ \
5970
init_ ##slabname## _with_stride(s, 1); \
6071
} \
6172
\
62-
static void clear_ ##slabname(struct slabname *s) \
73+
static MAYBE_UNUSED void clear_ ##slabname(struct slabname *s) \
6374
{ \
6475
int i; \
6576
for (i = 0; i < s->slab_count; i++) \
@@ -69,8 +80,8 @@ static void clear_ ##slabname(struct slabname *s) \
6980
s->slab = NULL; \
7081
} \
7182
\
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) \
7485
{ \
7586
int nth_slab, nth_slot; \
7687
\
@@ -80,7 +91,7 @@ static elemtype *slabname## _at(struct slabname *s, \
8091
if (s->slab_count <= nth_slab) { \
8192
int i; \
8293
s->slab = xrealloc(s->slab, \
83-
(nth_slab + 1) * sizeof(s->slab)); \
94+
(nth_slab + 1) * sizeof(*s->slab)); \
8495
stat_ ##slabname## realloc++; \
8596
for (i = s->slab_count; i <= nth_slab; i++) \
8697
s->slab[i] = NULL; \
@@ -94,4 +105,16 @@ static elemtype *slabname## _at(struct slabname *s, \
94105
\
95106
static int stat_ ##slabname## realloc
96107

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+
97120
#endif /* COMMIT_SLAB_H */

0 commit comments

Comments
 (0)