]> git.ipfire.org Git - thirdparty/git.git/commitdiff
odb/source-inmemory: implement `free()` callback
authorPatrick Steinhardt <ps@pks.im>
Fri, 10 Apr 2026 12:12:32 +0000 (14:12 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 14 May 2026 19:50:44 +0000 (04:50 +0900)
Implement the `free()` callback function for the "in-memory" source.

Note that this requires us to define `struct cached_object_entry` in
"odb/source-inmemory.h", as it is accessed in both "odb.c" and
"odb/source-inmemory.c" now. This will be fixed in subsequent commits
though.

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

diff --git a/odb.c b/odb.c
index 60e1eead25602a1682946b032f0b3a4e37d26ee2..1d65825ed3978a935a9f497a36e9738fbdc769d5 100644 (file)
--- a/odb.c
+++ b/odb.c
 KHASH_INIT(odb_path_map, const char * /* key: odb_path */,
        struct odb_source *, 1, fspathhash, fspatheq)
 
-/*
- * This is meant to hold a *small* number of objects that you would
- * want odb_read_object() to be able to return, but yet you do not want
- * to write them into the object store (e.g. a browse-only
- * application).
- */
-struct cached_object_entry {
-       struct object_id oid;
-       struct cached_object {
-               enum object_type type;
-               const void *buf;
-               unsigned long size;
-       } value;
-};
-
 static const struct cached_object *find_cached_object(struct object_database *object_store,
                                                      const struct object_id *oid)
 {
@@ -1109,6 +1094,10 @@ static void odb_free_sources(struct object_database *o)
                odb_source_free(o->sources);
                o->sources = next;
        }
+
+       odb_source_free(&o->inmemory_objects->base);
+       o->inmemory_objects = NULL;
+
        kh_destroy_odb_path_map(o->source_by_path);
        o->source_by_path = NULL;
 }
@@ -1126,12 +1115,6 @@ void odb_free(struct object_database *o)
        odb_close(o);
        odb_free_sources(o);
 
-       for (size_t i = 0; i < o->inmemory_objects->objects_nr; i++)
-               free((char *) o->inmemory_objects->objects[i].value.buf);
-       free(o->inmemory_objects->objects);
-       free(o->inmemory_objects->base.path);
-       free(o->inmemory_objects);
-
        string_list_clear(&o->submodule_source_paths, 0);
 
        free(o);
index c7ac5c24f08e4489abaf82c81a4bd15ba0b89bf3..ccbb622eaef031611f572c0ca6b589940a76311b 100644 (file)
@@ -1,6 +1,16 @@
 #include "git-compat-util.h"
 #include "odb/source-inmemory.h"
 
+static void odb_source_inmemory_free(struct odb_source *source)
+{
+       struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source);
+       for (size_t i = 0; i < inmemory->objects_nr; i++)
+               free((char *) inmemory->objects[i].value.buf);
+       free(inmemory->objects);
+       free(inmemory->base.path);
+       free(inmemory);
+}
+
 struct odb_source_inmemory *odb_source_inmemory_new(struct object_database *odb)
 {
        struct odb_source_inmemory *source;
@@ -8,5 +18,7 @@ struct odb_source_inmemory *odb_source_inmemory_new(struct object_database *odb)
        CALLOC_ARRAY(source, 1);
        odb_source_init(&source->base, odb, ODB_SOURCE_INMEMORY, "source", false);
 
+       source->base.free = odb_source_inmemory_free;
+
        return source;
 }
index 15db068ef70bb29f26153d32c244200efc4c6599..d1b05a3996468a56fd5c18f28c52209ca8201286 100644 (file)
@@ -3,7 +3,14 @@
 
 #include "odb/source.h"
 
-struct cached_object_entry;
+struct cached_object_entry {
+       struct object_id oid;
+       struct cached_object {
+               enum object_type type;
+               const void *buf;
+               unsigned long size;
+       } value;
+};
 
 /*
  * An in-memory source that you can write objects to that shall be made