Skip to content

Commit 71c871b

Browse files
pks-tgitster
authored andcommitted
refs: implement releasing ref storages
Ref storages are typically only initialized once for `the_repository` and then never released. Until now we got away with that without causing memory leaks because `the_repository` stays reachable, and because the ref backend is reachable via `the_repository` its memory basically never leaks. This is about to change though because of the upcoming migration logic, which will create a secondary ref storage. In that case, we will either have to release the old or new ref storage to avoid leaks. Implement a new `release` callback and expose it via a new `ref_storage_release()` function. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ed93ea1 commit 71c871b

File tree

7 files changed

+68
-0
lines changed

7 files changed

+68
-0
lines changed

refs.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,6 +2041,12 @@ static struct ref_store *ref_store_init(struct repository *repo,
20412041
return refs;
20422042
}
20432043

2044+
void ref_store_release(struct ref_store *ref_store)
2045+
{
2046+
ref_store->be->release(ref_store);
2047+
free(ref_store->gitdir);
2048+
}
2049+
20442050
struct ref_store *get_main_ref_store(struct repository *r)
20452051
{
20462052
if (r->refs_private)

refs.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ int is_branch(const char *refname);
118118

119119
int ref_store_create_on_disk(struct ref_store *refs, int flags, struct strbuf *err);
120120

121+
/*
122+
* Release all memory and resources associated with the ref store.
123+
*/
124+
void ref_store_release(struct ref_store *ref_store);
125+
121126
/*
122127
* Return the peeled value of the oid currently being iterated via
123128
* for_each_ref(), etc. This is equivalent to calling:

refs/debug.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_stor
3333
return (struct ref_store *)res;
3434
}
3535

36+
static void debug_release(struct ref_store *refs)
37+
{
38+
struct debug_ref_store *drefs = (struct debug_ref_store *)refs;
39+
drefs->refs->be->release(drefs->refs);
40+
trace_printf_key(&trace_refs, "release\n");
41+
}
42+
3643
static int debug_create_on_disk(struct ref_store *refs, int flags, struct strbuf *err)
3744
{
3845
struct debug_ref_store *drefs = (struct debug_ref_store *)refs;
@@ -427,6 +434,7 @@ static int debug_reflog_expire(struct ref_store *ref_store, const char *refname,
427434
struct ref_storage_be refs_be_debug = {
428435
.name = "debug",
429436
.init = NULL,
437+
.release = debug_release,
430438
.create_on_disk = debug_create_on_disk,
431439

432440
/*

refs/files-backend.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ static struct files_ref_store *files_downcast(struct ref_store *ref_store,
149149
return refs;
150150
}
151151

152+
static void files_ref_store_release(struct ref_store *ref_store)
153+
{
154+
struct files_ref_store *refs = files_downcast(ref_store, 0, "release");
155+
free_ref_cache(refs->loose);
156+
free(refs->gitcommondir);
157+
ref_store_release(refs->packed_ref_store);
158+
}
159+
152160
static void files_reflog_path(struct files_ref_store *refs,
153161
struct strbuf *sb,
154162
const char *refname)
@@ -3284,7 +3292,9 @@ static int files_ref_store_create_on_disk(struct ref_store *ref_store,
32843292
struct ref_storage_be refs_be_files = {
32853293
.name = "files",
32863294
.init = files_ref_store_init,
3295+
.release = files_ref_store_release,
32873296
.create_on_disk = files_ref_store_create_on_disk,
3297+
32883298
.transaction_prepare = files_transaction_prepare,
32893299
.transaction_finish = files_transaction_finish,
32903300
.transaction_abort = files_transaction_abort,

refs/packed-backend.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,15 @@ static void clear_snapshot(struct packed_ref_store *refs)
252252
}
253253
}
254254

255+
static void packed_ref_store_release(struct ref_store *ref_store)
256+
{
257+
struct packed_ref_store *refs = packed_downcast(ref_store, 0, "release");
258+
clear_snapshot(refs);
259+
rollback_lock_file(&refs->lock);
260+
delete_tempfile(&refs->tempfile);
261+
free(refs->path);
262+
}
263+
255264
static NORETURN void die_unterminated_line(const char *path,
256265
const char *p, size_t len)
257266
{
@@ -1707,7 +1716,9 @@ static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_s
17071716
struct ref_storage_be refs_be_packed = {
17081717
.name = "packed",
17091718
.init = packed_ref_store_init,
1719+
.release = packed_ref_store_release,
17101720
.create_on_disk = packed_ref_store_create_on_disk,
1721+
17111722
.transaction_prepare = packed_transaction_prepare,
17121723
.transaction_finish = packed_transaction_finish,
17131724
.transaction_abort = packed_transaction_abort,

refs/refs-internal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,10 @@ struct ref_store;
529529
typedef struct ref_store *ref_store_init_fn(struct repository *repo,
530530
const char *gitdir,
531531
unsigned int flags);
532+
/*
533+
* Release all memory and resources associated with the ref store.
534+
*/
535+
typedef void ref_store_release_fn(struct ref_store *refs);
532536

533537
typedef int ref_store_create_on_disk_fn(struct ref_store *refs,
534538
int flags,
@@ -668,6 +672,7 @@ typedef int read_symbolic_ref_fn(struct ref_store *ref_store, const char *refnam
668672
struct ref_storage_be {
669673
const char *name;
670674
ref_store_init_fn *init;
675+
ref_store_release_fn *release;
671676
ref_store_create_on_disk_fn *create_on_disk;
672677

673678
ref_transaction_prepare_fn *transaction_prepare;

refs/reftable-backend.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,27 @@ static struct ref_store *reftable_be_init(struct repository *repo,
293293
return &refs->base;
294294
}
295295

296+
static void reftable_be_release(struct ref_store *ref_store)
297+
{
298+
struct reftable_ref_store *refs = reftable_be_downcast(ref_store, 0, "release");
299+
struct strmap_entry *entry;
300+
struct hashmap_iter iter;
301+
302+
if (refs->main_stack) {
303+
reftable_stack_destroy(refs->main_stack);
304+
refs->main_stack = NULL;
305+
}
306+
307+
if (refs->worktree_stack) {
308+
reftable_stack_destroy(refs->worktree_stack);
309+
refs->worktree_stack = NULL;
310+
}
311+
312+
strmap_for_each_entry(&refs->worktree_stacks, &iter, entry)
313+
reftable_stack_destroy(entry->value);
314+
strmap_clear(&refs->worktree_stacks, 0);
315+
}
316+
296317
static int reftable_be_create_on_disk(struct ref_store *ref_store,
297318
int flags UNUSED,
298319
struct strbuf *err UNUSED)
@@ -2248,7 +2269,9 @@ static int reftable_be_reflog_expire(struct ref_store *ref_store,
22482269
struct ref_storage_be refs_be_reftable = {
22492270
.name = "reftable",
22502271
.init = reftable_be_init,
2272+
.release = reftable_be_release,
22512273
.create_on_disk = reftable_be_create_on_disk,
2274+
22522275
.transaction_prepare = reftable_be_transaction_prepare,
22532276
.transaction_finish = reftable_be_transaction_finish,
22542277
.transaction_abort = reftable_be_transaction_abort,

0 commit comments

Comments
 (0)