hashmap: use *_entry APIs to wrap container_of

Using `container_of' can be verbose and choosing names for
intermediate "struct hashmap_entry" pointers is a hard problem.
So introduce "*_entry" APIs inspired by similar linked-list
APIs in the Linux kernel.

Unfortunately, `__typeof__' is not portable C, so we need an
extra parameter to specify the type.

Signed-off-by: Eric Wong <e@80x24.org>
Reviewed-by: Derrick Stolee <stolee@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Eric Wong
2019-10-06 23:30:35 +00:00
committed by Junio C Hamano
parent 6bcbdfb277
commit f0e63c4113
6 changed files with 73 additions and 40 deletions

View File

@@ -702,17 +702,16 @@ void adjust_dirname_case(struct index_state *istate, char *name)
struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int icase)
{
struct cache_entry *ce;
struct hashmap_entry *ent;
unsigned int hash = memihash(name, namelen);
lazy_init_name_hash(istate);
ent = hashmap_get_from_hash(&istate->name_hash,
memihash(name, namelen), NULL);
while (ent) {
ce = container_of(ent, struct cache_entry, ent);
ce = hashmap_get_entry_from_hash(&istate->name_hash, hash, NULL,
struct cache_entry, ent);
hashmap_for_each_entry_from(&istate->name_hash, ce,
struct cache_entry, ent) {
if (same_name(ce, name, namelen, icase))
return ce;
ent = hashmap_get_next(&istate->name_hash, ent);
}
return NULL;
}