5 * Generic implementation of hash-based key-value mappings.
6 * See Documentation/technical/api-hashmap.txt.
11 extern unsigned int strhash(const char *buf);
12 extern unsigned int strihash(const char *buf);
13 extern unsigned int memhash(const void *buf, size_t len);
14 extern unsigned int memihash(const void *buf, size_t len);
15 extern unsigned int memihash_cont(unsigned int hash_seed, const void *buf, size_t len);
17 static inline unsigned int sha1hash(const unsigned char *sha1)
20 * Equivalent to 'return *(unsigned int *)sha1;', but safe on
21 * platforms that don't support unaligned reads.
24 memcpy(&hash, sha1, sizeof(hash));
30 struct hashmap_entry {
31 struct hashmap_entry *next;
35 typedef int (*hashmap_cmp_fn)(const void *entry, const void *entry_or_key,
39 struct hashmap_entry **table;
41 unsigned int size, tablesize, grow_at, shrink_at;
42 unsigned disallow_rehash : 1;
47 struct hashmap_entry *next;
48 unsigned int tablepos;
51 /* hashmap functions */
53 extern void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function,
55 extern void hashmap_free(struct hashmap *map, int free_entries);
57 /* hashmap_entry functions */
59 static inline void hashmap_entry_init(void *entry, unsigned int hash)
61 struct hashmap_entry *e = entry;
65 extern void *hashmap_get(const struct hashmap *map, const void *key,
67 extern void *hashmap_get_next(const struct hashmap *map, const void *entry);
68 extern void hashmap_add(struct hashmap *map, void *entry);
69 extern void *hashmap_put(struct hashmap *map, void *entry);
70 extern void *hashmap_remove(struct hashmap *map, const void *key,
73 static inline void *hashmap_get_from_hash(const struct hashmap *map,
74 unsigned int hash, const void *keydata)
76 struct hashmap_entry key;
77 hashmap_entry_init(&key, hash);
78 return hashmap_get(map, &key, keydata);
81 int hashmap_bucket(const struct hashmap *map, unsigned int hash);
84 * Disallow/allow rehashing of the hashmap.
85 * This is useful if the caller knows that the hashmap
86 * needs multi-threaded access. The caller is still
87 * required to guard/lock searches and inserts in a
88 * manner appropriate to their usage. This simply
89 * prevents the table from being unexpectedly re-mapped.
91 * If is up to the caller to ensure that the hashmap is
92 * initialized to a reasonable size to prevent poor
95 * When value=1, prevent future rehashes on adds and deleted.
96 * When value=0, allow future rehahses. This DOES NOT force
99 static inline void hashmap_disallow_rehash(struct hashmap *map, unsigned value)
101 map->disallow_rehash = value;
104 /* hashmap_iter functions */
106 extern void hashmap_iter_init(struct hashmap *map, struct hashmap_iter *iter);
107 extern void *hashmap_iter_next(struct hashmap_iter *iter);
108 static inline void *hashmap_iter_first(struct hashmap *map,
109 struct hashmap_iter *iter)
111 hashmap_iter_init(map, iter);
112 return hashmap_iter_next(iter);
115 /* string interning */
117 extern const void *memintern(const void *data, size_t len);
118 static inline const char *strintern(const char *string)
120 return memintern(string, strlen(string));