]> www.infradead.org Git - linux.git/commitdiff
perf tools: Fix possible compiler warnings in hashmap
authorNamhyung Kim <namhyung@kernel.org>
Wed, 9 Oct 2024 20:20:08 +0000 (13:20 -0700)
committerNamhyung Kim <namhyung@kernel.org>
Mon, 14 Oct 2024 19:04:31 +0000 (12:04 -0700)
The hashmap__for_each_entry[_safe] is accessing 'map' as if it's a
pointer.  But it does without parentheses so passing a static hash map
with an ampersand (like &slab_hash below) caused compiler warnings due
to unmatched types.

  In file included from util/bpf_lock_contention.c:5:
  util/bpf_lock_contention.c: In function ‘exit_slab_cache_iter’:
  linux/tools/perf/util/hashmap.h:169:32: error: invalid type argument of ‘->’ (have ‘struct hashmap’)
    169 |         for (bkt = 0; bkt < map->cap; bkt++)                                \
        |                                ^~
  util/bpf_lock_contention.c:105:9: note: in expansion of macro ‘hashmap__for_each_entry’
    105 |         hashmap__for_each_entry(&slab_hash, cur, bkt)
        |         ^~~~~~~~~~~~~~~~~~~~~~~
  /home/namhyung/project/linux/tools/perf/util/hashmap.h:170:31: error: invalid type argument of ‘->’ (have ‘struct hashmap’)
    170 |                 for (cur = map->buckets[bkt]; cur; cur = cur->next)
        |                               ^~
  util/bpf_lock_contention.c:105:9: note: in expansion of macro ‘hashmap__for_each_entry’
    105 |         hashmap__for_each_entry(&slab_hash, cur, bkt)
        |         ^~~~~~~~~~~~~~~~~~~~~~~

Cc: bpf@vger.kernel.org
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20241009202009.884884-1-namhyung@kernel.org
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
tools/perf/util/hashmap.h

index c12f8320e6682d50d0ec6a12e3e4d15802b9972c..0c4f155e8eb745d9179bb1dbc43b2dac46c71895 100644 (file)
@@ -166,8 +166,8 @@ bool hashmap_find(const struct hashmap *map, long key, long *value);
  * @bkt: integer used as a bucket loop cursor
  */
 #define hashmap__for_each_entry(map, cur, bkt)                             \
-       for (bkt = 0; bkt < map->cap; bkt++)                                \
-               for (cur = map->buckets[bkt]; cur; cur = cur->next)
+       for (bkt = 0; bkt < (map)->cap; bkt++)                              \
+               for (cur = (map)->buckets[bkt]; cur; cur = cur->next)
 
 /*
  * hashmap__for_each_entry_safe - iterate over all entries in hashmap, safe
@@ -178,8 +178,8 @@ bool hashmap_find(const struct hashmap *map, long key, long *value);
  * @bkt: integer used as a bucket loop cursor
  */
 #define hashmap__for_each_entry_safe(map, cur, tmp, bkt)                   \
-       for (bkt = 0; bkt < map->cap; bkt++)                                \
-               for (cur = map->buckets[bkt];                               \
+       for (bkt = 0; bkt < (map)->cap; bkt++)                              \
+               for (cur = (map)->buckets[bkt];                             \
                     cur && ({tmp = cur->next; true; });                    \
                     cur = tmp)
 
@@ -190,19 +190,19 @@ bool hashmap_find(const struct hashmap *map, long key, long *value);
  * @key: key to iterate entries for
  */
 #define hashmap__for_each_key_entry(map, cur, _key)                        \
-       for (cur = map->buckets                                             \
-                    ? map->buckets[hash_bits(map->hash_fn((_key), map->ctx), map->cap_bits)] \
+       for (cur = (map)->buckets                                           \
+                    ? (map)->buckets[hash_bits((map)->hash_fn((_key), (map)->ctx), (map)->cap_bits)] \
                     : NULL;                                                \
             cur;                                                           \
             cur = cur->next)                                               \
-               if (map->equal_fn(cur->key, (_key), map->ctx))
+               if ((map)->equal_fn(cur->key, (_key), (map)->ctx))
 
 #define hashmap__for_each_key_entry_safe(map, cur, tmp, _key)              \
-       for (cur = map->buckets                                             \
-                    ? map->buckets[hash_bits(map->hash_fn((_key), map->ctx), map->cap_bits)] \
+       for (cur = (map)->buckets                                           \
+                    ? (map)->buckets[hash_bits((map)->hash_fn((_key), (map)->ctx), (map)->cap_bits)] \
                     : NULL;                                                \
             cur && ({ tmp = cur->next; true; });                           \
             cur = tmp)                                                     \
-               if (map->equal_fn(cur->key, (_key), map->ctx))
+               if ((map)->equal_fn(cur->key, (_key), (map)->ctx))
 
 #endif /* __LIBBPF_HASHMAP_H */