hashmap.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
  2. /*
  3. * Generic non-thread safe hash map implementation.
  4. *
  5. * Copyright (c) 2019 Facebook
  6. */
  7. #ifndef __LIBBPF_HASHMAP_H
  8. #define __LIBBPF_HASHMAP_H
  9. #include <stdbool.h>
  10. #include <stddef.h>
  11. #include <limits.h>
  12. static inline size_t hash_bits(size_t h, int bits)
  13. {
  14. /* shuffle bits and return requested number of upper bits */
  15. if (bits == 0)
  16. return 0;
  17. #if (__SIZEOF_SIZE_T__ == __SIZEOF_LONG_LONG__)
  18. /* LP64 case */
  19. return (h * 11400714819323198485llu) >> (__SIZEOF_LONG_LONG__ * 8 - bits);
  20. #elif (__SIZEOF_SIZE_T__ <= __SIZEOF_LONG__)
  21. return (h * 2654435769lu) >> (__SIZEOF_LONG__ * 8 - bits);
  22. #else
  23. # error "Unsupported size_t size"
  24. #endif
  25. }
  26. /* generic C-string hashing function */
  27. static inline size_t str_hash(const char *s)
  28. {
  29. size_t h = 0;
  30. while (*s) {
  31. h = h * 31 + *s;
  32. s++;
  33. }
  34. return h;
  35. }
  36. typedef size_t (*hashmap_hash_fn)(const void *key, void *ctx);
  37. typedef bool (*hashmap_equal_fn)(const void *key1, const void *key2, void *ctx);
  38. struct hashmap_entry {
  39. const void *key;
  40. void *value;
  41. struct hashmap_entry *next;
  42. };
  43. struct hashmap {
  44. hashmap_hash_fn hash_fn;
  45. hashmap_equal_fn equal_fn;
  46. void *ctx;
  47. struct hashmap_entry **buckets;
  48. size_t cap;
  49. size_t cap_bits;
  50. size_t sz;
  51. };
  52. #define HASHMAP_INIT(hash_fn, equal_fn, ctx) { \
  53. .hash_fn = (hash_fn), \
  54. .equal_fn = (equal_fn), \
  55. .ctx = (ctx), \
  56. .buckets = NULL, \
  57. .cap = 0, \
  58. .cap_bits = 0, \
  59. .sz = 0, \
  60. }
  61. void hashmap__init(struct hashmap *map, hashmap_hash_fn hash_fn,
  62. hashmap_equal_fn equal_fn, void *ctx);
  63. struct hashmap *hashmap__new(hashmap_hash_fn hash_fn,
  64. hashmap_equal_fn equal_fn,
  65. void *ctx);
  66. void hashmap__clear(struct hashmap *map);
  67. void hashmap__free(struct hashmap *map);
  68. size_t hashmap__size(const struct hashmap *map);
  69. size_t hashmap__capacity(const struct hashmap *map);
  70. /*
  71. * Hashmap insertion strategy:
  72. * - HASHMAP_ADD - only add key/value if key doesn't exist yet;
  73. * - HASHMAP_SET - add key/value pair if key doesn't exist yet; otherwise,
  74. * update value;
  75. * - HASHMAP_UPDATE - update value, if key already exists; otherwise, do
  76. * nothing and return -ENOENT;
  77. * - HASHMAP_APPEND - always add key/value pair, even if key already exists.
  78. * This turns hashmap into a multimap by allowing multiple values to be
  79. * associated with the same key. Most useful read API for such hashmap is
  80. * hashmap__for_each_key_entry() iteration. If hashmap__find() is still
  81. * used, it will return last inserted key/value entry (first in a bucket
  82. * chain).
  83. */
  84. enum hashmap_insert_strategy {
  85. HASHMAP_ADD,
  86. HASHMAP_SET,
  87. HASHMAP_UPDATE,
  88. HASHMAP_APPEND,
  89. };
  90. /*
  91. * hashmap__insert() adds key/value entry w/ various semantics, depending on
  92. * provided strategy value. If a given key/value pair replaced already
  93. * existing key/value pair, both old key and old value will be returned
  94. * through old_key and old_value to allow calling code do proper memory
  95. * management.
  96. */
  97. int hashmap__insert(struct hashmap *map, const void *key, void *value,
  98. enum hashmap_insert_strategy strategy,
  99. const void **old_key, void **old_value);
  100. static inline int hashmap__add(struct hashmap *map,
  101. const void *key, void *value)
  102. {
  103. return hashmap__insert(map, key, value, HASHMAP_ADD, NULL, NULL);
  104. }
  105. static inline int hashmap__set(struct hashmap *map,
  106. const void *key, void *value,
  107. const void **old_key, void **old_value)
  108. {
  109. return hashmap__insert(map, key, value, HASHMAP_SET,
  110. old_key, old_value);
  111. }
  112. static inline int hashmap__update(struct hashmap *map,
  113. const void *key, void *value,
  114. const void **old_key, void **old_value)
  115. {
  116. return hashmap__insert(map, key, value, HASHMAP_UPDATE,
  117. old_key, old_value);
  118. }
  119. static inline int hashmap__append(struct hashmap *map,
  120. const void *key, void *value)
  121. {
  122. return hashmap__insert(map, key, value, HASHMAP_APPEND, NULL, NULL);
  123. }
  124. bool hashmap__delete(struct hashmap *map, const void *key,
  125. const void **old_key, void **old_value);
  126. bool hashmap__find(const struct hashmap *map, const void *key, void **value);
  127. /*
  128. * hashmap__for_each_entry - iterate over all entries in hashmap
  129. * @map: hashmap to iterate
  130. * @cur: struct hashmap_entry * used as a loop cursor
  131. * @bkt: integer used as a bucket loop cursor
  132. */
  133. #define hashmap__for_each_entry(map, cur, bkt) \
  134. for (bkt = 0; bkt < map->cap; bkt++) \
  135. for (cur = map->buckets[bkt]; cur; cur = cur->next)
  136. /*
  137. * hashmap__for_each_entry_safe - iterate over all entries in hashmap, safe
  138. * against removals
  139. * @map: hashmap to iterate
  140. * @cur: struct hashmap_entry * used as a loop cursor
  141. * @tmp: struct hashmap_entry * used as a temporary next cursor storage
  142. * @bkt: integer used as a bucket loop cursor
  143. */
  144. #define hashmap__for_each_entry_safe(map, cur, tmp, bkt) \
  145. for (bkt = 0; bkt < map->cap; bkt++) \
  146. for (cur = map->buckets[bkt]; \
  147. cur && ({tmp = cur->next; true; }); \
  148. cur = tmp)
  149. /*
  150. * hashmap__for_each_key_entry - iterate over entries associated with given key
  151. * @map: hashmap to iterate
  152. * @cur: struct hashmap_entry * used as a loop cursor
  153. * @key: key to iterate entries for
  154. */
  155. #define hashmap__for_each_key_entry(map, cur, _key) \
  156. for (cur = map->buckets \
  157. ? map->buckets[hash_bits(map->hash_fn((_key), map->ctx), map->cap_bits)] \
  158. : NULL; \
  159. cur; \
  160. cur = cur->next) \
  161. if (map->equal_fn(cur->key, (_key), map->ctx))
  162. #define hashmap__for_each_key_entry_safe(map, cur, tmp, _key) \
  163. for (cur = map->buckets \
  164. ? map->buckets[hash_bits(map->hash_fn((_key), map->ctx), map->cap_bits)] \
  165. : NULL; \
  166. cur && ({ tmp = cur->next; true; }); \
  167. cur = tmp) \
  168. if (map->equal_fn(cur->key, (_key), map->ctx))
  169. #endif /* __LIBBPF_HASHMAP_H */