Skip to contentSkip to conversation

Introduce data field in hashmap and migrate docs to header

PATCH Superseded Stefan Beller 5 participants 26 messages Jun 29, 2017 – Jul 5, 2017 Documentation

lore
Share & tools
lore
#0 lore raw

cover Introduce data field in hashmap and migrate docs to header

https://public-inbox.org/git/[email protected]/
for context why we need a new data field. Implement that.

Once upon a time we had a long discussion where to put documentation best. The answer was header files as there documentation has less chance to become stale and be out of date. Improve the docs by
* migrating them to the header
* clarifying how the compare function is to be used
* how the arguments to hashmap_get/remove should be used.

Thanks,
Stefan

Stefan Beller (2):
hashmap.h: compare function has access to a data field
hashmap: migrate documentation from Documentation/technical into
header

 Documentation/technical/api-hashmap.txt | 309 --------------------------------
 attr.c                                  |   4 +-
 builtin/describe.c                      |   6 +-
 builtin/difftool.c                      |  20 ++-
 builtin/fast-export.c                   |   5 +-
 config.c                                |   7 +-
 convert.c                               |   3 +-
 diffcore-rename.c                       |   2 +-
 hashmap.c                               |  17 +-
 hashmap.h                               | 256 +++++++++++++++++++++++---
 name-hash.c                             |  12 +-
 oidset.c                                |   5 +-
 patch-ids.c                             |   6 +-
 refs.c                                  |   4 +-
 remote.c                                |   7 +-
 sha1_file.c                             |   5 +-
 sub-process.c                           |   5 +-
 sub-process.h                           |   6 +-
 submodule-config.c                      |  10 +-
 t/helper/test-hashmap.c                 |  15 +-
 20 files changed, 325 insertions(+), 379 deletions(-)
 delete mode 100644 Documentation/technical/api-hashmap.txt
Signature · 1 line
2.13.0.31.g9b732c453e
#1 lore raw

PATCH 1/2 hashmap.h: compare function has access to a data field

When using the hashmap a common need is to have access to arbitrary data in the compare function. A couple of times we abuse the keydata field to pass in the data needed. This happens for example in patch-ids.c.

This patch changes the function signature of the compare function to have one more void pointer available. The pointer given for each invocation of the compare function must be defined in the init function of the hashmap and is just passed through.

Documentation of this new feature is deferred to a later patch.

While at it improve the naming of the fields of all compare functions used by hashmaps by ensuring unused parameters are prefixed with 'unused_' and naming the parameters what they are (instead of 'unused' make it 'unused_keydata').

---
 attr.c                  |  4 ++--
 builtin/describe.c      |  6 ++++--
 builtin/difftool.c      | 20 ++++++++++++--------
 builtin/fast-export.c   |  5 +++--
 config.c                |  7 +++++--
 convert.c               |  3 ++-
 diffcore-rename.c       |  2 +-
 hashmap.c               | 17 ++++++++++++-----
 hashmap.h               |  9 ++++++---
 name-hash.c             | 12 ++++++++----
 oidset.c                |  5 +++--
 patch-ids.c             |  6 ++++--
 refs.c                  |  4 ++--
 remote.c                |  7 +++++--
 sha1_file.c             |  5 +++--
 sub-process.c           |  5 +++--
 sub-process.h           |  6 ++++--
 submodule-config.c      | 10 ++++++----
 t/helper/test-hashmap.c | 15 ++++++++++-----
 19 files changed, 95 insertions(+), 53 deletions(-)
attr.c +2 -2
index 37454999d2..2f51417675 100644
@@ -78,7 +78,7 @@ struct attr_hash_entry {
 /* attr_hashmap comparison function */
 static int attr_hash_entry_cmp(const struct attr_hash_entry *a,
 			       const struct attr_hash_entry *b,
-			       void *unused)
+			       void *unused_keydata, void *unused_data)
 {
 	return (a->keylen != b->keylen) || strncmp(a->key, b->key, a->keylen);
 }
