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.
9 * After including this header file, using:
11 * define_commit_slab(indegee, int);
13 * will let you call the following functions:
15 * - int *indegree_at(struct indegree *, struct commit *);
17 * This function locates the data associated with the given commit in
18 * the indegree slab, and returns the pointer to it.
20 * - void init_indegree(struct indegree *);
21 * void init_indegree_with_stride(struct indegree *, int);
23 * Initializes the indegree slab that associates an array of integers
24 * to each commit. 'stride' specifies how big each array is. The slab
25 * that id initialied by the variant without "_with_stride" associates
26 * each commit with an array of one integer.
29 /* allocate ~512kB at once, allowing for malloc overhead */
30 #ifndef COMMIT_SLAB_SIZE
31 #define COMMIT_SLAB_SIZE (512*1024-32)
34 #define define_commit_slab(slabname, elemtype) \
39 unsigned slab_count; \
42 static int stat_ ##slabname## realloc; \
44 static void init_ ##slabname## _with_stride(struct slabname *s, \
47 unsigned int elem_size; \
51 elem_size = sizeof(elemtype) * stride; \
52 s->slab_size = COMMIT_SLAB_SIZE / elem_size; \
57 static void init_ ##slabname(struct slabname *s) \
59 init_ ##slabname## _with_stride(s, 1); \
62 static void clear_ ##slabname(struct slabname *s) \
65 for (i = 0; i < s->slab_count; i++) \
72 static elemtype *slabname## _at(struct slabname *s, \
73 const struct commit *c) \
75 int nth_slab, nth_slot; \
77 nth_slab = c->index / s->slab_size; \
78 nth_slot = c->index % s->slab_size; \
80 if (s->slab_count <= nth_slab) { \
82 s->slab = xrealloc(s->slab, \
83 (nth_slab + 1) * sizeof(s->slab)); \
84 stat_ ##slabname## realloc++; \
85 for (i = s->slab_count; i <= nth_slab; i++) \
87 s->slab_count = nth_slab + 1; \
89 if (!s->slab[nth_slab]) \
90 s->slab[nth_slab] = xcalloc(s->slab_size, \
91 sizeof(**s->slab) * s->stride); \
92 return &s->slab[nth_slab][nth_slot * s->stride]; \
95 static int stat_ ##slabname## realloc
97 #endif /* COMMIT_SLAB_H */