OFFSETOF_VAR macro to simplify hashmap iterators
While we cannot rely on a `__typeof__' operator being portable to use with `offsetof'; we can calculate the pointer offset using an existing pointer and the address of a member using pointer arithmetic for compilers without `__typeof__'. This allows us to simplify usage of hashmap iterator macros by not having to specify a type when a pointer of that type is already given. In the future, list iterator macros (e.g. list_for_each_entry) may also be implemented using OFFSETOF_VAR to save hackers the trouble of using container_of/list_entry macros and without relying on non-portable `__typeof__'. v3: use `__typeof__' to avoid clang warnings 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:

committed by
Junio C Hamano

parent
c8e424c9c9
commit
23dee69f53
@ -205,10 +205,8 @@ int cmd__hashmap(int argc, const char **argv)
|
||||
/* print result */
|
||||
if (!entry)
|
||||
puts("NULL");
|
||||
hashmap_for_each_entry_from(&map, entry,
|
||||
struct test_entry, ent) {
|
||||
hashmap_for_each_entry_from(&map, entry, ent)
|
||||
puts(get_value(entry));
|
||||
}
|
||||
|
||||
} else if (!strcmp("remove", cmd) && p1) {
|
||||
|
||||
@ -230,7 +228,6 @@ int cmd__hashmap(int argc, const char **argv)
|
||||
struct hashmap_iter iter;
|
||||
|
||||
hashmap_for_each_entry(&map, &iter, entry,
|
||||
struct test_entry,
|
||||
ent /* member name */)
|
||||
printf("%s %s\n", entry->key, get_value(entry));
|
||||
|
||||
|
Reference in New Issue
Block a user