Skip to content

Commit c8e424c

Browse files
Eric Wonggitster
authored andcommitted
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 <[email protected]> Reviewed-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8a973d0 commit c8e424c

17 files changed

+52
-34
lines changed

blame.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ static void get_fingerprint(struct fingerprint *result,
433433

434434
static void free_fingerprint(struct fingerprint *f)
435435
{
436-
hashmap_free(&f->map, 0);
436+
hashmap_free(&f->map);
437437
free(f->entries);
438438
}
439439

builtin/fetch.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ static void find_non_local_tags(const struct ref *refs,
366366
item = refname_hash_add(&remote_refs, ref->name, &ref->old_oid);
367367
string_list_insert(&remote_refs_list, ref->name);
368368
}
369-
hashmap_free(&existing_refs, 1);
369+
hashmap_free_entries(&existing_refs, struct refname_hash_entry, ent);
370370

371371
/*
372372
* We may have a final lightweight tag that needs to be
@@ -401,7 +401,7 @@ static void find_non_local_tags(const struct ref *refs,
401401
**tail = rm;
402402
*tail = &rm->next;
403403
}
404-
hashmap_free(&remote_refs, 1);
404+
hashmap_free_entries(&remote_refs, struct refname_hash_entry, ent);
405405
string_list_clear(&remote_refs_list, 0);
406406
}
407407

@@ -530,7 +530,7 @@ static struct ref *get_ref_map(struct remote *remote,
530530
}
531531
}
532532
}
533-
hashmap_free(&existing_refs, 1);
533+
hashmap_free_entries(&existing_refs, struct refname_hash_entry, ent);
534534

535535
return ref_map;
536536
}

config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1948,7 +1948,7 @@ void git_configset_clear(struct config_set *cs)
19481948
free(entry->key);
19491949
string_list_clear(&entry->value_list, 1);
19501950
}
1951-
hashmap_free(&cs->config_hash, 1);
1951+
hashmap_free_entries(&cs->config_hash, struct config_set_element, ent);
19521952
cs->hash_initialized = 0;
19531953
free(cs->list.items);
19541954
cs->list.nr = 0;

diff.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6236,8 +6236,10 @@ static void diff_flush_patch_all_file_pairs(struct diff_options *o)
62366236
if (o->color_moved == COLOR_MOVED_ZEBRA_DIM)
62376237
dim_moved_lines(o);
62386238

6239-
hashmap_free(&add_lines, 1);
6240-
hashmap_free(&del_lines, 1);
6239+
hashmap_free_entries(&add_lines, struct moved_entry,
6240+
ent);
6241+
hashmap_free_entries(&del_lines, struct moved_entry,
6242+
ent);
62416243
}
62426244

62436245
for (i = 0; i < esm.nr; i++)

diffcore-rename.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ static int find_exact_renames(struct diff_options *options)
358358
renames += find_identical_files(&file_table, i, options);
359359

360360
/* Free the hash data structure and entries */
361-
hashmap_free(&file_table, 1);
361+
hashmap_free_entries(&file_table, struct file_similarity, entry);
362362

363363
return renames;
364364
}

hashmap.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,21 @@ void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function,
171171
map->do_count_items = 1;
172172
}
173173

174-
void hashmap_free(struct hashmap *map, int free_entries)
174+
void hashmap_free_(struct hashmap *map, ssize_t entry_offset)
175175
{
176176
if (!map || !map->table)
177177
return;
178-
if (free_entries) {
178+
if (entry_offset >= 0) { /* called by hashmap_free_entries */
179179
struct hashmap_iter iter;
180180
struct hashmap_entry *e;
181+
181182
hashmap_iter_init(map, &iter);
182183
while ((e = hashmap_iter_next(&iter)))
183-
free(e);
184+
/*
185+
* like container_of, but using caller-calculated
186+
* offset (caller being hashmap_free_entries)
187+
*/
188+
free((char *)e - entry_offset);
184189
}
185190
free(map->table);
186191
memset(map, 0, sizeof(*map));

hashmap.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
* }
9797
*
9898
* if (!strcmp("end", action)) {
99-
* hashmap_free(&map, 1);
99+
* hashmap_free_entries(&map, struct long2string, ent);
100100
* break;
101101
* }
102102
* }
@@ -232,13 +232,20 @@ void hashmap_init(struct hashmap *map,
232232
const void *equals_function_data,
233233
size_t initial_size);
234234

235+
/* internal function for freeing hashmap */
236+
void hashmap_free_(struct hashmap *map, ssize_t offset);
237+
235238
/*
236-
* Frees a hashmap structure and allocated memory.
237-
*
238-
* If `free_entries` is true, each hashmap_entry in the map is freed as well
239-
* using stdlibs free().
239+
* Frees a hashmap structure and allocated memory, leaves entries undisturbed
240+
*/
241+
#define hashmap_free(map) hashmap_free_(map, -1)
242+
243+
/*
244+
* Frees @map and all entries. @type is the struct type of the entry
245+
* where @member is the hashmap_entry struct used to associate with @map
240246
*/
241-
void hashmap_free(struct hashmap *map, int free_entries);
247+
#define hashmap_free_entries(map, type, member) \
248+
hashmap_free_(map, offsetof(type, member));
242249

243250
/* hashmap_entry functions */
244251

merge-recursive.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2633,7 +2633,7 @@ static struct string_list *get_renames(struct merge_options *opt,
26332633
free(e->target_file);
26342634
string_list_clear(&e->source_files, 0);
26352635
}
2636-
hashmap_free(&collisions, 1);
2636+
hashmap_free_entries(&collisions, struct collision_entry, ent);
26372637
return renames;
26382638
}
26392639

