oidmap: migrate cleanup to oidmap_clear_with_free()
cover oidmap: migrate cleanup to oidmap_clear_with_free()
Hi,
This series replaces oidmap_clear(map, 1) with oidmap_clear_with_free() and introduces explicit free callbacks at the remaining call sites.
The old boolean-based API implicitly assumed plain free(), which obscures ownership semantics and does not work well when oidmap_entry is embedded inside larger structures. The callback-based API makes cleanup explicit and type-safe, and avoids relying on hidden assumptions about allocation.
This improves readability, maintainability, and correctness, and makes future refactoring of oidmap users more robust.
This is used in subsequent commits to adequately cleanup all usage site.
Thanks,
Seyi Kuforiji
Seyi Kufoiji (5):
oidmap: make entry cleanup explicit in oidmap_clear builtin/rev-list: migrate missing_objects cleanup to
oidmap_clear_with_free()
list-objects-filter: use oidmap_clear_with_free() for cleanup
odb: use oidmap_clear_with_free() to release replace_map entries
sequencer: use oidmap_clear_with_free() for string_entry cleanup
builtin/rev-list.c | 13 ++++++++++--- list-objects-filter.c | 9 ++++++++- odb.c | 11 ++++++++++- oidmap.c | 23 ++++++++++++++++++++--- oidmap.h | 15 +++++++++++++++ sequencer.c | 10 ++++++++-- t/unit-tests/u-oidmap.c | 41 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 112 insertions(+), 10 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/5 oidmap: make entry cleanup explicit in oidmap_clear
From: Seyi Kufoiji <[email protected]>
Replace oidmap's use of hashmap_clear_() and layout-dependent freeing with an explicit iteration and optional free callback. This removes reliance on struct layout assumptions while keeping the existing API intact.
Add tests for oidmap_clear_with_free behavior. test_oidmap__clear_with_free_callback verifies that entries are freed when a callback is provided, while test_oidmap__clear_without_free_callback verifies that entries are not freed when no callback is given. These tests ensure the new clear implementation behaves correctly and preserves ownership semantics.
--- oidmap.c | 23 ++++++++++++++++++++--- oidmap.h | 15 +++++++++++++++ t/unit-tests/u-oidmap.c | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 3 deletions(-)
oidmap.c +20 -3
@@ -24,11 +24,28 @@ void oidmap_init(struct oidmap *map, size_t initial_size)void oidmap_clear (struct oidmap *map,int free_entries) { -if (!map) +oidmap_clear_with_free (map, + free_entries ? free :NULL ); +} + +void oidmap_clear_with_free (struct oidmap *map, +oidmap_free_fn free_fn) +{ +struct hashmap_iter iter; +struct hashmap_entry *e; + +if (!map || !map->map.cmpfn)return ; -/* TODO: make oidmap itself not depend on struct layouts */ -hashmap_clear_ (&map->map, free_entries ?0 :-1 ); +hashmap_iter_init (&map->map, &iter); +while ((e =hashmap_iter_next (&iter))) { +struct oidmap_entry *entry = + container_of(e, struct oidmap_entry, internal_entry); +if (free_fn) +free_fn (entry); + } + +hashmap_clear (&map->map); }void *oidmap_get (const struct oidmap *map,const struct object_id *key)
oidmap.h +15 -0
@@ -35,6 +35,21 @@ struct oidmap {*/ void oidmap_init (struct oidmap *map,size_t initial_size); +/* +* Function type for functions that free oidmap entries. +*/ +typedef void (*oidmap_free_fn )(void *); + +/* +* Clear an oidmap, freeing any allocated memory. The map is empty and +* can be reused without another explicit init. +* +* The `free_fn`, if not NULL, is called for each oidmap entry in the map +* to free any user data associated with the entry. +*/ +void oidmap_clear_with_free (struct oidmap *map, +oidmap_free_fn free_fn); +/* * Clear an oidmap, freeing any allocated memory. The map is empty and * can be reused without another explicit init.
t/unit-tests/u-oidmap.c +41 -0
50 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH 2/5 builtin/rev-list: migrate missing_objects cleanup to oidmap_clear_with_free()
In reply to Seyi KuforijiFrom: Seyi Kufoiji <[email protected]>
As part of the conversion away from oidmap_clear(), switch the missing_objects map to use oidmap_clear_with_free().
missing_objects stores struct missing_objects_map_entry instances, which own an xstrdup()'d path string in addition to the container struct itself. Previously, rev-list manually freed entry->path before calling oidmap_clear(&missing_objects, true).
Introduce a dedicated free callback and pass it to oidmap_clear_with_free(), consolidating entry teardown into a single place and making cleanup semantics explicit. This improves clarity and maintainability.
--- builtin/rev-list.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
builtin/rev-list.c +10 -3
@@ -88,9 +88,17 @@ static int arg_print_omitted; /* print objects omitted by filter */struct missing_objects_map_entry {struct oidmap_entry entry; -const char *path; +char *path;unsigned type; }; + +static void free_missing_objects_entry (void *e) +{ +struct missing_objects_map_entry *entry = + container_of(e, struct missing_objects_map_entry, entry); +free (entry); +} +static struct oidmap missing_objects;enum missing_action {MA_ERROR =0 ,/* fail if any missing objects are encountered */
@@ -935,10 +943,9 @@ int cmd_rev_list(int argc,while ((entry =oidmap_iter_next (&iter))) {print_missing_object (entry, arg_missing_action ==MA_PRINT_INFO ); -free ((void *)entry->path); } -oidmap_clear (&missing_objects, true); +oidmap_clear_with_free (&missing_objects, free_missing_objects_entry); }stop_progress (&progress);
Signature · 1 line
2.43.0
Re: [PATCH 2/5] builtin/rev-list: migrate missing_objects cleanup to oidmap_clear_with_free()
Quoted 44 lines · Seyi Kuforiji
From: Seyi Kufoiji <[email protected]>
As part of the conversion away from
oidmap_clear(), switch themissing_objectsmap to useoidmap_clear_with_free().
missing_objectsstores structmissing_objects_map_entryinstances, which own an xstrdup()'d path string in addition to the container struct itself. Previously, rev-list manually freedentry->pathbefore callingoidmap_clear(&missing_objects, true).Introduce a dedicated free callback and pass it to
oidmap_clear_with_free(), consolidating entry teardown into a single place and making cleanup semantics explicit. This improves clarity and maintainability.Signed-off-by Seyi Kuforiji <[email protected]>--- builtin/rev-list.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)builtin/rev-list.c +9
-1@@ -88,9 +88,17 @@ static int arg_print_omitted; /* print objects omitted by filter */struct missing_objects_map_entry {struct oidmap_entry entry; -const char *path; +char *path;unsigned type; }; + +static void free_missing_objects_entry (void *e) +{ +struct missing_objects_map_entry *entry = + container_of(e, struct missing_objects_map_entry, entry); +free (entry); +} +static struct oidmap missing_objects;enum missing_action {MA_ERROR =0 ,/* fail if any missing objects are encountered */
builtin/rev-list.c int cmd_rev_list(int argc, · line 949
Quoted 9 lines
builtin/rev-list.c +1
-2@@ -935,10 +943,9 @@ int cmd_rev_list(int argc,while ((entry =oidmap_iter_next (&iter))) {print_missing_object (entry, arg_missing_action ==MA_PRINT_INFO ); -free ((void *)entry->path); } -oidmap_clear (&missing_objects, true); +oidmap_clear_with_free (&missing_objects, free_missing_objects_entry); }
Hmph, maybe I am confused, but the shape and memory ownership of the structure involved has not changed before or after this patch. We have bunch of missing_objects_map_entry instances that embeds oidmap_entry in each of them, and we iterate over this oidmap, enumerating all the missing_objects_map_entry instances.
In the original code, we used to not just clear the oidmap used to hold these missing_objects_map_entry instances, but dropped the string pointed at and owned by the .path member of them while iterating. With the new code, I can see the newer interface oidmap_clear_with_free() being told how to reclaim resources held by these missing_objects_map_entry instances, so it is a perfect place to not just free the "shell" structure, but also the piece of memory held by the .path member, isn't it? It appears to me that the string pointed at by .path is no longer freed, introducing a leak?
The above hardly convinces me of the claim in the proposed log message that this change improves clarity nor maintainability. I am puzzled...
PATCH 3/5 list-objects-filter: use oidmap_clear_with_free() for cleanup
In reply to Seyi KuforijiFrom: Seyi Kufoiji <[email protected]>
Replace the use of oidmap_clear(&seen_at_depth, 1) in filter_trees_free() with oidmap_clear_with_free().
The seen_at_depth map stores heap-allocated struct seen_map_entry objects. Previously, passing 1 relied on oidmap_clear() internally calling free() on each entry.
Convert this to the explicit oidmap_clear_with_free() API and provide a typed free_seen_map_entry() helper to free each container entry.
This makes the ownership and cleanup policy explicit and removes reliance on the legacy boolean free_entries parameter.
--- list-objects-filter.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
list-objects-filter.c +8 -1
@@ -143,6 +143,13 @@ struct seen_map_entry {size_t depth; }; +static void free_seen_map_entry (void *e) +{ +struct seen_map_entry *entry = + container_of(e, struct seen_map_entry, base); +free (entry); +} +/* Returns 1 if the oid was in the omits set before it was invoked. */ static int filter_trees_update_omits( struct object *obj,
@@ -244,7 +251,7 @@ static void filter_trees_free(void *filter_data) {struct filter_trees_depth_data *d = filter_data;if (!d)return ; -oidmap_clear (&d->seen_at_depth,1 ); +oidmap_clear_with_free (&d->seen_at_depth, free_seen_map_entry);free (d); }
Signature · 1 line
2.43.0
PATCH 4/5 odb: use oidmap_clear_with_free() to release replace_map entries
In reply to Seyi KuforijiFrom: Seyi Kufoiji <[email protected]>
Replace the direct oidmap_clear() call in odb_free() with oidmap_clear_with_free(), and introduce a free_replace_map_entry() helper to properly free each struct replace_object stored in the map.
This centralizes cleanup logic and ensures entries are released correctly via a dedicated callback.
--- odb.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
odb.c +10 -1
@@ -14,6 +14,7 @@#include "object-file-convert.h" #include "object-file.h" #include "odb.h" +#include "oidmap.h" #include "packfile.h" #include "path.h" #include "promisor-remote.h"
@@ -1089,6 +1090,13 @@ void odb_close(struct object_database *o)close_commit_graph (o); } +static void free_replace_map_entry (void *e) +{ +struct replace_object *entry = + container_of(e, struct replace_object, original); +free (entry); +} +static void odb_free_sources (struct object_database *o) {while (o->sources) {
@@ -1109,7 +1117,8 @@ void odb_free(struct object_database *o)free (o->alternate_db); -oidmap_clear (&o->replace_map,1 ); +if (o->replace_map_initialized) +oidmap_clear_with_free (&o->replace_map, free_replace_map_entry);pthread_mutex_destroy (&o->replace_mutex);odb_close (o);
Signature · 1 line
2.43.0
PATCH 5/5 sequencer: use oidmap_clear_with_free() for string_entry cleanup
In reply to Seyi KuforijiFrom: Seyi Kufoiji <[email protected]>
Switch cleanup of the string_entry oidmap to oidmap_clear_with_free() and introduce a free_string_entry() helper to properly free each allocated struct string_entry.
This aligns with the ongoing migration to use the callback-based oidmap cleanup API.
--- sequencer.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
sequencer.c +8 -2
@@ -5654,6 +5654,12 @@ struct string_entry {char string[FLEX_ARRAY ]; }; +static void free_string_entry (void *e) +{ + struct string_entry *entry = container_of(e, struct string_entry, entry); +free (entry); +} +struct label_state {struct oidmap commit2label;struct hashmap labels;
@@ -6044,8 +6050,8 @@ static int make_script_with_merges(struct pretty_print_context *pp,oidset_clear (&interesting);oidset_clear (&child_seen);oidset_clear (&shown); -oidmap_clear (&commit2todo,1 ); -oidmap_clear (&state.commit2label,1 ); +oidmap_clear_with_free (&commit2todo, free_string_entry); +oidmap_clear_with_free (&state.commit2label, free_string_entry); hashmap_clear_and_free(&state.labels, struct labels_entry, entry);strbuf_release (&state.buf);
Signature · 1 line
2.43.0
v2 cover oidmap: migrate cleanup to oidmap_clear_with_free()
In reply to Seyi KuforijiHi,
This series replaces oidmap_clear(map, 1) with oidmap_clear_with_free() and introduces explicit free callbacks at the remaining call sites.
The old boolean-based API implicitly assumed plain free(), which obscures ownership semantics and does not work well when oidmap_entry is embedded inside larger structures. The callback-based API makes cleanup explicit and type-safe, and avoids relying on hidden assumptions about allocation.
This is used in subsequent commits to adequately cleanup all usage site.
Changes in v2:
- fix missing .path cleanup
- modified commit message to be more accurate
Thanks
Seyi
Seyi Kufoiji (5):
oidmap: make entry cleanup explicit in oidmap_clear builtin/rev-list: migrate missing_objects cleanup to
oidmap_clear_with_free()
list-objects-filter: use oidmap_clear_with_free() for cleanup
odb: use oidmap_clear_with_free() to release replace_map entries
sequencer: use oidmap_clear_with_free() for string_entry cleanup
builtin/rev-list.c | 15 ++++++++++++--- list-objects-filter.c | 9 ++++++++- odb.c | 11 ++++++++++- oidmap.c | 23 ++++++++++++++++++++--- oidmap.h | 15 +++++++++++++++ sequencer.c | 10 ++++++++-- t/unit-tests/u-oidmap.c | 41 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 114 insertions(+), 10 deletions(-)
Range-diff against v1:
1: a0ab068630 < -: ---------- sparse-checkout: use string_list_sort_u 2: b2c0ee2593 < -: ---------- The 7th batch 3: 6af69c2c49 = 1: 1d544ef7d2 oidmap: make entry cleanup explicit in oidmap_clear 4: 452f5d8edb ! 2: f2c3a699bd builtin/rev-list: migrate missing_objects cleanup to oidmap_clear_with_free()
@@ Metadata
Author: Seyi Kufoiji <[email protected]>
## Commit message ##
- builtin/rev-list: migrate missing_objects cleanup to oidmap_clear_with_free()
+ builtin/rev-list: migrate missing_objects cleanup to
+ oidmap_clear_with_free()
As part of the conversion away from oidmap_clear(), switch the missing_objects map to use oidmap_clear_with_free().
@@ Commit message
Introduce a dedicated free callback and pass it to oidmap_clear_with_free(), consolidating entry teardown into a
- single place and making cleanup semantics explicit. This improves
- clarity and maintainability.
+ single place and making cleanup semantics explicit.
@@ builtin/rev-list.c: static int arg_print_omitted; /* print objects omitted by fi
+{
+ struct missing_objects_map_entry *entry =
+ container_of(e, struct missing_objects_map_entry, entry);
++
++ free(entry->path);
+ free(entry);
+}
+
5: cbfcc4e9cc = 3: a4e426bcca list-objects-filter: use oidmap_clear_with_free() for cleanup 6: bfbdb51d84 = 4: 4116e5491d odb: use oidmap_clear_with_free() to release replace_map entries 7: 4fc6e71f4c = 5: ad1f776a19 sequencer: use oidmap_clear_with_free() for string_entry cleanup
Signature · 1 line
2.43.0
PATCH v2 1/5 oidmap: make entry cleanup explicit in oidmap_clear
From: Seyi Kufoiji <[email protected]>
Replace oidmap's use of hashmap_clear_() and layout-dependent freeing with an explicit iteration and optional free callback. This removes reliance on struct layout assumptions while keeping the existing API intact.
Add tests for oidmap_clear_with_free behavior. test_oidmap__clear_with_free_callback verifies that entries are freed when a callback is provided, while test_oidmap__clear_without_free_callback verifies that entries are not freed when no callback is given. These tests ensure the new clear implementation behaves correctly and preserves ownership semantics.
--- oidmap.c | 23 ++++++++++++++++++++--- oidmap.h | 15 +++++++++++++++ t/unit-tests/u-oidmap.c | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 3 deletions(-)
oidmap.c +20 -3
@@ -24,11 +24,28 @@ void oidmap_init(struct oidmap *map, size_t initial_size)void oidmap_clear (struct oidmap *map,int free_entries) { -if (!map) +oidmap_clear_with_free (map, + free_entries ? free :NULL ); +} + +void oidmap_clear_with_free (struct oidmap *map, +oidmap_free_fn free_fn) +{ +struct hashmap_iter iter; +struct hashmap_entry *e; + +if (!map || !map->map.cmpfn)return ; -/* TODO: make oidmap itself not depend on struct layouts */ -hashmap_clear_ (&map->map, free_entries ?0 :-1 ); +hashmap_iter_init (&map->map, &iter); +while ((e =hashmap_iter_next (&iter))) { +struct oidmap_entry *entry = + container_of(e, struct oidmap_entry, internal_entry); +if (free_fn) +free_fn (entry); + } + +hashmap_clear (&map->map); }void *oidmap_get (const struct oidmap *map,const struct object_id *key)
oidmap.h +15 -0
@@ -35,6 +35,21 @@ struct oidmap {*/ void oidmap_init (struct oidmap *map,size_t initial_size); +/* +* Function type for functions that free oidmap entries. +*/ +typedef void (*oidmap_free_fn )(void *); + +/* +* Clear an oidmap, freeing any allocated memory. The map is empty and +* can be reused without another explicit init. +* +* The `free_fn`, if not NULL, is called for each oidmap entry in the map +* to free any user data associated with the entry. +*/ +void oidmap_clear_with_free (struct oidmap *map, +oidmap_free_fn free_fn); +/* * Clear an oidmap, freeing any allocated memory. The map is empty and * can be reused without another explicit init.
t/unit-tests/u-oidmap.c +41 -0
50 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v2 1/5] oidmap: make entry cleanup explicit in oidmap_clear
oidmap.c void oidmap_init(struct oidmap *map, size_t initial_size) · line 32
Quoted 9 lines · Seyi Kuforiji
void
oidmap_clear(struct oidmap *map, int free_entries){oidmap.c +6
-1-if (!map) +oidmap_clear_with_free (map, + free_entries ? free :NULL ); +} + +void oidmap_clear_with_free (struct oidmap *map, +oidmap_free_fn free_fn)
Made me briefly wonder if passing "void free(void *)" or NULL as oidmap_free_fn would be flagged by the compilers as suspicious, but "typedef void (*oidmap_free_fn)(void *);" is obviously compatible with both, so it is good.
oidmap.c void oidmap_init(struct oidmap *map, size_t initial_size) · line 38
Quoted 6 lines
oidmap.c +5
-0+{ +struct hashmap_iter iter; +struct hashmap_entry *e; + +if (!map || !map->map.cmpfn)return ;
The first half prepares our oidmap_clear() to be fed a NULL map, but what about the new condition? Where did it come from? What makes it suddenly necessary that in order to "clear" an oidmap you already have to have defined cmpfn?
oidmap.c void oidmap_init(struct oidmap *map, size_t initial_size) · line 31
Quoted 2 lines
oidmap.c +0
-2-/* TODO: make oidmap itself not depend on struct layouts */ -hashmap_clear_ (&map->map, free_entries ?0 :-1 );
This is now achieved by the use of container_of(), right? Nice.
Quoted 10 lines
oidmap.c +9
-0+hashmap_iter_init (&map->map, &iter); +while ((e =hashmap_iter_next (&iter))) { +struct oidmap_entry *entry = + container_of(e, struct oidmap_entry, internal_entry); +if (free_fn) +free_fn (entry); + } + +hashmap_clear (&map->map); }
PATCH v2 2/5 builtin/rev-list: migrate missing_objects cleanup to oidmap_clear_with_free()
In reply to Seyi KuforijiFrom: Seyi Kufoiji <[email protected]>
As part of the conversion away from oidmap_clear(), switch the missing_objects map to use oidmap_clear_with_free().
missing_objects stores struct missing_objects_map_entry instances, which own an xstrdup()'d path string in addition to the container struct itself. Previously, rev-list manually freed entry->path before calling oidmap_clear(&missing_objects, true).
Introduce a dedicated free callback and pass it to oidmap_clear_with_free(), consolidating entry teardown into a single place and making cleanup semantics explicit.
--- builtin/rev-list.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
builtin/rev-list.c +12 -3
@@ -88,9 +88,19 @@ static int arg_print_omitted; /* print objects omitted by filter */struct missing_objects_map_entry {struct oidmap_entry entry; -const char *path; +char *path;unsigned type; }; + +static void free_missing_objects_entry (void *e) +{ +struct missing_objects_map_entry *entry = + container_of(e, struct missing_objects_map_entry, entry); + +free (entry->path); +free (entry); +} +static struct oidmap missing_objects;enum missing_action {MA_ERROR =0 ,/* fail if any missing objects are encountered */
@@ -935,10 +945,9 @@ int cmd_rev_list(int argc,while ((entry =oidmap_iter_next (&iter))) {print_missing_object (entry, arg_missing_action ==MA_PRINT_INFO ); -free ((void *)entry->path); } -oidmap_clear (&missing_objects, true); +oidmap_clear_with_free (&missing_objects, free_missing_objects_entry); }stop_progress (&progress);
Signature · 1 line
2.43.0
Re: [PATCH v2 2/5] builtin/rev-list: migrate missing_objects cleanup to oidmap_clear_with_free()
Quoted 30 lines · Seyi Kuforiji
From: Seyi Kufoiji <[email protected]>
As part of the conversion away from
oidmap_clear(), switch themissing_objectsmap to useoidmap_clear_with_free().
missing_objectsstores structmissing_objects_map_entryinstances, which own an xstrdup()'d path string in addition to the container struct itself. Previously, rev-list manually freedentry->pathbefore callingoidmap_clear(&missing_objects, true).Introduce a dedicated free callback and pass it to
oidmap_clear_with_free(), consolidating entry teardown into a single place and making cleanup semantics explicit.Signed-off-by Seyi Kuforiji <[email protected]>--- builtin/rev-list.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)builtin/rev-list.c +1
-1@@ -88,9 +88,19 @@ static int arg_print_omitted; /* print objects omitted by filter */struct missing_objects_map_entry {struct oidmap_entry entry; -const char *path; +char *path;unsigned type;
builtin/rev-list.c static int arg_print_omitted; /* print objects omitted by filter */ · line 102
Quoted 10 lines
builtin/rev-list.c +9
-0}; + +static void free_missing_objects_entry (void *e) +{ +struct missing_objects_map_entry *entry = + container_of(e, struct missing_objects_map_entry, entry); + +free (entry->path); +free (entry); +}
Unlike the previous iteration, this makes the claim very believable that it makes the code easier to follow to have the shell and its contents released together in a single function.
Looking good.
Quoted 15 lines
builtin/rev-list.c +1
-2static struct oidmap missing_objects;enum missing_action {MA_ERROR =0 ,/* fail if any missing objects are encountered */ @@ -935,10 +945,9 @@ int cmd_rev_list(int argc,while ((entry =oidmap_iter_next (&iter))) {print_missing_object (entry, arg_missing_action ==MA_PRINT_INFO ); -free ((void *)entry->path); } -oidmap_clear (&missing_objects, true); +oidmap_clear_with_free (&missing_objects, free_missing_objects_entry); }stop_progress (&progress);
Re: [PATCH v2 2/5] builtin/rev-list: migrate missing_objects cleanup to oidmap_clear_with_free()
In reply to Seyi KuforijiQuoted 5 lines · Seyi Kuforiji
builtin/rev-list.c +0
-0
builtin/rev-list.c static int arg_print_omitted; /* print objects omitted by filter */ · line 95
Quoted 9 lines
builtin/rev-list.c +3
-1@@ -88,9 +88,19 @@ static int arg_print_omitted; /* print objects omitted by filter */struct missing_objects_map_entry {struct oidmap_entry entry; -const char *path; +char *path;unsigned type; }; + +static void free_missing_objects_entry (void *e)
Nit: this should be called missing_objects_map_entry_free() according to our coding guidelines.
Patrick
PATCH v2 3/5 list-objects-filter: use oidmap_clear_with_free() for cleanup
In reply to Seyi KuforijiFrom: Seyi Kufoiji <[email protected]>
Replace the use of oidmap_clear(&seen_at_depth, 1) in filter_trees_free() with oidmap_clear_with_free().
The seen_at_depth map stores heap-allocated struct seen_map_entry objects. Previously, passing 1 relied on oidmap_clear() internally calling free() on each entry.
Convert this to the explicit oidmap_clear_with_free() API and provide a typed free_seen_map_entry() helper to free each container entry.
This makes the ownership and cleanup policy explicit and removes reliance on the legacy boolean free_entries parameter.
--- list-objects-filter.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
list-objects-filter.c +8 -1
@@ -143,6 +143,13 @@ struct seen_map_entry {size_t depth; }; +static void free_seen_map_entry (void *e) +{ +struct seen_map_entry *entry = + container_of(e, struct seen_map_entry, base); +free (entry); +} +/* Returns 1 if the oid was in the omits set before it was invoked. */ static int filter_trees_update_omits( struct object *obj,
@@ -244,7 +251,7 @@ static void filter_trees_free(void *filter_data) {struct filter_trees_depth_data *d = filter_data;if (!d)return ; -oidmap_clear (&d->seen_at_depth,1 ); +oidmap_clear_with_free (&d->seen_at_depth, free_seen_map_entry);free (d); }
Signature · 1 line
2.43.0
Re: [PATCH v2 3/5] list-objects-filter: use oidmap_clear_with_free() for cleanup
Quoted 5 lines · Seyi Kuforiji
list-objects-filter.c +0
-0
list-objects-filter.c struct seen_map_entry { · line 151
Quoted 9 lines
list-objects-filter.c +6
-0@@ -143,6 +143,13 @@ struct seen_map_entry {size_t depth; }; +static void free_seen_map_entry (void *e) +{ +struct seen_map_entry *entry = + container_of(e, struct seen_map_entry, base); +free (entry); +}
As there is *no* extra resources held in seen_map_entry other than the shell itself, this step alone does not make the code any clearer to follow. But if we are going to add new members to the structure in the future, the story will change and we'll leap the same benefit as we saw in [PATCH v2 2/5].
Quoted 8 lines
list-objects-filter.c +1
-1@@ -244,7 +251,7 @@ static void filter_trees_free(void *filter_data) {struct filter_trees_depth_data *d = filter_data;if (!d)return ; -oidmap_clear (&d->seen_at_depth,1 ); +oidmap_clear_with_free (&d->seen_at_depth, free_seen_map_entry);free (d); }
Re: [PATCH v2 3/5] list-objects-filter: use oidmap_clear_with_free() for cleanup
Quoted 10 lines · Junio C Hamano
Quoted 8 lines · Seyi Kuforiji
list-objects-filter.c +0
-0@@ -143,6 +143,13 @@ struct seen_map_entry {size_t depth; };
Quoted 12 lines
list-objects-filter.c struct seen_map_entry { · line 151
Quoted 6 lines
list-objects-filter.c +6
-0+static void free_seen_map_entry (void *e) +{ +struct seen_map_entry *entry = + container_of(e, struct seen_map_entry, base); +free (entry); +}As there is *no* extra resources held in
seen_map_entryother than the shell itself, this step alone does not make the code any clearer to follow. But if we are going to add new members to the structure in the future, the story will change and we'll leap the same benefit as we saw in [PATCH v2 2/5].
Agreed. But I think with the current status quo I'd rather drop this patch though as it may otherwise make the reader scratch their head why we do the exercise in the first place.
Patrick
Re: [PATCH v2 3/5] list-objects-filter: use oidmap_clear_with_free() for cleanup
Quoted 3 lines · Patrick Steinhardt
Agreed. But I think with the current status quo I'd rather drop this patch though as it may otherwise make the reader scratch their head why we do the exercise in the first place.
I do not think too strongly either way myself, but you may be right.
Unless we are dropping the "we optionally let you free the shell" traditional interface, it is of questionable value to use the new interface.
Thanks.
Re: [PATCH v2 3/5] list-objects-filter: use oidmap_clear_with_free() for cleanup
Quoted 7 lines · Junio C Hamano
Quoted 3 lines · Patrick Steinhardt
Agreed. But I think with the current status quo I'd rather drop this patch though as it may otherwise make the reader scratch their head why we do the exercise in the first place.
Quoted 8 lines
I do not think too strongly either way myself, but you may be right.
Unless we are dropping the "we optionally let you free the shell" traditional interface, it is of questionable value to use the new interface.
Thanks.
Hello
Thank you so much for the reviews.
I'll send a new version dropping the [PATCH 3/5].
Thanks,
Seyi
Re: [PATCH v2 3/5] list-objects-filter: use oidmap_clear_with_free() for cleanup
Quoted 14 lines · Seyi Kuforiji
Quoted 13 lines · Junio C Hamano
Quoted 3 lines · Patrick Steinhardt
Agreed. But I think with the current status quo I'd rather drop this patch though as it may otherwise make the reader scratch their head why we do the exercise in the first place.
I do not think too strongly either way myself, but you may be right.
Unless we are dropping the "we optionally let you free the shell" traditional interface, it is of questionable value to use the new interface.
Quoted 8 lines
Quoted 2 lines
Thanks.
Hello
Thank you so much for the reviews.
I'll send a new version dropping the [PATCH 3/5].
I thought that Patrick wants to see only [1/5] and [2/5], discarding the rest (i.e. 3/5, 4/5, and 5/5). If that is the plan, I do not think we need any resend.
Thanks.
Re: [PATCH v2 3/5] list-objects-filter: use oidmap_clear_with_free() for cleanup
Quoted 21 lines · Junio C Hamano
Quoted 19 lines · Seyi Kuforiji
Quoted 7 lines · Junio C Hamano
Quoted 3 lines · Patrick Steinhardt
Agreed. But I think with the current status quo I'd rather drop this patch though as it may otherwise make the reader scratch their head why we do the exercise in the first place.
Quoted 8 lines
I do not think too strongly either way myself, but you may be right.
Unless we are dropping the "we optionally let you free the shell" traditional interface, it is of questionable value to use the new interface.
Thanks.
Hello
Quoted 7 lines
Quoted 3 lines
Thank you so much for the reviews.
I'll send a new version dropping the [PATCH 3/5].
I thought that Patrick wants to see only [1/5] and [2/5], discarding the rest (i.e. 3/5, 4/5, and 5/5). If that is the plan, I do not think we need any resend.
Ah, in https://lore.kernel.org/git/[email protected]/ he wants the callback to be renamed, so we do need a new iteration (v3). I still think that if you are to drop [3/5], then [4/5] and [5/5] should also be dropped, leaving only the first two patches.
Thanks.
PATCH v2 4/5 odb: use oidmap_clear_with_free() to release replace_map entries
In reply to Seyi KuforijiFrom: Seyi Kufoiji <[email protected]>
Replace the direct oidmap_clear() call in odb_free() with oidmap_clear_with_free(), and introduce a free_replace_map_entry() helper to properly free each struct replace_object stored in the map.
This centralizes cleanup logic and ensures entries are released correctly via a dedicated callback.
--- odb.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
odb.c +10 -1
@@ -14,6 +14,7 @@#include "object-file-convert.h" #include "object-file.h" #include "odb.h" +#include "oidmap.h" #include "packfile.h" #include "path.h" #include "promisor-remote.h"
@@ -1089,6 +1090,13 @@ void odb_close(struct object_database *o)close_commit_graph (o); } +static void free_replace_map_entry (void *e) +{ +struct replace_object *entry = + container_of(e, struct replace_object, original); +free (entry); +} +static void odb_free_sources (struct object_database *o) {while (o->sources) {
@@ -1109,7 +1117,8 @@ void odb_free(struct object_database *o)free (o->alternate_db); -oidmap_clear (&o->replace_map,1 ); +if (o->replace_map_initialized) +oidmap_clear_with_free (&o->replace_map, free_replace_map_entry);pthread_mutex_destroy (&o->replace_mutex);odb_close (o);
Signature · 1 line
2.43.0
Re: [PATCH v2 4/5] odb: use oidmap_clear_with_free() to release replace_map entries
odb.c void odb_close(struct object_database *o) · line 1099
Quoted 7 lines · Seyi Kuforiji
odb.c +7
-0+static void free_replace_map_entry (void *e) +{ +struct replace_object *entry = + container_of(e, struct replace_object, original); +free (entry); +} +
The same comment as [PATCH v2 3/5].
odb.c void odb_free(struct object_database *o) · line 1121
Quoted 7 lines
odb.c +2
-1@@ -1109,7 +1117,8 @@ void odb_free(struct object_database *o)free (o->alternate_db); -oidmap_clear (&o->replace_map,1 ); +if (o->replace_map_initialized) +oidmap_clear_with_free (&o->replace_map, free_replace_map_entry);
It is a bit unfortunate that we need to know how o->replace_map is initialized and maintained. I wondered if we can do this without peeking into o->replace_map_initialized, but o->replace_map is already an instance of the map, not a pointer that points at a lazily initialized instance of a map, so that cannot be done (and we would not have replace_map_initialized member in the object_database struct in the first place, if we can tell if o.replace_map needs clearing by simply looking at it).
So, all OK, I guess.
Quoted 3 lines
odb.c +0
-0pthread_mutex_destroy (&o->replace_mutex);odb_close (o);
PATCH v2 5/5 sequencer: use oidmap_clear_with_free() for string_entry cleanup
In reply to Seyi KuforijiFrom: Seyi Kufoiji <[email protected]>
Switch cleanup of the string_entry oidmap to oidmap_clear_with_free() and introduce a free_string_entry() helper to properly free each allocated struct string_entry.
This aligns with the ongoing migration to use the callback-based oidmap cleanup API.
--- sequencer.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
sequencer.c +8 -2
@@ -5654,6 +5654,12 @@ struct string_entry {char string[FLEX_ARRAY ]; }; +static void free_string_entry (void *e) +{ + struct string_entry *entry = container_of(e, struct string_entry, entry); +free (entry); +} +struct label_state {struct oidmap commit2label;struct hashmap labels;
@@ -6044,8 +6050,8 @@ static int make_script_with_merges(struct pretty_print_context *pp,oidset_clear (&interesting);oidset_clear (&child_seen);oidset_clear (&shown); -oidmap_clear (&commit2todo,1 ); -oidmap_clear (&state.commit2label,1 ); +oidmap_clear_with_free (&commit2todo, free_string_entry); +oidmap_clear_with_free (&state.commit2label, free_string_entry); hashmap_clear_and_free(&state.labels, struct labels_entry, entry);strbuf_release (&state.buf);
Signature · 1 line
2.43.0
Re: [PATCH v2 5/5] sequencer: use oidmap_clear_with_free() for string_entry cleanup
Quoted 19 lines · Seyi Kuforiji
From: Seyi Kufoiji <[email protected]>
Switch cleanup of the
string_entryoidmap tooidmap_clear_with_free()and introduce afree_string_entry()helper to properly free each allocated structstring_entry.This aligns with the ongoing migration to use the callback-based oidmap cleanup API.
Signed-off-by Seyi Kuforiji <[email protected]>--- sequencer.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)sequencer.c +0
-0
sequencer.c struct string_entry { · line 5661
Quoted 8 lines
sequencer.c +5
-0@@ -5654,6 +5654,12 @@ struct string_entry {char string[FLEX_ARRAY ]; }; +static void free_string_entry (void *e) +{ + struct string_entry *entry = container_of(e, struct string_entry, entry); +free (entry); +}
Exactly the same comment applies to this step as [PATCH v2 3/5].
In other words, with the current codebase, these three steps in the context of the current code are uninteresting with little value, but if we ever add a member to these entries that hold their own resources, it would become easier to manage the lifetime rules of them.
Quoted 10 lines
sequencer.c +2
-2@@ -6044,8 +6050,8 @@ static int make_script_with_merges(struct pretty_print_context *pp,oidset_clear (&interesting);oidset_clear (&child_seen);oidset_clear (&shown); -oidmap_clear (&commit2todo,1 ); -oidmap_clear (&state.commit2label,1 ); +oidmap_clear_with_free (&commit2todo, free_string_entry); +oidmap_clear_with_free (&state.commit2label, free_string_entry); hashmap_clear_and_free(&state.labels, struct labels_entry, entry);strbuf_release (&state.buf);
Re: [PATCH v2 5/5] sequencer: use oidmap_clear_with_free() for string_entry cleanup
Quoted 32 lines · Junio C Hamano
Quoted 19 lines · Seyi Kuforiji
From: Seyi Kufoiji <[email protected]>
Switch cleanup of the
string_entryoidmap tooidmap_clear_with_free()and introduce afree_string_entry()helper to properly free each allocated structstring_entry.This aligns with the ongoing migration to use the callback-based oidmap cleanup API.
Signed-off-by Seyi Kuforiji <[email protected]>--- sequencer.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)sequencer.c +0
-0sequencer.c struct string_entry { · line 5661
Quoted 8 lines
sequencer.c +5
-0@@ -5654,6 +5654,12 @@ struct string_entry {char string[FLEX_ARRAY ]; }; +static void free_string_entry (void *e) +{ + struct string_entry *entry = container_of(e, struct string_entry, entry); +free (entry); +}Exactly the same comment applies to this step as [PATCH v2 3/5].
Quoted 5 lines
In other words, with the current codebase, these three steps in the context of the current code are uninteresting with little value, but if we ever add a member to these entries that hold their own resources, it would become easier to manage the lifetime rules of them.
Personally I'd lean towards keeping the first two patches and drop the remaining ones though. I think it makes the code harder to understand to convert all callsites of oidmap_clear(), even if it doesn't actually provide a benefit.
We can still convert callsites to use oidmap_clear_with_free() in case they grow additional allocations per entry that we'll have to care about.
Thanks!
Patrick
v3 cover oidmap: migrate cleanup to oidmap_clear_with_free()
In reply to Seyi KuforijiHi,
This series replaces oidmap_clear(map, 1) with oidmap_clear_with_free() and introduces explicit free callbacks at the remaining call sites.
The old boolean-based API implicitly assumed plain free(), which obscures ownership semantics and does not work well when oidmap_entry is embedded inside larger structures. The callback-based API makes cleanup explicit and type-safe, and avoids relying on hidden assumptions about allocation.
This is used in subsequent commits to adequately cleanup all usage site.
Changes in v3:
- rename funtion to meet guidlines
- drop [PATCH 3/5]
Thanks
Seyi
Seyi Kufoiji (2):
oidmap: make entry cleanup explicit in oidmap_clear builtin/rev-list: migrate missing_objects cleanup to
oidmap_clear_with_free()
builtin/rev-list.c | 15 ++++++++++++--- oidmap.c | 23 ++++++++++++++++++++--- oidmap.h | 15 +++++++++++++++ t/unit-tests/u-oidmap.c | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 6 deletions(-)
Range-diff against v2:
1: 1d544ef7d2 = 1: a050491441 oidmap: make entry cleanup explicit in oidmap_clear
2: f2c3a699bd ! 2: b592d765e3 builtin/rev-list: migrate missing_objects cleanup to oidmap_clear_with_free()
@@ builtin/rev-list.c: static int arg_print_omitted; /* print objects omitted by fi
unsigned type;
};
+
-+static void free_missing_objects_entry(void *e)
++static void missing_objects_map_entry_free(void *e)
+{
+ struct missing_objects_map_entry *entry =
+ container_of(e, struct missing_objects_map_entry, entry);
@@ builtin/rev-list.c: int cmd_rev_list(int argc,
}
- oidmap_clear(&missing_objects, true);
-+ oidmap_clear_with_free(&missing_objects, free_missing_objects_entry);
++ oidmap_clear_with_free(&missing_objects, missing_objects_map_entry_free);
}
stop_progress(&progress);
3: a4e426bcca < -: ---------- list-objects-filter: use oidmap_clear_with_free() for cleanup
4: 4116e5491d < -: ---------- odb: use oidmap_clear_with_free() to release replace_map entries
5: ad1f776a19 < -: ---------- sequencer: use oidmap_clear_with_free() for string_entry cleanupSignature · 1 line
2.43.0
PATCH v3 1/2 oidmap: make entry cleanup explicit in oidmap_clear
From: Seyi Kufoiji <[email protected]>
Replace oidmap's use of hashmap_clear_() and layout-dependent freeing with an explicit iteration and optional free callback. This removes reliance on struct layout assumptions while keeping the existing API intact.
Add tests for oidmap_clear_with_free behavior. test_oidmap__clear_with_free_callback verifies that entries are freed when a callback is provided, while test_oidmap__clear_without_free_callback verifies that entries are not freed when no callback is given. These tests ensure the new clear implementation behaves correctly and preserves ownership semantics.
--- oidmap.c | 23 ++++++++++++++++++++--- oidmap.h | 15 +++++++++++++++ t/unit-tests/u-oidmap.c | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 3 deletions(-)
oidmap.c +20 -3
@@ -24,11 +24,28 @@ void oidmap_init(struct oidmap *map, size_t initial_size)void oidmap_clear (struct oidmap *map,int free_entries) { -if (!map) +oidmap_clear_with_free (map, + free_entries ? free :NULL ); +} + +void oidmap_clear_with_free (struct oidmap *map, +oidmap_free_fn free_fn) +{ +struct hashmap_iter iter; +struct hashmap_entry *e; + +if (!map || !map->map.cmpfn)return ; -/* TODO: make oidmap itself not depend on struct layouts */ -hashmap_clear_ (&map->map, free_entries ?0 :-1 ); +hashmap_iter_init (&map->map, &iter); +while ((e =hashmap_iter_next (&iter))) { +struct oidmap_entry *entry = + container_of(e, struct oidmap_entry, internal_entry); +if (free_fn) +free_fn (entry); + } + +hashmap_clear (&map->map); }void *oidmap_get (const struct oidmap *map,const struct object_id *key)
oidmap.h +15 -0
@@ -35,6 +35,21 @@ struct oidmap {*/ void oidmap_init (struct oidmap *map,size_t initial_size); +/* +* Function type for functions that free oidmap entries. +*/ +typedef void (*oidmap_free_fn )(void *); + +/* +* Clear an oidmap, freeing any allocated memory. The map is empty and +* can be reused without another explicit init. +* +* The `free_fn`, if not NULL, is called for each oidmap entry in the map +* to free any user data associated with the entry. +*/ +void oidmap_clear_with_free (struct oidmap *map, +oidmap_free_fn free_fn); +/* * Clear an oidmap, freeing any allocated memory. The map is empty and * can be reused without another explicit init.
t/unit-tests/u-oidmap.c +41 -0
50 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v3 2/2 builtin/rev-list: migrate missing_objects cleanup to oidmap_clear_with_free()
In reply to Seyi KuforijiFrom: Seyi Kufoiji <[email protected]>
As part of the conversion away from oidmap_clear(), switch the missing_objects map to use oidmap_clear_with_free().
missing_objects stores struct missing_objects_map_entry instances, which own an xstrdup()'d path string in addition to the container struct itself. Previously, rev-list manually freed entry->path before calling oidmap_clear(&missing_objects, true).
Introduce a dedicated free callback and pass it to oidmap_clear_with_free(), consolidating entry teardown into a single place and making cleanup semantics explicit.
--- builtin/rev-list.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
builtin/rev-list.c +12 -3
@@ -88,9 +88,19 @@ static int arg_print_omitted; /* print objects omitted by filter */struct missing_objects_map_entry {struct oidmap_entry entry; -const char *path; +char *path;unsigned type; }; + +static void missing_objects_map_entry_free (void *e) +{ +struct missing_objects_map_entry *entry = + container_of(e, struct missing_objects_map_entry, entry); + +free (entry->path); +free (entry); +} +static struct oidmap missing_objects;enum missing_action {MA_ERROR =0 ,/* fail if any missing objects are encountered */
@@ -935,10 +945,9 @@ int cmd_rev_list(int argc,while ((entry =oidmap_iter_next (&iter))) {print_missing_object (entry, arg_missing_action ==MA_PRINT_INFO ); -free ((void *)entry->path); } -oidmap_clear (&missing_objects, true); +oidmap_clear_with_free (&missing_objects, missing_objects_map_entry_free); }stop_progress (&progress);
Signature · 1 line
2.43.0
Quoted 12 lines · Seyi Kuforiji
Hi,
This series replaces
oidmap_clear(map, 1)withoidmap_clear_with_free()and introduces explicit free callbacks at the remaining call sites.The old boolean-based API implicitly assumed plain
free(), which obscures ownership semantics and does not work well whenoidmap_entryis embedded inside larger structures. The callback-based API makes cleanup explicit and type-safe, and avoids relying on hidden assumptions about allocation.
Quoted 6 lines
This is used in subsequent commits to adequately cleanup all usage site.
Changes in v3:
- rename funtion to meet guidlines
- drop [PATCH 3/5]
Thanks, this version looks good to me!
Patrick
Quoted 16 lines · Seyi Kuforiji
Range-diff against v2:
1: 1d544ef7d2 = 1: a050491441 oidmap: make entry cleanup explicit in oidmap_clear 2: f2c3a699bd ! 2: b592d765e3 builtin/rev-list: migrate missing_objects cleanup to oidmap_clear_with_free() @@ builtin/rev-list.c: static int arg_print_omitted; /* print objects omitted by fi unsigned type; }; + -+static void free_missing_objects_entry(void *e) ++static void missing_objects_map_entry_free(void *e) +{ + struct missing_objects_map_entry *entry = + container_of(e, struct missing_objects_map_entry, entry); @@ builtin/rev-list.c: int cmd_rev_list(int argc, } - oidmap_clear(&missing_objects, true);
Quoted 8 lines
-+ oidmap_clear_with_free(&missing_objects, free_missing_objects_entry);
++ oidmap_clear_with_free(&missing_objects, missing_objects_map_entry_free);
}
stop_progress(&progress);
3: a4e426bcca < -: ---------- list-objects-filter: use oidmap_clear_with_free() for cleanup
4: 4116e5491d < -: ---------- odb: use oidmap_clear_with_free() to release replace_map entries
5: ad1f776a19 < -: ---------- sequencer: use oidmap_clear_with_free() for string_entry cleanupI think these cover everything Patrick pointed out, and I agree with what these remaining two patches do. Will queue.
Let me mark the topic for 'next'.
Thanks.