hashmap: introduce hashmap_free_entries

`hashmap_free_entries' behaves like `container_of' and passes
the offset of the hashmap_entry struct to the internal
`hashmap_free_' function, allowing the function to free any
struct pointer regardless of where the hashmap_entry field
is located.

`hashmap_free' no longer takes any arguments aside from
the hashmap itself.

Signed-off-by: Eric Wong <e@80x24.org>
Reviewed-by: Derrick Stolee <stolee@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Eric Wong
2019-10-06 23:30:40 +00:00
committed by Junio C Hamano
parent 8a973d0bb3
commit c8e424c9c9
17 changed files with 52 additions and 34 deletions

View File

@ -171,16 +171,21 @@ void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function,
map->do_count_items = 1;
}
void hashmap_free(struct hashmap *map, int free_entries)
void hashmap_free_(struct hashmap *map, ssize_t entry_offset)
{
if (!map || !map->table)
return;
if (free_entries) {
if (entry_offset >= 0) { /* called by hashmap_free_entries */
struct hashmap_iter iter;
struct hashmap_entry *e;
hashmap_iter_init(map, &iter);
while ((e = hashmap_iter_next(&iter)))
free(e);
/*
* like container_of, but using caller-calculated
* offset (caller being hashmap_free_entries)
*/
free((char *)e - entry_offset);
}
free(map->table);
memset(map, 0, sizeof(*map));