Browse Source

hashmap: provide deallocation function names

hashmap_free(), hashmap_free_entries(), and hashmap_free_() have existed
for a while, but aren't necessarily the clearest names, especially with
hashmap_partial_clear() being added to the mix and lazy-initialization
now being supported.  Peff suggested we adopt the following names[1]:

  - hashmap_clear() - remove all entries and de-allocate any
    hashmap-specific data, but be ready for reuse

  - hashmap_clear_and_free() - ditto, but free the entries themselves

  - hashmap_partial_clear() - remove all entries but don't deallocate
    table

  - hashmap_partial_clear_and_free() - ditto, but free the entries

This patch provides the new names and converts all existing callers over
to the new naming scheme.

[1] https://lore.kernel.org/git/20201030125059.GA3277724@coredump.intra.peff.net/

Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
pull/933/head
Elijah Newren 2 years ago committed by Junio C Hamano
parent
commit
6da1a25814
  1. 2
      add-interactive.c
  2. 2
      blame.c
  3. 2
      bloom.c
  4. 6
      builtin/fetch.c
  5. 2
      builtin/shortlog.c
  6. 2
      config.c
  7. 4
      diff.c
  8. 2
      diffcore-rename.c
  9. 8
      dir.c
  10. 6
      hashmap.c
  11. 44
      hashmap.h
  12. 6
      merge-recursive.c
  13. 4
      name-hash.c
  14. 2
      object.c
  15. 2
      oidmap.c
  16. 2
      patch-ids.c
  17. 2
      range-diff.c
  18. 2
      ref-filter.c
  19. 2
      revision.c
  20. 4
      sequencer.c
  21. 4
      submodule-config.c
  22. 6
      t/helper/test-hashmap.c

2
add-interactive.c

@ -557,7 +557,7 @@ static int get_modified_files(struct repository *r,
if (ps)
clear_pathspec(&rev.prune_data);
}
hashmap_free_entries(&s.file_map, struct pathname_entry, ent);
hashmap_clear_and_free(&s.file_map, struct pathname_entry, ent);
if (unmerged_count)
*unmerged_count = s.unmerged_count;
if (binary_count)

2
blame.c

@ -435,7 +435,7 @@ static void get_fingerprint(struct fingerprint *result,
static void free_fingerprint(struct fingerprint *f)
{
hashmap_free(&f->map);
hashmap_clear(&f->map);
free(f->entries);
}

2
bloom.c

@ -287,7 +287,7 @@ struct bloom_filter *get_or_compute_bloom_filter(struct repository *r,
}
cleanup:
hashmap_free_entries(&pathmap, struct pathmap_hash_entry, entry);
hashmap_clear_and_free(&pathmap, struct pathmap_hash_entry, entry);
} else {
for (i = 0; i < diff_queued_diff.nr; i++)
diff_free_filepair(diff_queued_diff.queue[i]);

6
builtin/fetch.c

@ -393,7 +393,7 @@ static void find_non_local_tags(const struct ref *refs,
item = refname_hash_add(&remote_refs, ref->name, &ref->old_oid);
string_list_insert(&remote_refs_list, ref->name);
}
hashmap_free_entries(&existing_refs, struct refname_hash_entry, ent);
hashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent);
/*
* We may have a final lightweight tag that needs to be
@ -428,7 +428,7 @@ static void find_non_local_tags(const struct ref *refs,
**tail = rm;
*tail = &rm->next;
}
hashmap_free_entries(&remote_refs, struct refname_hash_entry, ent);
hashmap_clear_and_free(&remote_refs, struct refname_hash_entry, ent);
string_list_clear(&remote_refs_list, 0);
oidset_clear(&fetch_oids);
}
@ -573,7 +573,7 @@ static struct ref *get_ref_map(struct remote *remote,
}
}
if (existing_refs_populated)
hashmap_free_entries(&existing_refs, struct refname_hash_entry, ent);
hashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent);
return ref_map;
}

2
builtin/shortlog.c

@ -220,7 +220,7 @@ static void strset_clear(struct strset *ss)
{
if (!ss->map.table)
return;
hashmap_free_entries(&ss->map, struct strset_item, ent);
hashmap_clear_and_free(&ss->map, struct strset_item, ent);
}
static void insert_records_from_trailers(struct shortlog *log,

2
config.c

@ -1963,7 +1963,7 @@ void git_configset_clear(struct config_set *cs)
free(entry->key);
string_list_clear(&entry->value_list, 1);
}
hashmap_free_entries(&cs->config_hash, struct config_set_element, ent);
hashmap_clear_and_free(&cs->config_hash, struct config_set_element, ent);
cs->hash_initialized = 0;
free(cs->list.items);
cs->list.nr = 0;

4
diff.c

@ -6289,9 +6289,9 @@ static void diff_flush_patch_all_file_pairs(struct diff_options *o)
if (o->color_moved == COLOR_MOVED_ZEBRA_DIM)
dim_moved_lines(o);
hashmap_free_entries(&add_lines, struct moved_entry,
hashmap_clear_and_free(&add_lines, struct moved_entry,
ent);
hashmap_free_entries(&del_lines, struct moved_entry,
hashmap_clear_and_free(&del_lines, struct moved_entry,
ent);
}

2
diffcore-rename.c

@ -407,7 +407,7 @@ static int find_exact_renames(struct diff_options *options)
renames += find_identical_files(&file_table, i, options);
/* Free the hash data structure and entries */
hashmap_free_entries(&file_table, struct file_similarity, entry);
hashmap_clear_and_free(&file_table, struct file_similarity, entry);
return renames;
}