@@ -86,7 +86,7 @@ static int attr_hash_entry_cmp(const struct attr_hash_entry *a,
 /* Initialize an 'attr_hashmap' object */
 static void attr_hashmap_init(struct attr_hashmap *map)
 {
-	hashmap_init(&map->map, (hashmap_cmp_fn) attr_hash_entry_cmp, 0);
+	hashmap_init(&map->map, (hashmap_cmp_fn) attr_hash_entry_cmp, NULL, 0);
 }
 
 /*
builtin/describe.c +4 -2
index 70eb144608..a6c5a969a0 100644
@@ -55,7 +55,9 @@ static const char *prio_names[] = {
 };
 
 static int commit_name_cmp(const struct commit_name *cn1,
-		const struct commit_name *cn2, const void *peeled)
+			   const struct commit_name *cn2,
+			   const void *peeled,
+			   const void *unused_data)
 {
 	return oidcmp(&cn1->peeled, peeled ? peeled : &cn2->peeled);
 }
@@ -501,7 +503,7 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
 		return cmd_name_rev(args.argc, args.argv, prefix);
 	}
 
-	hashmap_init(&names, (hashmap_cmp_fn) commit_name_cmp, 0);
+	hashmap_init(&names, (hashmap_cmp_fn) commit_name_cmp, NULL, 0);
 	for_each_rawref(get_name, NULL);
 	if (!names.size && !always)
 		die(_("No names found, cannot describe anything."));
builtin/difftool.c +12 -8
index 9199227f6e..80786a95ab 100644
@@ -131,7 +131,9 @@ struct working_tree_entry {
 };
 
 static int working_tree_entry_cmp(struct working_tree_entry *a,
-				  struct working_tree_entry *b, void *keydata)
+				  struct working_tree_entry *b,
+				  void *unused_keydata,
+				  void *unused_data)
 {
 	return strcmp(a->path, b->path);
 }
@@ -146,7 +148,8 @@ struct pair_entry {
 	const char path[FLEX_ARRAY];
 };
 
-static int pair_cmp(struct pair_entry *a, struct pair_entry *b, void *keydata)
+static int pair_cmp(struct pair_entry *a, struct pair_entry *b,
+		    void *unused_keydata, void *unused_data)
 {
 	return strcmp(a->path, b->path);
 }
@@ -174,7 +177,8 @@ struct path_entry {
 	char path[FLEX_ARRAY];
 };
 
-static int path_entry_cmp(struct path_entry *a, struct path_entry *b, void *key)
+static int path_entry_cmp(struct path_entry *a, struct path_entry *b,
+			  void *key, void *unused_data)
 {
 	return strcmp(a->path, key ? key : b->path);
 }
@@ -367,9 +371,9 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
 	wtdir_len = wtdir.len;
 
 	hashmap_init(&working_tree_dups,
-		     (hashmap_cmp_fn)working_tree_entry_cmp, 0);
-	hashmap_init(&submodules, (hashmap_cmp_fn)pair_cmp, 0);
-	hashmap_init(&symlinks2, (hashmap_cmp_fn)pair_cmp, 0);
+		     (hashmap_cmp_fn)working_tree_entry_cmp, NULL, 0);
+	hashmap_init(&submodules, (hashmap_cmp_fn)pair_cmp, NULL, 0);
+	hashmap_init(&symlinks2, (hashmap_cmp_fn)pair_cmp, NULL, 0);
 
 	child.no_stdin = 1;
 	child.git_cmd = 1;
@@ -580,9 +584,9 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
 	 * files through the symlink.
 	 */
 	hashmap_init(&wt_modified, (hashmap_cmp_fn)path_entry_cmp,
-		     wtindex.cache_nr);
+		     NULL, wtindex.cache_nr);
 	hashmap_init(&tmp_modified, (hashmap_cmp_fn)path_entry_cmp,
-		     wtindex.cache_nr);
+		     NULL, wtindex.cache_nr);
 
 	for (i = 0; i < wtindex.cache_nr; i++) {
 		struct hashmap_entry dummy;
builtin/fast-export.c +3 -2
index 12d501bfde..fa389fe252 100644
@@ -94,7 +94,8 @@ struct anonymized_entry {
 };
 
 static int anonymized_entry_cmp(const void *va, const void *vb,
-				const void *data)
+				const void *unused_keydata,
+				const void *unused_data)
 {
 	const struct anonymized_entry *a = va, *b = vb;
 	return a->orig_len != b->orig_len ||
@@ -113,7 +114,7 @@ static const void *anonymize_mem(struct hashmap *map,
 	struct anonymized_entry key, *ret;
 
 	if (!map->cmpfn)
-		hashmap_init(map, anonymized_entry_cmp, 0);
+		hashmap_init(map, anonymized_entry_cmp, NULL, 0);
 
 	hashmap_entry_init(&key, memhash(orig, *len));
 	key.orig = orig;
config.c +5 -2
index 1cd40a5fe6..aa0198a5fd 100644
@@ -1754,14 +1754,17 @@ static int configset_add_value(struct config_set *cs, const char *key, const cha
 }
 
 static int config_set_element_cmp(const struct config_set_element *e1,
-				 const struct config_set_element *e2, const void *unused)
+				 const struct config_set_element *e2,
+				 const void *unused_keydata,
+				 const void *unused_data)
 {
 	return strcmp(e1->key, e2->key);
 }
 
 void git_configset_init(struct config_set *cs)
 {
-	hashmap_init(&cs->config_hash, (hashmap_cmp_fn)config_set_element_cmp, 0);
+	hashmap_init(&cs->config_hash, (hashmap_cmp_fn)config_set_element_cmp,
+		     NULL, 0);
 	cs->hash_initialized = 1;
 	cs->list.nr = 0;
 	cs->list.alloc = 0;
convert.c +2 -1
index 7d2a519daf..deaf0ba7b3 100644
@@ -583,7 +583,8 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t len
 
 	if (!subprocess_map_initialized) {
 		subprocess_map_initialized = 1;
-		hashmap_init(&subprocess_map, (hashmap_cmp_fn) cmd2process_cmp, 0);
+		hashmap_init(&subprocess_map, (hashmap_cmp_fn) cmd2process_cmp,
+			     NULL, 0);
 		entry = NULL;
 	} else {
 		entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd);
diffcore-rename.c +1 -1
index 1e4678b7d7..786f389498 100644
@@ -341,7 +341,7 @@ static int find_exact_renames(struct diff_options *options)
 	/* Add all sources to the hash table in reverse order, because
 	 * later on they will be retrieved in LIFO order.
 	 */
-	hashmap_init(&file_table, NULL, rename_src_nr);
+	hashmap_init(&file_table, NULL, NULL, rename_src_nr);
 	for (i = rename_src_nr-1; i >= 0; i--)
 		insert_file_table(&file_table, i, rename_src[i].p->one);
 
hashmap.c +12 -5
index 7d1044eb5d..82d70b329b 100644
@@ -95,7 +95,9 @@ static inline int entry_equals(const struct hashmap *map,
 		const struct hashmap_entry *e1, const struct hashmap_entry *e2,
 		const void *keydata)
 {
-	return (e1 == e2) || (e1->hash == e2->hash && !map->cmpfn(e1, e2, keydata));
+	return (e1 == e2) ||
+	       (e1->hash == e2->hash &&
+		!map->cmpfn(e1, e2, keydata, map->data));
 }
 
 static inline unsigned int bucket(const struct hashmap *map,
@@ -140,19 +142,23 @@ static inline struct hashmap_entry **find_entry_ptr(const struct hashmap *map,
 	return e;
 }
 
-static int always_equal(const void *unused1, const void *unused2, const void *unused3)
+static int always_equal(const void *unused1,
+			const void *unused2,
+			const void *unused_keydata,
+			const void *unused_data)
 {
 	return 0;
 }
 
 void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function,
-		size_t initial_size)
+		const void *data, size_t initial_size)
 {
 	unsigned int size = HASHMAP_INITIAL_SIZE;
 
 	memset(map, 0, sizeof(*map));
 
 	map->cmpfn = equals_function ? equals_function : always_equal;
+	map->data = data;
 
 	/* calculate initial table size and allocate the table */
 	initial_size = (unsigned int) ((uint64_t) initial_size * 100
@@ -262,7 +268,8 @@ struct pool_entry {
 
 static int pool_entry_cmp(const struct pool_entry *e1,
 			  const struct pool_entry *e2,
-			  const unsigned char *keydata)
+			  const unsigned char *keydata,
+			  const void *unused_data)
 {
 	return e1->data != keydata &&
 	       (e1->len != e2->len || memcmp(e1->data, keydata, e1->len));
@@ -275,7 +282,7 @@ const void *memintern(const void *data, size_t len)
 
 	/* initialize string pool hashmap */
 	if (!map.tablesize)
-		hashmap_init(&map, (hashmap_cmp_fn) pool_entry_cmp, 0);
+		hashmap_init(&map, (hashmap_cmp_fn) pool_entry_cmp, NULL, 0);
 
 	/* lookup interned string in pool */
 	hashmap_entry_init(&key, memhash(data, len));
hashmap.h +6 -3
index de6022a3a9..1c26bbad5b 100644
@@ -33,11 +33,12 @@ struct hashmap_entry {
 };
 
 typedef int (*hashmap_cmp_fn)(const void *entry, const void *entry_or_key,
-		const void *keydata);
+		const void *keydata, const void *cbdata);
 
 struct hashmap {
 	struct hashmap_entry **table;
 	hashmap_cmp_fn cmpfn;
+	const void *data;
 	unsigned int size, tablesize, grow_at, shrink_at;
 	unsigned disallow_rehash : 1;
 };
@@ -50,8 +51,10 @@ struct hashmap_iter {
 
 /* hashmap functions */
 
-extern void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function,
-		size_t initial_size);
+extern void hashmap_init(struct hashmap *map,
+			 hashmap_cmp_fn equals_function,
+			 const void *data,
+			 size_t initial_size);
 extern void hashmap_free(struct hashmap *map, int free_entries);
 
 /* hashmap_entry functions */
name-hash.c +8 -4
index 39309efb7f..4e9eac925a 100644
@@ -17,7 +17,9 @@ struct dir_entry {
 };
 
 static int dir_entry_cmp(const struct dir_entry *e1,
-		const struct dir_entry *e2, const char *name)
+			 const struct dir_entry *e2,
+			 const char *name,
+			 const void *unused_data)
 {
 	return e1->namelen != e2->namelen || strncasecmp(e1->name,
 			name ? name : e2->name, e1->namelen);
@@ -108,7 +110,9 @@ static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
 }
 
 static int cache_entry_cmp(const struct cache_entry *ce1,
-		const struct cache_entry *ce2, const void *remove)
+			   const struct cache_entry *ce2,
+			   const void *remove,
+			   const void *unused_data)
 {
 	/*
 	 * For remove_name_hash, find the exact entry (pointer equality); for
@@ -571,9 +575,9 @@ static void lazy_init_name_hash(struct index_state *istate)
 	if (istate->name_hash_initialized)
 		return;
 	hashmap_init(&istate->name_hash, (hashmap_cmp_fn) cache_entry_cmp,
-			istate->cache_nr);
+			NULL, istate->cache_nr);
 	hashmap_init(&istate->dir_hash, (hashmap_cmp_fn) dir_entry_cmp,
-			istate->cache_nr);
+			NULL, istate->cache_nr);
 
 	if (lookup_lazy_params(istate)) {
 		hashmap_disallow_rehash(&istate->dir_hash, 1);
oidset.c +3 -2
index ac169f05d3..f20e6e4df3 100644
@@ -7,7 +7,8 @@ struct oidset_entry {
 };
 
 static int oidset_hashcmp(const void *va, const void *vb,
-			  const void *vkey)
+			  const void *vkey,
+			  const void *unused_data)
 {
 	const struct oidset_entry *a = va, *b = vb;
 	const struct object_id *key = vkey;
@@ -30,7 +31,7 @@ int oidset_insert(struct oidset *set, const struct object_id *oid)
 	struct oidset_entry *entry;
 
 	if (!set->map.cmpfn)
-		hashmap_init(&set->map, oidset_hashcmp, 0);
+		hashmap_init(&set->map, oidset_hashcmp, NULL, 0);
 
 	if (oidset_contains(set, oid))
 		return 1;
patch-ids.c +4 -2
index 9c0ab9e67a..b9b2ebbad0 100644
@@ -37,6 +37,7 @@ int commit_patch_id(struct commit *commit, struct diff_options *options,
  */
 static int patch_id_cmp(struct patch_id *a,
 			struct patch_id *b,
+			const void *unused_keydata,
 			struct diff_options *opt)
 {
 	if (is_null_oid(&a->patch_id) &&
@@ -57,7 +58,8 @@ int init_patch_ids(struct patch_ids *ids)
 	ids->diffopts.detect_rename = 0;
 	DIFF_OPT_SET(&ids->diffopts, RECURSIVE);
 	diff_setup_done(&ids->diffopts);
-	hashmap_init(&ids->patches, (hashmap_cmp_fn)patch_id_cmp, 256);
+	hashmap_init(&ids->patches, (hashmap_cmp_fn)patch_id_cmp,
+		     &ids->diffopts, 256);
 	return 0;
 }
 
@@ -93,7 +95,7 @@ struct patch_id *has_commit_patch_id(struct commit *commit,
 	if (init_patch_id_entry(&patch, commit, ids))
 		return NULL;
 
-	return hashmap_get(&ids->patches, &patch, &ids->diffopts);
+	return hashmap_get(&ids->patches, &patch, NULL);
 }
 
 struct patch_id *add_commit_patch_id(struct commit *commit,
refs.c +2 -2
index 88658ba769..71e0170feb 100644
@@ -1526,7 +1526,7 @@ struct ref_store_hash_entry
 };
 
 static int ref_store_hash_cmp(const void *entry, const void *entry_or_key,
-			      const void *keydata)
+			      const void *keydata, const void *unused_data)
 {
 	const struct ref_store_hash_entry *e1 = entry, *e2 = entry_or_key;
 	const char *name = keydata ? keydata : e2->name;
@@ -1608,7 +1608,7 @@ static void register_ref_store_map(struct hashmap *map,
 				   const char *name)
 {
 	if (!map->tablesize)
-		hashmap_init(map, ref_store_hash_cmp, 0);
+		hashmap_init(map, ref_store_hash_cmp, NULL, 0);
 
 	if (hashmap_put(map, alloc_ref_store_hash_entry(name, refs)))
 		die("BUG: %s ref_store '%s' initialized twice", type, name);
remote.c +5 -2
index d87482573d..b5e05890ee 100644
@@ -133,7 +133,10 @@ struct remotes_hash_key {
 	int len;
 };
 
-static int remotes_hash_cmp(const struct remote *a, const struct remote *b, const struct remotes_hash_key *key)
+static int remotes_hash_cmp(const struct remote *a,
+			    const struct remote *b,
+			    const struct remotes_hash_key *key,
+			    const void *unused_data)
 {
 	if (key)
 		return strncmp(a->name, key->str, key->len) || a->name[key->len];
@@ -144,7 +147,7 @@ static int remotes_hash_cmp(const struct remote *a, const struct remote *b, cons
 static inline void init_remotes_hash(void)
 {
 	if (!remotes_hash.cmpfn)
-		hashmap_init(&remotes_hash, (hashmap_cmp_fn)remotes_hash_cmp, 0);
+		hashmap_init(&remotes_hash, (hashmap_cmp_fn)remotes_hash_cmp, NULL, 0);
 }
 
 static struct remote *make_remote(const char *name, int len)
sha1_file.c +3 -2
index fb1fd809dc..b748db31ed 100644
@@ -2390,7 +2390,8 @@ static int delta_base_cache_key_eq(const struct delta_base_cache_key *a,
 }
 
 static int delta_base_cache_hash_cmp(const void *va, const void *vb,
-				     const void *vkey)
+				     const void *vkey,
+				     const void *unused_data)
 {
 	const struct delta_base_cache_entry *a = va, *b = vb;
 	const struct delta_base_cache_key *key = vkey;
@@ -2472,7 +2473,7 @@ static void add_delta_base_cache(struct packed_git *p, off_t base_offset,
 	list_add_tail(&ent->lru, &delta_base_cache_lru);
 
 	if (!delta_base_cache.cmpfn)
-		hashmap_init(&delta_base_cache, delta_base_cache_hash_cmp, 0);
+		hashmap_init(&delta_base_cache, delta_base_cache_hash_cmp, NULL, 0);
 	hashmap_entry_init(ent, pack_entry_hash(p, base_offset));
 	hashmap_add(&delta_base_cache, ent);
 }
sub-process.c +3 -2
index 92f8aea70a..68b53cf48a 100644
@@ -6,8 +6,9 @@
 #include "pkt-line.h"
 
 int cmd2process_cmp(const struct subprocess_entry *e1,
-			   const struct subprocess_entry *e2,
-			   const void *unused)
+		    const struct subprocess_entry *e2,
+		    const void *unused_keydata,
+		    const void *unused_data)
 {
 	return strcmp(e1->cmd, e2->cmd);
 }
sub-process.h +4 -2
index d9a45cd359..a19b4d9936 100644
@@ -20,8 +20,10 @@ struct subprocess_entry {
 
 /* subprocess functions */
 
-int cmd2process_cmp(const struct subprocess_entry *e1,
-	const struct subprocess_entry *e2, const void *unused);
+extern int cmd2process_cmp(const struct subprocess_entry *e1,
+			   const struct subprocess_entry *e2,
+			   const void *unused_keydata,
+			   const void *unused_data);
 
 typedef int(*subprocess_start_fn)(struct subprocess_entry *entry);
 int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, const char *cmd,
submodule-config.c +6 -4
index d8f8d5ea32..5c2fc489b8 100644
@@ -36,7 +36,8 @@ static int is_cache_init;
 
 static int config_path_cmp(const struct submodule_entry *a,
 			   const struct submodule_entry *b,
-			   const void *unused)
+			   const void *unused_keydata,
+			   const void *unused_data)
 {
 	return strcmp(a->config->path, b->config->path) ||
 	       hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
@@ -44,7 +45,8 @@ static int config_path_cmp(const struct submodule_entry *a,
 
 static int config_name_cmp(const struct submodule_entry *a,
 			   const struct submodule_entry *b,
-			   const void *unused)
+			   const void *unused_keydata,
+			   const void *unused_data)
 {
 	return strcmp(a->config->name, b->config->name) ||
 	       hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
@@ -52,8 +54,8 @@ static int config_name_cmp(const struct submodule_entry *a,
 
 static void cache_init(struct submodule_cache *cache)
 {
-	hashmap_init(&cache->for_path, (hashmap_cmp_fn) config_path_cmp, 0);
-	hashmap_init(&cache->for_name, (hashmap_cmp_fn) config_name_cmp, 0);
+	hashmap_init(&cache->for_path, (hashmap_cmp_fn) config_path_cmp, NULL, 0);
+	hashmap_init(&cache->for_name, (hashmap_cmp_fn) config_name_cmp, NULL, 0);
 }
 
 static void free_one_config(struct submodule_entry *entry)
t/helper/test-hashmap.c +10 -5
index 7aa9440e27..72cdc4a6b1 100644
@@ -14,13 +14,17 @@ static const char *get_value(const struct test_entry *e)
 }
 
 static int test_entry_cmp(const struct test_entry *e1,
-		const struct test_entry *e2, const char* key)
+			  const struct test_entry *e2,
+			  const char* key,
+			  const void *unused_data)
 {
 	return strcmp(e1->key, key ? key : e2->key);
 }
 
 static int test_entry_cmp_icase(const struct test_entry *e1,
-		const struct test_entry *e2, const char* key)
+				const struct test_entry *e2,
+				const char* key,
+				const void *unused_data)
 {
 	return strcasecmp(e1->key, key ? key : e2->key);
 }
@@ -92,7 +96,8 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
 	if (method & TEST_ADD) {
 		/* test adding to the map */
 		for (j = 0; j < rounds; j++) {
-			hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp, 0);
+			hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp,
+				     NULL, 0);
 
 			/* add entries */
 			for (i = 0; i < TEST_SIZE; i++) {
@@ -104,7 +109,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
 		}
 	} else {
 		/* test map lookups */
-		hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp, 0);
+		hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp, NULL, 0);
 
 		/* fill the map (sparsely if specified) */
 		j = (method & TEST_SPARSE) ? TEST_SIZE / 10 : TEST_SIZE;
@@ -147,7 +152,7 @@ int cmd_main(int argc, const char **argv)
 	/* init hash map */
 	icase = argc > 1 && !strcmp("ignorecase", argv[1]);
 	hashmap_init(&map, (hashmap_cmp_fn) (icase ? test_entry_cmp_icase
-			: test_entry_cmp), 0);
+			: test_entry_cmp), NULL, 0);
 
 	/* process commands from stdin */
 	while (fgets(line, sizeof(line), stdin)) {
Signature · 1 line
2.13.0.31.g9b732c453e
#2 lore raw

Re: [PATCH 1/2] hashmap.h: compare function has access to a data field

Quoted 3 lines · Stefan Beller

When using the hashmap a common need is to have access to arbitrary data in the compare function. A couple of times we abuse the keydata field to pass in the data needed. This happens for example in patch-ids.c.

It is not "arbitrary data"; it is very important to streess that we are not just passing random crud, but adding a mechansim to tailor/curry the function in a way that is fixed throughout the lifetime of a hashmap.

I haven't looked at the use of keydata in patch-ids.c and friends to decide if that "abuse" claim is correct; if it were the case, should we expect that a follow-up patch to clean up the existing mess by using the new mechanism? Or does fixing the "abuse" take another mechanism that is different from this one?

Quoted 13 lines

While at it improve the naming of the fields of all compare functions used by hashmaps by ensuring unused parameters are prefixed with 'unused_' and naming the parameters what they are (instead of 'unused' make it 'unused_keydata').

---

hashmap.h +0 -0
index de6022a3a9..1c26bbad5b 100644

hashmap.h struct hashmap_entry { · line 36

Quoted 5 lines
hashmap.h +1 -1
@@ -33,11 +33,12 @@ struct hashmap_entry {
 };
 
 typedef int (*hashmap_cmp_fn)(const void *entry, const void *entry_or_key,
-		const void *keydata);
+		const void *keydata, const void *cbdata);

As I view the new "data" thing the C's (read: poor-man's) way to cutomize the function, I would have tweaked the function signature by giving the customization data at the front, i.e.

	fn(fndata, entry, entry_or_key, keydata);

as I think the way we wish to be able to express it in a better language would have been something like:

(partial_apply(fn, fndata))(entry, entry_or_key, keydata)

But what you did is OK, too.

hashmap.h struct hashmap_iter { · line 57

Quoted 4 lines
hashmap.h +4 -0
+extern void hashmap_init(struct hashmap *map,
+			 hashmap_cmp_fn equals_function,
+			 const void *data,
+			 size_t initial_size);

And this does match my expectation ;-).

#3 lore raw

Re: [PATCH 1/2] hashmap.h: compare function has access to a data field

Quoted 5 lines · Junio C Hamano

I haven't looked at the use of keydata in patch-ids.c and friends to decide if that "abuse" claim is correct; if it were the case, should we expect that a follow-up patch to clean up the existing mess by using the new mechanism? Or does fixing the "abuse" take another mechanism that is different from this one?

I see that you corrected patch-ids.c "while at it". That may make it harder to revert only that "while at it", I suspect.

Thanks.

#4 lore raw

Re: [PATCH 1/2] hashmap.h: compare function has access to a data field

Quoted 8 lines · Junio C Hamano
Quoted 5 lines · Junio C Hamano

I haven't looked at the use of keydata in patch-ids.c and friends to decide if that "abuse" claim is correct; if it were the case, should we expect that a follow-up patch to clean up the existing mess by using the new mechanism? Or does fixing the "abuse" take another mechanism that is different from this one?

Quoted 4 lines

I see that you corrected patch-ids.c "while at it". That may make it harder to revert only that "while at it", I suspect.

Thanks.

Yes it was a last minute squash before sending it out, as the fix was only two lines whereas the conversion is a lot. If it were separated I could have claimed the introduction to be a rather mechanical patch, but I did not make use of coccinelle or such, so the likelihood for errors is just as high.

So I decided to squash them.

Thanks,
Stefan

#5 lore raw

Re: [PATCH 1/2] hashmap.h: compare function has access to a data field

Quoted 6 lines · Stefan Beller

Yes it was a last minute squash before sending it out, as the fix was only two lines whereas the conversion is a lot. If it were separated I could have claimed the introduction to be a rather mechanical patch, but I did not make use of coccinelle or such, so the likelihood for errors is just as high.

So I decided to squash them.

I somehow think that logic leads to a suboptimal workflow. If they were separated, somebody else could have done an independent mechanical conversion to verify the result matches yours, which would give us more confidence. When such an independent mechanical conversion does not match, we need one round-trip to ask you if it was a misconversion or a manual tweak.

In any case, I think I've looked at it long enough to be reasonably OK with the conversion result myself, so let's move it forward. Of course I welcome independent eyeballing by others.

Thanks.

#6 lore raw

PATCH 2/2 hashmap: migrate documentation from Documentation/technical into header

In reply to Stefan Beller

While at it, clarify the use of key, keydata, entry_or_key as well as documenting the new data pointer for the compare function.

---
 Documentation/technical/api-hashmap.txt | 309 --------------------------------
 hashmap.h                               | 249 +++++++++++++++++++++++--
 2 files changed, 231 insertions(+), 327 deletions(-)
 delete mode 100644 Documentation/technical/api-hashmap.txt
Documentation/technical/api-hashmap.txt deleted +0 -309
deleted file mode 100644
index ccc634bbd7..0000000000
@@ -1,309 +0,0 @@
-hashmap API
-===========
-
-The hashmap API is a generic implementation of hash-based key-value mappings.
-
-Data Structures
----------------
-
-`struct hashmap`::
-
-	The hash table structure. Members can be used as follows, but should
-	not be modified directly:
-+
-The `size` member keeps track of the total number of entries (0 means the
-hashmap is empty).
-+
-`tablesize` is the allocated size of the hash table. A non-0 value indicates
-that the hashmap is initialized. It may also be useful for statistical purposes
-(i.e. `size / tablesize` is the current load factor).
-+
-`cmpfn` stores the comparison function specified in `hashmap_init()`. In
-advanced scenarios, it may be useful to change this, e.g. to switch between
-case-sensitive and case-insensitive lookup.
-+
-When `disallow_rehash` is set, automatic rehashes are prevented during inserts
-and deletes.
-
-`struct hashmap_entry`::
-
-	An opaque structure representing an entry in the hash table, which must
-	be used as first member of user data structures. Ideally it should be
-	followed by an int-sized member to prevent unused memory on 64-bit
-	systems due to alignment.
-+
-The `hash` member is the entry's hash code and the `next` member points to the
-next entry in case of collisions (i.e. if multiple entries map to the same
-bucket).
-
-`struct hashmap_iter`::
-
-	An iterator structure, to be used with hashmap_iter_* functions.
-
-Types
------
-
-`int (*hashmap_cmp_fn)(const void *entry, const void *entry_or_key, const void *keydata)`::
-
-	User-supplied function to test two hashmap entries for equality. Shall
-	return 0 if the entries are equal.
-+
-This function is always called with non-NULL `entry` / `entry_or_key`
-parameters that have the same hash code. When looking up an entry, the `key`
-and `keydata` parameters to hashmap_get and hashmap_remove are always passed
-as second and third argument, respectively. Otherwise, `keydata` is NULL.
-
-Functions
----------
-
-`unsigned int strhash(const char *buf)`::
-`unsigned int strihash(const char *buf)`::
-`unsigned int memhash(const void *buf, size_t len)`::
-`unsigned int memihash(const void *buf, size_t len)`::
-`unsigned int memihash_cont(unsigned int hash_seed, const void *buf, size_t len)`::
-
-	Ready-to-use hash functions for strings, using the FNV-1 algorithm (see
-	http://www.isthe.com/chongo/tech/comp/fnv).
-+
-`strhash` and `strihash` take 0-terminated strings, while `memhash` and
-`memihash` operate on arbitrary-length memory.
-+
-`strihash` and `memihash` are case insensitive versions.
-+
-`memihash_cont` is a variant of `memihash` that allows a computation to be
-continued with another chunk of data.
-
-`unsigned int sha1hash(const unsigned char *sha1)`::
-
-	Converts a cryptographic hash (e.g. SHA-1) into an int-sized hash code
-	for use in hash tables. Cryptographic hashes are supposed to have
-	uniform distribution, so in contrast to `memhash()`, this just copies
-	the first `sizeof(int)` bytes without shuffling any bits. Note that
-	the results will be different on big-endian and little-endian
-	platforms, so they should not be stored or transferred over the net.
-
-`void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function, size_t initial_size)`::
-
-	Initializes a hashmap structure.
-+
-`map` is the hashmap to initialize.
-+
-The `equals_function` can be specified to compare two entries for equality.
-If NULL, entries are considered equal if their hash codes are equal.
-+
-If the total number of entries is known in advance, the `initial_size`
-parameter may be used to preallocate a sufficiently large table and thus
-prevent expensive resizing. If 0, the table is dynamically resized.
-
-`void hashmap_free(struct hashmap *map, int free_entries)`::
-
-	Frees a hashmap structure and allocated memory.
-+
-`map` is the hashmap to free.
-+
-If `free_entries` is true, each hashmap_entry in the map is freed as well
-(using stdlib's free()).
-
-`void hashmap_entry_init(void *entry, unsigned int hash)`::
-
-	Initializes a hashmap_entry structure.
-+
-`entry` points to the entry to initialize.
-+
-`hash` is the hash code of the entry.
-+
-The hashmap_entry structure does not hold references to external resources,
-and it is safe to just discard it once you are done with it (i.e. if
-your structure was allocated with xmalloc(), you can just free(3) it,
-and if it is on stack, you can just let it go out of scope).
-
-`void *hashmap_get(const struct hashmap *map, const void *key, const void *keydata)`::
-
-	Returns the hashmap entry for the specified key, or NULL if not found.
-+
-`map` is the hashmap structure.
-+
-`key` is a hashmap_entry structure (or user data structure that starts with
-hashmap_entry) that has at least been initialized with the proper hash code
-(via `hashmap_entry_init`).
-+
-If an entry with matching hash code is found, `key` and `keydata` are passed
-to `hashmap_cmp_fn` to decide whether the entry matches the key.
-
-`void *hashmap_get_from_hash(const struct hashmap *map, unsigned int hash, const void *keydata)`::
-
-	Returns the hashmap entry for the specified hash code and key data,
-	or NULL if not found.
-+
-`map` is the hashmap structure.
-+
-`hash` is the hash code of the entry to look up.
-+
-If an entry with matching hash code is found, `keydata` is passed to
-`hashmap_cmp_fn` to decide whether the entry matches the key. The
-`entry_or_key` parameter points to a bogus hashmap_entry structure that
-should not be used in the comparison.
-
-`void *hashmap_get_next(const struct hashmap *map, const void *entry)`::
-
-	Returns the next equal hashmap entry, or NULL if not found. This can be
-	used to iterate over duplicate entries (see `hashmap_add`).
-+
-`map` is the hashmap structure.
-+
-`entry` is the hashmap_entry to start the search from, obtained via a previous
-call to `hashmap_get` or `hashmap_get_next`.
-
-`void hashmap_add(struct hashmap *map, void *entry)`::
-
-	Adds a hashmap entry. This allows to add duplicate entries (i.e.
-	separate values with the same key according to hashmap_cmp_fn).
-+
-`map` is the hashmap structure.
-+
-`entry` is the entry to add.
-
-`void *hashmap_put(struct hashmap *map, void *entry)`::
-
-	Adds or replaces a hashmap entry. If the hashmap contains duplicate
-	entries equal to the specified entry, only one of them will be replaced.
-+
-`map` is the hashmap structure.
-+
-`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_remove(struct hashmap *map, const void *key, const void *keydata)`::
-
-	Removes a hashmap entry matching the specified key. If the hashmap
-	contains duplicate entries equal to the specified key, only one of
-	them will be removed.
-+
-`map` is the hashmap structure.
-+
-`key` is a hashmap_entry structure (or user data structure that starts with
-hashmap_entry) that has at least been initialized with the proper hash code
-(via `hashmap_entry_init`).
-+
-If an entry with matching hash code is found, `key` and `keydata` are
-passed to `hashmap_cmp_fn` to decide whether the entry matches the key.
-+
-Returns the removed entry, or NULL if not found.
-
-`void hashmap_disallow_rehash(struct hashmap *map, unsigned value)`::
-
-	Disallow/allow automatic rehashing of the hashmap during inserts
-	and deletes.
-+
-This is useful if the caller knows that the hashmap will be accessed
-by multiple threads.
-+
-The caller is still responsible for any necessary locking; this simply
-prevents unexpected rehashing.  The caller is also responsible for properly
-sizing the initial hashmap to ensure good performance.
-+
-A call to allow rehashing does not force a rehash; that might happen
-with the next insert or delete.
-
-`void hashmap_iter_init(struct hashmap *map, struct hashmap_iter *iter)`::
-`void *hashmap_iter_next(struct hashmap_iter *iter)`::
-`void *hashmap_iter_first(struct hashmap *map, struct hashmap_iter *iter)`::
-
-	Used to iterate over all entries of a hashmap. Note that it is
-	not safe to add or remove entries to the hashmap while
-	iterating.
-+
-`hashmap_iter_init` initializes a `hashmap_iter` structure.
-+
-`hashmap_iter_next` returns the next hashmap_entry, or NULL if there are no
-more entries.
-+
-`hashmap_iter_first` is a combination of both (i.e. initializes the iterator
-and returns the first entry, if any).
-
-`const char *strintern(const char *string)`::
-`const void *memintern(const void *data, size_t len)`::
-
-	Returns the unique, interned version of the specified string or data,
-	similar to the `String.intern` API in Java and .NET, respectively.
-	Interned strings remain valid for the entire lifetime of the process.
-+
-Can be used as `[x]strdup()` or `xmemdupz` replacement, except that interned
-strings / data must not be modified or freed.
-+
-Interned strings are best used for short strings with high probability of
-duplicates.
-+
-Uses a hashmap to store the pool of interned strings.
-
-Usage example
--------------
-
-Here's a simple usage example that maps long keys to double values.
-------------
-struct hashmap map;
-
-struct long2double {
-	struct hashmap_entry ent; /* must be the first member! */
-	long key;
-	double value;
-};
-
-static int long2double_cmp(const struct long2double *e1, const struct long2double *e2, const void *unused)
-{
-	return !(e1->key == e2->key);
-}
-
-void long2double_init(void)
-{
-	hashmap_init(&map, (hashmap_cmp_fn) long2double_cmp, 0);
-}
-
-void long2double_free(void)
-{
-	hashmap_free(&map, 1);
-}
-
-static struct long2double *find_entry(long key)
-{
-	struct long2double k;
-	hashmap_entry_init(&k, memhash(&key, sizeof(long)));
-	k.key = key;
-	return hashmap_get(&map, &k, NULL);
-}
-
-double get_value(long key)
-{
-	struct long2double *e = find_entry(key);
-	return e ? e->value : 0;
-}
-
-void set_value(long key, double value)
-{
-	struct long2double *e = find_entry(key);
-	if (!e) {
-		e = malloc(sizeof(struct long2double));
-		hashmap_entry_init(e, memhash(&key, sizeof(long)));
-		e->key = key;
-		hashmap_add(&map, e);
-	}
-	e->value = value;
-}
-------------
-
-Using variable-sized keys
--------------------------
-
-The `hashmap_entry_get` and `hashmap_entry_remove` functions expect an ordinary
-`hashmap_entry` structure as key to find the correct entry. If the key data is
-variable-sized (e.g. a FLEX_ARRAY string) or quite large, it is undesirable
-to create a full-fledged entry structure on the heap and copy all the key data
-into the structure.
-
-In this case, the `keydata` parameter can be used to pass
-variable-sized key data directly to the comparison function, and the `key`
-parameter can be a stripped-down, fixed size entry structure allocated on the
-stack.
-
-See test-hashmap.c for an example using arbitrary-length strings as keys.
hashmap.h +231 -18

335 lines not on this page · raw message

Signature · 1 line
2.13.0.31.g9b732c453e
#7 lore raw

Re: [PATCH 2/2] hashmap: migrate documentation from Documentation/technical into header

Quoted 10 lines · Stefan Beller

While at it, clarify the use of key, keydata, entry_or_key as well as documenting the new data pointer for the compare function.

---
  Documentation/technical/api-hashmap.txt | 309 --------------------------------
  hashmap.h                               | 249 +++++++++++++++++++++++--
  2 files changed, 231 insertions(+), 327 deletions(-)

delete mode 100644 Documentation/technical/api-hashmap.txt
...

This looks very nice. Thank you!

Jeff

#8 lore raw

Re: [PATCH 2/2] hashmap: migrate documentation from Documentation/technical into header

In reply to Stefan Beller
Quoted 11 lines · Stefan Beller

Subject: hashmap: migrate documentation from Documentation/technical into header

While at it, clarify the use of key, keydata, entry_or_key as well as documenting the new data pointer for the compare function.

---
 Documentation/technical/api-hashmap.txt | 309 --------------------------------
 hashmap.h                               | 249 +++++++++++++++++++++++--
 2 files changed, 231 insertions(+), 327 deletions(-)
 delete mode 100644 Documentation/technical/api-hashmap.txt

Yay, I love this.

[...]

Documentation/technical/api-hashmap.txt

Quoted 3 lines
Documentation/technical/api-hashmap.txt deleted +0 -0
@@ -1,309 +0,0 @@

Verified that these docs have all been migrated to the header, except where noted below.

[...]

Documentation/technical/api-hashmap.txt line 54

Quoted 9 lines
Documentation/technical/api-hashmap.txt deleted +0 -9
-`int (*hashmap_cmp_fn)(const void *entry, const void *entry_or_key, const void *keydata)`::
-
-	User-supplied function to test two hashmap entries for equality. Shall
-	return 0 if the entries are equal.
-+
-This function is always called with non-NULL `entry` / `entry_or_key`
-parameters that have the same hash code. When looking up an entry, the `key`
-and `keydata` parameters to hashmap_get and hashmap_remove are always passed
-as second and third argument, respectively. Otherwise, `keydata` is NULL.

This was really heard to read in the preimage. Thanks for cleaning it up.

[...]

Documentation/technical/api-hashmap.txt line 243

Quoted 4 lines
Documentation/technical/api-hashmap.txt deleted +0 -4
-Usage example
--------------
-
-Here's a simple usage example that maps long keys to double values.

What happened to this? I think it would be useful to include in a long comment at the top of the header. A worked example like this makes it easier to get one's bearings and know which functions to look at.

[...]

Documentation/technical/api-hashmap.txt line 296

Quoted 2 lines
Documentation/technical/api-hashmap.txt deleted +0 -2
-Using variable-sized keys
--------------------------

Same question: should this discussion go in the hashmap_get() docstring, with a pointer from hashmap_remove()? Or should it go in test-hashmap.c, with a pointer in the docstrings of both?

[...]

Documentation/technical/api-hashmap.txt line 7

Quoted 8 lines
Documentation/technical/api-hashmap.txt deleted +2 -1
+++ b/hashmap.h
@@ -3,11 +3,18 @@
 
 /*
  * Generic implementation of hash-based key-value mappings.
- * See Documentation/technical/api-hashmap.txt.
+ *
  */

nit: why the blank line?

Quoted 17 lines
Documentation/technical/api-hashmap.txt deleted +9 -2
-/* FNV-1 functions */
-
+/*
+ * Ready-to-use hash functions for strings, using the FNV-1 algorithm (see
+ * http://www.isthe.com/chongo/tech/comp/fnv).
+ * `strhash` and `strihash` take 0-terminated strings, while `memhash` and
+ * `memihash` operate on arbitrary-length memory.
+ * `strihash` and `memihash` are case insensitive versions.
+ * `memihash_cont` is a variant of `memihash` that allows a computation to be
+ * continued with another chunk of data.
+ */
 extern unsigned int strhash(const char *buf);
 extern unsigned int strihash(const char *buf);
 extern unsigned int memhash(const void *buf, size_t len);
@@ -16,6 +23,15 @@ extern unsigned int memihash_cont(unsigned int hash_seed, const void *buf, size_
 

Documentation/technical/api-hashmap.txt extern unsigned int memihash_cont(unsigned int hash_seed, const void *buf, size_ · line 33

Quoted 10 lines
Documentation/technical/api-hashmap.txt deleted +8 -0
 static inline unsigned int sha1hash(const unsigned char *sha1)
 {
+	/*
+	 * Converts a cryptographic hash (e.g. SHA-1) into an int-sized hash code
+	 * for use in hash tables. Cryptographic hashes are supposed to have
+	 * uniform distribution, so in contrast to `memhash()`, this just copies
+	 * the first `sizeof(int)` bytes without shuffling any bits. Note that
+	 * the results will be different on big-endian and little-endian
+	 * platforms, so they should not be stored or transferred over the net.
+	 */

This comment should go in front of the function instead of in its body, like this:

	/*
	 * Converts a crypto [...]
	 */
	static inline unsigned int sha1hash(...

because it is describing the purpose and usage of the function and not its implementation.

[...]

Documentation/technical/api-hashmap.txt extern unsigned int memihash_cont(unsigned int hash_seed, const void *buf, size_

Quoted 11 lines
Documentation/technical/api-hashmap.txt deleted +11 -0
+/*
+ * User-supplied function to test two hashmap entries for equality. Shall
+ * return 0 if the entries are equal.
+ *
+ * This function is always called with non-NULL `entry` and `entry_or_key`
+ * parameters that have the same hash code.
+ *
+ * When looking up an entry, the `key` and `keydata` parameters to hashmap_get
+ * and hashmap_remove are always passed
+ * as second `entry_or_key` and third argument `keydata`, respectively.
+ * Otherwise, `keydata` is NULL.

strange wrapping

Quoted 5 lines

+ *
+ * There are two modes for this function to be used:
+ * (a) When looking for an exact match of a given entry, then keydata
+ * ought to be NULL, and this function should cast entry and
+ * entry_or_key to the user entries and check for equality.

What does it mean that keydata ought to be NULL? Are you saying it will be NULL, or that someone has to ensure it's NULL, or something else?

Quoted 4 lines

+ * (b) When it is too expensive to allocate such a user entry (either because

+ *     it is large or varialbe sized, such that it is not on the stack),
+ *     Then the relevant data to check for equality should be passed via
+ *     ` keydata`.

stray space in " keydata".

Quoted 4 lines

+ *
+ * Resulting from these modes, this function should compare keydata to entry
+ * when keydata is not NULL. entry_or_key may be a bogus hashmap_entry (see
+ * hashmap_get_from_hash).

What is meant by a bogus hashmap_entry here? (See also below.)

[...]

Quoted 5 lines

+/*
+ * struct hashmap is the hash table structure. Members can be used as follows,
+ * but should not be modified directly.
+ */
struct hashmap {

[...]

Documentation/technical/api-hashmap.txt

Quoted 3 lines
Documentation/technical/api-hashmap.txt deleted +2 -0
+	/* Prevent automatic rehashes during inserts and deletes when set. */
+	unsigned disallow_rehash : 1;
 };

Please add a reference to hashmap_disallow_rehash so people know how to set this.

Quoted 12 lines

/* hashmap functions */

+/*
+ * Initializes a hashmap structure.
+ *
+ * map is the hashmap to initialize.
+ *
+ * The equals_function can be specified to compare two entries for equality.
+ * If NULL, entries are considered equal if their hash codes are equal.
+ *
+ * When the compare function needs specific user supplied data, it should

nit: it's not user supplied so much as caller supplied. Maybe this can say something like

The data parameter can be used to provide additional data (a callback cookie) that will be passed to equals_function each time it is called. This allows a single equals_function to implement multiple comparison functions.

[...]

Quoted 5 lines

+/*
+ * Frees a hashmap structure and allocated memory.
+ *
+ * If free_entries is true, each hashmap_entry in the map is freed as well.
+ */

nit: Documentation/technical said the hashmap_entrys get freed using free(). Can't hurt to carry that note over, to save the reader from hunting down whether the struct hashmap allows setting a custom free function.

[...]

Quoted 6 lines

+/*
+ * Returns the hashmap entry for the specified key, or NULL if not found.
+ *
+ * map is the hashmap structure.
+ * key is a user data structure that starts with hashmap_entry that has at
+ * least been initialized with the proper hash code (via hashmap_entry_init).

nit: strange alignment (extra space before "least").

Should these argument lists be formatted as a list somehow (e.g. leading "- " bullets, or with a blank line between them? (I don't have a strong opinion, just noticed they can be hard to read with everything run together.)

Quoted 9 lines

+ * keydata is a data structure that holds just enough information to check
+ * for equality to a given entry.
+ *
+ * One of key or keydata shall be NULL.
+ *
+ * If an entry with matching hash code is found, key and keydata are passed
+ * to hashmap_cmp_fn to decide whether the entry matches the key.
+ */
extern void *hashmap_get(const struct hashmap *map, const void *key,

[...]

Quoted 7 lines

+ * Removes a hashmap entry matching the specified key. If the hashmap
+ * contains duplicate entries equal to the specified key, only one of
+ * them will be removed.
+ *
+ * map is the hashmap structure.
+ * key is a user data structure that starts with hashmap_entry that has at
+ * least been initialized with the proper hash code (via hashmap_entry_init).

nit: strange alignment again

[...]

Quoted 1 line

extern void *hashmap_remove(struct hashmap *map, const void *key,

[...]

Quoted 11 lines

+/*
+ * Returns the hashmap entry for the specified hash code and key data,
+ * or NULL if not found.
+ *
+ * map is the hashmap structure.
+ * hash is the hash code of the entry to look up.
+ *
+ * If an entry with matching hash code is found, keydata is passed to
+ * hashmap_cmp_fn to decide whether the entry matches the key. The
+ * entry_or_key parameter points to a bogus hashmap_entry structure that
+ * should not be used in the comparison.

What is going on with this note about bogus entry_or_key? I have no idea what it is trying to say, neither in the preimage nor the postimage.

Documentation/technical/api-hashmap.txt

Quoted 5 lines

+ */
static inline void *hashmap_get_from_hash(const struct hashmap *map,

Documentation/technical/api-hashmap.txt deleted +2 -1
-		unsigned int hash, const void *keydata)
+					  unsigned int hash,
+					  const void *keydata)

In Documentation/technical, hashmap_get_from_hash was immediately after hashmap_get, making it more discoverable.

Should the descriptions of these two functions mention each other to bring about a similar effect?

[...]

Documentation/technical/api-hashmap.txt int hashmap_bucket(const struct hashmap *map, unsigned int hash); · line 279

Quoted 9 lines
Documentation/technical/api-hashmap.txt deleted +1 -1
@@ -91,7 +273,7 @@ int hashmap_bucket(const struct hashmap *map, unsigned int hash);
  * manner appropriate to their usage.  This simply
  * prevents the table from being unexpectedly re-mapped.
  *
- * If is up to the caller to ensure that the hashmap is
+ * It is up to the caller to ensure that the hashmap is
  * initialized to a reasonable size to prevent poor
  * performance.
  *

The Documentation/technical/ text says:

A call to allow rehashing does not force a rehash; that might happen with the next insert or delete.

which I find easier on the eyes than

* When value=0, allow future rehahses. This DOES NOT force
* a rehash now.

Why not make this comment use the full description from Documentation/technical/ verbatim?

Quoted 1 line
Documentation/technical/api-hashmap.txt deleted +0 -0
@@ -104,10 +286,28 @@ static inline void hashmap_disallow_rehash(struct hashmap *map, unsigned value)

[...]

Documentation/technical/api-hashmap.txt

Quoted 11 lines
Documentation/technical/api-hashmap.txt deleted +9 -1
-/* hashmap_iter functions */
 
+/*
+ * hashmap_iter functions
+ *
+ * Used to iterate over all entries of a hashmap. Note that it is
+ * not safe to add or remove entries to the hashmap while
+ * iterating.
+ */
+
+struct hashmap_iter {

nit: might be simpler to associate this comment with the struct:

	/*
	 * Used to iterate over ...
	 */
	struct hashmap_iter {
		...
	}

That way, the scope of what the comment is describing is clearer. When looking up the functions, I'm likely to look up the hashmap_iter type they use, so I'd end up reading this comment anyway.

Documentation/technical/api-hashmap.txt

Quoted 12 lines
Documentation/technical/api-hashmap.txt deleted +10 -0
+	struct hashmap *map;
+	struct hashmap_entry *next;
+	unsigned int tablepos;
+};
+
+/* Initializes a `hashmap_iter` structure. */
 extern void hashmap_iter_init(struct hashmap *map, struct hashmap_iter *iter);
+
+/* Returns the next hashmap_entry, or NULL if there are no more entries. */
 extern void *hashmap_iter_next(struct hashmap_iter *iter);
+
+/* Initializes the iterator and returns the first entry, if any). */

Stray paren?

Probably worth mentioning this is a convenience wrapper for iter_init
+ iter_next, like the Documentation/technical/ text did.

Quoted 1 line

static inline void *hashmap_iter_first(struct hashmap *map,

The rest looks good.

Thanks and hope that helps,
Jonathan

#9 lore raw

Re: [PATCH 2/2] hashmap: migrate documentation from Documentation/technical into header

Took all suggestions so far, but the last one:

Quoted 3 lines

Probably worth mentioning this is a convenience wrapper for iter_init
+ iter_next, like the Documentation/technical/ text did.

No. The code to observe that this is a convenience wrapper IS RIGHT THERE (it's a header file), and I find this header a bit bloated already. So I'll keep it as a one liner.

#10 lore raw

v2 cover Introduce data field in hashmap and migrate docs to header

In reply to Stefan Beller

v2:

addressed all but the last point of Jonathan Nieder.

Thanks,
Stefan

v1:

https://public-inbox.org/git/[email protected]/
for context why we need a new data field. Implement that.

Once upon a time we had a long discussion where to put documentation best. The answer was header files as there documentation has less chance to become stale and be out of date. Improve the docs by
* migrating them to the header
* clarifying how the compare function is to be used
* how the arguments to hashmap_get/remove should be used.

Thanks,
Stefan

Stefan Beller (2):
hashmap.h: compare function has access to a data field
hashmap: migrate documentation from Documentation/technical into
header

 Documentation/technical/api-hashmap.txt | 309 ---------------------------
 attr.c                                  |   4 +-
 builtin/describe.c                      |   6 +-
 builtin/difftool.c                      |  20 +-
 builtin/fast-export.c                   |   5 +-
 config.c                                |   7 +-
 convert.c                               |   3 +-
 diffcore-rename.c                       |   2 +-
 hashmap.c                               |  17 +-
 hashmap.h                               | 358 ++++++++++++++++++++++++++++----
 name-hash.c                             |  12 +-
 oidset.c                                |   5 +-
 patch-ids.c                             |   6 +-
 refs.c                                  |   4 +-
 remote.c                                |   7 +-
 sha1_file.c                             |   5 +-
 sub-process.c                           |   5 +-
 sub-process.h                           |   6 +-
 submodule-config.c                      |  10 +-
 t/helper/test-hashmap.c                 |  15 +-
 20 files changed, 412 insertions(+), 394 deletions(-)
 delete mode 100644 Documentation/technical/api-hashmap.txt
Signature · 1 line
2.13.0.31.g9b732c453e
#11 lore raw

PATCH v2 1/2 hashmap.h: compare function has access to a data field

When using the hashmap a common need is to have access to arbitrary data in the compare function. A couple of times we abuse the keydata field to pass in the data needed. This happens for example in patch-ids.c.

This patch changes the function signature of the compare function to have one more void pointer available. The pointer given for each invocation of the compare function must be defined in the init function of the hashmap and is just passed through.

Documentation of this new feature is deferred to a later patch.

While at it improve the naming of the fields of all compare functions used by hashmaps by ensuring unused parameters are prefixed with 'unused_' and naming the parameters what they are (instead of 'unused' make it 'unused_keydata').

---
 attr.c                  |  4 ++--
 builtin/describe.c      |  6 ++++--
 builtin/difftool.c      | 20 ++++++++++++--------
 builtin/fast-export.c   |  5 +++--
 config.c                |  7 +++++--
 convert.c               |  3 ++-
 diffcore-rename.c       |  2 +-
 hashmap.c               | 17 ++++++++++++-----
 hashmap.h               |  9 ++++++---
 name-hash.c             | 12 ++++++++----
 oidset.c                |  5 +++--
 patch-ids.c             |  6 ++++--
 refs.c                  |  4 ++--
 remote.c                |  7 +++++--
 sha1_file.c             |  5 +++--
 sub-process.c           |  5 +++--
 sub-process.h           |  6 ++++--
 submodule-config.c      | 10 ++++++----
 t/helper/test-hashmap.c | 15 ++++++++++-----
 19 files changed, 95 insertions(+), 53 deletions(-)
attr.c +2 -2
index 37454999d2..2f51417675 100644
@@ -78,7 +78,7 @@ struct attr_hash_entry {
 /* attr_hashmap comparison function */
 static int attr_hash_entry_cmp(const struct attr_hash_entry *a,
 			       const struct attr_hash_entry *b,
-			       void *unused)
+			       void *unused_keydata, void *unused_data)
 {
 	return (a->keylen != b->keylen) || strncmp(a->key, b->key, a->keylen);
 }
@@ -86,7 +86,7 @@ static int attr_hash_entry_cmp(const struct attr_hash_entry *a,
 /* Initialize an 'attr_hashmap' object */
 static void attr_hashmap_init(struct attr_hashmap *map)
 {
-	hashmap_init(&map->map, (hashmap_cmp_fn) attr_hash_entry_cmp, 0);
+	hashmap_init(&map->map, (hashmap_cmp_fn) attr_hash_entry_cmp, NULL, 0);
 }
 
 /*
builtin/describe.c +4 -2
index 70eb144608..a6c5a969a0 100644
@@ -55,7 +55,9 @@ static const char *prio_names[] = {
 };
 
 static int commit_name_cmp(const struct commit_name *cn1,
-		const struct commit_name *cn2, const void *peeled)
+			   const struct commit_name *cn2,
+			   const void *peeled,
+			   const void *unused_data)
 {
 	return oidcmp(&cn1->peeled, peeled ? peeled : &cn2->peeled);
 }
@@ -501,7 +503,7 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
 		return cmd_name_rev(args.argc, args.argv, prefix);
 	}
 
-	hashmap_init(&names, (hashmap_cmp_fn) commit_name_cmp, 0);
+	hashmap_init(&names, (hashmap_cmp_fn) commit_name_cmp, NULL, 0);
 	for_each_rawref(get_name, NULL);
 	if (!names.size && !always)
 		die(_("No names found, cannot describe anything."));
builtin/difftool.c +12 -8
index 9199227f6e..80786a95ab 100644
@@ -131,7 +131,9 @@ struct working_tree_entry {
 };
 
 static int working_tree_entry_cmp(struct working_tree_entry *a,
-				  struct working_tree_entry *b, void *keydata)
+				  struct working_tree_entry *b,
+				  void *unused_keydata,
+				  void *unused_data)
 {
 	return strcmp(a->path, b->path);
 }
@@ -146,7 +148,8 @@ struct pair_entry {
 	const char path[FLEX_ARRAY];
 };
 
-static int pair_cmp(struct pair_entry *a, struct pair_entry *b, void *keydata)
+static int pair_cmp(struct pair_entry *a, struct pair_entry *b,
+		    void *unused_keydata, void *unused_data)
 {
 	return strcmp(a->path, b->path);
 }
@@ -174,7 +177,8 @@ struct path_entry {
 	char path[FLEX_ARRAY];
 };
 
-static int path_entry_cmp(struct path_entry *a, struct path_entry *b, void *key)
+static int path_entry_cmp(struct path_entry *a, struct path_entry *b,
+			  void *key, void *unused_data)
 {
 	return strcmp(a->path, key ? key : b->path);
 }
@@ -367,9 +371,9 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
 	wtdir_len = wtdir.len;
 
 	hashmap_init(&working_tree_dups,
-		     (hashmap_cmp_fn)working_tree_entry_cmp, 0);
-	hashmap_init(&submodules, (hashmap_cmp_fn)pair_cmp, 0);
-	hashmap_init(&symlinks2, (hashmap_cmp_fn)pair_cmp, 0);
+		     (hashmap_cmp_fn)working_tree_entry_cmp, NULL, 0);
+	hashmap_init(&submodules, (hashmap_cmp_fn)pair_cmp, NULL, 0);
+	hashmap_init(&symlinks2, (hashmap_cmp_fn)pair_cmp, NULL, 0);
 
 	child.no_stdin = 1;
 	child.git_cmd = 1;
@@ -580,9 +584,9 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
 	 * files through the symlink.
 	 */
 	hashmap_init(&wt_modified, (hashmap_cmp_fn)path_entry_cmp,
-		     wtindex.cache_nr);
+		     NULL, wtindex.cache_nr);
 	hashmap_init(&tmp_modified, (hashmap_cmp_fn)path_entry_cmp,
-		     wtindex.cache_nr);
+		     NULL, wtindex.cache_nr);
 
 	for (i = 0; i < wtindex.cache_nr; i++) {
 		struct hashmap_entry dummy;
builtin/fast-export.c +3 -2
index 12d501bfde..fa389fe252 100644
@@ -94,7 +94,8 @@ struct anonymized_entry {
 };
 
 static int anonymized_entry_cmp(const void *va, const void *vb,
-				const void *data)
+				const void *unused_keydata,
+				const void *unused_data)
 {
 	const struct anonymized_entry *a = va, *b = vb;
 	return a->orig_len != b->orig_len ||
@@ -113,7 +114,7 @@ static const void *anonymize_mem(struct hashmap *map,
 	struct anonymized_entry key, *ret;
 
 	if (!map->cmpfn)
-		hashmap_init(map, anonymized_entry_cmp, 0);
+		hashmap_init(map, anonymized_entry_cmp, NULL, 0);
 
 	hashmap_entry_init(&key, memhash(orig, *len));
 	key.orig = orig;
config.c +5 -2
index 1cd40a5fe6..aa0198a5fd 100644
@@ -1754,14 +1754,17 @@ static int configset_add_value(struct config_set *cs, const char *key, const cha
 }
 
 static int config_set_element_cmp(const struct config_set_element *e1,
-				 const struct config_set_element *e2, const void *unused)
+				 const struct config_set_element *e2,
+				 const void *unused_keydata,
+				 const void *unused_data)
 {
 	return strcmp(e1->key, e2->key);
 }
 
 void git_configset_init(struct config_set *cs)
 {
-	hashmap_init(&cs->config_hash, (hashmap_cmp_fn)config_set_element_cmp, 0);
+	hashmap_init(&cs->config_hash, (hashmap_cmp_fn)config_set_element_cmp,
+		     NULL, 0);
 	cs->hash_initialized = 1;
 	cs->list.nr = 0;
 	cs->list.alloc = 0;
convert.c +2 -1
index 7d2a519daf..deaf0ba7b3 100644
@@ -583,7 +583,8 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t len
 
 	if (!subprocess_map_initialized) {
 		subprocess_map_initialized = 1;
-		hashmap_init(&subprocess_map, (hashmap_cmp_fn) cmd2process_cmp, 0);
+		hashmap_init(&subprocess_map, (hashmap_cmp_fn) cmd2process_cmp,
+			     NULL, 0);
 		entry = NULL;
 	} else {
 		entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd);
diffcore-rename.c +1 -1
index 1e4678b7d7..786f389498 100644
@@ -341,7 +341,7 @@ static int find_exact_renames(struct diff_options *options)
 	/* Add all sources to the hash table in reverse order, because
 	 * later on they will be retrieved in LIFO order.
 	 */
-	hashmap_init(&file_table, NULL, rename_src_nr);
+	hashmap_init(&file_table, NULL, NULL, rename_src_nr);
 	for (i = rename_src_nr-1; i >= 0; i--)
 		insert_file_table(&file_table, i, rename_src[i].p->one);
 
hashmap.c +12 -5
index 7d1044eb5d..82d70b329b 100644
@@ -95,7 +95,9 @@ static inline int entry_equals(const struct hashmap *map,
 		const struct hashmap_entry *e1, const struct hashmap_entry *e2,
 		const void *keydata)
 {
-	return (e1 == e2) || (e1->hash == e2->hash && !map->cmpfn(e1, e2, keydata));
+	return (e1 == e2) ||
+	       (e1->hash == e2->hash &&
+		!map->cmpfn(e1, e2, keydata, map->data));
 }
 
 static inline unsigned int bucket(const struct hashmap *map,
@@ -140,19 +142,23 @@ static inline struct hashmap_entry **find_entry_ptr(const struct hashmap *map,
 	return e;
 }
 
-static int always_equal(const void *unused1, const void *unused2, const void *unused3)
+static int always_equal(const void *unused1,
+			const void *unused2,
+			const void *unused_keydata,
+			const void *unused_data)
 {
 	return 0;
 }
 
 void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function,
-		size_t initial_size)
+		const void *data, size_t initial_size)
 {
 	unsigned int size = HASHMAP_INITIAL_SIZE;
 
 	memset(map, 0, sizeof(*map));
 
 	map->cmpfn = equals_function ? equals_function : always_equal;
+	map->data = data;
 
 	/* calculate initial table size and allocate the table */
 	initial_size = (unsigned int) ((uint64_t) initial_size * 100
@@ -262,7 +268,8 @@ struct pool_entry {
 
 static int pool_entry_cmp(const struct pool_entry *e1,
 			  const struct pool_entry *e2,
-			  const unsigned char *keydata)
+			  const unsigned char *keydata,
+			  const void *unused_data)
 {
 	return e1->data != keydata &&
 	       (e1->len != e2->len || memcmp(e1->data, keydata, e1->len));
@@ -275,7 +282,7 @@ const void *memintern(const void *data, size_t len)
 
 	/* initialize string pool hashmap */
 	if (!map.tablesize)
-		hashmap_init(&map, (hashmap_cmp_fn) pool_entry_cmp, 0);
+		hashmap_init(&map, (hashmap_cmp_fn) pool_entry_cmp, NULL, 0);
 
 	/* lookup interned string in pool */
 	hashmap_entry_init(&key, memhash(data, len));
hashmap.h +6 -3
index de6022a3a9..1c26bbad5b 100644
@@ -33,11 +33,12 @@ struct hashmap_entry {
 };
 
 typedef int (*hashmap_cmp_fn)(const void *entry, const void *entry_or_key,
-		const void *keydata);
+		const void *keydata, const void *cbdata);
 
 struct hashmap {
 	struct hashmap_entry **table;
 	hashmap_cmp_fn cmpfn;
+	const void *data;
 	unsigned int size, tablesize, grow_at, shrink_at;
 	unsigned disallow_rehash : 1;
 };
@@ -50,8 +51,10 @@ struct hashmap_iter {
 
 /* hashmap functions */
 
-extern void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function,
-		size_t initial_size);
+extern void hashmap_init(struct hashmap *map,
+			 hashmap_cmp_fn equals_function,
+			 const void *data,
+			 size_t initial_size);
 extern void hashmap_free(struct hashmap *map, int free_entries);
 
 /* hashmap_entry functions */
name-hash.c +8 -4
index 39309efb7f..4e9eac925a 100644
@@ -17,7 +17,9 @@ struct dir_entry {
 };
 
 static int dir_entry_cmp(const struct dir_entry *e1,
-		const struct dir_entry *e2, const char *name)
+			 const struct dir_entry *e2,
+			 const char *name,
+			 const void *unused_data)
 {
 	return e1->namelen != e2->namelen || strncasecmp(e1->name,
 			name ? name : e2->name, e1->namelen);
@@ -108,7 +110,9 @@ static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
 }
 
 static int cache_entry_cmp(const struct cache_entry *ce1,
-		const struct cache_entry *ce2, const void *remove)
+			   const struct cache_entry *ce2,
+			   const void *remove,
+			   const void *unused_data)
 {
 	/*
 	 * For remove_name_hash, find the exact entry (pointer equality); for
@@ -571,9 +575,9 @@ static void lazy_init_name_hash(struct index_state *istate)
 	if (istate->name_hash_initialized)
 		return;
 	hashmap_init(&istate->name_hash, (hashmap_cmp_fn) cache_entry_cmp,
-			istate->cache_nr);
+			NULL, istate->cache_nr);
 	hashmap_init(&istate->dir_hash, (hashmap_cmp_fn) dir_entry_cmp,
-			istate->cache_nr);
+			NULL, istate->cache_nr);
 
 	if (lookup_lazy_params(istate)) {
 		hashmap_disallow_rehash(&istate->dir_hash, 1);
oidset.c +3 -2
index ac169f05d3..f20e6e4df3 100644
@@ -7,7 +7,8 @@ struct oidset_entry {
 };
 
 static int oidset_hashcmp(const void *va, const void *vb,
-			  const void *vkey)
+			  const void *vkey,
+			  const void *unused_data)
 {
 	const struct oidset_entry *a = va, *b = vb;
 	const struct object_id *key = vkey;
@@ -30,7 +31,7 @@ int oidset_insert(struct oidset *set, const struct object_id *oid)
 	struct oidset_entry *entry;
 
 	if (!set->map.cmpfn)
-		hashmap_init(&set->map, oidset_hashcmp, 0);
+		hashmap_init(&set->map, oidset_hashcmp, NULL, 0);
 
 	if (oidset_contains(set, oid))
 		return 1;
patch-ids.c +4 -2
index 9c0ab9e67a..b9b2ebbad0 100644
@@ -37,6 +37,7 @@ int commit_patch_id(struct commit *commit, struct diff_options *options,
  */
 static int patch_id_cmp(struct patch_id *a,
 			struct patch_id *b,
+			const void *unused_keydata,
 			struct diff_options *opt)
 {
 	if (is_null_oid(&a->patch_id) &&
@@ -57,7 +58,8 @@ int init_patch_ids(struct patch_ids *ids)
 	ids->diffopts.detect_rename = 0;
 	DIFF_OPT_SET(&ids->diffopts, RECURSIVE);
 	diff_setup_done(&ids->diffopts);
-	hashmap_init(&ids->patches, (hashmap_cmp_fn)patch_id_cmp, 256);
+	hashmap_init(&ids->patches, (hashmap_cmp_fn)patch_id_cmp,
+		     &ids->diffopts, 256);
 	return 0;
 }
 
@@ -93,7 +95,7 @@ struct patch_id *has_commit_patch_id(struct commit *commit,
 	if (init_patch_id_entry(&patch, commit, ids))
 		return NULL;
 
-	return hashmap_get(&ids->patches, &patch, &ids->diffopts);
+	return hashmap_get(&ids->patches, &patch, NULL);
 }
 
 struct patch_id *add_commit_patch_id(struct commit *commit,
refs.c +2 -2
index 88658ba769..71e0170feb 100644
@@ -1526,7 +1526,7 @@ struct ref_store_hash_entry
 };
 
 static int ref_store_hash_cmp(const void *entry, const void *entry_or_key,
-			      const void *keydata)
+			      const void *keydata, const void *unused_data)
 {
 	const struct ref_store_hash_entry *e1 = entry, *e2 = entry_or_key;
 	const char *name = keydata ? keydata : e2->name;
@@ -1608,7 +1608,7 @@ static void register_ref_store_map(struct hashmap *map,
 				   const char *name)
 {
 	if (!map->tablesize)
-		hashmap_init(map, ref_store_hash_cmp, 0);
+		hashmap_init(map, ref_store_hash_cmp, NULL, 0);
 
 	if (hashmap_put(map, alloc_ref_store_hash_entry(name, refs)))
 		die("BUG: %s ref_store '%s' initialized twice", type, name);
remote.c +5 -2
index d87482573d..b5e05890ee 100644
@@ -133,7 +133,10 @@ struct remotes_hash_key {
 	int len;
 };
 
-static int remotes_hash_cmp(const struct remote *a, const struct remote *b, const struct remotes_hash_key *key)
+static int remotes_hash_cmp(const struct remote *a,
+			    const struct remote *b,
+			    const struct remotes_hash_key *key,
+			    const void *unused_data)
 {
 	if (key)
 		return strncmp(a->name, key->str, key->len) || a->name[key->len];
@@ -144,7 +147,7 @@ static int remotes_hash_cmp(const struct remote *a, const struct remote *b, cons
 static inline void init_remotes_hash(void)
 {
 	if (!remotes_hash.cmpfn)
-		hashmap_init(&remotes_hash, (hashmap_cmp_fn)remotes_hash_cmp, 0);
+		hashmap_init(&remotes_hash, (hashmap_cmp_fn)remotes_hash_cmp, NULL, 0);
 }
 
 static struct remote *make_remote(const char *name, int len)
sha1_file.c +3 -2
index fb1fd809dc..b748db31ed 100644
@@ -2390,7 +2390,8 @@ static int delta_base_cache_key_eq(const struct delta_base_cache_key *a,
 }
 
 static int delta_base_cache_hash_cmp(const void *va, const void *vb,
-				     const void *vkey)
+				     const void *vkey,
+				     const void *unused_data)
 {
 	const struct delta_base_cache_entry *a = va, *b = vb;
 	const struct delta_base_cache_key *key = vkey;
@@ -2472,7 +2473,7 @@ static void add_delta_base_cache(struct packed_git *p, off_t base_offset,
 	list_add_tail(&ent->lru, &delta_base_cache_lru);
 
 	if (!delta_base_cache.cmpfn)
-		hashmap_init(&delta_base_cache, delta_base_cache_hash_cmp, 0);
+		hashmap_init(&delta_base_cache, delta_base_cache_hash_cmp, NULL, 0);
 	hashmap_entry_init(ent, pack_entry_hash(p, base_offset));
 	hashmap_add(&delta_base_cache, ent);
 }
sub-process.c +3 -2
index 92f8aea70a..68b53cf48a 100644
@@ -6,8 +6,9 @@
 #include "pkt-line.h"
 
 int cmd2process_cmp(const struct subprocess_entry *e1,
-			   const struct subprocess_entry *e2,
-			   const void *unused)
+		    const struct subprocess_entry *e2,
+		    const void *unused_keydata,
+		    const void *unused_data)
 {
 	return strcmp(e1->cmd, e2->cmd);
 }
sub-process.h +4 -2
index d9a45cd359..a19b4d9936 100644
@@ -20,8 +20,10 @@ struct subprocess_entry {
 
 /* subprocess functions */
 
-int cmd2process_cmp(const struct subprocess_entry *e1,
-	const struct subprocess_entry *e2, const void *unused);
+extern int cmd2process_cmp(const struct subprocess_entry *e1,
+			   const struct subprocess_entry *e2,
+			   const void *unused_keydata,
+			   const void *unused_data);
 
 typedef int(*subprocess_start_fn)(struct subprocess_entry *entry);
 int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, const char *cmd,
submodule-config.c +6 -4
index d8f8d5ea32..5c2fc489b8 100644
@@ -36,7 +36,8 @@ static int is_cache_init;
 
 static int config_path_cmp(const struct submodule_entry *a,
 			   const struct submodule_entry *b,
-			   const void *unused)
+			   const void *unused_keydata,
+			   const void *unused_data)
 {
 	return strcmp(a->config->path, b->config->path) ||
 	       hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
@@ -44,7 +45,8 @@ static int config_path_cmp(const struct submodule_entry *a,
 
 static int config_name_cmp(const struct submodule_entry *a,
 			   const struct submodule_entry *b,
-			   const void *unused)
+			   const void *unused_keydata,
+			   const void *unused_data)
 {
 	return strcmp(a->config->name, b->config->name) ||
 	       hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
@@ -52,8 +54,8 @@ static int config_name_cmp(const struct submodule_entry *a,
 
 static void cache_init(struct submodule_cache *cache)
 {
-	hashmap_init(&cache->for_path, (hashmap_cmp_fn) config_path_cmp, 0);
-	hashmap_init(&cache->for_name, (hashmap_cmp_fn) config_name_cmp, 0);
+	hashmap_init(&cache->for_path, (hashmap_cmp_fn) config_path_cmp, NULL, 0);
+	hashmap_init(&cache->for_name, (hashmap_cmp_fn) config_name_cmp, NULL, 0);
 }
 
 static void free_one_config(struct submodule_entry *entry)
t/helper/test-hashmap.c +10 -5
index 7aa9440e27..72cdc4a6b1 100644
@@ -14,13 +14,17 @@ static const char *get_value(const struct test_entry *e)
 }
 
 static int test_entry_cmp(const struct test_entry *e1,
-		const struct test_entry *e2, const char* key)
+			  const struct test_entry *e2,
+			  const char* key,
+			  const void *unused_data)
 {
 	return strcmp(e1->key, key ? key : e2->key);
 }
 
 static int test_entry_cmp_icase(const struct test_entry *e1,
-		const struct test_entry *e2, const char* key)
+				const struct test_entry *e2,
+				const char* key,
+				const void *unused_data)
 {
 	return strcasecmp(e1->key, key ? key : e2->key);
 }
@@ -92,7 +96,8 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
 	if (method & TEST_ADD) {
 		/* test adding to the map */
 		for (j = 0; j < rounds; j++) {
-			hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp, 0);
+			hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp,
+				     NULL, 0);
 
 			/* add entries */
 			for (i = 0; i < TEST_SIZE; i++) {
@@ -104,7 +109,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
 		}
 	} else {
 		/* test map lookups */
-		hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp, 0);
+		hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp, NULL, 0);
 
 		/* fill the map (sparsely if specified) */
 		j = (method & TEST_SPARSE) ? TEST_SIZE / 10 : TEST_SIZE;
@@ -147,7 +152,7 @@ int cmd_main(int argc, const char **argv)
 	/* init hash map */
 	icase = argc > 1 && !strcmp("ignorecase", argv[1]);
 	hashmap_init(&map, (hashmap_cmp_fn) (icase ? test_entry_cmp_icase
-			: test_entry_cmp), 0);
+			: test_entry_cmp), NULL, 0);
 
 	/* process commands from stdin */
 	while (fgets(line, sizeof(line), stdin)) {
Signature · 1 line
2.13.0.31.g9b732c453e
#12 lore raw

Re: [PATCHv2 1/2] hashmap.h: compare function has access to a data field

Quoted 3 lines · Stefan Beller

When using the hashmap a common need is to have access to arbitrary data in the compare function. A couple of times we abuse the keydata field to pass in the data needed. This happens for example in patch-ids.c.

It is not "arbitrary data"; it is very important to streess that we are not just passing random crud, but adding a mechanism to tailor/curry the function in a way that is fixed throughout the lifetime of a hashmap.

hashmap.h struct hashmap_entry { · line 36

Quoted 10 lines
hashmap.h +1 -1
index de6022a3a9..1c26bbad5b 100644
@@ -33,11 +33,12 @@ struct hashmap_entry {
 };
 
 typedef int (*hashmap_cmp_fn)(const void *entry, const void *entry_or_key,
-		const void *keydata);
+		const void *keydata, const void *cbdata);

As I view the new "data" thing the C's (read: poor-man's) way to cutomize the function, I would have tweaked the function signature by giving the customization data at the front, i.e.

	fn(fndata, entry, entry_or_key, keydata);

That would hopefully make it more obvious that the new thing is pairs with fn, not with other arguments (and entry-or-key and keydata pairs, instead of three old arguments standing as equals).

As I think the way we wish to be able to express it in a better language would have been something like:

(partial_apply(fn, fndata))(entry, entry_or_key, keydata)

that order would match what is going on better.

#13 lore raw

Re: [PATCHv2 1/2] hashmap.h: compare function has access to a data field

Quoted 10 lines · Junio C Hamano
Quoted 3 lines · Stefan Beller

When using the hashmap a common need is to have access to arbitrary data in the compare function. A couple of times we abuse the keydata field to pass in the data needed. This happens for example in patch-ids.c.

It is not "arbitrary data"; it is very important to streess that we are not just passing random crud, but adding a mechanism to tailor/curry the function in a way that is fixed throughout the lifetime of a hashmap.

Ah yes, I forgot to fix patch1, when spending all time on the docs in p2.

Quoted 16 lines

hashmap.h struct hashmap_entry { · line 36

Quoted 11 lines
hashmap.h +1 -1
index de6022a3a9..1c26bbad5b 100644
@@ -33,11 +33,12 @@ struct hashmap_entry {
 };
 
 typedef int (*hashmap_cmp_fn)(const void *entry, const void *entry_or_key,
-             const void *keydata);
+             const void *keydata, const void *cbdata);

As I view the new "data" thing the C's (read: poor-man's) way to cutomize the function, I would have tweaked the function signature by giving the customization data at the front, i.e.

Quoted 5 lines
        fn(fndata, entry, entry_or_key, keydata);

That would hopefully make it more obvious that the new thing is pairs with fn, not with other arguments (and entry-or-key and keydata pairs, instead of three old arguments standing as equals).

Ok, let me redo the patch to have fndata at the front.

Looking at other places (that have a similar mechanism mechanically, but are semantically different), such as the callback functions for the diff machinery, we have the user provided pointer at the end of the list. But that is because the diff_options are the objects that should be in front row (they are bound to the callback more than some caller needed data).

typedef void (*diff_format_fn_t)(struct diff_queue_struct *q,
struct diff_options *options, void *data);

typedef struct strbuf *(*diff_prefix_fn_t)(struct diff_options
*opt, void *data);

Thanks!

Quoted 6 lines

As I think the way we wish to be able to express it in a better language would have been something like:

(partial_apply(fn, fndata))(entry, entry_or_key, keydata)

that order would match what is going on better.

#14 lore raw

Re: [PATCHv2 1/2] hashmap.h: compare function has access to a data field

Quoted 8 lines · Stefan Beller

Ok, let me redo the patch to have fndata at the front.

Looking at other places (that have a similar mechanism mechanically, but are semantically different), such as the callback functions for the diff machinery, we have the user provided pointer at the end of the list. But that is because the diff_options are the objects that should be in front row (they are bound to the callback more than some caller needed data).

Quite honestly, I do not care too deeply about an API specific to a particular area like "diff" that passes its "configuration" data that everybody in the API knows, i.e. diff_options. If the convention for ordinary functions in the API is to pass that in a particular location in the parameter list, I would think it is good for a application-supplied callback function to follow that pattern. After all, it is to configure the behaviour of the "diff" and the caller-supplied callback could have been part of a (hypothetically richer) API implementation.

But I view a comparison function that is given to hashmap that is supplied by the caller a bit differently. It is not about "hashing", so the reason to have the data close to function pointer is stronger there.

#15 lore raw

Re: [PATCHv2 1/2] hashmap.h: compare function has access to a data field

Quoted 21 lines · Junio C Hamano
Quoted 8 lines · Stefan Beller

Ok, let me redo the patch to have fndata at the front.

Looking at other places (that have a similar mechanism mechanically, but are semantically different), such as the callback functions for the diff machinery, we have the user provided pointer at the end of the list. But that is because the diff_options are the objects that should be in front row (they are bound to the callback more than some caller needed data).

Quite honestly, I do not care too deeply about an API specific to a particular area like "diff" that passes its "configuration" data that everybody in the API knows, i.e. diff_options. If the convention for ordinary functions in the API is to pass that in a particular location in the parameter list, I would think it is good for a application-supplied callback function to follow that pattern. After all, it is to configure the behaviour of the "diff" and the caller-supplied callback could have been part of a (hypothetically richer) API implementation.

Quoted 4 lines

But I view a comparison function that is given to hashmap that is supplied by the caller a bit differently. It is not about "hashing", so the reason to have the data close to function pointer is stronger there.

Yes I agree with that, I forgot to say so.

I just cited the example to see if we have a preference in the project already and that's about it, but as it is different, we can put the fndata first.

Thanks,
Stefan

#16 lore raw

Re: [PATCHv2 1/2] hashmap.h: compare function has access to a data field

In reply to Stefan Beller
Quoted 23 lines · Stefan Beller
patch-ids.c +3 -1
index 9c0ab9e67a..b9b2ebbad0 100644
@@ -37,6 +37,7 @@ int commit_patch_id(struct commit *commit, struct diff_options *options,
  */
 static int patch_id_cmp(struct patch_id *a,
 			struct patch_id *b,
+			const void *unused_keydata,
 			struct diff_options *opt)
 {
 	if (is_null_oid(&a->patch_id) &&
@@ -57,7 +58,8 @@ int init_patch_ids(struct patch_ids *ids)
 	ids->diffopts.detect_rename = 0;
 	DIFF_OPT_SET(&ids->diffopts, RECURSIVE);
 	diff_setup_done(&ids->diffopts);
-	hashmap_init(&ids->patches, (hashmap_cmp_fn)patch_id_cmp, 256);
+	hashmap_init(&ids->patches, (hashmap_cmp_fn)patch_id_cmp,
+		     &ids->diffopts, 256);
 	return 0;
 }
 

patch-ids.c struct patch_id *has_commit_patch_id(struct commit *commit, · line 99

Quoted 6 lines
patch-ids.c +1 -1
@@ -93,7 +95,7 @@ struct patch_id *has_commit_patch_id(struct commit *commit,
 	if (init_patch_id_entry(&patch, commit, ids))
 		return NULL;
 
-	return hashmap_get(&ids->patches, &patch, &ids->diffopts);
+	return hashmap_get(&ids->patches, &patch, NULL);
 }

This actually makes me wonder if we can demonstrate an existing breakage in tests. The old code used to pass NULL to the diffopts, causing it to be passed down through commit_patch_id() function down to diff_tree_oid() or diff_root_tree_oid(). When the codepath triggers the issue that Peff warned about in his old article (was it about rehashing or something?) that makes two entries compared (i.e. not using keydata, because we are not comparing an existing entry with a key and data only to see if that already exists in the hashmap), wouldn't that cause ll_diff_tree_oid() that is called from diff_tree_oid() to dereference NULL?

Thanks.

#17 lore raw

Re: [PATCHv2 1/2] hashmap.h: compare function has access to a data field

Quoted 24 lines · Junio C Hamano
Quoted 22 lines · Stefan Beller
patch-ids.c +3 -1
index 9c0ab9e67a..b9b2ebbad0 100644
@@ -37,6 +37,7 @@ int commit_patch_id(struct commit *commit, struct diff_options *options,
  */
 static int patch_id_cmp(struct patch_id *a,
                      struct patch_id *b,
+                     const void *unused_keydata,
                      struct diff_options *opt)
 {
      if (is_null_oid(&a->patch_id) &&
@@ -57,7 +58,8 @@ int init_patch_ids(struct patch_ids *ids)
      ids->diffopts.detect_rename = 0;
      DIFF_OPT_SET(&ids->diffopts, RECURSIVE);
      diff_setup_done(&ids->diffopts);
-     hashmap_init(&ids->patches, (hashmap_cmp_fn)patch_id_cmp, 256);
+     hashmap_init(&ids->patches, (hashmap_cmp_fn)patch_id_cmp,
+                  &ids->diffopts, 256);
      return 0;
 }
 
Quoted 7 lines

patch-ids.c struct patch_id *has_commit_patch_id(struct commit *commit, · line 99

Quoted 7 lines
patch-ids.c +1 -1
@@ -93,7 +95,7 @@ struct patch_id *has_commit_patch_id(struct commit *commit,
      if (init_patch_id_entry(&patch, commit, ids))
              return NULL;
 
-     return hashmap_get(&ids->patches, &patch, &ids->diffopts);
+     return hashmap_get(&ids->patches, &patch, NULL);
 }

+cc Peff

Quoted 12 lines

This actually makes me wonder if we can demonstrate an existing breakage in tests. The old code used to pass NULL to the diffopts, causing it to be passed down through commit_patch_id() function down to diff_tree_oid() or diff_root_tree_oid(). When the codepath triggers the issue that Peff warned about in his old article (was it about rehashing or something?) that makes two entries compared (i.e. not using keydata, because we are not comparing an existing entry with a key and data only to see if that already exists in the hashmap), wouldn't that cause ll_diff_tree_oid() that is called from diff_tree_oid() to dereference NULL?

Quoted 1 line

Thanks.

I am at a loss here after re-reading your answer over and over, but I think you are asking if patch_id_cmp can break, as we have a callchain like

patch_id_cmp
commit_patch_id
(diff_root_tree_oid)
diff_tree_oid
ll_diff_tree_oid

passing diff_options down there, and patch_id_cmp may have gotten NULL.

The answer is no, it was safe. (by accident?)

That is because we never use hashmap_get_next on the hashmap that uses patch_id_cmp as the compare function.

hashmap_get_next is the only function that does not pass on a keydata, any other has valid caller provided keydata.

Thanks,
Stefan

#18 lore raw

Re: [PATCHv2 1/2] hashmap.h: compare function has access to a data field

Quoted 13 lines · Stefan Beller

I am at a loss here after re-reading your answer over and over, but I think you are asking if patch_id_cmp can break, as we have a callchain like

patch_id_cmp
commit_patch_id
(diff_root_tree_oid)
diff_tree_oid
ll_diff_tree_oid

passing diff_options down there, and patch_id_cmp may have gotten NULL.

Quoted 8 lines

The answer is no, it was safe. (by accident?)

That is because we never use hashmap_get_next on the hashmap that uses patch_id_cmp as the compare function.

hashmap_get_next is the only function that does not pass on a keydata, any other has valid caller provided keydata.

Ah, thanks for clarifying. I think I misread the earlier discussion. So unless somebody goes in to patch-ids.c and adds a call to do get_next, we won't see a breakage, and it is not worth to do a test-patch-ids.c that peeks into the hashmap patch-ids.c uses and does get_next() only to demonstrate a potential future breakage.

OK.

#19 lore raw

PATCH v2 2/2 hashmap: migrate documentation from Documentation/technical into header

In reply to Stefan Beller

While at it, clarify the use of key, keydata, entry_or_key as well as documenting the new data pointer for the compare function.

---
 Documentation/technical/api-hashmap.txt | 309 ----------------------------
 hashmap.h                               | 351 +++++++++++++++++++++++++++++---
 2 files changed, 318 insertions(+), 342 deletions(-)
 delete mode 100644 Documentation/technical/api-hashmap.txt
Documentation/technical/api-hashmap.txt deleted +0 -309
deleted file mode 100644
index ccc634bbd7..0000000000
@@ -1,309 +0,0 @@
-hashmap API
-===========
-
-The hashmap API is a generic implementation of hash-based key-value mappings.
-
-Data Structures
----------------
-
-`struct hashmap`::
-
-	The hash table structure. Members can be used as follows, but should
-	not be modified directly:
-+
-The `size` member keeps track of the total number of entries (0 means the
-hashmap is empty).
-+
-`tablesize` is the allocated size of the hash table. A non-0 value indicates
-that the hashmap is initialized. It may also be useful for statistical purposes
-(i.e. `size / tablesize` is the current load factor).
-+
-`cmpfn` stores the comparison function specified in `hashmap_init()`. In
-advanced scenarios, it may be useful to change this, e.g. to switch between
-case-sensitive and case-insensitive lookup.
-+
-When `disallow_rehash` is set, automatic rehashes are prevented during inserts
-and deletes.
-
-`struct hashmap_entry`::
-
-	An opaque structure representing an entry in the hash table, which must
-	be used as first member of user data structures. Ideally it should be
-	followed by an int-sized member to prevent unused memory on 64-bit
-	systems due to alignment.
-+
-The `hash` member is the entry's hash code and the `next` member points to the
-next entry in case of collisions (i.e. if multiple entries map to the same
-bucket).
-
-`struct hashmap_iter`::
-
-	An iterator structure, to be used with hashmap_iter_* functions.
-
-Types
------
-
-`int (*hashmap_cmp_fn)(const void *entry, const void *entry_or_key, const void *keydata)`::
-
-	User-supplied function to test two hashmap entries for equality. Shall
-	return 0 if the entries are equal.
-+
-This function is always called with non-NULL `entry` / `entry_or_key`
-parameters that have the same hash code. When looking up an entry, the `key`
-and `keydata` parameters to hashmap_get and hashmap_remove are always passed
-as second and third argument, respectively. Otherwise, `keydata` is NULL.
-
-Functions
----------
-
-`unsigned int strhash(const char *buf)`::
-`unsigned int strihash(const char *buf)`::
-`unsigned int memhash(const void *buf, size_t len)`::
-`unsigned int memihash(const void *buf, size_t len)`::
-`unsigned int memihash_cont(unsigned int hash_seed, const void *buf, size_t len)`::
-
-	Ready-to-use hash functions for strings, using the FNV-1 algorithm (see
-	http://www.isthe.com/chongo/tech/comp/fnv).
-+
-`strhash` and `strihash` take 0-terminated strings, while `memhash` and
-`memihash` operate on arbitrary-length memory.
-+
-`strihash` and `memihash` are case insensitive versions.
-+
-`memihash_cont` is a variant of `memihash` that allows a computation to be
-continued with another chunk of data.
-
-`unsigned int sha1hash(const unsigned char *sha1)`::
-
-	Converts a cryptographic hash (e.g. SHA-1) into an int-sized hash code
-	for use in hash tables. Cryptographic hashes are supposed to have
-	uniform distribution, so in contrast to `memhash()`, this just copies
-	the first `sizeof(int)` bytes without shuffling any bits. Note that
-	the results will be different on big-endian and little-endian
-	platforms, so they should not be stored or transferred over the net.
-
-`void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function, size_t initial_size)`::
-
-	Initializes a hashmap structure.
-+
-`map` is the hashmap to initialize.
-+
-The `equals_function` can be specified to compare two entries for equality.
-If NULL, entries are considered equal if their hash codes are equal.
-+
-If the total number of entries is known in advance, the `initial_size`
-parameter may be used to preallocate a sufficiently large table and thus
-prevent expensive resizing. If 0, the table is dynamically resized.
-
-`void hashmap_free(struct hashmap *map, int free_entries)`::
-
-	Frees a hashmap structure and allocated memory.
-+
-`map` is the hashmap to free.
-+
-If `free_entries` is true, each hashmap_entry in the map is freed as well
-(using stdlib's free()).
-
-`void hashmap_entry_init(void *entry, unsigned int hash)`::
-
-	Initializes a hashmap_entry structure.
-+
-`entry` points to the entry to initialize.
-+
-`hash` is the hash code of the entry.
-+
-The hashmap_entry structure does not hold references to external resources,
-and it is safe to just discard it once you are done with it (i.e. if
-your structure was allocated with xmalloc(), you can just free(3) it,
-and if it is on stack, you can just let it go out of scope).
-
-`void *hashmap_get(const struct hashmap *map, const void *key, const void *keydata)`::
-
-	Returns the hashmap entry for the specified key, or NULL if not found.
-+
-`map` is the hashmap structure.
-+
-`key` is a hashmap_entry structure (or user data structure that starts with
-hashmap_entry) that has at least been initialized with the proper hash code
-(via `hashmap_entry_init`).
-+
-If an entry with matching hash code is found, `key` and `keydata` are passed
-to `hashmap_cmp_fn` to decide whether the entry matches the key.
-
-`void *hashmap_get_from_hash(const struct hashmap *map, unsigned int hash, const void *keydata)`::
-
-	Returns the hashmap entry for the specified hash code and key data,
-	or NULL if not found.
-+
-`map` is the hashmap structure.
-+
-`hash` is the hash code of the entry to look up.
-+
-If an entry with matching hash code is found, `keydata` is passed to
-`hashmap_cmp_fn` to decide whether the entry matches the key. The
-`entry_or_key` parameter points to a bogus hashmap_entry structure that
-should not be used in the comparison.
-
-`void *hashmap_get_next(const struct hashmap *map, const void *entry)`::
-
-	Returns the next equal hashmap entry, or NULL if not found. This can be
-	used to iterate over duplicate entries (see `hashmap_add`).
-+
-`map` is the hashmap structure.
-+
-`entry` is the hashmap_entry to start the search from, obtained via a previous
-call to `hashmap_get` or `hashmap_get_next`.
-
-`void hashmap_add(struct hashmap *map, void *entry)`::
-
-	Adds a hashmap entry. This allows to add duplicate entries (i.e.
-	separate values with the same key according to hashmap_cmp_fn).
-+
-`map` is the hashmap structure.
-+
-`entry` is the entry to add.
-
-`void *hashmap_put(struct hashmap *map, void *entry)`::
-
-	Adds or replaces a hashmap entry. If the hashmap contains duplicate
-	entries equal to the specified entry, only one of them will be replaced.
-+
-`map` is the hashmap structure.
-+
-`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_remove(struct hashmap *map, const void *key, const void *keydata)`::
-
-	Removes a hashmap entry matching the specified key. If the hashmap
-	contains duplicate entries equal to the specified key, only one of
-	them will be removed.
-+
-`map` is the hashmap structure.
-+
-`key` is a hashmap_entry structure (or user data structure that starts with
-hashmap_entry) that has at least been initialized with the proper hash code
-(via `hashmap_entry_init`).
-+
-If an entry with matching hash code is found, `key` and `keydata` are
-passed to `hashmap_cmp_fn` to decide whether the entry matches the key.
-+
-Returns the removed entry, or NULL if not found.
-
-`void hashmap_disallow_rehash(struct hashmap *map, unsigned value)`::
-
-	Disallow/allow automatic rehashing of the hashmap during inserts
-	and deletes.
-+
-This is useful if the caller knows that the hashmap will be accessed
-by multiple threads.
-+
-The caller is still responsible for any necessary locking; this simply
-prevents unexpected rehashing.  The caller is also responsible for properly
-sizing the initial hashmap to ensure good performance.
-+
-A call to allow rehashing does not force a rehash; that might happen
-with the next insert or delete.
-
-`void hashmap_iter_init(struct hashmap *map, struct hashmap_iter *iter)`::
-`void *hashmap_iter_next(struct hashmap_iter *iter)`::
-`void *hashmap_iter_first(struct hashmap *map, struct hashmap_iter *iter)`::
-
-	Used to iterate over all entries of a hashmap. Note that it is
-	not safe to add or remove entries to the hashmap while
-	iterating.
-+
-`hashmap_iter_init` initializes a `hashmap_iter` structure.
-+
-`hashmap_iter_next` returns the next hashmap_entry, or NULL if there are no
-more entries.
-+
-`hashmap_iter_first` is a combination of both (i.e. initializes the iterator
-and returns the first entry, if any).
-
-`const char *strintern(const char *string)`::
-`const void *memintern(const void *data, size_t len)`::
-
-	Returns the unique, interned version of the specified string or data,
-	similar to the `String.intern` API in Java and .NET, respectively.
-	Interned strings remain valid for the entire lifetime of the process.
-+
-Can be used as `[x]strdup()` or `xmemdupz` replacement, except that interned
-strings / data must not be modified or freed.
-+
-Interned strings are best used for short strings with high probability of
-duplicates.
-+
-Uses a hashmap to store the pool of interned strings.
-
-Usage example
--------------
-
-Here's a simple usage example that maps long keys to double values.
-------------
-struct hashmap map;
-
-struct long2double {
-	struct hashmap_entry ent; /* must be the first member! */
-	long key;
-	double value;
-};
-
-static int long2double_cmp(const struct long2double *e1, const struct long2double *e2, const void *unused)
-{
-	return !(e1->key == e2->key);
-}
-
-void long2double_init(void)
-{
-	hashmap_init(&map, (hashmap_cmp_fn) long2double_cmp, 0);
-}
-
-void long2double_free(void)
-{
-	hashmap_free(&map, 1);
-}
-
-static struct long2double *find_entry(long key)
-{
-	struct long2double k;
-	hashmap_entry_init(&k, memhash(&key, sizeof(long)));
-	k.key = key;
-	return hashmap_get(&map, &k, NULL);
-}
-
-double get_value(long key)
-{
-	struct long2double *e = find_entry(key);
-	return e ? e->value : 0;
-}
-
-void set_value(long key, double value)
-{
-	struct long2double *e = find_entry(key);
-	if (!e) {
-		e = malloc(sizeof(struct long2double));
-		hashmap_entry_init(e, memhash(&key, sizeof(long)));
-		e->key = key;
-		hashmap_add(&map, e);
-	}
-	e->value = value;
-}
-------------
-
-Using variable-sized keys
--------------------------
-
-The `hashmap_entry_get` and `hashmap_entry_remove` functions expect an ordinary
-`hashmap_entry` structure as key to find the correct entry. If the key data is
-variable-sized (e.g. a FLEX_ARRAY string) or quite large, it is undesirable
-to create a full-fledged entry structure on the heap and copy all the key data
-into the structure.
-
-In this case, the `keydata` parameter can be used to pass
-variable-sized key data directly to the comparison function, and the `key`
-parameter can be a stripped-down, fixed size entry structure allocated on the
-stack.
-
-See test-hashmap.c for an example using arbitrary-length strings as keys.
hashmap.h +318 -33

432 lines not on this page · raw message

Signature · 1 line
2.13.0.31.g9b732c453e
#20 lore raw

v3 cover Introduce data field in hashmap and migrate docs to header

In reply to Stefan Beller

v3:

* fixed first patch to call the data 'caller provided' instead of arbitrary.
* moved the position of the caller provided data to first position
* split up the rather mechanical change of function signature with fixing the API usage of patch-ids.c

v2:

addressed all but the last point of Jonathan Nieder.

Thanks,
Stefan

v1:

https://public-inbox.org/git/[email protected]/
for context why we need a new data field. Implement that.

Once upon a time we had a long discussion where to put documentation best. The answer was header files as there documentation has less chance to become stale and be out of date. Improve the docs by
* migrating them to the header
* clarifying how the compare function is to be used
* how the arguments to hashmap_get/remove should be used.

Thanks,
Stefan

Stefan Beller (3):
hashmap.h: compare function has access to a data field patch-ids.c: use hashmap correctly
hashmap: migrate documentation from Documentation/technical into
header

 Documentation/technical/api-hashmap.txt | 309 ---------------------------
 attr.c                                  |   7 +-
 builtin/describe.c                      |   8 +-
 builtin/difftool.c                      |  24 ++-
 builtin/fast-export.c                   |   7 +-
 config.c                                |   9 +-
 convert.c                               |   3 +-
 diffcore-rename.c                       |   2 +-
 hashmap.c                               |  17 +-
 hashmap.h                               | 360 ++++++++++++++++++++++++++++----
 name-hash.c                             |  16 +-
 oidset.c                                |   5 +-
 patch-ids.c                             |  10 +-
 refs.c                                  |   5 +-
 remote.c                                |   7 +-
 sha1_file.c                             |   5 +-
 sub-process.c                           |   7 +-
 sub-process.h                           |   6 +-
 submodule-config.c                      |  14 +-
 t/helper/test-hashmap.c                 |  19 +-
 20 files changed, 431 insertions(+), 409 deletions(-)
 delete mode 100644 Documentation/technical/api-hashmap.txt
Signature · 1 line
2.13.0.31.g9b732c453e
#21 lore raw

PATCH v3 1/3 hashmap.h: compare function has access to a data field

When using the hashmap a common need is to have access to caller provided data in the compare function. A couple of times we abuse the keydata field to pass in the data needed. This happens for example in patch-ids.c.

This patch changes the function signature of the compare function to have one more void pointer available. The pointer given for each invocation of the compare function must be defined in the init function of the hashmap and is just passed through.

Documentation of this new feature is deferred to a later patch. This is a rather mechanical conversion, just adding the new pass-through parameter. However while at it improve the naming of the fields of all compare functions used by hashmaps by ensuring unused parameters are prefixed with 'unused_' and naming the parameters what they are (instead of 'unused' make it 'unused_keydata').

---
 attr.c                  |  7 ++++---
 builtin/describe.c      |  8 +++++---
 builtin/difftool.c      | 24 +++++++++++++++---------
 builtin/fast-export.c   |  7 ++++---
 config.c                |  9 ++++++---
 convert.c               |  3 ++-
 diffcore-rename.c       |  2 +-
 hashmap.c               | 17 ++++++++++++-----
 hashmap.h               | 12 ++++++++----
 name-hash.c             | 16 ++++++++++------
 oidset.c                |  5 +++--
 patch-ids.c             |  6 ++++--
 refs.c                  |  5 +++--
 remote.c                |  7 +++++--
 sha1_file.c             |  5 +++--
 sub-process.c           |  7 ++++---
 sub-process.h           |  6 ++++--
 submodule-config.c      | 14 ++++++++------
 t/helper/test-hashmap.c | 19 ++++++++++++-------
 19 files changed, 113 insertions(+), 66 deletions(-)
attr.c +4 -3
index 37454999d2..56961f0236 100644
@@ -76,9 +76,10 @@ struct attr_hash_entry {
 };
 
 /* attr_hashmap comparison function */
-static int attr_hash_entry_cmp(const struct attr_hash_entry *a,
+static int attr_hash_entry_cmp(void *unused_cmp_data,
+			       const struct attr_hash_entry *a,
 			       const struct attr_hash_entry *b,
-			       void *unused)
+			       void *unused_keydata)
 {
 	return (a->keylen != b->keylen) || strncmp(a->key, b->key, a->keylen);
 }
@@ -86,7 +87,7 @@ static int attr_hash_entry_cmp(const struct attr_hash_entry *a,
 /* Initialize an 'attr_hashmap' object */
 static void attr_hashmap_init(struct attr_hashmap *map)
 {
-	hashmap_init(&map->map, (hashmap_cmp_fn) attr_hash_entry_cmp, 0);
+	hashmap_init(&map->map, (hashmap_cmp_fn) attr_hash_entry_cmp, NULL, 0);
 }
 
 /*
builtin/describe.c +5 -3
index 70eb144608..8868f00ed0 100644
@@ -54,8 +54,10 @@ static const char *prio_names[] = {
 	N_("head"), N_("lightweight"), N_("annotated"),
 };
 
-static int commit_name_cmp(const struct commit_name *cn1,
-		const struct commit_name *cn2, const void *peeled)
+static int commit_name_cmp(const void *unused_cmp_data,
+			   const struct commit_name *cn1,
+			   const struct commit_name *cn2,
+			   const void *peeled)
 {
 	return oidcmp(&cn1->peeled, peeled ? peeled : &cn2->peeled);
 }
@@ -501,7 +503,7 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
 		return cmd_name_rev(args.argc, args.argv, prefix);
 	}
 
-	hashmap_init(&names, (hashmap_cmp_fn) commit_name_cmp, 0);
+	hashmap_init(&names, (hashmap_cmp_fn) commit_name_cmp, NULL, 0);
 	for_each_rawref(get_name, NULL);
 	if (!names.size && !always)
 		die(_("No names found, cannot describe anything."));
builtin/difftool.c +15 -9
index 9199227f6e..a1a26ba891 100644
@@ -130,8 +130,10 @@ struct working_tree_entry {
 	char path[FLEX_ARRAY];
 };
 
-static int working_tree_entry_cmp(struct working_tree_entry *a,
-				  struct working_tree_entry *b, void *keydata)
+static int working_tree_entry_cmp(const void *unused_cmp_data,
+				  struct working_tree_entry *a,
+				  struct working_tree_entry *b,
+				  void *unused_keydata)
 {
 	return strcmp(a->path, b->path);
 }
@@ -146,7 +148,9 @@ struct pair_entry {
 	const char path[FLEX_ARRAY];
 };
 
-static int pair_cmp(struct pair_entry *a, struct pair_entry *b, void *keydata)
+static int pair_cmp(const void *unused_cmp_data,
+		    struct pair_entry *a, struct pair_entry *b,
+		    void *unused_keydata)
 {
 	return strcmp(a->path, b->path);
 }
@@ -174,7 +178,9 @@ struct path_entry {
 	char path[FLEX_ARRAY];
 };
 
-static int path_entry_cmp(struct path_entry *a, struct path_entry *b, void *key)
+static int path_entry_cmp(const void *unused_cmp_data,
+			  struct path_entry *a, struct path_entry *b,
+			  void *key)
 {
 	return strcmp(a->path, key ? key : b->path);
 }
@@ -367,9 +373,9 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
 	wtdir_len = wtdir.len;
 
 	hashmap_init(&working_tree_dups,
-		     (hashmap_cmp_fn)working_tree_entry_cmp, 0);
-	hashmap_init(&submodules, (hashmap_cmp_fn)pair_cmp, 0);
-	hashmap_init(&symlinks2, (hashmap_cmp_fn)pair_cmp, 0);
+		     (hashmap_cmp_fn)working_tree_entry_cmp, NULL, 0);
+	hashmap_init(&submodules, (hashmap_cmp_fn)pair_cmp, NULL, 0);
+	hashmap_init(&symlinks2, (hashmap_cmp_fn)pair_cmp, NULL, 0);
 
 	child.no_stdin = 1;
 	child.git_cmd = 1;
@@ -580,9 +586,9 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
 	 * files through the symlink.
 	 */
 	hashmap_init(&wt_modified, (hashmap_cmp_fn)path_entry_cmp,
-		     wtindex.cache_nr);
+		     NULL, wtindex.cache_nr);
 	hashmap_init(&tmp_modified, (hashmap_cmp_fn)path_entry_cmp,
-		     wtindex.cache_nr);
+		     NULL, wtindex.cache_nr);
 
 	for (i = 0; i < wtindex.cache_nr; i++) {
 		struct hashmap_entry dummy;
builtin/fast-export.c +4 -3
index 12d501bfde..d412c0a8f3 100644
@@ -93,8 +93,9 @@ struct anonymized_entry {
 	size_t anon_len;
 };
 
-static int anonymized_entry_cmp(const void *va, const void *vb,
-				const void *data)
+static int anonymized_entry_cmp(const void *unused_cmp_data,
+				const void *va, const void *vb,
+				const void *unused_keydata)
 {
 	const struct anonymized_entry *a = va, *b = vb;
 	return a->orig_len != b->orig_len ||
@@ -113,7 +114,7 @@ static const void *anonymize_mem(struct hashmap *map,
 	struct anonymized_entry key, *ret;
 
 	if (!map->cmpfn)
-		hashmap_init(map, anonymized_entry_cmp, 0);
+		hashmap_init(map, anonymized_entry_cmp, NULL, 0);
 
 	hashmap_entry_init(&key, memhash(orig, *len));
 	key.orig = orig;
config.c +6 -3
index 1cd40a5fe6..4a31e31ac3 100644
@@ -1753,15 +1753,18 @@ static int configset_add_value(struct config_set *cs, const char *key, const cha
 	return 0;
 }
 
-static int config_set_element_cmp(const struct config_set_element *e1,
-				 const struct config_set_element *e2, const void *unused)
+static int config_set_element_cmp(const void *unused_cmp_data,
+				  const struct config_set_element *e1,
+				  const struct config_set_element *e2,
+				  const void *unused_keydata)
 {
 	return strcmp(e1->key, e2->key);
 }
 
 void git_configset_init(struct config_set *cs)
 {
-	hashmap_init(&cs->config_hash, (hashmap_cmp_fn)config_set_element_cmp, 0);
+	hashmap_init(&cs->config_hash, (hashmap_cmp_fn)config_set_element_cmp,
+		     NULL, 0);
 	cs->hash_initialized = 1;
 	cs->list.nr = 0;
 	cs->list.alloc = 0;
convert.c +2 -1
index 7d2a519daf..deaf0ba7b3 100644
@@ -583,7 +583,8 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t len
 
 	if (!subprocess_map_initialized) {
 		subprocess_map_initialized = 1;
-		hashmap_init(&subprocess_map, (hashmap_cmp_fn) cmd2process_cmp, 0);
+		hashmap_init(&subprocess_map, (hashmap_cmp_fn) cmd2process_cmp,
+			     NULL, 0);
 		entry = NULL;
 	} else {
 		entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd);
diffcore-rename.c +1 -1
index 1e4678b7d7..786f389498 100644
@@ -341,7 +341,7 @@ static int find_exact_renames(struct diff_options *options)
 	/* Add all sources to the hash table in reverse order, because
 	 * later on they will be retrieved in LIFO order.
 	 */
-	hashmap_init(&file_table, NULL, rename_src_nr);
+	hashmap_init(&file_table, NULL, NULL, rename_src_nr);
 	for (i = rename_src_nr-1; i >= 0; i--)
 		insert_file_table(&file_table, i, rename_src[i].p->one);
 
hashmap.c +12 -5
index 7d1044eb5d..9b6a12859b 100644
@@ -95,7 +95,9 @@ static inline int entry_equals(const struct hashmap *map,
 		const struct hashmap_entry *e1, const struct hashmap_entry *e2,
 		const void *keydata)
 {
-	return (e1 == e2) || (e1->hash == e2->hash && !map->cmpfn(e1, e2, keydata));
+	return (e1 == e2) ||
+	       (e1->hash == e2->hash &&
+		!map->cmpfn(map->cmpfn_data, e1, e2, keydata));
 }
 
 static inline unsigned int bucket(const struct hashmap *map,
@@ -140,19 +142,23 @@ static inline struct hashmap_entry **find_entry_ptr(const struct hashmap *map,
 	return e;
 }
 
-static int always_equal(const void *unused1, const void *unused2, const void *unused3)
+static int always_equal(const void *unused_cmp_data,
+			const void *unused1,
+			const void *unused2,
+			const void *unused_keydata)
 {
 	return 0;
 }
 
 void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function,
-		size_t initial_size)
+		const void *cmpfn_data, size_t initial_size)
 {
 	unsigned int size = HASHMAP_INITIAL_SIZE;
 
 	memset(map, 0, sizeof(*map));
 
 	map->cmpfn = equals_function ? equals_function : always_equal;
+	map->cmpfn_data = cmpfn_data;
 
 	/* calculate initial table size and allocate the table */
 	initial_size = (unsigned int) ((uint64_t) initial_size * 100
@@ -260,7 +266,8 @@ struct pool_entry {
 	unsigned char data[FLEX_ARRAY];
 };
 
-static int pool_entry_cmp(const struct pool_entry *e1,
+static int pool_entry_cmp(const void *unused_cmp_data,
+			  const struct pool_entry *e1,
 			  const struct pool_entry *e2,
 			  const unsigned char *keydata)
 {
@@ -275,7 +282,7 @@ const void *memintern(const void *data, size_t len)
 
 	/* initialize string pool hashmap */
 	if (!map.tablesize)
-		hashmap_init(&map, (hashmap_cmp_fn) pool_entry_cmp, 0);
+		hashmap_init(&map, (hashmap_cmp_fn) pool_entry_cmp, NULL, 0);
 
 	/* lookup interned string in pool */
 	hashmap_entry_init(&key, memhash(data, len));
hashmap.h +8 -4
index de6022a3a9..aaf09e047e 100644
@@ -32,12 +32,14 @@ struct hashmap_entry {
 	unsigned int hash;
 };
 
-typedef int (*hashmap_cmp_fn)(const void *entry, const void *entry_or_key,
-		const void *keydata);
+typedef int (*hashmap_cmp_fn)(const void *hashmap_cmp_fn_data,
+			      const void *entry, const void *entry_or_key,
+			      const void *keydata);
 
 struct hashmap {
 	struct hashmap_entry **table;
 	hashmap_cmp_fn cmpfn;
+	const void *cmpfn_data;
 	unsigned int size, tablesize, grow_at, shrink_at;
 	unsigned disallow_rehash : 1;
 };
@@ -50,8 +52,10 @@ struct hashmap_iter {
 
 /* hashmap functions */
 
-extern void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function,
-		size_t initial_size);
+extern void hashmap_init(struct hashmap *map,
+			 hashmap_cmp_fn equals_function,
+			 const void *equals_function_data,
+			 size_t initial_size);
 extern void hashmap_free(struct hashmap *map, int free_entries);
 
 /* hashmap_entry functions */
name-hash.c +10 -6
index 39309efb7f..0e10f3eab8 100644
@@ -16,8 +16,10 @@ struct dir_entry {
 	char name[FLEX_ARRAY];
 };
 
-static int dir_entry_cmp(const struct dir_entry *e1,
-		const struct dir_entry *e2, const char *name)
+static int dir_entry_cmp(const void *unused_cmp_data,
+			 const struct dir_entry *e1,
+			 const struct dir_entry *e2,
+			 const char *name)
 {
 	return e1->namelen != e2->namelen || strncasecmp(e1->name,
 			name ? name : e2->name, e1->namelen);
@@ -107,8 +109,10 @@ static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
 		add_dir_entry(istate, ce);
 }
 
-static int cache_entry_cmp(const struct cache_entry *ce1,
-		const struct cache_entry *ce2, const void *remove)
+static int cache_entry_cmp(const void *unused_cmp_data,
+			   const struct cache_entry *ce1,
+			   const struct cache_entry *ce2,
+			   const void *remove)
 {
 	/*
 	 * For remove_name_hash, find the exact entry (pointer equality); for
@@ -571,9 +575,9 @@ static void lazy_init_name_hash(struct index_state *istate)
 	if (istate->name_hash_initialized)
 		return;
 	hashmap_init(&istate->name_hash, (hashmap_cmp_fn) cache_entry_cmp,
-			istate->cache_nr);
+			NULL, istate->cache_nr);
 	hashmap_init(&istate->dir_hash, (hashmap_cmp_fn) dir_entry_cmp,
-			istate->cache_nr);
+			NULL, istate->cache_nr);
 
 	if (lookup_lazy_params(istate)) {
 		hashmap_disallow_rehash(&istate->dir_hash, 1);
oidset.c +3 -2
index ac169f05d3..a6a08ba52a 100644
@@ -6,7 +6,8 @@ struct oidset_entry {
 	struct object_id oid;
 };
 
-static int oidset_hashcmp(const void *va, const void *vb,
+static int oidset_hashcmp(const void *unused_cmp_data,
+			  const void *va, const void *vb,
 			  const void *vkey)
 {
 	const struct oidset_entry *a = va, *b = vb;
@@ -30,7 +31,7 @@ int oidset_insert(struct oidset *set, const struct object_id *oid)
 	struct oidset_entry *entry;
 
 	if (!set->map.cmpfn)
-		hashmap_init(&set->map, oidset_hashcmp, 0);
+		hashmap_init(&set->map, oidset_hashcmp, NULL, 0);
 
 	if (oidset_contains(set, oid))
 		return 1;
patch-ids.c +4 -2
index 9c0ab9e67a..815c115811 100644
@@ -35,7 +35,8 @@ int commit_patch_id(struct commit *commit, struct diff_options *options,
  * the side of safety.  The actual value being negative does not have
  * any significance; only that it is non-zero matters.
  */
-static int patch_id_cmp(struct patch_id *a,
+static int patch_id_cmp(const void *unused_cmp_data,
+			struct patch_id *a,
 			struct patch_id *b,
 			struct diff_options *opt)
 {
@@ -57,7 +58,8 @@ int init_patch_ids(struct patch_ids *ids)
 	ids->diffopts.detect_rename = 0;
 	DIFF_OPT_SET(&ids->diffopts, RECURSIVE);
 	diff_setup_done(&ids->diffopts);
-	hashmap_init(&ids->patches, (hashmap_cmp_fn)patch_id_cmp, 256);
+	hashmap_init(&ids->patches, (hashmap_cmp_fn)patch_id_cmp,
+		     NULL, 256);
 	return 0;
 }
 
refs.c +3 -2
index 88658ba769..eef1a7b67d 100644
@@ -1525,7 +1525,8 @@ struct ref_store_hash_entry
 	char name[FLEX_ARRAY];
 };
 
-static int ref_store_hash_cmp(const void *entry, const void *entry_or_key,
+static int ref_store_hash_cmp(const void *unused_cmp_data,
+			      const void *entry, const void *entry_or_key,
 			      const void *keydata)
 {
 	const struct ref_store_hash_entry *e1 = entry, *e2 = entry_or_key;
@@ -1608,7 +1609,7 @@ static void register_ref_store_map(struct hashmap *map,
 				   const char *name)
 {
 	if (!map->tablesize)
-		hashmap_init(map, ref_store_hash_cmp, 0);
+		hashmap_init(map, ref_store_hash_cmp, NULL, 0);
 
 	if (hashmap_put(map, alloc_ref_store_hash_entry(name, refs)))
 		die("BUG: %s ref_store '%s' initialized twice", type, name);
remote.c +5 -2
index d87482573d..60d0043921 100644
@@ -133,7 +133,10 @@ struct remotes_hash_key {
 	int len;
 };
 
-static int remotes_hash_cmp(const struct remote *a, const struct remote *b, const struct remotes_hash_key *key)
+static int remotes_hash_cmp(const void *unused_cmp_data,
+			    const struct remote *a,
+			    const struct remote *b,
+			    const struct remotes_hash_key *key)
 {
 	if (key)
 		return strncmp(a->name, key->str, key->len) || a->name[key->len];
@@ -144,7 +147,7 @@ static int remotes_hash_cmp(const struct remote *a, const struct remote *b, cons
 static inline void init_remotes_hash(void)
 {
 	if (!remotes_hash.cmpfn)
-		hashmap_init(&remotes_hash, (hashmap_cmp_fn)remotes_hash_cmp, 0);
+		hashmap_init(&remotes_hash, (hashmap_cmp_fn)remotes_hash_cmp, NULL, 0);
 }
 
 static struct remote *make_remote(const char *name, int len)
sha1_file.c +3 -2
index fb1fd809dc..8c523a5b74 100644
@@ -2389,7 +2389,8 @@ static int delta_base_cache_key_eq(const struct delta_base_cache_key *a,
 	return a->p == b->p && a->base_offset == b->base_offset;
 }
 
-static int delta_base_cache_hash_cmp(const void *va, const void *vb,
+static int delta_base_cache_hash_cmp(const void *unused_cmp_data,
+				     const void *va, const void *vb,
 				     const void *vkey)
 {
 	const struct delta_base_cache_entry *a = va, *b = vb;
@@ -2472,7 +2473,7 @@ static void add_delta_base_cache(struct packed_git *p, off_t base_offset,
 	list_add_tail(&ent->lru, &delta_base_cache_lru);
 
 	if (!delta_base_cache.cmpfn)
-		hashmap_init(&delta_base_cache, delta_base_cache_hash_cmp, 0);
+		hashmap_init(&delta_base_cache, delta_base_cache_hash_cmp, NULL, 0);
 	hashmap_entry_init(ent, pack_entry_hash(p, base_offset));
 	hashmap_add(&delta_base_cache, ent);
 }
sub-process.c +4 -3
index 92f8aea70a..a3cfab1a9d 100644
@@ -5,9 +5,10 @@
 #include "sigchain.h"
 #include "pkt-line.h"
 
-int cmd2process_cmp(const struct subprocess_entry *e1,
-			   const struct subprocess_entry *e2,
-			   const void *unused)
+int cmd2process_cmp(const void *unused_cmp_data,
+		    const struct subprocess_entry *e1,
+		    const struct subprocess_entry *e2,
+		    const void *unused_keydata)
 {
 	return strcmp(e1->cmd, e2->cmd);
 }
sub-process.h +4 -2
index d9a45cd359..96a2cca360 100644
@@ -20,8 +20,10 @@ struct subprocess_entry {
 
 /* subprocess functions */
 
-int cmd2process_cmp(const struct subprocess_entry *e1,
-	const struct subprocess_entry *e2, const void *unused);
+extern int cmd2process_cmp(const void *unused_cmp_data,
+			   const struct subprocess_entry *e1,
+			   const struct subprocess_entry *e2,
+			   const void *unused_keydata);
 
 typedef int(*subprocess_start_fn)(struct subprocess_entry *entry);
 int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, const char *cmd,
submodule-config.c +8 -6
index d8f8d5ea32..0e1126183d 100644
@@ -34,17 +34,19 @@ enum lookup_type {
 static struct submodule_cache the_submodule_cache;
 static int is_cache_init;
 
-static int config_path_cmp(const struct submodule_entry *a,
+static int config_path_cmp(const void *unused_cmp_data,
+			   const struct submodule_entry *a,
 			   const struct submodule_entry *b,
-			   const void *unused)
+			   const void *unused_keydata)
 {
 	return strcmp(a->config->path, b->config->path) ||
 	       hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
 }
 
-static int config_name_cmp(const struct submodule_entry *a,
+static int config_name_cmp(const void *unused_cmp_data,
+			   const struct submodule_entry *a,
 			   const struct submodule_entry *b,
-			   const void *unused)
+			   const void *unused_keydata)
 {
 	return strcmp(a->config->name, b->config->name) ||
 	       hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
@@ -52,8 +54,8 @@ static int config_name_cmp(const struct submodule_entry *a,
 
 static void cache_init(struct submodule_cache *cache)
 {
-	hashmap_init(&cache->for_path, (hashmap_cmp_fn) config_path_cmp, 0);
-	hashmap_init(&cache->for_name, (hashmap_cmp_fn) config_name_cmp, 0);
+	hashmap_init(&cache->for_path, (hashmap_cmp_fn) config_path_cmp, NULL, 0);
+	hashmap_init(&cache->for_name, (hashmap_cmp_fn) config_name_cmp, NULL, 0);
 }
 
 static void free_one_config(struct submodule_entry *entry)
t/helper/test-hashmap.c +12 -7
index 7aa9440e27..095d7395f3 100644
@@ -13,14 +13,18 @@ static const char *get_value(const struct test_entry *e)
 	return e->key + strlen(e->key) + 1;
 }
 
-static int test_entry_cmp(const struct test_entry *e1,
-		const struct test_entry *e2, const char* key)
+static int test_entry_cmp(const void *unused_cmp_data,
+			  const struct test_entry *e1,
+			  const struct test_entry *e2,
+			  const char* key)
 {
 	return strcmp(e1->key, key ? key : e2->key);
 }
 
-static int test_entry_cmp_icase(const struct test_entry *e1,
-		const struct test_entry *e2, const char* key)
+static int test_entry_cmp_icase(const void *unused_cmp_data,
+				const struct test_entry *e1,
+				const struct test_entry *e2,
+				const char* key)
 {
 	return strcasecmp(e1->key, key ? key : e2->key);
 }
@@ -92,7 +96,8 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
 	if (method & TEST_ADD) {
 		/* test adding to the map */
 		for (j = 0; j < rounds; j++) {
-			hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp, 0);
+			hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp,
+				     NULL, 0);
 
 			/* add entries */
 			for (i = 0; i < TEST_SIZE; i++) {
@@ -104,7 +109,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
 		}
 	} else {
 		/* test map lookups */
-		hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp, 0);
+		hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp, NULL, 0);
 
 		/* fill the map (sparsely if specified) */
 		j = (method & TEST_SPARSE) ? TEST_SIZE / 10 : TEST_SIZE;
@@ -147,7 +152,7 @@ int cmd_main(int argc, const char **argv)
 	/* init hash map */
 	icase = argc > 1 && !strcmp("ignorecase", argv[1]);
 	hashmap_init(&map, (hashmap_cmp_fn) (icase ? test_entry_cmp_icase
-			: test_entry_cmp), 0);
+			: test_entry_cmp), NULL, 0);
 
 	/* process commands from stdin */
 	while (fgets(line, sizeof(line), stdin)) {
Signature · 1 line
2.13.0.31.g9b732c453e
#22 lore raw

PATCH v3 2/3 patch-ids.c: use hashmap correctly

In reply to Stefan Beller

As eluded to in the previous patch, the code in patch-ids.c is using the hashmaps API wrong.

Luckily we do not have a bug, as all hashmap functionality that we use here (hashmap_get) passes through the keydata. If hashmap_get_next were to be used, a bug would occur as that passes NULL for the key_data.

So instead use the hashmap API correctly and provide the caller required data in the compare function via the first argument that always gets passed and was setup via the hashmap_init function.

---
 patch-ids.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
patch-ids.c +4 -4
index 815c115811..b4166b0f38 100644
@@ -35,10 +35,10 @@ int commit_patch_id(struct commit *commit, struct diff_options *options,
  * the side of safety.  The actual value being negative does not have
  * any significance; only that it is non-zero matters.
  */
-static int patch_id_cmp(const void *unused_cmp_data,
+static int patch_id_cmp(struct diff_options *opt,
 			struct patch_id *a,
 			struct patch_id *b,
-			struct diff_options *opt)
+			const void *unused_keydata)
 {
 	if (is_null_oid(&a->patch_id) &&
 	    commit_patch_id(a->commit, opt, &a->patch_id, 0))
@@ -59,7 +59,7 @@ int init_patch_ids(struct patch_ids *ids)
 	DIFF_OPT_SET(&ids->diffopts, RECURSIVE);
 	diff_setup_done(&ids->diffopts);
 	hashmap_init(&ids->patches, (hashmap_cmp_fn)patch_id_cmp,
-		     NULL, 256);
+		     &ids->diffopts, 256);
 	return 0;
 }
 
@@ -95,7 +95,7 @@ struct patch_id *has_commit_patch_id(struct commit *commit,
 	if (init_patch_id_entry(&patch, commit, ids))
 		return NULL;
 
-	return hashmap_get(&ids->patches, &patch, &ids->diffopts);
+	return hashmap_get(&ids->patches, &patch, NULL);
 }
 
 struct patch_id *add_commit_patch_id(struct commit *commit,
Signature · 1 line
2.13.0.31.g9b732c453e
#23 lore raw

Re: [PATCHv3 2/3] patch-ids.c: use hashmap correctly

Quoted 7 lines · Stefan Beller

As eluded to in the previous patch, the code in patch-ids.c is using the hashmaps API wrong.

Luckily we do not have a bug, as all hashmap functionality that we use here (hashmap_get) passes through the keydata. If hashmap_get_next were to be used, a bug would occur as that passes NULL for the key_data.

Quoted 8 lines

So instead use the hashmap API correctly and provide the caller required data in the compare function via the first argument that always gets passed and was setup via the hashmap_init function.

---
 patch-ids.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Nicely explained. Thanks for polishing the series.

Quoted 36 lines
patch-ids.c +4 -4
index 815c115811..b4166b0f38 100644
@@ -35,10 +35,10 @@ int commit_patch_id(struct commit *commit, struct diff_options *options,
  * the side of safety.  The actual value being negative does not have
  * any significance; only that it is non-zero matters.
  */
-static int patch_id_cmp(const void *unused_cmp_data,
+static int patch_id_cmp(struct diff_options *opt,
 			struct patch_id *a,
 			struct patch_id *b,
-			struct diff_options *opt)
+			const void *unused_keydata)
 {
 	if (is_null_oid(&a->patch_id) &&
 	    commit_patch_id(a->commit, opt, &a->patch_id, 0))
@@ -59,7 +59,7 @@ int init_patch_ids(struct patch_ids *ids)
 	DIFF_OPT_SET(&ids->diffopts, RECURSIVE);
 	diff_setup_done(&ids->diffopts);
 	hashmap_init(&ids->patches, (hashmap_cmp_fn)patch_id_cmp,
-		     NULL, 256);
+		     &ids->diffopts, 256);
 	return 0;
 }
 
@@ -95,7 +95,7 @@ struct patch_id *has_commit_patch_id(struct commit *commit,
 	if (init_patch_id_entry(&patch, commit, ids))
 		return NULL;
 
-	return hashmap_get(&ids->patches, &patch, &ids->diffopts);
+	return hashmap_get(&ids->patches, &patch, NULL);
 }
 
 struct patch_id *add_commit_patch_id(struct commit *commit,
#24 lore raw

Re: [PATCHv3 2/3] patch-ids.c: use hashmap correctly

In reply to Stefan Beller
Quoted 10 lines · Stefan Beller

As eluded to in the previous patch, the code in patch-ids.c is using the hashmaps API wrong.

Luckily we do not have a bug, as all hashmap functionality that we use here (hashmap_get) passes through the keydata. If hashmap_get_next were to be used, a bug would occur as that passes NULL for the key_data.

So instead use the hashmap API correctly and provide the caller required data in the compare function via the first argument that always gets passed and was setup via the hashmap_init function.

Reviewing this a bit late, but it looks good to me. And I think the explanation above nicely covers what is going on (and why it isn't a bug).

-Peff

PS I think you meant "alluded" in the first sentence, unless you really
were trying to escape the previous patch. :)

#25 lore raw

PATCH v3 3/3 hashmap: migrate documentation from Documentation/technical into header

In reply to Stefan Beller

While at it, clarify the use of key, keydata, entry_or_key as well as documenting the new data pointer for the compare function.

Rework the example.

---
 Documentation/technical/api-hashmap.txt | 309 ----------------------------
 hashmap.h                               | 348 +++++++++++++++++++++++++++++---
 2 files changed, 316 insertions(+), 341 deletions(-)
 delete mode 100644 Documentation/technical/api-hashmap.txt
Documentation/technical/api-hashmap.txt deleted +0 -309
deleted file mode 100644
index ccc634bbd7..0000000000
@@ -1,309 +0,0 @@
-hashmap API
-===========
-
-The hashmap API is a generic implementation of hash-based key-value mappings.
-
-Data Structures
----------------
-
-`struct hashmap`::
-
-	The hash table structure. Members can be used as follows, but should
-	not be modified directly:
-+
-The `size` member keeps track of the total number of entries (0 means the
-hashmap is empty).
-+
-`tablesize` is the allocated size of the hash table. A non-0 value indicates
-that the hashmap is initialized. It may also be useful for statistical purposes
-(i.e. `size / tablesize` is the current load factor).
-+
-`cmpfn` stores the comparison function specified in `hashmap_init()`. In
-advanced scenarios, it may be useful to change this, e.g. to switch between
-case-sensitive and case-insensitive lookup.
-+
-When `disallow_rehash` is set, automatic rehashes are prevented during inserts
-and deletes.
-
-`struct hashmap_entry`::
-
-	An opaque structure representing an entry in the hash table, which must
-	be used as first member of user data structures. Ideally it should be
-	followed by an int-sized member to prevent unused memory on 64-bit
-	systems due to alignment.
-+
-The `hash` member is the entry's hash code and the `next` member points to the
-next entry in case of collisions (i.e. if multiple entries map to the same
-bucket).
-
-`struct hashmap_iter`::
-
-	An iterator structure, to be used with hashmap_iter_* functions.
-
-Types
------
-
-`int (*hashmap_cmp_fn)(const void *entry, const void *entry_or_key, const void *keydata)`::
-
-	User-supplied function to test two hashmap entries for equality. Shall
-	return 0 if the entries are equal.
-+
-This function is always called with non-NULL `entry` / `entry_or_key`
-parameters that have the same hash code. When looking up an entry, the `key`
-and `keydata` parameters to hashmap_get and hashmap_remove are always passed
-as second and third argument, respectively. Otherwise, `keydata` is NULL.
-
-Functions
----------
-
-`unsigned int strhash(const char *buf)`::
-`unsigned int strihash(const char *buf)`::
-`unsigned int memhash(const void *buf, size_t len)`::
-`unsigned int memihash(const void *buf, size_t len)`::
-`unsigned int memihash_cont(unsigned int hash_seed, const void *buf, size_t len)`::
-
-	Ready-to-use hash functions for strings, using the FNV-1 algorithm (see
-	http://www.isthe.com/chongo/tech/comp/fnv).
-+
-`strhash` and `strihash` take 0-terminated strings, while `memhash` and
-`memihash` operate on arbitrary-length memory.
-+
-`strihash` and `memihash` are case insensitive versions.
-+
-`memihash_cont` is a variant of `memihash` that allows a computation to be
-continued with another chunk of data.
-
-`unsigned int sha1hash(const unsigned char *sha1)`::
-
-	Converts a cryptographic hash (e.g. SHA-1) into an int-sized hash code
-	for use in hash tables. Cryptographic hashes are supposed to have
-	uniform distribution, so in contrast to `memhash()`, this just copies
-	the first `sizeof(int)` bytes without shuffling any bits. Note that
-	the results will be different on big-endian and little-endian
-	platforms, so they should not be stored or transferred over the net.
-
-`void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function, size_t initial_size)`::
-
-	Initializes a hashmap structure.
-+
-`map` is the hashmap to initialize.
-+
-The `equals_function` can be specified to compare two entries for equality.
-If NULL, entries are considered equal if their hash codes are equal.
-+
-If the total number of entries is known in advance, the `initial_size`
-parameter may be used to preallocate a sufficiently large table and thus
-prevent expensive resizing. If 0, the table is dynamically resized.
-
-`void hashmap_free(struct hashmap *map, int free_entries)`::
-
-	Frees a hashmap structure and allocated memory.
-+
-`map` is the hashmap to free.
-+
-If `free_entries` is true, each hashmap_entry in the map is freed as well
-(using stdlib's free()).
-
-`void hashmap_entry_init(void *entry, unsigned int hash)`::
-
-	Initializes a hashmap_entry structure.
-+
-`entry` points to the entry to initialize.
-+
-`hash` is the hash code of the entry.
-+
-The hashmap_entry structure does not hold references to external resources,
-and it is safe to just discard it once you are done with it (i.e. if
-your structure was allocated with xmalloc(), you can just free(3) it,
-and if it is on stack, you can just let it go out of scope).
-
-`void *hashmap_get(const struct hashmap *map, const void *key, const void *keydata)`::
-
-	Returns the hashmap entry for the specified key, or NULL if not found.
-+
-`map` is the hashmap structure.
-+
-`key` is a hashmap_entry structure (or user data structure that starts with
-hashmap_entry) that has at least been initialized with the proper hash code
-(via `hashmap_entry_init`).
-+
-If an entry with matching hash code is found, `key` and `keydata` are passed
-to `hashmap_cmp_fn` to decide whether the entry matches the key.
-
-`void *hashmap_get_from_hash(const struct hashmap *map, unsigned int hash, const void *keydata)`::
-
-	Returns the hashmap entry for the specified hash code and key data,
-	or NULL if not found.
-+
-`map` is the hashmap structure.
-+
-`hash` is the hash code of the entry to look up.
-+
-If an entry with matching hash code is found, `keydata` is passed to
-`hashmap_cmp_fn` to decide whether the entry matches the key. The
-`entry_or_key` parameter points to a bogus hashmap_entry structure that
-should not be used in the comparison.
-
-`void *hashmap_get_next(const struct hashmap *map, const void *entry)`::
-
-	Returns the next equal hashmap entry, or NULL if not found. This can be
-	used to iterate over duplicate entries (see `hashmap_add`).
-+
-`map` is the hashmap structure.
-+
-`entry` is the hashmap_entry to start the search from, obtained via a previous
-call to `hashmap_get` or `hashmap_get_next`.
-
-`void hashmap_add(struct hashmap *map, void *entry)`::
-
-	Adds a hashmap entry. This allows to add duplicate entries (i.e.
-	separate values with the same key according to hashmap_cmp_fn).
-+
-`map` is the hashmap structure.
-+
-`entry` is the entry to add.
-
-`void *hashmap_put(struct hashmap *map, void *entry)`::
-
-	Adds or replaces a hashmap entry. If the hashmap contains duplicate
-	entries equal to the specified entry, only one of them will be replaced.
-+
-`map` is the hashmap structure.
-+
-`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_remove(struct hashmap *map, const void *key, const void *keydata)`::
-
-	Removes a hashmap entry matching the specified key. If the hashmap
-	contains duplicate entries equal to the specified key, only one of
-	them will be removed.
-+
-`map` is the hashmap structure.
-+
-`key` is a hashmap_entry structure (or user data structure that starts with
-hashmap_entry) that has at least been initialized with the proper hash code
-(via `hashmap_entry_init`).
-+
-If an entry with matching hash code is found, `key` and `keydata` are
-passed to `hashmap_cmp_fn` to decide whether the entry matches the key.
-+
-Returns the removed entry, or NULL if not found.
-
-`void hashmap_disallow_rehash(struct hashmap *map, unsigned value)`::
-
-	Disallow/allow automatic rehashing of the hashmap during inserts
-	and deletes.
-+
-This is useful if the caller knows that the hashmap will be accessed
-by multiple threads.
-+
-The caller is still responsible for any necessary locking; this simply
-prevents unexpected rehashing.  The caller is also responsible for properly
-sizing the initial hashmap to ensure good performance.
-+
-A call to allow rehashing does not force a rehash; that might happen
-with the next insert or delete.
-
-`void hashmap_iter_init(struct hashmap *map, struct hashmap_iter *iter)`::
-`void *hashmap_iter_next(struct hashmap_iter *iter)`::
-`void *hashmap_iter_first(struct hashmap *map, struct hashmap_iter *iter)`::
-
-	Used to iterate over all entries of a hashmap. Note that it is
-	not safe to add or remove entries to the hashmap while
-	iterating.
-+
-`hashmap_iter_init` initializes a `hashmap_iter` structure.
-+
-`hashmap_iter_next` returns the next hashmap_entry, or NULL if there are no
-more entries.
-+
-`hashmap_iter_first` is a combination of both (i.e. initializes the iterator
-and returns the first entry, if any).
-
-`const char *strintern(const char *string)`::
-`const void *memintern(const void *data, size_t len)`::
-
-	Returns the unique, interned version of the specified string or data,
-	similar to the `String.intern` API in Java and .NET, respectively.
-	Interned strings remain valid for the entire lifetime of the process.
-+
-Can be used as `[x]strdup()` or `xmemdupz` replacement, except that interned
-strings / data must not be modified or freed.
-+
-Interned strings are best used for short strings with high probability of
-duplicates.
-+
-Uses a hashmap to store the pool of interned strings.
-
-Usage example
--------------
-
-Here's a simple usage example that maps long keys to double values.
-------------
-struct hashmap map;
-
-struct long2double {
-	struct hashmap_entry ent; /* must be the first member! */
-	long key;
-	double value;
-};
-
-static int long2double_cmp(const struct long2double *e1, const struct long2double *e2, const void *unused)
-{
-	return !(e1->key == e2->key);
-}
-
-void long2double_init(void)
-{
-	hashmap_init(&map, (hashmap_cmp_fn) long2double_cmp, 0);
-}
-
-void long2double_free(void)
-{
-	hashmap_free(&map, 1);
-}
-
-static struct long2double *find_entry(long key)
-{
-	struct long2double k;
-	hashmap_entry_init(&k, memhash(&key, sizeof(long)));
-	k.key = key;
-	return hashmap_get(&map, &k, NULL);
-}
-
-double get_value(long key)
-{
-	struct long2double *e = find_entry(key);
-	return e ? e->value : 0;
-}
-
-void set_value(long key, double value)
-{
-	struct long2double *e = find_entry(key);
-	if (!e) {
-		e = malloc(sizeof(struct long2double));
-		hashmap_entry_init(e, memhash(&key, sizeof(long)));
-		e->key = key;
-		hashmap_add(&map, e);
-	}
-	e->value = value;
-}
-------------
-
-Using variable-sized keys
--------------------------
-
-The `hashmap_entry_get` and `hashmap_entry_remove` functions expect an ordinary
-`hashmap_entry` structure as key to find the correct entry. If the key data is
-variable-sized (e.g. a FLEX_ARRAY string) or quite large, it is undesirable
-to create a full-fledged entry structure on the heap and copy all the key data
-into the structure.
-
-In this case, the `keydata` parameter can be used to pass
-variable-sized key data directly to the comparison function, and the `key`
-parameter can be a stripped-down, fixed size entry structure allocated on the
-stack.
-
-See test-hashmap.c for an example using arbitrary-length strings as keys.
hashmap.h +316 -32

431 lines not on this page · raw message

Signature · 1 line
2.13.0.31.g9b732c453e