]> git.ipfire.org Git - thirdparty/git.git/commitdiff
refs: implement releasing ref storages
authorPatrick Steinhardt <ps@pks.im>
Fri, 17 May 2024 08:18:24 +0000 (10:18 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 17 May 2024 17:33:37 +0000 (10:33 -0700)
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>
refs.c
refs.h
refs/debug.c
refs/files-backend.c
refs/packed-backend.c
refs/refs-internal.h
refs/reftable-backend.c

diff --git a/refs.c b/refs.c
index ebc6de81e905b33f62cd3c204f0f79cc5be922ee..0f4d327c47539e30648c9a0d17a594c150443beb 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -2041,6 +2041,12 @@ static struct ref_store *ref_store_init(struct repository *repo,
        return refs;
 }
 
+void ref_store_release(struct ref_store *ref_store)
+{
+       ref_store->be->release(ref_store);
+       free(ref_store->gitdir);
+}
+
 struct ref_store *get_main_ref_store(struct repository *r)
 {
        if (r->refs_private)
diff --git a/refs.h b/refs.h
index 421ff9fdb735699929ea79e907aa83eba19621cc..b11c250e8a45c239237395d9f7d0cd19a5774fc2 100644 (file)
--- a/refs.h
+++ b/refs.h
@@ -118,6 +118,11 @@ int is_branch(const char *refname);
 
 int ref_store_create_on_disk(struct ref_store *refs, int flags, struct strbuf *err);
 
+/*
+ * Release all memory and resources associated with the ref store.
+ */
+void ref_store_release(struct ref_store *ref_store);
+
 /*
  * Return the peeled value of the oid currently being iterated via
  * for_each_ref(), etc. This is equivalent to calling:
index 4cc491097421b7d540fa903b9829df756288a4f9..3a063077ba281680d5c1e8217cebb7d5c3587567 100644 (file)
@@ -33,6 +33,13 @@ struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_stor
        return (struct ref_store *)res;
 }
 
+static void debug_release(struct ref_store *refs)
+{
+       struct debug_ref_store *drefs = (struct debug_ref_store *)refs;
+       drefs->refs->be->release(drefs->refs);
+       trace_printf_key(&trace_refs, "release\n");
+}
+
 static int debug_create_on_disk(struct ref_store *refs, int flags, struct strbuf *err)
 {
        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,
 struct ref_storage_be refs_be_debug = {
        .name = "debug",
        .init = NULL,
+       .release = debug_release,
        .create_on_disk = debug_create_on_disk,
 
        /*
index f9f15d1f76388dc767d169ccc4701380e68eef9d..62acd2721d2d32a0c89d93e55d3c65a077909979 100644 (file)
@@ -149,6 +149,14 @@ static struct files_ref_store *files_downcast(struct ref_store *ref_store,
        return refs;
 }
 
+static void files_ref_store_release(struct ref_store *ref_store)
+{
+       struct files_ref_store *refs = files_downcast(ref_store, 0, "release");
+       free_ref_cache(refs->loose);
+       free(refs->gitcommondir);
+       ref_store_release(refs->packed_ref_store);
+}
+
 static void files_reflog_path(struct files_ref_store *refs,
                              struct strbuf *sb,
                              const char *refname)
@@ -3284,7 +3292,9 @@ static int files_ref_store_create_on_disk(struct ref_store *ref_store,
 struct ref_storage_be refs_be_files = {
        .name = "files",
        .init = files_ref_store_init,
+       .release = files_ref_store_release,
        .create_on_disk = files_ref_store_create_on_disk,
+
        .transaction_prepare = files_transaction_prepare,
        .transaction_finish = files_transaction_finish,
        .transaction_abort = files_transaction_abort,
index b94183034eb9a0c2915d2c51bdf2d5dd2a89b3b4..9c98e6295ffa7d6f2cecbb9c7c8c1fff36b7d630 100644 (file)
@@ -252,6 +252,15 @@ static void clear_snapshot(struct packed_ref_store *refs)
        }
 }
 
+static void packed_ref_store_release(struct ref_store *ref_store)
+{
+       struct packed_ref_store *refs = packed_downcast(ref_store, 0, "release");
+       clear_snapshot(refs);
+       rollback_lock_file(&refs->lock);
+       delete_tempfile(&refs->tempfile);
+       free(refs->path);
+}
+
 static NORETURN void die_unterminated_line(const char *path,
                                           const char *p, size_t len)
 {
@@ -1707,7 +1716,9 @@ static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_s
 struct ref_storage_be refs_be_packed = {
        .name = "packed",
        .init = packed_ref_store_init,
+       .release = packed_ref_store_release,
        .create_on_disk = packed_ref_store_create_on_disk,
+
        .transaction_prepare = packed_transaction_prepare,
        .transaction_finish = packed_transaction_finish,
        .transaction_abort = packed_transaction_abort,
index c3d5f0a6cd97cd916dd24363c8088cd86d25b0e2..8624477e19cfb981128b60b400b232f6fd31ee8f 100644 (file)
@@ -529,6 +529,10 @@ struct ref_store;
 typedef struct ref_store *ref_store_init_fn(struct repository *repo,
                                            const char *gitdir,
                                            unsigned int flags);
+/*
+ * Release all memory and resources associated with the ref store.
+ */
+typedef void ref_store_release_fn(struct ref_store *refs);
 
 typedef int ref_store_create_on_disk_fn(struct ref_store *refs,
                                        int flags,
@@ -668,6 +672,7 @@ typedef int read_symbolic_ref_fn(struct ref_store *ref_store, const char *refnam
 struct ref_storage_be {
        const char *name;
        ref_store_init_fn *init;
+       ref_store_release_fn *release;
        ref_store_create_on_disk_fn *create_on_disk;
 
        ref_transaction_prepare_fn *transaction_prepare;
index 8583a0cdba464c1b1ea727d05cc0450fc296e575..7b73f73f5913c43165457156bbfa0942078bc270 100644 (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,