The fifth batch for 2.18
[git/git.git] / commit-slab.h
CommitLineData
a84b794a
JH
1#ifndef COMMIT_SLAB_H
2#define COMMIT_SLAB_H
3
4/*
5 * define_commit_slab(slabname, elemtype) creates boilerplate code to define
6 * a new struct (struct slabname) that is used to associate a piece of data
7 * of elemtype to commits, and a few functions to use that struct.
8 *
9 * After including this header file, using:
10 *
2e3a16b2 11 * define_commit_slab(indegree, int);
a84b794a
JH
12 *
13 * will let you call the following functions:
14 *
15 * - int *indegree_at(struct indegree *, struct commit *);
16 *
17 * This function locates the data associated with the given commit in
862e730e
JH
18 * the indegree slab, and returns the pointer to it. The location to
19 * store the data is allocated as necessary.
20 *
21 * - int *indegree_peek(struct indegree *, struct commit *);
22 *
23 * This function is similar to indegree_at(), but it will return NULL
24 * until a call to indegree_at() was made for the commit.
a84b794a
JH
25 *
26 * - void init_indegree(struct indegree *);
27 * void init_indegree_with_stride(struct indegree *, int);
28 *
29 * Initializes the indegree slab that associates an array of integers
30 * to each commit. 'stride' specifies how big each array is. The slab
dcbbc8fa 31 * that is initialized by the variant without "_with_stride" associates
a84b794a 32 * each commit with an array of one integer.
dcbbc8fa
TR
33 *
34 * - void clear_indegree(struct indegree *);
35 *
36 * Empties the slab. The slab can be reused with the same stride
37 * without calling init_indegree() again or can be reconfigured to a
38 * different stride by calling init_indegree_with_stride().
39 *
40 * Call this function before the slab falls out of scope to avoid
41 * leaking memory.
a84b794a
JH
42 */
43
44/* allocate ~512kB at once, allowing for malloc overhead */
45#ifndef COMMIT_SLAB_SIZE
46#define COMMIT_SLAB_SIZE (512*1024-32)
47#endif
48
e9e03a77
TR
49#define MAYBE_UNUSED __attribute__((__unused__))
50
a84b794a
JH
51#define define_commit_slab(slabname, elemtype) \
52 \
53struct slabname { \
54 unsigned slab_size; \
55 unsigned stride; \
56 unsigned slab_count; \
57 elemtype **slab; \
58}; \
59static int stat_ ##slabname## realloc; \
60 \
e9e03a77
TR
61static MAYBE_UNUSED void init_ ##slabname## _with_stride(struct slabname *s, \
62 unsigned stride) \
a84b794a
JH
63{ \
64 unsigned int elem_size; \
65 if (!stride) \
66 stride = 1; \
67 s->stride = stride; \
d7a1d629 68 elem_size = sizeof(elemtype) * stride; \
a84b794a
JH
69 s->slab_size = COMMIT_SLAB_SIZE / elem_size; \
70 s->slab_count = 0; \
71 s->slab = NULL; \
72} \
73 \
e9e03a77 74static MAYBE_UNUSED void init_ ##slabname(struct slabname *s) \
a84b794a
JH
75{ \
76 init_ ##slabname## _with_stride(s, 1); \
77} \
78 \
e9e03a77 79static MAYBE_UNUSED void clear_ ##slabname(struct slabname *s) \
a84b794a 80{ \
fddfedc3 81 unsigned int i; \
a84b794a
JH
82 for (i = 0; i < s->slab_count; i++) \
83 free(s->slab[i]); \
84 s->slab_count = 0; \
88ce3ef6 85 FREE_AND_NULL(s->slab); \
a84b794a
JH
86} \
87 \
862e730e
JH
88static MAYBE_UNUSED elemtype *slabname## _at_peek(struct slabname *s, \
89 const struct commit *c, \
90 int add_if_missing) \
a84b794a 91{ \
fddfedc3 92 unsigned int nth_slab, nth_slot; \
a84b794a 93 \
d7a1d629
RJ
94 nth_slab = c->index / s->slab_size; \
95 nth_slot = c->index % s->slab_size; \
a84b794a
JH
96 \
97 if (s->slab_count <= nth_slab) { \
fddfedc3 98 unsigned int i; \
862e730e
JH
99 if (!add_if_missing) \
100 return NULL; \
2756ca43 101 REALLOC_ARRAY(s->slab, nth_slab + 1); \
a84b794a
JH
102 stat_ ##slabname## realloc++; \
103 for (i = s->slab_count; i <= nth_slab; i++) \
104 s->slab[i] = NULL; \
105 s->slab_count = nth_slab + 1; \
106 } \
862e730e
JH
107 if (!s->slab[nth_slab]) { \
108 if (!add_if_missing) \
109 return NULL; \
a84b794a 110 s->slab[nth_slab] = xcalloc(s->slab_size, \
d7a1d629 111 sizeof(**s->slab) * s->stride); \
862e730e
JH
112 } \
113 return &s->slab[nth_slab][nth_slot * s->stride]; \
114} \
115 \
116static MAYBE_UNUSED elemtype *slabname## _at(struct slabname *s, \
117 const struct commit *c) \
118{ \
119 return slabname##_at_peek(s, c, 1); \
120} \
121 \
122static MAYBE_UNUSED elemtype *slabname## _peek(struct slabname *s, \
123 const struct commit *c) \
124{ \
125 return slabname##_at_peek(s, c, 0); \
a84b794a
JH
126} \
127 \
af920e36 128struct slabname
a84b794a 129
e9e03a77 130/*
af920e36 131 * Note that this redundant forward declaration is required
e9e03a77
TR
132 * to allow a terminating semicolon, which makes instantiations look
133 * like function declarations. I.e., the expansion of
134 *
135 * define_commit_slab(indegree, int);
136 *
af920e36 137 * ends in 'struct indegree;'. This would otherwise
e9e03a77
TR
138 * be a syntax error according (at least) to ISO C. It's hard to
139 * catch because GCC silently parses it by default.
140 */
141
80cdaba5
JK
142/*
143 * Statically initialize a commit slab named "var". Note that this
144 * evaluates "stride" multiple times! Example:
145 *
146 * struct indegree indegrees = COMMIT_SLAB_INIT(1, indegrees);
147 *
148 */
149#define COMMIT_SLAB_INIT(stride, var) { \
150 COMMIT_SLAB_SIZE / sizeof(**((var).slab)) / (stride), \
151 (stride), 0, NULL \
152}
153
a84b794a 154#endif /* COMMIT_SLAB_H */