git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 16/20] hashmap: hashmap_{put,remove} return hashmap_entry *

From
EWEric Wong <e@80x24.org>
Date
Oct 6, 2019, 23:30 UTC
Message-ID
<20191006233043.3516-17-e@80x24.org>
In-Reply-To
<20191006233043.3516-1-e@80x24.org>

And add *_entry variants to perform container_of as necessary to simplify most callers.

Signed-off-by: Eric Wong <e@80x24.org>
Reviewed-by: Derrick Stolee <stolee@gmail.com>
---
 hashmap.c               |  8 +++++---
 hashmap.h               | 15 ++++++++++++---
 range-diff.c            |  4 +++-
 remote.c                |  3 ++-
 submodule-config.c      |  4 +++-
 t/helper/test-hashmap.c |  9 +++++++--
 6 files changed, 32 insertions(+), 11 deletions(-)
diff --git a/hashmap.c b/hashmap.c
index deb5fdf28c..1b60f97cf2 100644
--- a/hashmap.c
+++ b/hashmap.c
@@ -219,8 +219,9 @@ void hashmap_add(struct hashmap *map, struct hashmap_entry *entry)
 	}
 }
 
-void *hashmap_remove(struct hashmap *map, const struct hashmap_entry *key,
-		const void *keydata)
+struct hashmap_entry *hashmap_remove(struct hashmap *map,
+					const struct hashmap_entry *key,
+					const void *keydata)
 {
 	struct hashmap_entry *old;
 	struct hashmap_entry **e = find_entry_ptr(map, key, keydata);
@@ -242,7 +243,8 @@ void *hashmap_remove(struct hashmap *map, const struct hashmap_entry *key,
 	return old;
 }
 
-void *hashmap_put(struct hashmap *map, struct hashmap_entry *entry)
+struct hashmap_entry *hashmap_put(struct hashmap *map,
+				struct hashmap_entry *entry)
 {
 	struct hashmap_entry *old = hashmap_remove(map, entry, NULL);
 	hashmap_add(map, entry);
diff --git a/hashmap.h b/hashmap.h
index 8d4b3907b4..bc3b10e097 100644
--- a/hashmap.h
+++ b/hashmap.h
@@ -349,7 +349,11 @@ void hashmap_add(struct hashmap *map, struct hashmap_entry *entry);
  * `entry` is the entry to add or replace.
  * Returns the replaced entry, or NULL if not found (i.e. the entry was added).
  */
-void *hashmap_put(struct hashmap *map, struct hashmap_entry *entry);
+struct hashmap_entry *hashmap_put(struct hashmap *map,
+				struct hashmap_entry *entry);
+
+#define hashmap_put_entry(map, keyvar, type, member) \
+	container_of_or_null(hashmap_put(map, &(keyvar)->member), type, member)
 
 /*
  * Removes a hashmap entry matching the specified key. If the hashmap contains
@@ -358,8 +362,13 @@ void *hashmap_put(struct hashmap *map, struct hashmap_entry *entry);
  *
  * Argument explanation is the same as in `hashmap_get`.
  */
-void *hashmap_remove(struct hashmap *map, const struct hashmap_entry *key,
-		const void *keydata);
+struct hashmap_entry *hashmap_remove(struct hashmap *map,
+					const struct hashmap_entry *key,
+					const void *keydata);
+
+#define hashmap_remove_entry(map, keyvar, keydata, type, member) \
+	container_of_or_null(hashmap_remove(map, &(keyvar)->member, keydata), \
+				type, member)
 
 /*
  * Returns the `bucket` an entry is stored in.
diff --git a/range-diff.c b/range-diff.c
index c51cfd5556..e5e7820bfe 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -229,7 +229,9 @@ static void find_exact_matches(struct string_list *a, struct string_list *b)
 		util->patch = b->items[i].string;
 		util->diff = util->patch + util->diff_offset;
 		hashmap_entry_init(&util->e, strhash(util->diff));
-		other = hashmap_remove(&map, &util->e, NULL);
+		other = hashmap_remove_entry(&map, util, NULL,
+					struct patch_util,
+					e /* member name */);
 		if (other) {
 			if (other->matching >= 0)
 				BUG("already assigned!");
diff --git a/remote.c b/remote.c
index fa9cadcfbd..5fcddcd88d 100644
--- a/remote.c
+++ b/remote.c
@@ -162,7 +162,8 @@ static struct remote *make_remote(const char *name, int len)
 	remotes[remotes_nr++] = ret;
 
 	hashmap_entry_init(&ret->ent, lookup_entry.hash);
-	replaced = hashmap_put(&remotes_hash, &ret->ent);
+	replaced = hashmap_put_entry(&remotes_hash, ret, struct remote,
+					ent /* member name */);
 	assert(replaced == NULL);  /* no previous entry overwritten */
 	return ret;
 }
diff --git a/submodule-config.c b/submodule-config.c
index 5319933e1d..a289d195f6 100644
--- a/submodule-config.c
+++ b/submodule-config.c
@@ -141,7 +141,9 @@ static void cache_remove_path(struct submodule_cache *cache,
 	struct submodule_entry *removed;
 	hashmap_entry_init(&e.ent, hash);
 	e.config = submodule;
-	removed = hashmap_remove(&cache->for_path, &e.ent, NULL);
+	removed = hashmap_remove_entry(&cache->for_path, &e, NULL,
+					struct submodule_entry,
+					ent /* member name */);
 	free(removed);
 }
 
diff --git a/t/helper/test-hashmap.c b/t/helper/test-hashmap.c
index 4ec5e11556..07a93a2aec 100644
--- a/t/helper/test-hashmap.c
+++ b/t/helper/test-hashmap.c
@@ -189,7 +189,9 @@ int cmd__hashmap(int argc, const char **argv)
 			entry = alloc_test_entry(hash, p1, p2);
 
 			/* add / replace entry */
-			entry = hashmap_put(&map, &entry->ent);
+			entry = hashmap_put_entry(&map, entry,
+						struct test_entry,
+						ent /* member name */);
 
 			/* print and free replaced entry, if any */
 			puts(entry ? get_value(entry) : "NULL");
@@ -212,10 +214,13 @@ int cmd__hashmap(int argc, const char **argv)
 
 			/* setup static key */
 			struct hashmap_entry key;
+			struct hashmap_entry *rm;
 			hashmap_entry_init(&key, hash);
 
 			/* remove entry from hashmap */
-			entry = hashmap_remove(&map, &key, p1);
+			rm = hashmap_remove(&map, &key, p1);
+			entry = rm ? container_of(rm, struct test_entry, ent)
+					: NULL;
 
 			/* print result and free entry*/
 			puts(entry ? get_value(entry) : "NULL");
Previous: Eric WongNext: Eric Wong
Message 17 of 30 in “hashmap bug/safety/ease-of-use fixes”
  1. 00/20 hashmap bug/safety/ease-of-use fixesEric Wong, Oct 6, 2019
  2. 01/20 diff: use hashmap_entry_init on moved_entry.entEric Wong, Oct 6, 2019
  3. 02/20 coccicheck: detect hashmap_entry.hash assignmentEric Wong, Oct 6, 2019
  4. 04/20 hashmap_entry_init takes "struct hashmap_entry *"Eric Wong, Oct 6, 2019
  5. 03/20 packfile: use hashmap_entry in delta_base_cache_entryEric Wong, Oct 6, 2019
  6. 05/20 hashmap_get_next takes "const struct hashmap_entry *"Eric Wong, Oct 6, 2019
  7. 06/20 hashmap_add takes "struct hashmap_entry *"Eric Wong, Oct 6, 2019
  8. 07/20 hashmap_get takes "const struct hashmap_entry *"Eric Wong, Oct 6, 2019
  9. 08/20 hashmap_remove takes "const struct hashmap_entry *"Eric Wong, Oct 6, 2019
  10. 09/20 hashmap_put takes "struct hashmap_entry *"Eric Wong, Oct 6, 2019
  11. 10/20 introduce container_of macroEric Wong, Oct 6, 2019
  12. 11/20 hashmap_get_next returns "struct hashmap_entry *"Eric Wong, Oct 6, 2019
  13. 12/20 hashmap: use *_entry APIs to wrap container_ofEric Wong, Oct 6, 2019
  14. 13/20 hashmap_get{,_from_hash} return "struct hashmap_entry *"Eric Wong, Oct 6, 2019
  15. 14/20 hashmap_cmp_fn takes hashmap_entry paramsEric Wong, Oct 6, 2019
  16. 15/20 hashmap: use *_entry APIs for iterationEric Wong, Oct 6, 2019
  17. 16/20 hashmap: hashmap_{put,remove} return hashmap_entry *Eric Wong, Oct 6, 2019
  18. 17/20 hashmap: introduce hashmap_free_entriesEric Wong, Oct 6, 2019
  19. 18/20 OFFSETOF_VAR macro to simplify hashmap iteratorsEric Wong, Oct 6, 2019
  20. 19/20 hashmap: remove type arg from hashmap_{get,put,remove}_entryEric Wong, Oct 6, 2019
  21. 20/20 hashmap_entry: remove first member requirement from docsEric Wong, Oct 6, 2019
  22. Junio C HamanoOct 7, 2019
  23. 21/20 convert: drop invalid comment for subprocess_entryEric Wong, Oct 7, 2019
  24. Junio C HamanoOct 8, 2019
  25. Eric WongOct 8, 2019
  26. Junio C HamanoOct 7, 2019
  27. Johannes SchindelinOct 8, 2019
  28. Derrick StoleeOct 8, 2019
  29. Derrick StoleeOct 8, 2019
  30. Junio C HamanoOct 9, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.