Git fork

commit-slab: add a function to deep free entries on the slab

clear_##slabname() frees only the memory allocated for a commit slab
itself, but entries in the commit slab might own additional memory
outside the slab that should be freed as well. We already have (at
least) one such commit slab, and this patch series is about to add one
more.

To free all additional memory owned by entries on the commit slab the
user of such a slab could iterate over all commits it knows about,
peek whether there is a valid entry associated with each commit, and
free the additional memory, if any. Or it could rely on intimate
knowledge about the internals of the commit slab implementation, and
could itself iterate directly through all entries in the slab, and
free the additional memory. Or it could just leak the additional
memory...

Introduce deep_clear_##slabname() to allow releasing memory owned by
commit slab entries by invoking the 'void free_fn(elemtype *ptr)'
function specified as parameter for each entry in the slab.

Use it in get_shallow_commits() in 'shallow.c' to replace an
open-coded iteration over a commit slab's entries.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

authored by

SZEDER Gábor and committed by
Junio C Hamano
1df15f8d 6141cdfd

+29 -9
+1
commit-slab-decl.h
··· 32 32 void init_ ##slabname## _with_stride(struct slabname *s, unsigned stride); \ 33 33 void init_ ##slabname(struct slabname *s); \ 34 34 void clear_ ##slabname(struct slabname *s); \ 35 + void deep_clear_ ##slabname(struct slabname *s, void (*free_fn)(elemtype *ptr)); \ 35 36 elemtype *slabname## _at_peek(struct slabname *s, const struct commit *c, int add_if_missing); \ 36 37 elemtype *slabname## _at(struct slabname *s, const struct commit *c); \ 37 38 elemtype *slabname## _peek(struct slabname *s, const struct commit *c)
+13
commit-slab-impl.h
··· 38 38 FREE_AND_NULL(s->slab); \ 39 39 } \ 40 40 \ 41 + scope void deep_clear_ ##slabname(struct slabname *s, void (*free_fn)(elemtype *)) \ 42 + { \ 43 + unsigned int i; \ 44 + for (i = 0; i < s->slab_count; i++) { \ 45 + unsigned int j; \ 46 + if (!s->slab[i]) \ 47 + continue; \ 48 + for (j = 0; j < s->slab_size; j++) \ 49 + free_fn(&s->slab[i][j * s->stride]); \ 50 + } \ 51 + clear_ ##slabname(s); \ 52 + } \ 53 + \ 41 54 scope elemtype *slabname## _at_peek(struct slabname *s, \ 42 55 const struct commit *c, \ 43 56 int add_if_missing) \
+10
commit-slab.h
··· 47 47 * 48 48 * Call this function before the slab falls out of scope to avoid 49 49 * leaking memory. 50 + * 51 + * - void deep_clear_indegree(struct indegree *, void (*free_fn)(int*)) 52 + * 53 + * Empties the slab, similar to clear_indegree(), but in addition it 54 + * calls the given 'free_fn' for each slab entry to release any 55 + * additional memory that might be owned by the entry (but not the 56 + * entry itself!). 57 + * Note that 'free_fn' might be called even for entries for which no 58 + * indegree_at() call has been made; in this case 'free_fn' is invoked 59 + * with a pointer to a zero-initialized location. 50 60 */ 51 61 52 62 #define define_commit_slab(slabname, elemtype) \
+5 -9
shallow.c
··· 84 84 * supports a "valid" flag. 85 85 */ 86 86 define_commit_slab(commit_depth, int *); 87 + static void free_depth_in_slab(int **ptr) 88 + { 89 + FREE_AND_NULL(*ptr); 90 + } 87 91 struct commit_list *get_shallow_commits(struct object_array *heads, int depth, 88 92 int shallow_flag, int not_shallow_flag) 89 93 { ··· 150 154 } 151 155 } 152 156 } 153 - for (i = 0; i < depths.slab_count; i++) { 154 - int j; 155 - 156 - if (!depths.slab[i]) 157 - continue; 158 - for (j = 0; j < depths.slab_size; j++) 159 - free(depths.slab[i][j]); 160 - } 161 - clear_commit_depth(&depths); 157 + deep_clear_commit_depth(&depths, free_depth_in_slab); 162 158 163 159 return result; 164 160 }