]> git.ipfire.org Git - thirdparty/git.git/commitdiff
hashmap: provide deallocation function names
authorElijah Newren <newren@gmail.com>
Mon, 2 Nov 2020 18:55:05 +0000 (18:55 +0000)
committerJunio C Hamano <gitster@pobox.com>
Mon, 2 Nov 2020 20:15:50 +0000 (12:15 -0800)
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>
22 files changed:
add-interactive.c
blame.c
bloom.c
builtin/fetch.c
builtin/shortlog.c
config.c
diff.c
diffcore-rename.c
dir.c
hashmap.c
hashmap.h
merge-recursive.c
name-hash.c
object.c
oidmap.c
patch-ids.c
range-diff.c
ref-filter.c
revision.c
sequencer.c
submodule-config.c
t/helper/test-hashmap.c

index 555c4abf324f553b6bb70f6640554d407c12677a..a14c0feaa2a99e827a8e5c59db9f4c7d6ff38540 100644 (file)
@@ -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)
diff --git a/blame.c b/blame.c
index 686845b2b43dffe35d9f9811b61c80a0e0536332..229beb6452bdcf1a640d3c5f023cd5175b378871 100644 (file)
--- a/blame.c
+++ b/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);
 }
 
diff --git a/bloom.c b/bloom.c
index 68c73200a54aa4fdad582d6031c59313c2c90d73..719c313a1c080abdce292dc98ba05ecf11c952f8 100644 (file)
--- a/bloom.c
+++ b/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]);
index f9c3c49f14d9cd508102405f99d6010c21691541..ecf8537605077a8485e59d6dbba6d4d447642910 100644 (file)
@@ -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;
 }
index 0a5c4968f64e1655b5cf16e3555209d52dae065c..83f0a739b4cba7520b30ab78bd7d51af4ddd6cfe 100644 (file)
@@ -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,
index 2bdff4457be7d5b1e1fd5f6acb558ac41b946a8c..8f324ed3a6f6b8d516516e729afe627610051c22 100644 (file)
--- a/config.c
+++ b/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;
diff --git a/diff.c b/diff.c
index 2bb2f8f57e8b7ce5a6d861b7a1d598a7fbaf68e9..8e0e59f5cf793ebd06e8a029d295689273b19d48 100644 (file)
--- a/diff.c
+++ b/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);
                }
 
index 99e63e90f89afaf55ef16bb3c11c1546c4d76c2a..d367a6d24434727fd822754436db50d3fc277764 100644 (file)
@@ -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;
 }
diff --git a/dir.c b/dir.c
index 78387110e609e16d769b4c42bcba1d1effb731e0..161dce121e991a616412d597b163ce9c2e7123e6 100644 (file)
--- a/dir.c
+++ b/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));
 }
index 922ed07954b748e852eed1150e51b208a861d039..5009471800e8c61b50bfd670a7714daaa26a01c7 100644 (file)
--- a/hashmap.c
+++ b/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));
index e9430d582a94fa9a640a7287b0eb9e392c0787e6..7251687d730d608437a8ed24e4551cf9344fd7f4 100644 (file)
--- a/hashmap.h
+++ b/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 */
 
index d0214335a79ddef7202e0552b80111a05c36f14c..f736a0f63234fee610b42abdf2d7458d932e1d70 100644 (file)
@@ -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) {
index fb526a3775f69eb02d6b7e47629bf60a8a0153fa..5d3c7b12c1805cad19e8cef165e79e232d0f4de4 100644 (file)
@@ -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);
 }
index 32575186562507b2774c6abf097df7810e9cce5e..b8406409d583c08c17b114634390510793aecf55 100644 (file)
--- a/object.c
+++ b/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)
index 423aa014a33eda2c6b695906eb0c349552d9e2b8..286a04a53c20119fe567eec4accbaba4d9f04a97 100644 (file)
--- a/oidmap.c
+++ b/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)
index 12aa6d494b4ed47d1a486307b6b9d9024ee78734..21973e49332c36532438bb067c8821a0e377baf9 100644 (file)
@@ -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;
 }
 
index 24dc435e482c0364ebc88e7ad55930f437d73ad2..befeecae448844ca086da07dee38e384d509e692 100644 (file)
@@ -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)
index c62f6b482271e124474aab6984820dcdd2db698c..5e66b8cd765e0397b7ac41369ec121940671ae1b 100644 (file)
@@ -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;
index aa62212040814e29d17b8d1d7cafc15d1d50612d..f27649d45d1dde74e67d75310affd4d78f504bc5 100644 (file)
@@ -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,
index 00acb1249624398452eb60685b33c0ede344b9fe..23a09c3e7abe5122364e3da4d145bf1a4513d7a2 100644 (file)
@@ -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);
 
index c569e22aa3678bf694c080785f499d619b4d2e23..f50250556698231762092f4e4b6be68800c2089c 100644 (file)
@@ -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;
 }
index f38706216f44c3c881f1d52b250fe74d5140bb37..2475663b49cefc08fe6a9d9d1c1cb3f7a551cc24 100644 (file)
@@ -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;
 }