Junio C Hamano | a84b794 | 2013-04-13 11:56:41 -0700 | [diff] [blame] | 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 | * |
Ville Skyttä | 2e3a16b | 2016-08-09 11:53:38 +0300 | [diff] [blame] | 11 | * define_commit_slab(indegree, int); |
Junio C Hamano | a84b794 | 2013-04-13 11:56:41 -0700 | [diff] [blame] | 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 |
Junio C Hamano | 862e730 | 2015-05-14 15:25:52 -0700 | [diff] [blame] | 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. |
Junio C Hamano | a84b794 | 2013-04-13 11:56:41 -0700 | [diff] [blame] | 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 |
Thomas Rast | dcbbc8f | 2013-11-25 20:02:00 +0100 | [diff] [blame] | 31 | * that is initialized by the variant without "_with_stride" associates |
Junio C Hamano | a84b794 | 2013-04-13 11:56:41 -0700 | [diff] [blame] | 32 | * each commit with an array of one integer. |
Thomas Rast | dcbbc8f | 2013-11-25 20:02:00 +0100 | [diff] [blame] | 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. |
Junio C Hamano | a84b794 | 2013-04-13 11:56:41 -0700 | [diff] [blame] | 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 | |
Thomas Rast | e9e03a7 | 2013-11-25 21:04:08 +0100 | [diff] [blame] | 49 | #define MAYBE_UNUSED __attribute__((__unused__)) |
| 50 | |
Junio C Hamano | a84b794 | 2013-04-13 11:56:41 -0700 | [diff] [blame] | 51 | #define define_commit_slab(slabname, elemtype) \ |
| 52 | \ |
| 53 | struct slabname { \ |
| 54 | unsigned slab_size; \ |
| 55 | unsigned stride; \ |
| 56 | unsigned slab_count; \ |
| 57 | elemtype **slab; \ |
| 58 | }; \ |
| 59 | static int stat_ ##slabname## realloc; \ |
| 60 | \ |
Thomas Rast | e9e03a7 | 2013-11-25 21:04:08 +0100 | [diff] [blame] | 61 | static MAYBE_UNUSED void init_ ##slabname## _with_stride(struct slabname *s, \ |
| 62 | unsigned stride) \ |
Junio C Hamano | a84b794 | 2013-04-13 11:56:41 -0700 | [diff] [blame] | 63 | { \ |
| 64 | unsigned int elem_size; \ |
| 65 | if (!stride) \ |
| 66 | stride = 1; \ |
| 67 | s->stride = stride; \ |
Ramsay Jones | d7a1d62 | 2013-07-27 20:00:07 +0100 | [diff] [blame] | 68 | elem_size = sizeof(elemtype) * stride; \ |
Junio C Hamano | a84b794 | 2013-04-13 11:56:41 -0700 | [diff] [blame] | 69 | s->slab_size = COMMIT_SLAB_SIZE / elem_size; \ |
| 70 | s->slab_count = 0; \ |
| 71 | s->slab = NULL; \ |
| 72 | } \ |
| 73 | \ |
Thomas Rast | e9e03a7 | 2013-11-25 21:04:08 +0100 | [diff] [blame] | 74 | static MAYBE_UNUSED void init_ ##slabname(struct slabname *s) \ |
Junio C Hamano | a84b794 | 2013-04-13 11:56:41 -0700 | [diff] [blame] | 75 | { \ |
| 76 | init_ ##slabname## _with_stride(s, 1); \ |
| 77 | } \ |
| 78 | \ |
Thomas Rast | e9e03a7 | 2013-11-25 21:04:08 +0100 | [diff] [blame] | 79 | static MAYBE_UNUSED void clear_ ##slabname(struct slabname *s) \ |
Junio C Hamano | a84b794 | 2013-04-13 11:56:41 -0700 | [diff] [blame] | 80 | { \ |
| 81 | int i; \ |
| 82 | for (i = 0; i < s->slab_count; i++) \ |
| 83 | free(s->slab[i]); \ |
| 84 | s->slab_count = 0; \ |
| 85 | free(s->slab); \ |
| 86 | s->slab = NULL; \ |
| 87 | } \ |
| 88 | \ |
Junio C Hamano | 862e730 | 2015-05-14 15:25:52 -0700 | [diff] [blame] | 89 | static MAYBE_UNUSED elemtype *slabname## _at_peek(struct slabname *s, \ |
| 90 | const struct commit *c, \ |
| 91 | int add_if_missing) \ |
Junio C Hamano | a84b794 | 2013-04-13 11:56:41 -0700 | [diff] [blame] | 92 | { \ |
Ramsay Jones | d7a1d62 | 2013-07-27 20:00:07 +0100 | [diff] [blame] | 93 | int nth_slab, nth_slot; \ |
Junio C Hamano | a84b794 | 2013-04-13 11:56:41 -0700 | [diff] [blame] | 94 | \ |
Ramsay Jones | d7a1d62 | 2013-07-27 20:00:07 +0100 | [diff] [blame] | 95 | nth_slab = c->index / s->slab_size; \ |
| 96 | nth_slot = c->index % s->slab_size; \ |
Junio C Hamano | a84b794 | 2013-04-13 11:56:41 -0700 | [diff] [blame] | 97 | \ |
| 98 | if (s->slab_count <= nth_slab) { \ |
| 99 | int i; \ |
Junio C Hamano | 862e730 | 2015-05-14 15:25:52 -0700 | [diff] [blame] | 100 | if (!add_if_missing) \ |
| 101 | return NULL; \ |
René Scharfe | 2756ca4 | 2014-09-16 20:56:57 +0200 | [diff] [blame] | 102 | REALLOC_ARRAY(s->slab, nth_slab + 1); \ |
Junio C Hamano | a84b794 | 2013-04-13 11:56:41 -0700 | [diff] [blame] | 103 | stat_ ##slabname## realloc++; \ |
| 104 | for (i = s->slab_count; i <= nth_slab; i++) \ |
| 105 | s->slab[i] = NULL; \ |
| 106 | s->slab_count = nth_slab + 1; \ |
| 107 | } \ |
Junio C Hamano | 862e730 | 2015-05-14 15:25:52 -0700 | [diff] [blame] | 108 | if (!s->slab[nth_slab]) { \ |
| 109 | if (!add_if_missing) \ |
| 110 | return NULL; \ |
Junio C Hamano | a84b794 | 2013-04-13 11:56:41 -0700 | [diff] [blame] | 111 | s->slab[nth_slab] = xcalloc(s->slab_size, \ |
Ramsay Jones | d7a1d62 | 2013-07-27 20:00:07 +0100 | [diff] [blame] | 112 | sizeof(**s->slab) * s->stride); \ |
Junio C Hamano | 862e730 | 2015-05-14 15:25:52 -0700 | [diff] [blame] | 113 | } \ |
| 114 | return &s->slab[nth_slab][nth_slot * s->stride]; \ |
| 115 | } \ |
| 116 | \ |
| 117 | static MAYBE_UNUSED elemtype *slabname## _at(struct slabname *s, \ |
| 118 | const struct commit *c) \ |
| 119 | { \ |
| 120 | return slabname##_at_peek(s, c, 1); \ |
| 121 | } \ |
| 122 | \ |
| 123 | static MAYBE_UNUSED elemtype *slabname## _peek(struct slabname *s, \ |
| 124 | const struct commit *c) \ |
| 125 | { \ |
| 126 | return slabname##_at_peek(s, c, 0); \ |
Junio C Hamano | a84b794 | 2013-04-13 11:56:41 -0700 | [diff] [blame] | 127 | } \ |
| 128 | \ |
Johannes Sixt | af920e3 | 2016-08-09 16:17:56 +0200 | [diff] [blame] | 129 | struct slabname |
Junio C Hamano | a84b794 | 2013-04-13 11:56:41 -0700 | [diff] [blame] | 130 | |
Thomas Rast | e9e03a7 | 2013-11-25 21:04:08 +0100 | [diff] [blame] | 131 | /* |
Johannes Sixt | af920e3 | 2016-08-09 16:17:56 +0200 | [diff] [blame] | 132 | * Note that this redundant forward declaration is required |
Thomas Rast | e9e03a7 | 2013-11-25 21:04:08 +0100 | [diff] [blame] | 133 | * to allow a terminating semicolon, which makes instantiations look |
| 134 | * like function declarations. I.e., the expansion of |
| 135 | * |
| 136 | * define_commit_slab(indegree, int); |
| 137 | * |
Johannes Sixt | af920e3 | 2016-08-09 16:17:56 +0200 | [diff] [blame] | 138 | * ends in 'struct indegree;'. This would otherwise |
Thomas Rast | e9e03a7 | 2013-11-25 21:04:08 +0100 | [diff] [blame] | 139 | * be a syntax error according (at least) to ISO C. It's hard to |
| 140 | * catch because GCC silently parses it by default. |
| 141 | */ |
| 142 | |
Jeff King | 80cdaba | 2014-06-10 17:42:51 -0400 | [diff] [blame] | 143 | /* |
| 144 | * Statically initialize a commit slab named "var". Note that this |
| 145 | * evaluates "stride" multiple times! Example: |
| 146 | * |
| 147 | * struct indegree indegrees = COMMIT_SLAB_INIT(1, indegrees); |
| 148 | * |
| 149 | */ |
| 150 | #define COMMIT_SLAB_INIT(stride, var) { \ |
| 151 | COMMIT_SLAB_SIZE / sizeof(**((var).slab)) / (stride), \ |
| 152 | (stride), 0, NULL \ |
| 153 | } |
| 154 | |
Junio C Hamano | a84b794 | 2013-04-13 11:56:41 -0700 | [diff] [blame] | 155 | #endif /* COMMIT_SLAB_H */ |