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 <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Patrick Steinhardt
2024-05-17 10:18:24 +02:00
committed by Junio C Hamano
parent ed93ea1602
commit 71c871b48d
7 changed files with 68 additions and 0 deletions

View File

@ -293,6 +293,27 @@ done:
return &refs->base;
}
static void reftable_be_release(struct ref_store *ref_store)
{
struct reftable_ref_store *refs = reftable_be_downcast(ref_store, 0, "release");
struct strmap_entry *entry;
struct hashmap_iter iter;
if (refs->main_stack) {
reftable_stack_destroy(refs->main_stack);
refs->main_stack = NULL;
}
if (refs->worktree_stack) {
reftable_stack_destroy(refs->worktree_stack);
refs->worktree_stack = NULL;
}
strmap_for_each_entry(&refs->worktree_stacks, &iter, entry)
reftable_stack_destroy(entry->value);
strmap_clear(&refs->worktree_stacks, 0);
}
static int reftable_be_create_on_disk(struct ref_store *ref_store,
int flags UNUSED,
struct strbuf *err UNUSED)
@ -2248,7 +2269,9 @@ done:
struct ref_storage_be refs_be_reftable = {
.name = "reftable",
.init = reftable_be_init,
.release = reftable_be_release,
.create_on_disk = reftable_be_create_on_disk,
.transaction_prepare = reftable_be_transaction_prepare,
.transaction_finish = reftable_be_transaction_finish,
.transaction_abort = reftable_be_transaction_abort,