Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.kernel.org/pub/scm/git/git.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElijah Newren <newren@gmail.com>2020-11-02 21:55:05 +0300
committerJunio C Hamano <gitster@pobox.com>2020-11-02 23:15:50 +0300
commit6da1a258142ac2422c8c57c54b92eaed3c86226e (patch)
tree480a35cb1c8754d02f21233d2efd8e5d28ff24d0 /hashmap.h
parent33f20d82177871225e17d9dd44169a52a36c9f1d (diff)
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>
Diffstat (limited to 'hashmap.h')
-rw-r--r--hashmap.h44
1 files changed, 27 insertions, 17 deletions
diff --git a/hashmap.h b/hashmap.h
index e9430d582a..7251687d73 100644
--- 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 */