8
dir.c

@ -817,8 +817,8 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern
clear_hashmaps:
warning(_("disabling cone pattern matching"));
hashmap_free_entries(&pl->parent_hashmap, struct pattern_entry, ent);
hashmap_free_entries(&pl->recursive_hashmap, struct pattern_entry, ent);
hashmap_clear_and_free(&pl->parent_hashmap, struct pattern_entry, ent);
hashmap_clear_and_free(&pl->recursive_hashmap, struct pattern_entry, ent);
pl->use_cone_patterns = 0;
}
@ -921,8 +921,8 @@ void clear_pattern_list(struct pattern_list *pl)
free(pl->patterns[i]);
free(pl->patterns);
free(pl->filebuf);
hashmap_free_entries(&pl->recursive_hashmap, struct pattern_entry, ent);
hashmap_free_entries(&pl->parent_hashmap, struct pattern_entry, ent);
hashmap_clear_and_free(&pl->recursive_hashmap, struct pattern_entry, ent);
hashmap_clear_and_free(&pl->parent_hashmap, struct pattern_entry, ent);
memset(pl, 0, sizeof(*pl));
}

6
hashmap.c

@ -183,7 +183,7 @@ static void free_individual_entries(struct hashmap *map, ssize_t entry_offset)
while ((e = hashmap_iter_next(&iter)))
/*
* like container_of, but using caller-calculated
* offset (caller being hashmap_free_entries)
* offset (caller being hashmap_clear_and_free)
*/
free((char *)e - entry_offset);
}
@ -199,11 +199,11 @@ void hashmap_partial_clear_(struct hashmap *map, ssize_t entry_offset)
map->private_size = 0;
}
void hashmap_free_(struct hashmap *map, ssize_t entry_offset)
void hashmap_clear_(struct hashmap *map, ssize_t entry_offset)
{
if (!map || !map->table)
return;
if (entry_offset >= 0) /* called by hashmap_free_entries */
if (entry_offset >= 0) /* called by hashmap_clear_and_free */
free_individual_entries(map, entry_offset);
free(map->table);
memset(map, 0, sizeof(*map));

44
hashmap.h

@ -96,7 +96,7 @@
* }
*
* if (!strcmp("end", action)) {
* hashmap_free_entries(&map, struct long2string, ent);
* hashmap_clear_and_free(&map, struct long2string, ent);
* break;
* }
* }
@ -237,7 +237,7 @@ void hashmap_init(struct hashmap *map,
/* internal functions for clearing or freeing hashmap */
void hashmap_partial_clear_(struct hashmap *map, ssize_t offset);
void hashmap_free_(struct hashmap *map, ssize_t offset);
void hashmap_clear_(struct hashmap *map, ssize_t offset);
/*
* Frees a hashmap structure and allocated memory for the table, but does not
@ -253,40 +253,50 @@ void hashmap_free_(struct hashmap *map, ssize_t offset);
* free(e->somefield);
* free(e);
* }
* hashmap_free(map);
* hashmap_clear(map);
*
* instead of
*
* hashmap_for_each_entry(map, hashmap_iter, e, hashmap_entry_name) {
* free(e->somefield);
* }
* hashmap_free_entries(map, struct my_entry_struct, hashmap_entry_name);
* hashmap_clear_and_free(map, struct my_entry_struct, hashmap_entry_name);
*
* to avoid the implicit extra loop over the entries. However, if there are
* no special fields in your entry that need to be freed beyond the entry
* itself, it is probably simpler to avoid the explicit loop and just call
* hashmap_free_entries().
* hashmap_clear_and_free().
*/
#define hashmap_free(map) hashmap_free_(map, -1)
#define hashmap_clear(map) hashmap_clear_(map, -1)
/*
* Basically the same as calling hashmap_free() followed by hashmap_init(),
* but doesn't incur the overhead of deallocating and reallocating
* map->table; it leaves map->table allocated and the same size but zeroes
* it out so it's ready for use again as an empty map. As with
* hashmap_free(), you may need to free the entries yourself before calling
* this function.
* Similar to hashmap_clear(), except that the table is no deallocated; it
* is merely zeroed out but left the same size as before. If the hashmap
* will be reused, this avoids the overhead of deallocating and
* reallocating map->table. As with hashmap_clear(), you may need to free
* the entries yourself before calling this function.
*/
#define hashmap_partial_clear(map) hashmap_partial_clear_(map, -1)
/*
* Frees @map and all entries. @type is the struct type of the entry
* where @member is the hashmap_entry struct used to associate with @map.
* Similar to hashmap_clear() but also frees all entries. @type is the
* struct type of the entry where @member is the hashmap_entry struct used
* to associate with @map.
*
* See usage note above hashmap_free().
* See usage note above hashmap_clear().
*/
#define hashmap_free_entries(map, type, member) \
hashmap_free_(map, offsetof(type, member));
#define hashmap_clear_and_free(map, type, member) \
hashmap_clear_(map, offsetof(type, member))
/*
* Similar to hashmap_partial_clear() but also frees all entries. @type is
* the struct type of the entry where @member is the hashmap_entry struct
* used to associate with @map.
*
* See usage note above hashmap_clear().
*/
#define hashmap_partial_clear_and_free(map, type, member) \
hashmap_partial_clear_(map, offsetof(type, member))
/* hashmap_entry functions */

6
merge-recursive.c

@ -2651,7 +2651,7 @@ static struct string_list *get_renames(struct merge_options *opt,
free(e->target_file);
string_list_clear(&e->source_files, 0);
}
hashmap_free_entries(&collisions, struct collision_entry, ent);
hashmap_clear_and_free(&collisions, struct collision_entry, ent);
return renames;
}
@ -2870,7 +2870,7 @@ static void initial_cleanup_rename(struct diff_queue_struct *pairs,
strbuf_release(&e->new_dir);
/* possible_new_dirs already cleared in get_directory_renames */
}
hashmap_free_entries(dir_renames, struct dir_rename_entry, ent);
hashmap_clear_and_free(dir_renames, struct dir_rename_entry, ent);
free(dir_renames);
free(pairs->queue);
@ -3497,7 +3497,7 @@ static int merge_trees_internal(struct merge_options *opt,
string_list_clear(entries, 1);
free(entries);
hashmap_free_entries(&opt->priv->current_file_dir_set,
hashmap_clear_and_free(&opt->priv->current_file_dir_set,
struct path_hashmap_entry, e);
if (clean < 0) {

4
name-hash.c

@ -726,6 +726,6 @@ void free_name_hash(struct index_state *istate)
return;
istate->name_hash_initialized = 0;
hashmap_free(&istate->name_hash);
hashmap_free_entries(&istate->dir_hash, struct dir_entry, ent);
hashmap_clear(&istate->name_hash);
hashmap_clear_and_free(&istate->dir_hash, struct dir_entry, ent);
}

2
object.c

@ -532,7 +532,7 @@ void raw_object_store_clear(struct raw_object_store *o)
close_object_store(o);
o->packed_git = NULL;
hashmap_free(&o->pack_map);
hashmap_clear(&o->pack_map);
}
void parsed_object_pool_clear(struct parsed_object_pool *o)

2
oidmap.c

@ -27,7 +27,7 @@ void oidmap_free(struct oidmap *map, int free_entries)
return;
/* TODO: make oidmap itself not depend on struct layouts */
hashmap_free_(&map->map, free_entries ? 0 : -1);
hashmap_clear_(&map->map, free_entries ? 0 : -1);
}
void *oidmap_get(const struct oidmap *map, const struct object_id *key)

