[PATCH v1 26/26] perf hashmap: Remove untyped struct hashmap API

From: Ian Rogers

Date: Fri Nov 13 2026 - 19:06:51 EST


Now that all hashmap users in tools/perf have been migrated to
DECLARE_HASHMAP(), remove the legacy untyped struct hashmap wrapper,
hashmap__new(), __hashmap__new(), __hashmap__free(), the
untyped-hashmap compatibility branches in hashmap__insert/find/delete,
and the unused hashmap__for_each_entry* macros from util/hashmap.h, and
rename struct hashmap_entry to struct __hashmap_entry.

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/util/hashmap.c | 75 +++++++----------
tools/perf/util/hashmap.h | 173 +++++++-------------------------------
2 files changed, 62 insertions(+), 186 deletions(-)

diff --git a/tools/perf/util/hashmap.c b/tools/perf/util/hashmap.c
index f6bfaeaee9d7..946105919197 100644
--- a/tools/perf/util/hashmap.c
+++ b/tools/perf/util/hashmap.c
@@ -21,15 +21,15 @@
/* start with 4 buckets */
#define HASHMAP_MIN_CAP_BITS 2

-static void __hashmap__add_entry(struct hashmap_entry **pprev,
- struct hashmap_entry *entry)
+static void __hashmap__add_entry(struct __hashmap_entry **pprev,
+ struct __hashmap_entry *entry)
{
entry->next = *pprev;
*pprev = entry;
}

