Skip to content

Commit 1df15f8

Browse files
szedergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6141cdf commit 1df15f8

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

commit-slab-decl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ struct slabname { \
3232
void init_ ##slabname## _with_stride(struct slabname *s, unsigned stride); \
3333
void init_ ##slabname(struct slabname *s); \
3434
void clear_ ##slabname(struct slabname *s); \
35+
void deep_clear_ ##slabname(struct slabname *s, void (*free_fn)(elemtype *ptr)); \
3536
elemtype *slabname## _at_peek(struct slabname *s, const struct commit *c, int add_if_missing); \
3637
elemtype *slabname## _at(struct slabname *s, const struct commit *c); \
3738
elemtype *slabname## _peek(struct slabname *s, const struct commit *c)

commit-slab-impl.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@ scope void clear_ ##slabname(struct slabname *s) \
3838
FREE_AND_NULL(s->slab); \
3939
} \
4040
\
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+
\
4154
scope elemtype *slabname## _at_peek(struct slabname *s, \
4255
const struct commit *c, \
4356
int add_if_missing) \

commit-slab.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@
4747
*
4848
* Call this function before the slab falls out of scope to avoid
4949
* 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.
5060
*/
5161

5262
#define define_commit_slab(slabname, elemtype) \

shallow.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ int is_repository_shallow(struct repository *r)
8484
* supports a "valid" flag.
8585
*/
8686
define_commit_slab(commit_depth, int *);
87+
static void free_depth_in_slab(int **ptr)
88+
{
89+
FREE_AND_NULL(*ptr);
90+
}
8791
struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
8892
int shallow_flag, int not_shallow_flag)
8993
{
@@ -150,15 +154,7 @@ struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
150154
}
151155
}
152156
}
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);
162158

163159
return result;
164160
}

0 commit comments

Comments
 (0)