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>
main
Patrick Steinhardt 2026-04-10 14:12:32 +02:00 committed by Junio C Hamano
parent 822d403651
commit 8caa2e090f
3 changed files with 24 additions and 22 deletions

25
odb.c
View File

@ -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);

View 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;
}

View 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