2
patch-ids.c

@ -71,7 +71,7 @@ int init_patch_ids(struct repository *r, struct patch_ids *ids)
int free_patch_ids(struct patch_ids *ids)
{
hashmap_free_entries(&ids->patches, struct patch_id, ent);
hashmap_clear_and_free(&ids->patches, struct patch_id, ent);
return 0;
}

2
range-diff.c

@ -266,7 +266,7 @@ static void find_exact_matches(struct string_list *a, struct string_list *b)
}
}
hashmap_free(&map);
hashmap_clear(&map);
}
static void diffsize_consume(void *data, char *line, unsigned long len)

2
ref-filter.c

@ -2222,7 +2222,7 @@ void ref_array_clear(struct ref_array *array)
used_atom_cnt = 0;
if (ref_to_worktree_map.worktrees) {
hashmap_free_entries(&(ref_to_worktree_map.map),
hashmap_clear_and_free(&(ref_to_worktree_map.map),
struct ref_to_worktree_entry, ent);
free_worktrees(ref_to_worktree_map.worktrees);
ref_to_worktree_map.worktrees = NULL;

2
revision.c

@ -139,7 +139,7 @@ static void paths_and_oids_clear(struct hashmap *map)
free(entry->path);
}
hashmap_free_entries(map, struct path_and_oids_entry, ent);
hashmap_clear_and_free(map, struct path_and_oids_entry, ent);
}
static void paths_and_oids_insert(struct hashmap *map,

4
sequencer.c

@ -5058,7 +5058,7 @@ static int make_script_with_merges(struct pretty_print_context *pp,
oidmap_free(&commit2todo, 1);
oidmap_free(&state.commit2label, 1);
hashmap_free_entries(&state.labels, struct labels_entry, entry);
hashmap_clear_and_free(&state.labels, struct labels_entry, entry);
strbuf_release(&state.buf);
return 0;
@ -5577,7 +5577,7 @@ int todo_list_rearrange_squash(struct todo_list *todo_list)
for (i = 0; i < todo_list->nr; i++)
free(subjects[i]);
free(subjects);
hashmap_free_entries(&subject2item, struct subject2item_entry, entry);
hashmap_clear_and_free(&subject2item, struct subject2item_entry, entry);
clear_commit_todo_item(&commit_todo);

4
submodule-config.c

@ -103,8 +103,8 @@ static void submodule_cache_clear(struct submodule_cache *cache)
ent /* member name */)
free_one_config(entry);
hashmap_free_entries(&cache->for_path, struct submodule_entry, ent);
hashmap_free_entries(&cache->for_name, struct submodule_entry, ent);
hashmap_clear_and_free(&cache->for_path, struct submodule_entry, ent);
hashmap_clear_and_free(&cache->for_name, struct submodule_entry, ent);
cache->initialized = 0;
cache->gitmodules_read = 0;
}

6
t/helper/test-hashmap.c

@ -110,7 +110,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
hashmap_add(&map, &entries[i]->ent);
}
hashmap_free(&map);
hashmap_clear(&map);
}
} else {
/* test map lookups */
@ -130,7 +130,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
}
}
hashmap_free(&map);
hashmap_clear(&map);
}
}
@ -262,6 +262,6 @@ int cmd__hashmap(int argc, const char **argv)
}
strbuf_release(&line);
hashmap_free_entries(&map, struct test_entry, ent);
hashmap_clear_and_free(&map, struct test_entry, ent);
return 0;
}

Loading…
Cancel
Save