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
@@ -32,21 +32,6 @@
32
KHASH_INIT(odb_path_map, const char * /* key: odb_path */,
33
struct odb_source *, 1, fspathhash, fspatheq)
34
35
-/*
36
- * This is meant to hold a *small* number of objects that you would
37
- * want odb_read_object() to be able to return, but yet you do not want
38
- * to write them into the object store (e.g. a browse-only
39
- * application).
40
- */
41
-struct cached_object_entry {
42
- struct object_id oid;
43
- struct cached_object {
44
- enum object_type type;
45
- const void *buf;
46
- unsigned long size;
47
- } value;
48
-};
49
-
35
static const struct cached_object *find_cached_object(struct object_database *object_store,
36
const struct object_id *oid)
37
{
@@ -1109,6 +1094,10 @@ static void odb_free_sources(struct object_database *o)
1094
odb_source_free(o->sources);
1095
o->sources = next;
1096
}
1097
+
1098
+ odb_source_free(&o->inmemory_objects->base);
1099
+ o->inmemory_objects = NULL;
1100
+
1101
kh_destroy_odb_path_map(o->source_by_path);
1102
o->source_by_path = NULL;
1103
}
@@ -1126,12 +1115,6 @@ void odb_free(struct object_database *o)
1115
odb_close(o);
1116
odb_free_sources(o);
1117
1129
- for (size_t i = 0; i < o->inmemory_objects->objects_nr; i++)
1130
- free((char *) o->inmemory_objects->objects[i].value.buf);
1131
- free(o->inmemory_objects->objects);
1132
- free(o->inmemory_objects->base.path);
1133
- free(o->inmemory_objects);
1134
-
1118
string_list_clear(&o->submodule_source_paths, 0);
1119
1120
free(o);
odb/source-inmemory.c
+12
@@ -1,6 +1,16 @@
1
#include "git-compat-util.h"
2
#include "odb/source-inmemory.h"
3
4
+static void odb_source_inmemory_free(struct odb_source *source)
5
+{
6
+ struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source);
7
+ for (size_t i = 0; i < inmemory->objects_nr; i++)
8
+ free((char *) inmemory->objects[i].value.buf);
9
+ free(inmemory->objects);
10
+ free(inmemory->base.path);
11
+ free(inmemory);
12
+}
13
+
14
struct odb_source_inmemory *odb_source_inmemory_new(struct object_database *odb)
15
{
16
struct odb_source_inmemory *source;
@@ -8,5 +18,7 @@ struct odb_source_inmemory *odb_source_inmemory_new(struct object_database *odb)
18
CALLOC_ARRAY(source, 1);
19
odb_source_init(&source->base, odb, ODB_SOURCE_INMEMORY, "source", false);
20
21
+ source->base.free = odb_source_inmemory_free;
22
+
23
return source;
24
}
odb/source-inmemory.h
+8
-1
@@ -3,7 +3,14 @@
3
4
#include "odb/source.h"
5
6
-struct cached_object_entry;
6
+struct cached_object_entry {
7
+ struct object_id oid;
8
+ struct cached_object {
9
+ enum object_type type;
10
+ const void *buf;
11
+ unsigned long size;
12
+ } value;
13
+};
14
15
/*
16
* An in-memory source that you can write objects to that shall be made