]> git.ipfire.org Git - thirdparty/git.git/commitdiff
object-file: hide internals when we need to reprepare loose sources
authorPatrick Steinhardt <ps@pks.im>
Mon, 3 Nov 2025 07:42:02 +0000 (08:42 +0100)
committerJunio C Hamano <gitster@pobox.com>
Mon, 3 Nov 2025 20:18:46 +0000 (12:18 -0800)
There are two different situations where we have to clear the cache of
loose objects:

  - When freeing the loose object source itself to avoid memory leaks.

  - When repreparing the loose object source so that any potentially-
    stale data is getting evicted from the cache.

The former is already handled by `odb_source_loose_free()`. But the
latter case is still done manually by in `odb_reprepare()`, so we are
leaking internals into that code.

Introduce a new `odb_source_loose_reprepare()` function as an equivalent
to `packfile_store_prepare()` to hide these implementation details.
Furthermore, while at it, rename the function `odb_clear_loose_cache()`
to `odb_source_loose_clear()`.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
object-file.c
object-file.h
odb.c

index fef00d6d3d082aa5bd80c67c46024b93652dd6fe..20daa629a1dda97f9045c50d9bc8c8de60f82fab 100644 (file)
@@ -1834,12 +1834,17 @@ struct oidtree *odb_source_loose_cache(struct odb_source *source,
        return source->loose->cache;
 }
 
-void odb_clear_loose_cache(struct odb_source *source)
+static void odb_source_loose_clear_cache(struct odb_source_loose *loose)
 {
-       oidtree_clear(source->loose->cache);
-       FREE_AND_NULL(source->loose->cache);
-       memset(&source->loose->subdir_seen, 0,
-              sizeof(source->loose->subdir_seen));
+       oidtree_clear(loose->cache);
+       FREE_AND_NULL(loose->cache);
+       memset(&loose->subdir_seen, 0,
+              sizeof(loose->subdir_seen));
+}
+
+void odb_source_loose_reprepare(struct odb_source *source)
+{
+       odb_source_loose_clear_cache(source->loose);
 }
 
 static int check_stream_oid(git_zstream *stream,
@@ -2008,6 +2013,6 @@ void odb_source_loose_free(struct odb_source_loose *loose)
 {
        if (!loose)
                return;
-       odb_clear_loose_cache(loose->source);
+       odb_source_loose_clear_cache(loose);
        free(loose);
 }
index 90da69cf5f7d59aaf9bd9c6c45e48179b2652cfd..bec855e8e53f952776c8c3a5b799ffbb452910b3 100644 (file)
@@ -37,6 +37,9 @@ struct odb_source_loose {
 struct odb_source_loose *odb_source_loose_new(struct odb_source *source);
 void odb_source_loose_free(struct odb_source_loose *loose);
 
+/* Reprepare the loose source by emptying the loose object cache. */
+void odb_source_loose_reprepare(struct odb_source *source);
+
 /*
  * Populate and return the loose object cache array corresponding to the
  * given object ID.
@@ -44,9 +47,6 @@ void odb_source_loose_free(struct odb_source_loose *loose);
 struct oidtree *odb_source_loose_cache(struct odb_source *source,
                                       const struct object_id *oid);
 
-/* Empty the loose object cache for the specified object directory. */
-void odb_clear_loose_cache(struct odb_source *source);
-
 /*
  * Put in `buf` the name of the file in the local object database that
  * would be used to store a loose object with the specified oid.
diff --git a/odb.c b/odb.c
index 87d84688c638cce55c92e401dd324c5e3136fb1a..b3e8d4a49cb07e6e8ecdb625be040134208848ca 100644 (file)
--- a/odb.c
+++ b/odb.c
@@ -1071,7 +1071,7 @@ void odb_reprepare(struct object_database *o)
        odb_prepare_alternates(o);
 
        for (source = o->sources; source; source = source->next)
-               odb_clear_loose_cache(source);
+               odb_source_loose_reprepare(source);
 
        o->approximate_object_count_valid = 0;