hashmap_get{,_from_hash} return "struct hashmap_entry *"

Update callers to use hashmap_get_entry, hashmap_get_entry_from_hash or container_of as appropriate. This is another step towards eliminating the requirement of hashmap_entry being the first field in a struct. Signed-off-by: Eric Wong <e@80x24.org> Reviewed-by: Derrick Stolee <stolee@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Wong committed Oct 6, 2019 at 23:30 UTC f23a465132a22860684ac66052cf9a954a18e27d
20 files changed +72 -41
attr.c
+1 -1
@@ -101,7 +101,7 @@ static void *attr_hashmap_get(struct attr_hashmap *map,
101 hashmap_entry_init(&k.ent, memhash(key, keylen));
102 k.key = key;
103 k.keylen = keylen;
104 - e = hashmap_get(&map->map, &k.ent, NULL);
104 + e = hashmap_get_entry(&map->map, &k, NULL, struct attr_hash_entry, ent);
105
106 return e ? e->value : NULL;
107 }
blame.c
+8 -3
@@ -419,7 +419,8 @@ static void get_fingerprint(struct fingerprint *result,
419 continue;
420 hashmap_entry_init(&entry->entry, hash);
421
422 - found_entry = hashmap_get(&result->map, &entry->entry, NULL);
422 + found_entry = hashmap_get_entry(&result->map, entry, NULL,
423 + struct fingerprint_entry, entry);
424 if (found_entry) {
425 found_entry->count += 1;
426 } else {
@@ -452,7 +453,9 @@ static int fingerprint_similarity(struct fingerprint *a, struct fingerprint *b)
453 hashmap_iter_init(&b->map, &iter);
454
455 while ((entry_b = hashmap_iter_next(&iter))) {
455 - if ((entry_a = hashmap_get(&a->map, &entry_b->entry, NULL))) {
456 + entry_a = hashmap_get_entry(&a->map, entry_b, NULL,
457 + struct fingerprint_entry, entry);
458 + if (entry_a) {
459 intersection += entry_a->count < entry_b->count ?
460 entry_a->count : entry_b->count;
461 }
@@ -471,7 +474,9 @@ static void fingerprint_subtract(struct fingerprint *a, struct fingerprint *b)
474 hashmap_iter_init(&b->map, &iter);
475
476 while ((entry_b = hashmap_iter_next(&iter))) {
474 - if ((entry_a = hashmap_get(&a->map, &entry_b->entry, NULL))) {
477 + entry_a = hashmap_get_entry(&a->map, entry_b, NULL,
478 + struct fingerprint_entry, entry);
479 + if (entry_a) {
480 if (entry_a->count <= entry_b->count)
481 hashmap_remove(&a->map, &entry_b->entry, NULL);
482 else
builtin/describe.c
+2 -1
@@ -76,7 +76,8 @@ static int commit_name_neq(const void *unused_cmp_data,
76
77 static inline struct commit_name *find_commit_name(const struct object_id *peeled)
78 {
79 - return hashmap_get_from_hash(&names, oidhash(peeled), peeled);
79 + return hashmap_get_entry_from_hash(&names, oidhash(peeled), peeled,
80 + struct commit_name, entry);
81 }
82
83 static int replace_name(struct commit_name *e,
builtin/difftool.c
+1 -1
@@ -162,7 +162,7 @@ static void add_left_or_right(struct hashmap *map, const char *path,
162
163 FLEX_ALLOC_STR(e, path, path);
164 hashmap_entry_init(&e->entry, strhash(path));
165 - existing = hashmap_get(map, &e->entry, NULL);
165 + existing = hashmap_get_entry(map, e, NULL, struct pair_entry, entry);
166 if (existing) {
167 free(e);
168 e = existing;
builtin/fast-export.c
+1 -1
@@ -151,7 +151,7 @@ static const void *anonymize_mem(struct hashmap *map,
151 hashmap_entry_init(&key.hash, memhash(orig, *len));
152 key.orig = orig;
153 key.orig_len = *len;
154 - ret = hashmap_get(map, &key.hash, NULL);
154 + ret = hashmap_get_entry(map, &key, NULL, struct anonymized_entry, hash);
155
156 if (!ret) {
157 ret = xmalloc(sizeof(*ret));
builtin/fetch.c
+7 -4
@@ -383,8 +383,10 @@ static void find_non_local_tags(const struct ref *refs,
383 for_each_string_list_item(remote_ref_item, &remote_refs_list) {
384 const char *refname = remote_ref_item->string;
385 struct ref *rm;
386 + unsigned int hash = strhash(refname);
387
387 - item = hashmap_get_from_hash(&remote_refs, strhash(refname), refname);
388 + item = hashmap_get_entry_from_hash(&remote_refs, hash, refname,
389 + struct refname_hash_entry, ent);
390 if (!item)
391 BUG("unseen remote ref?");
392
@@ -516,10 +518,11 @@ static struct ref *get_ref_map(struct remote *remote,
518 if (rm->peer_ref) {
519 const char *refname = rm->peer_ref->name;
520 struct refname_hash_entry *peer_item;
521 + unsigned int hash = strhash(refname);
522
520 - peer_item = hashmap_get_from_hash(&existing_refs,
521 - strhash(refname),
522 - refname);
523 + peer_item = hashmap_get_entry_from_hash(&existing_refs,
524 + hash, refname,
525 + struct refname_hash_entry, ent);
526 if (peer_item) {
527 struct object_id *old_oid = &peer_item->oid;
528 oidcpy(&rm->peer_ref->old_oid, old_oid);
config.c
+2 -1
@@ -1863,7 +1863,8 @@ static struct config_set_element *configset_find_element(struct config_set *cs,
1863
1864 hashmap_entry_init(&k.ent, strhash(normalized_key));
1865 k.key = normalized_key;
1866 - found_entry = hashmap_get(&cs->config_hash, &k.ent, NULL);
1866 + found_entry = hashmap_get_entry(&cs->config_hash, &k, NULL,
1867 + struct config_set_element, ent);
1868 free(normalized_key);
1869 return found_entry;
1870 }
hashmap.c
+4 -3
@@ -186,8 +186,9 @@ void hashmap_free(struct hashmap *map, int free_entries)
186 memset(map, 0, sizeof(*map));
187 }
188
189 -void *hashmap_get(const struct hashmap *map, const struct hashmap_entry *key,
190 - const void *keydata)
189 +struct hashmap_entry *hashmap_get(const struct hashmap *map,
190 + const struct hashmap_entry *key,
191 + const void *keydata)
192 {
193 return *find_entry_ptr(map, key, keydata);
194 }
@@ -298,7 +299,7 @@ const void *memintern(const void *data, size_t len)
299 /* lookup interned string in pool */
300 hashmap_entry_init(&key.ent, memhash(data, len));
301 key.len = len;
301 - e = hashmap_get(&map, &key.ent, data);
302 + e = hashmap_get_entry(&map, &key, data, struct pool_entry, ent);
303 if (!e) {
304 /* not found: create it */
305 FLEX_ALLOC_MEM(e, data, data, len);
hashmap.h
+7 -5
@@ -290,8 +290,9 @@ static inline unsigned int hashmap_get_size(struct hashmap *map)
290 * If an entry with matching hash code is found, `key` and `keydata` are passed
291 * to `hashmap_cmp_fn` to decide whether the entry matches the key.
292 */
293 -void *hashmap_get(const struct hashmap *map, const struct hashmap_entry *key,
294 - const void *keydata);
293 +struct hashmap_entry *hashmap_get(const struct hashmap *map,
294 + const struct hashmap_entry *key,
295 + const void *keydata);
296
297 /*
298 * Returns the hashmap entry for the specified hash code and key data,
@@ -305,9 +306,10 @@ void *hashmap_get(const struct hashmap *map, const struct hashmap_entry *key,
306 * `entry_or_key` parameter of `hashmap_cmp_fn` points to a hashmap_entry
307 * structure that should not be used in the comparison.
308 */
308 -static inline void *hashmap_get_from_hash(const struct hashmap *map,
309 - unsigned int hash,
310 - const void *keydata)
309 +static inline struct hashmap_entry *hashmap_get_from_hash(
310 + const struct hashmap *map,
311 + unsigned int hash,
312 + const void *keydata)
313 {
314 struct hashmap_entry key;
315 hashmap_entry_init(&key, hash);
merge-recursive.c
+4 -2
@@ -63,7 +63,8 @@ static struct dir_rename_entry *dir_rename_find_entry(struct hashmap *hashmap,
63 return NULL;
64 hashmap_entry_init(&key.ent, strhash(dir));
65 key.dir = dir;
66 - return hashmap_get(hashmap, &key.ent, NULL);
66 + return hashmap_get_entry(hashmap, &key, NULL,
67 + struct dir_rename_entry, ent);
68 }
69
70 static int dir_rename_cmp(const void *unused_cmp_data,
@@ -99,7 +100,8 @@ static struct collision_entry *collision_find_entry(struct hashmap *hashmap,
100
101 hashmap_entry_init(&key.ent, strhash(target_file));
102 key.target_file = target_file;
102 - return hashmap_get(hashmap, &key.ent, NULL);
103 + return hashmap_get_entry(hashmap, &key, NULL,
104 + struct collision_entry, ent);
105 }
106
107 static int collision_cmp(void *unused_cmp_data,
name-hash.c
+2 -1
@@ -35,7 +35,8 @@ static struct dir_entry *find_dir_entry__hash(struct index_state *istate,
35 struct dir_entry key;
36 hashmap_entry_init(&key.ent, hash);
37 key.namelen = namelen;
38 - return hashmap_get(&istate->dir_hash, &key.ent, name);
38 + return hashmap_get_entry(&istate->dir_hash, &key, name,
39 + struct dir_entry, ent);
40 }
41
42 static struct dir_entry *find_dir_entry(struct index_state *istate,
packfile.c
+3 -2
@@ -1381,7 +1381,7 @@ static unsigned int pack_entry_hash(struct packed_git *p, off_t base_offset)
1381 static struct delta_base_cache_entry *
1382 get_delta_base_cache_entry(struct packed_git *p, off_t base_offset)
1383 {
1384 - struct hashmap_entry entry;
1384 + struct hashmap_entry entry, *e;
1385 struct delta_base_cache_key key;
1386
1387 if (!delta_base_cache.cmpfn)
@@ -1390,7 +1390,8 @@ get_delta_base_cache_entry(struct packed_git *p, off_t base_offset)
1390 hashmap_entry_init(&entry, pack_entry_hash(p, base_offset));
1391 key.p = p;
1392 key.base_offset = base_offset;
1393 - return hashmap_get(&delta_base_cache, &entry, &key);
1393 + e = hashmap_get(&delta_base_cache, &entry, &key);
1394 + return e ? container_of(e, struct delta_base_cache_entry, ent) : NULL;
1395 }
1396
1397 static int delta_base_cache_key_eq(const struct delta_base_cache_key *a,
patch-ids.c
+2 -1
@@ -99,7 +99,8 @@ struct patch_id *has_commit_patch_id(struct commit *commit,
99 if (init_patch_id_entry(&patch, commit, ids))
100 return NULL;
101
102 - return hashmap_get(&ids->patches, &patch.ent, NULL);
102 + return hashmap_get_entry(&ids->patches, &patch, NULL,
103 + struct patch_id, ent);
104 }
105
106 struct patch_id *add_commit_patch_id(struct commit *commit,
ref-filter.c
+7 -5
@@ -1585,18 +1585,20 @@ static void lazy_init_worktree_map(void)
1585
1586 static char *get_worktree_path(const struct used_atom *atom, const struct ref_array_item *ref)
1587 {
1588 - struct hashmap_entry entry;
1588 + struct hashmap_entry entry, *e;
1589 struct ref_to_worktree_entry *lookup_result;
1590
1591 lazy_init_worktree_map();
1592
1593 hashmap_entry_init(&entry, strhash(ref->refname));
1594 - lookup_result = hashmap_get(&(ref_to_worktree_map.map), &entry, ref->refname);
1594 + e = hashmap_get(&(ref_to_worktree_map.map), &entry, ref->refname);
1595
1596 - if (lookup_result)
1597 - return xstrdup(lookup_result->wt->path);
1598 - else
1596 + if (!e)
1597 return xstrdup("");
1598 +
1599 + lookup_result = container_of(e, struct ref_to_worktree_entry, ent);
1600 +
1601 + return xstrdup(lookup_result->wt->path);
1602 }
1603
1604 /*
refs.c
+4 -1
@@ -1815,12 +1815,15 @@ static struct ref_store *lookup_ref_store_map(struct hashmap *map,
1815 const char *name)
1816 {
1817 struct ref_store_hash_entry *entry;
1818 + unsigned int hash;
1819
1820 if (!map->tablesize)
1821 /* It's initialized on demand in register_ref_store(). */
1822 return NULL;
1823
1823 - entry = hashmap_get_from_hash(map, strhash(name), name);
1824 + hash = strhash(name);
1825 + entry = hashmap_get_entry_from_hash(map, hash, name,
1826 + struct ref_store_hash_entry, ent);
1827 return entry ? entry->refs : NULL;
1828 }
1829
remote.c
+4 -3
@@ -135,7 +135,7 @@ static struct remote *make_remote(const char *name, int len)
135 {
136 struct remote *ret, *replaced;
137 struct remotes_hash_key lookup;
138 - struct hashmap_entry lookup_entry;
138 + struct hashmap_entry lookup_entry, *e;
139
140 if (!len)
141 len = strlen(name);
@@ -145,8 +145,9 @@ static struct remote *make_remote(const char *name, int len)
145 lookup.len = len;
146 hashmap_entry_init(&lookup_entry, memhash(name, len));
147
148 - if ((ret = hashmap_get(&remotes_hash, &lookup_entry, &lookup)) != NULL)
149 - return ret;
148 + e = hashmap_get(&remotes_hash, &lookup_entry, &lookup);
149 + if (e)
150 + return container_of(e, struct remote, ent);
151
152 ret = xcalloc(1, sizeof(struct remote));
153 ret->prune = -1; /* unspecified */
revision.c
+2 -1
@@ -147,7 +147,8 @@ static void paths_and_oids_insert(struct hashmap *map,
147 key.path = (char *)path;
148 oidset_init(&key.trees, 0);
149
150 - entry = hashmap_get(map, &key.ent, NULL);
150 + entry = hashmap_get_entry(map, &key, NULL,
151 + struct path_and_oids_entry, ent);
152 if (!entry) {
153 entry = xcalloc(1, sizeof(struct path_and_oids_entry));
154 hashmap_entry_init(&entry->ent, hash);
sequencer.c
+5 -2
@@ -5217,8 +5217,11 @@ int todo_list_rearrange_squash(struct todo_list *todo_list)
5217 break;
5218 }
5219
5220 - if ((entry = hashmap_get_from_hash(&subject2item,
5221 - strhash(p), p)))
5220 + entry = hashmap_get_entry_from_hash(&subject2item,
5221 + strhash(p), p,
5222 + struct subject2item_entry,
5223 + entry);
5224 + if (entry)
5225 /* found by title */
5226 i2 = entry->i;
5227 else if (!strchr(p, ' ') &&
sub-process.c
+2 -1
@@ -22,7 +22,8 @@ struct subprocess_entry *subprocess_find_entry(struct hashmap *hashmap, const ch
22
23 hashmap_entry_init(&key.ent, strhash(cmd));
24 key.cmd = cmd;
25 - return hashmap_get(hashmap, &key.ent, NULL);
25 + return hashmap_get_entry(hashmap, &key, NULL,
26 + struct subprocess_entry, ent);
27 }
28
29 int subprocess_read_status(int fd, struct strbuf *status)
submodule-config.c
+4 -2
@@ -166,7 +166,8 @@ static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
166 hashmap_entry_init(&key.ent, hash);
167 key.config = &key_config;
168
169 - entry = hashmap_get(&cache->for_path, &key.ent, NULL);
169 + entry = hashmap_get_entry(&cache->for_path, &key, NULL,
170 + struct submodule_entry, ent);
171 if (entry)
172 return entry->config;
173 return NULL;
@@ -186,7 +187,8 @@ static struct submodule *cache_lookup_name(struct submodule_cache *cache,
187 hashmap_entry_init(&key.ent, hash);
188 key.config = &key_config;
189
189 - entry = hashmap_get(&cache->for_name, &key.ent, NULL);
190 + entry = hashmap_get_entry(&cache->for_name, &key, NULL,
191 + struct submodule_entry, ent);
192 if (entry)
193 return entry->config;
194 return NULL;