-static void __hashmap__del_entry(struct hashmap_entry **pprev,
- struct hashmap_entry *entry)
+static void __hashmap__del_entry(struct __hashmap_entry **pprev,
+ struct __hashmap_entry *entry)
{
*pprev = entry->next;
entry->next = NULL;
@@ -48,42 +48,23 @@ void __hashmap__init(struct __hashmap *map, hashmap_hash_fn hash_fn,
map->sz = 0;
}

-struct __hashmap *__hashmap__new(hashmap_hash_fn hash_fn,
- hashmap_equal_fn equal_fn,
- void *ctx)
-{
- struct __hashmap *map = malloc(sizeof(*map));
-
- if (!map) {
- errno = ENOMEM;
- return NULL;
- }
- __hashmap__init(map, hash_fn, equal_fn, ctx);
- return map;
-}
-
void __hashmap__clear(struct __hashmap *map)
{
- struct hashmap_entry *cur, *tmp;
- size_t bkt;
+ for (size_t bkt = 0; bkt < map->cap; bkt++) {
+ struct __hashmap_entry *cur = map->buckets[bkt];
+
+ while (cur) {
+ struct __hashmap_entry *next = cur->next;

- hashmap__for_each_entry_safe(map, cur, tmp, bkt) {
- free(cur);
+ free(cur);
+ cur = next;
+ }
}
free(map->buckets);
map->buckets = NULL;
map->cap = map->cap_bits = map->sz = 0;
}

-void __hashmap__free(struct __hashmap *map)
-{
- if (!map)
- return;
-
- __hashmap__clear(map);
- free(map);
-}
-
size_t __hashmap__size(const struct __hashmap *map)
{
return map->sz;
@@ -102,10 +83,9 @@ static bool __hashmap__needs_to_grow(struct __hashmap *map)

static int __hashmap__grow(struct __hashmap *map)
{
- struct hashmap_entry **new_buckets;
- struct hashmap_entry *cur, *tmp;
+ struct __hashmap_entry **new_buckets;
size_t new_cap_bits, new_cap;
- size_t h, bkt;
+ size_t h;

new_cap_bits = map->cap_bits + 1;
if (new_cap_bits < HASHMAP_MIN_CAP_BITS)
@@ -116,9 +96,16 @@ static int __hashmap__grow(struct __hashmap *map)
if (!new_buckets)
return -ENOMEM;

- hashmap__for_each_entry_safe(map, cur, tmp, bkt) {
- h = hash_bits(map->hash_fn(cur->key, map->ctx), new_cap_bits);
- __hashmap__add_entry(&new_buckets[h], cur);
+ for (size_t bkt = 0; bkt < map->cap; bkt++) {
+ struct __hashmap_entry *cur = map->buckets[bkt];
+
+ while (cur) {
+ struct __hashmap_entry *next = cur->next;
+
+ h = hash_bits(map->hash_fn(cur->key, map->ctx), new_cap_bits);
+ __hashmap__add_entry(&new_buckets[h], cur);
+ cur = next;
+ }
}

map->cap = new_cap;
@@ -131,10 +118,10 @@ static int __hashmap__grow(struct __hashmap *map)

static bool __hashmap__find_entry(const struct __hashmap *map,
const long key, size_t hash,
- struct hashmap_entry ***pprev,
- struct hashmap_entry **entry)
+ struct __hashmap_entry ***pprev,
+ struct __hashmap_entry **entry)
{
- struct hashmap_entry *cur, **prev_ptr;
+ struct __hashmap_entry *cur, **prev_ptr;

if (!map->buckets)
return false;
@@ -157,7 +144,7 @@ int __hashmap__insert(struct __hashmap *map, long key, long value,
enum hashmap_insert_strategy strategy,
void *old_key, void *old_value)
{
- struct hashmap_entry *entry;
+ struct __hashmap_entry *entry;
size_t h;
int err;

@@ -193,7 +180,7 @@ int __hashmap__insert(struct __hashmap *map, long key, long value,
h = hash_bits(map->hash_fn(key, map->ctx), map->cap_bits);
}

- entry = malloc(sizeof(struct hashmap_entry));
+ entry = malloc(sizeof(struct __hashmap_entry));
if (!entry)
return -ENOMEM;

@@ -207,7 +194,7 @@ int __hashmap__insert(struct __hashmap *map, long key, long value,

bool __hashmap__find(const struct __hashmap *map, long key, void *value)
{
- struct hashmap_entry *entry;
+ struct __hashmap_entry *entry;
size_t h;

h = hash_bits(map->hash_fn(key, map->ctx), map->cap_bits);
@@ -222,7 +209,7 @@ bool __hashmap__find(const struct __hashmap *map, long key, void *value)
bool __hashmap__delete(struct __hashmap *map, long key,
void *old_key, void *old_value)
{
- struct hashmap_entry **pprev, *entry;
+ struct __hashmap_entry **pprev, *entry;
size_t h;

if (old_key)
diff --git a/tools/perf/util/hashmap.h b/tools/perf/util/hashmap.h
index bb85a65486be..e515443d3c70 100644
--- a/tools/perf/util/hashmap.h
+++ b/tools/perf/util/hashmap.h
@@ -92,16 +92,10 @@ typedef bool (*hashmap_equal_fn)(long key1, long key2, void *ctx);
* (e.g. bool, int, pid_t) are safe from buffer overflows, endianness bugs,
* and strict-aliasing violations.
*/
-struct hashmap_entry {
- union {
- long key;
- const void *pkey;
- };
- union {
- long value;
- void *pvalue;
- };
- struct hashmap_entry *next;
+struct __hashmap_entry {
+ long key;
+ long value;
+ struct __hashmap_entry *next;
};

struct __hashmap {
@@ -109,57 +103,19 @@ struct __hashmap {
hashmap_equal_fn equal_fn;
void *ctx;

- struct hashmap_entry **buckets;
+ struct __hashmap_entry **buckets;
size_t cap;
size_t cap_bits;
size_t sz;
};

-/* Legacy untyped hashmap, kept while migrating callers to DECLARE_HASHMAP. */
-struct hashmap {
- union {
- struct __hashmap map;
- struct {
- hashmap_hash_fn hash_fn;
- hashmap_equal_fn equal_fn;
- void *ctx;
-
- struct hashmap_entry **buckets;
- size_t cap;
- size_t cap_bits;
- size_t sz;
- };
- long *__key;
- long *__value;
- char __untyped;
- };
-};
-
void __hashmap__init(struct __hashmap *map, hashmap_hash_fn hash_fn,
hashmap_equal_fn equal_fn, void *ctx);
-struct __hashmap *__hashmap__new(hashmap_hash_fn hash_fn,
- hashmap_equal_fn equal_fn,
- void *ctx);
void __hashmap__clear(struct __hashmap *map);
-void __hashmap__free(struct __hashmap *map);

size_t __hashmap__size(const struct __hashmap *map);
size_t __hashmap__capacity(const struct __hashmap *map);

-static inline struct hashmap *hashmap__new(hashmap_hash_fn hash_fn,
- hashmap_equal_fn equal_fn,
- void *ctx)
-{
- struct hashmap *map = malloc(sizeof(*map));
-
- if (!map) {
- errno = ENOMEM;
- return NULL;
- }
- __hashmap__init(&map->map, hash_fn, equal_fn, ctx);
- return map;
-}
-
/**
* enum hashmap_insert_strategy - Strategy for hashmap__insert().
* @HASHMAP_ADD: Only add key/value if key doesn't exist yet; returns -EEXIST
@@ -195,8 +151,8 @@ bool __hashmap__find(const struct __hashmap *map, long key, void *value);
struct __hashmap_iter {
const struct __hashmap *map;
size_t bkt;
- struct hashmap_entry *cur;
- struct hashmap_entry *next;
+ struct __hashmap_entry *cur;
+ struct __hashmap_entry *next;
};

static inline struct __hashmap_iter
@@ -210,7 +166,7 @@ __hashmap_iter__init(const struct __hashmap *map)
};
}

-static inline struct hashmap_entry *
+static inline struct __hashmap_entry *
__hashmap_iter__advance(struct __hashmap_iter *it)
{
if (!it->map || !it->map->buckets)
@@ -232,14 +188,14 @@ __hashmap_iter__advance(struct __hashmap_iter *it)
struct __hashmap_key_iter {
const struct __hashmap *map;
long key;
- struct hashmap_entry *cur;
- struct hashmap_entry *next;
+ struct __hashmap_entry *cur;
+ struct __hashmap_entry *next;
};

static inline struct __hashmap_key_iter
__hashmap_key_iter__init(const struct __hashmap *map, long key)
{
- struct hashmap_entry *first = NULL;
+ struct __hashmap_entry *first = NULL;

if (map && map->buckets) {
size_t h = hash_bits(map->hash_fn(key, map->ctx), map->cap_bits);
@@ -254,11 +210,11 @@ __hashmap_key_iter__init(const struct __hashmap *map, long key)
};
}

-static inline struct hashmap_entry *
+static inline struct __hashmap_entry *
__hashmap_key_iter__advance(struct __hashmap_key_iter *it)
{
while (it->next) {
- struct hashmap_entry *entry = it->next;
+ struct __hashmap_entry *entry = it->next;

it->next = entry->next;
if (it->map->equal_fn(entry->key, it->key, it->map->ctx)) {
@@ -289,7 +245,6 @@ struct name { \
struct __hashmap map; \
key_type *__key; \
value_type *__value; \
- int __untyped; \
}; \
_Static_assert(sizeof(key_type) <= sizeof(long), \
#name " key_type exceeds sizeof(long)"); \
@@ -328,19 +283,6 @@ static inline __maybe_unused void name##__free(struct name *hmap) \
*(hm)->__key, (char *)NULL)) *)NULL, \
*(hm)->__key))

-#define ___hashmap__choose(hm, untyped_expr, typed_expr) \
- __builtin_choose_expr(sizeof((hm)->__untyped) == 1, \
- (untyped_expr), (typed_expr))
-
-#define ___hashmap__in_val(hm, expr) \
- ___hashmap__choose(hm, (long)(expr), (expr))
-
-#define ___hashmap__check_untyped_ptr(hm, ptr) \
- _Static_assert(sizeof((hm)->__untyped) != 1 || \
- (__builtin_constant_p((ptr)) ? (ptr) == NULL : 0) || \
- sizeof(*(ptr)) == sizeof(long), \
- #ptr " pointee should be a long-sized integer or a pointer")
-
/**
* hashmap__init() - Initialize an embedded/stack-allocated typed hashmap.
* @hmap: Pointer to a hashmap struct declared with DECLARE_HASHMAP().
@@ -397,25 +339,16 @@ do { \
__hashmap__capacity(&(hmap)->map)

#define ___hashmap__insert(hmap, key, value, strategy, old_key, old_value, \
- hm, k, v, okp, ovp, raw_ok, raw_ov, ok, ov, err) \
+ hm, k, v, okp, ovp, ok, ov, err) \
({ \
typeof(hmap) hm = (hmap); \
- typeof(*(hm)->__key) k = ___hashmap__in_val(hm, key); \
- typeof(*(hm)->__value) v = ___hashmap__in_val(hm, value); \
+ typeof(*(hm)->__key) k = (key); \
+ typeof(*(hm)->__value) v = (value); \
+ typeof((hm)->__key) okp = (old_key); \
+ typeof((hm)->__value) ovp = (old_value); \
long ok = 0, ov = 0; \
- typeof((hm)->__key) okp = \
- ___hashmap__choose(hm, (long *)NULL, (old_key)); \
- typeof((hm)->__value) ovp = \
- ___hashmap__choose(hm, (long *)NULL, (old_value)); \
- void *raw_ok = ___hashmap__choose(hm, (void *)(old_key), \
- (void *)(okp ? &ok : NULL)); \
- void *raw_ov = ___hashmap__choose(hm, (void *)(old_value), \
- (void *)(ovp ? &ov : NULL)); \
- int err; \
- ___hashmap__check_untyped_ptr(hm, old_key); \
- ___hashmap__check_untyped_ptr(hm, old_value); \
- err = __hashmap__insert(&(hm)->map, (long)k, (long)v, (strategy), \
- raw_ok, raw_ov); \
+ int err = __hashmap__insert(&(hm)->map, (long)k, (long)v, (strategy), \
+ okp ? &ok : NULL, ovp ? &ov : NULL); \
if (okp) \
*okp = (typeof(*(hm)->__key))ok; \
if (ovp) \
@@ -427,7 +360,6 @@ do { \
___hashmap__insert(hmap, key, value, strategy, old_key, old_value, \
__PASTE(__hm_, u), __PASTE(__k_, u), __PASTE(__v_, u), \
__PASTE(__okp_, u), __PASTE(__ovp_, u), \
- __PASTE(__rok_, u), __PASTE(__rov_, u), \
__PASTE(__ok_, u), __PASTE(__ov_, u), __PASTE(__err_, u))

/**
@@ -503,18 +435,13 @@ do { \
#define hashmap__append(hmap, key, value) \
hashmap__insert(hmap, key, value, HASHMAP_APPEND, NULL, NULL)

-#define ___hashmap__find(hmap, key, value_ptr, hm, k, vp, raw_vp, v, found) \
+#define ___hashmap__find(hmap, key, value_ptr, hm, k, vp, v, found) \
({ \
typeof(hmap) hm = (hmap); \
- ___hashmap__lookup_key_type(hm) k = ___hashmap__in_val(hm, key); \
+ ___hashmap__lookup_key_type(hm) k = (key); \
+ typeof((hm)->__value) vp = (value_ptr); \
long v = 0; \
- typeof((hm)->__value) vp = \
- ___hashmap__choose(hm, (long *)NULL, (value_ptr)); \
- void *raw_vp = ___hashmap__choose(hm, (void *)(value_ptr), \
- (void *)(vp ? &v : NULL)); \
- bool found; \
- ___hashmap__check_untyped_ptr(hm, value_ptr); \
- found = __hashmap__find(&(hm)->map, (long)k, raw_vp); \
+ bool found = __hashmap__find(&(hm)->map, (long)k, vp ? &v : NULL); \
if (found && vp) \
*vp = (typeof(*(hm)->__value))v; \
found; \
@@ -523,7 +450,7 @@ do { \
#define _hashmap__find(hmap, key, value_ptr, u) \
___hashmap__find(hmap, key, value_ptr, \
__PASTE(__hm_, u), __PASTE(__k_, u), __PASTE(__vp_, u), \
- __PASTE(__rvp_, u), __PASTE(__v_, u), __PASTE(__found_, u))
+ __PASTE(__v_, u), __PASTE(__found_, u))

/**
* hashmap__find() - Look up a key in a typed hashmap.
@@ -538,23 +465,15 @@ do { \
_hashmap__find(hmap, key, value_ptr, __COUNTER__)

#define ___hashmap__delete(hmap, key, old_key, old_value, \
- hm, k, okp, ovp, raw_ok, raw_ov, ok, ov, rem) \
+ hm, k, okp, ovp, ok, ov, rem) \
({ \
typeof(hmap) hm = (hmap); \
- ___hashmap__lookup_key_type(hm) k = ___hashmap__in_val(hm, key); \
+ ___hashmap__lookup_key_type(hm) k = (key); \
+ typeof((hm)->__key) okp = (old_key); \
+ typeof((hm)->__value) ovp = (old_value); \
long ok = 0, ov = 0; \
- typeof((hm)->__key) okp = \
- ___hashmap__choose(hm, (long *)NULL, (old_key)); \
- typeof((hm)->__value) ovp = \
- ___hashmap__choose(hm, (long *)NULL, (old_value)); \
- void *raw_ok = ___hashmap__choose(hm, (void *)(old_key), \
- (void *)(okp ? &ok : NULL)); \
- void *raw_ov = ___hashmap__choose(hm, (void *)(old_value), \
- (void *)(ovp ? &ov : NULL)); \
- bool rem; \
- ___hashmap__check_untyped_ptr(hm, old_key); \
- ___hashmap__check_untyped_ptr(hm, old_value); \
- rem = __hashmap__delete(&(hm)->map, (long)k, raw_ok, raw_ov); \
+ bool rem = __hashmap__delete(&(hm)->map, (long)k, \
+ okp ? &ok : NULL, ovp ? &ov : NULL); \
if (okp) \
*okp = (typeof(*(hm)->__key))ok; \
if (ovp) \
@@ -566,7 +485,6 @@ do { \
___hashmap__delete(hmap, key, old_key, old_value, \
__PASTE(__hm_, u), __PASTE(__k_, u), \
__PASTE(__okp_, u), __PASTE(__ovp_, u), \
- __PASTE(__rok_, u), __PASTE(__rov_, u), \
__PASTE(__ok_, u), __PASTE(__ov_, u), __PASTE(__rem_, u))

/**
@@ -660,33 +578,4 @@ do { \
#define hashmap__for_each_key_safe(hmap, key, val_var) \
hashmap__for_each_key(hmap, key, val_var)

-/*
- * Legacy entry iterators for unconverted struct hashmap callers.
- */
-#define hashmap__for_each_entry(map, cur, bkt) \
- for (bkt = 0; bkt < (map)->cap; bkt++) \
- for (cur = (map)->buckets[bkt]; cur; cur = cur->next)
-
-#define hashmap__for_each_entry_safe(map, cur, tmp, bkt) \
- for (bkt = 0; bkt < (map)->cap; bkt++) \
- for (cur = (map)->buckets[bkt]; \
- cur && ({tmp = cur->next; true; }); \
- cur = tmp)
-
-#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)] \
- : NULL; \
- cur; \
- cur = cur->next) \
- 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)] \
- : NULL; \
- cur && ({ tmp = cur->next; true; }); \
- cur = tmp) \
- if ((map)->equal_fn(cur->key, (_key), (map)->ctx))
-
#endif /* __PERF_UTIL_HASHMAP_H */
--
2.56.1.703.gf848ed807c-goog