odb/source-inmemory: implement `free()` callback
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>
Patrick Steinhardt committed
Apr 10, 2026 at 14:12 UTC
8caa2e090f1b83df7c0fc82ed7f7c8772f3ec5f4
3 files changed
+24
-22
odb.c
+4
-21
index 60e1eead25..1d65825ed3 100644
--- a/odb.c
+++ b/odb.c
@@ -32,21 +32,6 @@
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);
odb/source-inmemory.c
+12
index c7ac5c24f0..ccbb622eae 100644
--- a/odb/source-inmemory.c
+++ b/odb/source-inmemory.c
@@ -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;
}
odb/source-inmemory.h
+8
-1
index 15db068ef7..d1b05a3996 100644
--- a/odb/source-inmemory.h
+++ b/odb/source-inmemory.h
@@ -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