Commit | Line | Data |
---|---|---|
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 | * | |
11 | * define_commit_slab(indegee, int); | |
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 | |
18 | * the indegree slab, and returns the pointer to it. | |
19 | * | |
20 | * - void init_indegree(struct indegree *); | |
21 | * void init_indegree_with_stride(struct indegree *, int); | |
22 | * | |
23 | * Initializes the indegree slab that associates an array of integers | |
24 | * to each commit. 'stride' specifies how big each array is. The slab | |
dcbbc8fa | 25 | * that is initialized by the variant without "_with_stride" associates |
a84b794a | 26 | * each commit with an array of one integer. |
dcbbc8fa TR |
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. | |
a84b794a JH |
36 | */ |
37 | ||
38 | /* allocate ~512kB at once, allowing for malloc overhead */ | |
39 | #ifndef COMMIT_SLAB_SIZE | |
40 | #define COMMIT_SLAB_SIZE (512*1024-32) | |
41 | #endif | |
42 | ||
43 | #define define_commit_slab(slabname, elemtype) \ | |
44 | \ | |
45 | struct slabname { \ | |
46 | unsigned slab_size; \ | |
47 | unsigned stride; \ | |
48 | unsigned slab_count; \ | |
49 | elemtype **slab; \ | |
50 | }; \ | |
51 | static int stat_ ##slabname## realloc; \ | |
52 | \ | |
53 | static void init_ ##slabname## _with_stride(struct slabname *s, \ | |
54 | unsigned stride) \ | |
55 | { \ | |
56 | unsigned int elem_size; \ | |
57 | if (!stride) \ | |
58 | stride = 1; \ | |
59 | s->stride = stride; \ | |
d7a1d629 | 60 | elem_size = sizeof(elemtype) * stride; \ |
a84b794a JH |
61 | s->slab_size = COMMIT_SLAB_SIZE / elem_size; \ |
62 | s->slab_count = 0; \ | |
63 | s->slab = NULL; \ | |
64 | } \ | |
65 | \ | |
66 | static void init_ ##slabname(struct slabname *s) \ | |
67 | { \ | |
68 | init_ ##slabname## _with_stride(s, 1); \ | |
69 | } \ | |
70 | \ | |
71 | static void clear_ ##slabname(struct slabname *s) \ | |
72 | { \ | |
73 | int i; \ | |
74 | for (i = 0; i < s->slab_count; i++) \ | |
75 | free(s->slab[i]); \ | |
76 | s->slab_count = 0; \ | |
77 | free(s->slab); \ | |
78 | s->slab = NULL; \ | |
79 | } \ | |
80 | \ | |
81 | static elemtype *slabname## _at(struct slabname *s, \ | |
82 | const struct commit *c) \ | |
83 | { \ | |
d7a1d629 | 84 | int nth_slab, nth_slot; \ |
a84b794a | 85 | \ |
d7a1d629 RJ |
86 | nth_slab = c->index / s->slab_size; \ |
87 | nth_slot = c->index % s->slab_size; \ | |
a84b794a JH |
88 | \ |
89 | if (s->slab_count <= nth_slab) { \ | |
90 | int i; \ | |
91 | s->slab = xrealloc(s->slab, \ | |
92 | (nth_slab + 1) * sizeof(s->slab)); \ | |
93 | stat_ ##slabname## realloc++; \ | |
94 | for (i = s->slab_count; i <= nth_slab; i++) \ | |
95 | s->slab[i] = NULL; \ | |
96 | s->slab_count = nth_slab + 1; \ | |
97 | } \ | |
98 | if (!s->slab[nth_slab]) \ | |
99 | s->slab[nth_slab] = xcalloc(s->slab_size, \ | |
d7a1d629 RJ |
100 | sizeof(**s->slab) * s->stride); \ |
101 | return &s->slab[nth_slab][nth_slot * s->stride]; \ | |
a84b794a JH |
102 | } \ |
103 | \ | |
104 | static int stat_ ##slabname## realloc | |
105 | ||
106 | #endif /* COMMIT_SLAB_H */ |