odb/source: make `free()` function pluggable

Introduce a new callback function in `struct odb_source` to make the function pluggable. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Mar 5, 2026 at 15:19 UTC 47b965079ddd9ced04810d0a8738a1ca94f02268
4 files changed +11 -9
odb/source-files.c
+4 -3
@@ -18,10 +18,9 @@ static void odb_source_files_reparent(const char *name UNUSED,
18 files->base.path = path;
19 }
20
21 -void odb_source_files_free(struct odb_source_files *files)
21 +static void odb_source_files_free(struct odb_source *source)
22 {
23 - if (!files)
24 - return;
23 + struct odb_source_files *files = odb_source_files_downcast(source);
24 chdir_notify_unregister(NULL, odb_source_files_reparent, files);
25 odb_source_loose_free(files->loose);
26 packfile_store_free(files->packed);
@@ -40,6 +39,8 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
39 files->loose = odb_source_loose_new(&files->base);
40 files->packed = packfile_store_new(&files->base);
41
42 + files->base.free = odb_source_files_free;
43 +
44 /*
45 * Ideally, we would only ever store absolute paths in the source. This
46 * is not (yet) possible though because we access and assume relative
odb/source-files.h
-3
@@ -21,9 +21,6 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
21 const char *path,
22 bool local);
23
24 -/* Free the object source and release all associated resources. */
25 -void odb_source_files_free(struct odb_source_files *files);
26 -
24 /*
25 * Cast the given object database source to the files backend. This will cause
26 * a BUG in case the source doesn't use this backend.
odb/source.c
+1 -3
@@ -25,11 +25,9 @@ void odb_source_init(struct odb_source *source,
25
26 void odb_source_free(struct odb_source *source)
27 {
28 - struct odb_source_files *files;
28 if (!source)
29 return;
31 - files = odb_source_files_downcast(source);
32 - odb_source_files_free(files);
30 + source->free(source);
31 }
32
33 void odb_source_release(struct odb_source *source)
odb/source.h
+6
@@ -51,6 +51,12 @@ struct odb_source {
51 * the current working directory.
52 */
53 char *path;
54 +
55 + /*
56 + * This callback is expected to free the underlying object database source and
57 + * all associated resources. The function will never be called with a NULL pointer.
58 + */
59 + void (*free)(struct odb_source *source);
60 };
61
62 /*