@matheusbsbs / matheus-bagliano / commits / 1df15f8dee
Fork 0

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>

SZEDER Gábor committed Jun 5, 2020 at 13:00 UTC 1df15f8dee5c637c9013d11d5b8e72e189a04f06
4 files changed +29 -9
commit-slab-decl.h
+1
@@ -32,6 +32,7 @@ struct slabname { \
32 void init_ ##slabname## _with_stride(struct slabname *s, unsigned stride); \
33 void init_ ##slabname(struct slabname *s); \
34 void clear_ ##slabname(struct slabname *s); \
35 +void deep_clear_ ##slabname(struct slabname *s, void (*free_fn)(elemtype *ptr)); \
36 elemtype *slabname## _at_peek(struct slabname *s, const struct commit *c, int add_if_missing); \
37 elemtype *slabname## _at(struct slabname *s, const struct commit *c); \
38 elemtype *slabname## _peek(struct slabname *s, const struct commit *c)
commit-slab-impl.h
+13
@@ -38,6 +38,19 @@ scope void clear_ ##slabname(struct slabname *s) \
38 FREE_AND_NULL(s->slab); \
39 } \
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 + \
54 scope elemtype *slabname## _at_peek(struct slabname *s, \
55 const struct commit *c, \
56 int add_if_missing) \
commit-slab.h
+10
@@ -47,6 +47,16 @@
47 *
48 * Call this function before the slab falls out of scope to avoid
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.
60 */
61
62 #define define_commit_slab(slabname, elemtype) \
shallow.c
+5 -9
@@ -84,6 +84,10 @@ int is_repository_shallow(struct repository *r)
84 * supports a "valid" flag.
85 */
86 define_commit_slab(commit_depth, int *);
87 +static void free_depth_in_slab(int **ptr)
88 +{
89 + FREE_AND_NULL(*ptr);
90 +}
91 struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
92 int shallow_flag, int not_shallow_flag)
93 {
@@ -150,15 +154,7 @@ struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
154 }
155 }
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);
158
159 return result;
160 }