Skip to content

Commit f23a465

Browse files
Eric Wonggitster
Eric Wong
authored andcommitted
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 <[email protected]> Reviewed-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f0e63c4 commit f23a465

20 files changed

+72
-41
lines changed

attr.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static void *attr_hashmap_get(struct attr_hashmap *map,
101101
hashmap_entry_init(&k.ent, memhash(key, keylen));
102102
k.key = key;
103103
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);
105105

106106
return e ? e->value : NULL;
107107
}

blame.c

+8-3
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,8 @@ static void get_fingerprint(struct fingerprint *result,
419419
continue;
420420
hashmap_entry_init(&entry->entry, hash);
421421

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);
423424
if (found_entry) {
424425
found_entry->count += 1;
425426
} else {
@@ -452,7 +453,9 @@ static int fingerprint_similarity(struct fingerprint *a, struct fingerprint *b)
452453
hashmap_iter_init(&b->map, &iter);
453454

454455
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) {
456459
intersection += entry_a->count < entry_b->count ?
457460
entry_a->count : entry_b->count;
458461
}
@@ -471,7 +474,9 @@ static void fingerprint_subtract(struct fingerprint *a, struct fingerprint *b)
471474
hashmap_iter_init(&b->map, &iter);
472475

473476
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) {
475480
if (entry_a->count <= entry_b->count)
476481
hashmap_remove(&a->map, &entry_b->entry, NULL);
477482
else

builtin/describe.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ static int commit_name_neq(const void *unused_cmp_data,
7676

7777
static inline struct commit_name *find_commit_name(const struct object_id *peeled)
7878
{
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);
8081
}
8182

8283
static int replace_name(struct commit_name *e,

builtin/difftool.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ static void add_left_or_right(struct hashmap *map, const char *path,
162162

163163
FLEX_ALLOC_STR(e, path, path);
164164
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);
166166
if (existing) {
167167
free(e);
168168
e = existing;

builtin/fast-export.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ static const void *anonymize_mem(struct hashmap *map,
151151
hashmap_entry_init(&key.hash, memhash(orig, *len));
152152
key.orig = orig;
153153
key.orig_len = *len;
154-
ret = hashmap_get(map, &key.hash, NULL);
154+
ret = hashmap_get_entry(map, &key, NULL, struct anonymized_entry, hash);
155155

156156
if (!ret) {
157157
ret = xmalloc(sizeof(*ret));

builtin/fetch.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,10 @@ static void find_non_local_tags(const struct ref *refs,
383383
for_each_string_list_item(remote_ref_item, &remote_refs_list) {
384384
const char *refname = remote_ref_item->string;
385385
struct ref *rm;
386+
unsigned int hash = strhash(refname);
386387

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);
388390
if (!item)
389391
BUG("unseen remote ref?");
390392

@@ -516,10 +518,11 @@ static struct ref *get_ref_map(struct remote *remote,
516518
if (rm->peer_ref) {
517519
const char *refname = rm->peer_ref->name;
518520
struct refname_hash_entry *peer_item;
521+
unsigned int hash = strhash(refname);
519522

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);
523526
if (peer_item) {
524527
struct object_id *old_oid = &peer_item->oid;
525528
oidcpy(&rm->peer_ref->old_oid, old_oid);

config.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1863,7 +1863,8 @@ static struct config_set_element *configset_find_element(struct config_set *cs,
18631863

18641864
hashmap_entry_init(&k.ent, strhash(normalized_key));
18651865
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);
18671868
free(normalized_key);
18681869
return found_entry;
18691870
}

hashmap.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,9 @@ void hashmap_free(struct hashmap *map, int free_entries)
186186
memset(map, 0, sizeof(*map));
187187
}
188188

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)
191192
{
192193
return *find_entry_ptr(map, key, keydata);
193194
}
@@ -298,7 +299,7 @@ const void *memintern(const void *data, size_t len)
298299
/* lookup interned string in pool */
299300
hashmap_entry_init(&key.ent, memhash(data, len));
300301
key.len = len;
301-
e = hashmap_get(&map, &key.ent, data);
302+
e = hashmap_get_entry(&map, &key, data, struct pool_entry, ent);
302303
if (!e) {
303304
/* not found: create it */
304305
FLEX_ALLOC_MEM(e, data, data, len);

hashmap.h

+7-5
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,9 @@ static inline unsigned int hashmap_get_size(struct hashmap *map)
290290
* If an entry with matching hash code is found, `key` and `keydata` are passed
291291
* to `hashmap_cmp_fn` to decide whether the entry matches the key.
292292
*/
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);
295296

296297
/*
297298
* 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,
305306
* `entry_or_key` parameter of `hashmap_cmp_fn` points to a hashmap_entry
306307
* structure that should not be used in the comparison.
307308
*/
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)
311313
{
312314
struct hashmap_entry key;
313315
hashmap_entry_init(&key, hash);

merge-recursive.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ static struct dir_rename_entry *dir_rename_find_entry(struct hashmap *hashmap,
6363
return NULL;
6464
hashmap_entry_init(&key.ent, strhash(dir));
6565
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);
6768
}
6869

6970
static int dir_rename_cmp(const void *unused_cmp_data,
@@ -99,7 +100,8 @@ static struct collision_entry *collision_find_entry(struct hashmap *hashmap,
99100

100101
hashmap_entry_init(&key.ent, strhash(target_file));
101102
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);
103105
}
104106

105107
static int collision_cmp(void *unused_cmp_data,

name-hash.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ static struct dir_entry *find_dir_entry__hash(struct index_state *istate,
3535
struct dir_entry key;
3636
hashmap_entry_init(&key.ent, hash);
3737
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);
3940
}
4041

4142
static struct dir_entry *find_dir_entry(struct index_state *istate,

packfile.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,7 @@ static unsigned int pack_entry_hash(struct packed_git *p, off_t base_offset)
13811381
static struct delta_base_cache_entry *
13821382
get_delta_base_cache_entry(struct packed_git *p, off_t base_offset)
13831383
{
1384-
struct hashmap_entry entry;
1384+
struct hashmap_entry entry, *e;
13851385
struct delta_base_cache_key key;
13861386

13871387
if (!delta_base_cache.cmpfn)
@@ -1390,7 +1390,8 @@ get_delta_base_cache_entry(struct packed_git *p, off_t base_offset)
13901390
hashmap_entry_init(&entry, pack_entry_hash(p, base_offset));
13911391
key.p = p;
13921392
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;
13941395
}
13951396

13961397
static int delta_base_cache_key_eq(const struct delta_base_cache_key *a,

patch-ids.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ struct patch_id *has_commit_patch_id(struct commit *commit,
9999
if (init_patch_id_entry(&patch, commit, ids))
100100
return NULL;
101101

102-
return hashmap_get(&ids->patches, &patch.ent, NULL);
102+
return hashmap_get_entry(&ids->patches, &patch, NULL,
103+
struct patch_id, ent);
103104
}
104105

105106
struct patch_id *add_commit_patch_id(struct commit *commit,

ref-filter.c

+7-5
Original file line numberDiff line numberDiff line change
@@ -1585,18 +1585,20 @@ static void lazy_init_worktree_map(void)
15851585

15861586
static char *get_worktree_path(const struct used_atom *atom, const struct ref_array_item *ref)
15871587
{
1588-
struct hashmap_entry entry;
1588+
struct hashmap_entry entry, *e;
15891589
struct ref_to_worktree_entry *lookup_result;
15901590

15911591
lazy_init_worktree_map();
15921592

15931593
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);
15951595

1596-
if (lookup_result)
1597-
return xstrdup(lookup_result->wt->path);
1598-
else
1596+
if (!e)
15991597
return xstrdup("");
1598+
1599+
lookup_result = container_of(e, struct ref_to_worktree_entry, ent);
1600+
1601+
return xstrdup(lookup_result->wt->path);
16001602
}
16011603

16021604
/*

refs.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -1815,12 +1815,15 @@ static struct ref_store *lookup_ref_store_map(struct hashmap *map,
18151815
const char *name)
18161816
{
18171817
struct ref_store_hash_entry *entry;
1818+
unsigned int hash;
18181819

18191820
if (!map->tablesize)
18201821
/* It's initialized on demand in register_ref_store(). */
18211822
return NULL;
18221823

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);
18241827
return entry ? entry->refs : NULL;
18251828
}
18261829

remote.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ static struct remote *make_remote(const char *name, int len)
135135
{
136136
struct remote *ret, *replaced;
137137
struct remotes_hash_key lookup;
138-
struct hashmap_entry lookup_entry;
138+
struct hashmap_entry lookup_entry, *e;
139139

140140
if (!len)
141141
len = strlen(name);
@@ -145,8 +145,9 @@ static struct remote *make_remote(const char *name, int len)
145145
lookup.len = len;
146146
hashmap_entry_init(&lookup_entry, memhash(name, len));
147147

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);
150151

151152
ret = xcalloc(1, sizeof(struct remote));
152153
ret->prune = -1; /* unspecified */

revision.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ static void paths_and_oids_insert(struct hashmap *map,
147147
key.path = (char *)path;
148148
oidset_init(&key.trees, 0);
149149

150-
entry = hashmap_get(map, &key.ent, NULL);
150+
entry = hashmap_get_entry(map, &key, NULL,
151+
struct path_and_oids_entry, ent);
151152
if (!entry) {
152153
entry = xcalloc(1, sizeof(struct path_and_oids_entry));
153154
hashmap_entry_init(&entry->ent, hash);

sequencer.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -5217,8 +5217,11 @@ int todo_list_rearrange_squash(struct todo_list *todo_list)
52175217
break;
52185218
}
52195219

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)
52225225
/* found by title */
52235226
i2 = entry->i;
52245227
else if (!strchr(p, ' ') &&

sub-process.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ struct subprocess_entry *subprocess_find_entry(struct hashmap *hashmap, const ch
2222

2323
hashmap_entry_init(&key.ent, strhash(cmd));
2424
key.cmd = cmd;
25-
return hashmap_get(hashmap, &key.ent, NULL);
25+
return hashmap_get_entry(hashmap, &key, NULL,
26+
struct subprocess_entry, ent);
2627
}
2728

2829
int subprocess_read_status(int fd, struct strbuf *status)

submodule-config.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
166166
hashmap_entry_init(&key.ent, hash);
167167
key.config = &key_config;
168168

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);
170171
if (entry)
171172
return entry->config;
172173
return NULL;
@@ -186,7 +187,8 @@ static struct submodule *cache_lookup_name(struct submodule_cache *cache,
186187
hashmap_entry_init(&key.ent, hash);
187188
key.config = &key_config;
188189

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);
190192
if (entry)
191193
return entry->config;
192194
return NULL;

0 commit comments

Comments
 (0)