@@ -2853,7 +2853,7 @@ static void initial_cleanup_rename(struct diff_queue_struct *pairs,
28532853
strbuf_release(&e->new_dir);
28542854
/* possible_new_dirs already cleared in get_directory_renames */
28552855
}
2856-
hashmap_free(dir_renames, 1);
2856+
hashmap_free_entries(dir_renames, struct dir_rename_entry, ent);
28572857
free(dir_renames);
28582858

28592859
free(pairs->queue);
@@ -3482,7 +3482,8 @@ int merge_trees(struct merge_options *opt,
34823482
string_list_clear(entries, 1);
34833483
free(entries);
34843484

3485-
hashmap_free(&opt->current_file_dir_set, 1);
3485+
hashmap_free_entries(&opt->current_file_dir_set,
3486+
struct path_hashmap_entry, e);
34863487

34873488
if (clean < 0) {
34883489
unpack_trees_finish(opt);

name-hash.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,6 @@ void free_name_hash(struct index_state *istate)
728728
return;
729729
istate->name_hash_initialized = 0;
730730

731-
hashmap_free(&istate->name_hash, 0);
732-
hashmap_free(&istate->dir_hash, 1);
731+
hashmap_free(&istate->name_hash);
732+
hashmap_free_entries(&istate->dir_hash, struct dir_entry, ent);
733733
}

oidmap.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ void oidmap_free(struct oidmap *map, int free_entries)
2525
{
2626
if (!map)
2727
return;
28-
hashmap_free(&map->map, free_entries);
28+
29+
/* TODO: make oidmap itself not depend on struct layouts */
30+
hashmap_free_(&map->map, free_entries ? 0 : -1);
2931
}
3032

3133
void *oidmap_get(const struct oidmap *map, const struct object_id *key)

patch-ids.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ int init_patch_ids(struct repository *r, struct patch_ids *ids)
7171

7272
int free_patch_ids(struct patch_ids *ids)
7373
{
74-
hashmap_free(&ids->patches, 1);
74+
hashmap_free_entries(&ids->patches, struct patch_id, ent);
7575
return 0;
7676
}
7777

range-diff.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ static void find_exact_matches(struct string_list *a, struct string_list *b)
241241
}
242242
}
243243

244-
hashmap_free(&map, 0);
244+
hashmap_free(&map);
245245
}
246246

247247
static void diffsize_consume(void *data, char *line, unsigned long len)

ref-filter.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2172,7 +2172,8 @@ void ref_array_clear(struct ref_array *array)
21722172
used_atom_cnt = 0;
21732173

21742174
if (ref_to_worktree_map.worktrees) {
2175-
hashmap_free(&(ref_to_worktree_map.map), 1);
2175+
hashmap_free_entries(&(ref_to_worktree_map.map),
2176+
struct ref_to_worktree_entry, ent);
21762177
free_worktrees(ref_to_worktree_map.worktrees);
21772178
ref_to_worktree_map.worktrees = NULL;
21782179
}

revision.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ static void paths_and_oids_clear(struct hashmap *map)
136136
free(entry->path);
137137
}
138138

139-
hashmap_free(map, 1);
139+
hashmap_free_entries(map, struct path_and_oids_entry, ent);
140140
}
141141

142142
static void paths_and_oids_insert(struct hashmap *map,

sequencer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4772,7 +4772,7 @@ static int make_script_with_merges(struct pretty_print_context *pp,
47724772

47734773
oidmap_free(&commit2todo, 1);
47744774
oidmap_free(&state.commit2label, 1);
4775-
hashmap_free(&state.labels, 1);
4775+
hashmap_free_entries(&state.labels, struct labels_entry, entry);
47764776
strbuf_release(&state.buf);
47774777

47784778
return 0;
@@ -5301,7 +5301,7 @@ int todo_list_rearrange_squash(struct todo_list *todo_list)
53015301
for (i = 0; i < todo_list->nr; i++)
53025302
free(subjects[i]);
53035303
free(subjects);
5304-
hashmap_free(&subject2item, 1);
5304+
hashmap_free_entries(&subject2item, struct subject2item_entry, entry);
53055305

53065306
clear_commit_todo_item(&commit_todo);
53075307

submodule-config.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ static void submodule_cache_clear(struct submodule_cache *cache)
103103
struct submodule_entry, ent /* member name */)
104104
free_one_config(entry);
105105

106-
hashmap_free(&cache->for_path, 1);
107-
hashmap_free(&cache->for_name, 1);
106+
hashmap_free_entries(&cache->for_path, struct submodule_entry, ent);
107+
hashmap_free_entries(&cache->for_name, struct submodule_entry, ent);
108108
cache->initialized = 0;
109109
cache->gitmodules_read = 0;
110110
}

t/helper/test-hashmap.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
109109
hashmap_add(&map, &entries[i]->ent);
110110
}
111111

112-
hashmap_free(&map, 0);
112+
hashmap_free(&map);
113113
}
114114
} else {
115115
/* test map lookups */
@@ -129,7 +129,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
129129
}
130130
}
131131

132-
hashmap_free(&map, 0);
132+
hashmap_free(&map);
133133
}
134134
}
135135

@@ -266,6 +266,6 @@ int cmd__hashmap(int argc, const char **argv)
266266
}
267267

268268
strbuf_release(&line);
269-
hashmap_free(&map, 1);
269+
hashmap_free_entries(&map, struct test_entry, ent);
270270
return 0;
271271
}

0 commit comments

Comments
 (0)