Skip to content

Commit f23a465

Browse files
Eric Wonggitster
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 8 additions & 3 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 7 additions & 4 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 4 additions & 3 deletions
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

Lines changed: 7 additions & 5 deletions
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

Lines changed: 4 additions & 2 deletions
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,

0 commit comments

Comments
 (0)