1 /* Licensed under LGPLv2+ - see LICENSE file for details */
5 #include <ccan/str/str.h>
10 /* Define CCAN_HTABLE_DEBUG for expensive debugging checks on each call. */
11 #define HTABLE_LOC __FILE__ ":" stringify(__LINE__)
12 #ifdef CCAN_HTABLE_DEBUG
13 #define htable_debug(h, loc) htable_check((h), loc)
15 #define htable_debug(h, loc) ((void)loc, h)
19 * struct htable - private definition of a htable.
21 * It's exposed here so you can put it in your structures and so we can
22 * supply inline functions.
25 size_t (*rehash)(const void *elem, void *priv);
27 unsigned int bits, perfect_bitnum;
28 size_t elems, deleted;
29 /* These are the bits which are the same in all pointers. */
30 uintptr_t common_mask, common_bits;
35 * HTABLE_INITIALIZER - static initialization for a hash table.
36 * @name: name of this htable.
37 * @rehash: hash function to use for rehashing.
38 * @priv: private argument to @rehash function.
40 * This is useful for setting up static and global hash tables.
43 * // For simplicity's sake, say hash value is contents of elem.
44 * static size_t rehash(const void *elem, void *unused)
47 * return *(size_t *)elem;
49 * static struct htable ht = HTABLE_INITIALIZER(ht, rehash, NULL);
51 #define HTABLE_INITIALIZER(name, rehash, priv) \
52 { rehash, priv, 0, 0, 0, 0, -1, 0, &name.common_bits }
55 * htable_init - initialize an empty hash table.
56 * @ht: the hash table to initialize
57 * @rehash: hash function to use for rehashing.
58 * @priv: private argument to @rehash function.
60 void htable_init(struct htable *ht,
61 size_t (*rehash)(const void *elem, void *priv), void *priv);
64 * htable_init_sized - initialize an empty hash table of given size.
65 * @ht: the hash table to initialize
66 * @rehash: hash function to use for rehashing.
67 * @priv: private argument to @rehash function.
68 * @size: the number of element.
70 * If this returns false, @ht is still usable, but may need to do reallocation
71 * upon an add. If this returns true, it will not need to reallocate within
74 bool htable_init_sized(struct htable *ht,
75 size_t (*rehash)(const void *elem, void *priv),
76 void *priv, size_t size);
79 * htable_clear - empty a hash table.
80 * @ht: the hash table to clear
82 * This doesn't do anything to any pointers left in it.
84 void htable_clear(struct htable *ht);
88 * htable_check - check hash table for consistency
90 * @abortstr: the location to print on aborting, or NULL.
92 * Because hash tables have redundant information, consistency checking that
93 * each element is in the correct location can be done. This is useful as a
94 * debugging check. If @abortstr is non-NULL, that will be printed in a
95 * diagnostic if the htable is inconsistent, and the function will abort.
97 * Returns the htable if it is consistent, NULL if not (it can never return
98 * NULL if @abortstr is set).
100 struct htable *htable_check(const struct htable *ht, const char *abortstr);
103 * htable_copy - duplicate a hash table.
104 * @dst: the hash table to overwrite
105 * @src: the hash table to copy
107 * Only fails on out-of-memory.
109 * Equivalent to (but faster than):
110 * if (!htable_init_sized(dst, src->rehash, src->priv, 1U << src->bits))
112 * v = htable_first(src, &i);
114 * htable_add(dst, v);
115 * v = htable_next(src, i);
119 #define htable_copy(dst, src) htable_copy_(dst, htable_debug(src, HTABLE_LOC))
120 bool htable_copy_(struct htable *dst, const struct htable *src);
123 * htable_add - add a pointer into a hash table.
125 * @hash: the hash value of the object
126 * @p: the non-NULL pointer
128 * Also note that this can only fail due to allocation failure. Otherwise, it
131 #define htable_add(ht, hash, p) \
132 htable_add_(htable_debug(ht, HTABLE_LOC), hash, p)
133 bool htable_add_(struct htable *ht, size_t hash, const void *p);
136 * htable_del - remove a pointer from a hash table
138 * @hash: the hash value of the object
141 * Returns true if the pointer was found (and deleted).
143 #define htable_del(ht, hash, p) \
144 htable_del_(htable_debug(ht, HTABLE_LOC), hash, p)
145 bool htable_del_(struct htable *ht, size_t hash, const void *p);
148 * struct htable_iter - iterator or htable_first or htable_firstval etc.
150 * This refers to a location inside the hashtable.
157 * htable_firstval - find a candidate for a given hash value
158 * @htable: the hashtable
159 * @i: the struct htable_iter to initialize
160 * @hash: the hash value
162 * You'll need to check the value is what you want; returns NULL if none.
166 #define htable_firstval(htable, i, hash) \
167 htable_firstval_(htable_debug(htable, HTABLE_LOC), i, hash)
169 void *htable_firstval_(const struct htable *htable,
170 struct htable_iter *i, size_t hash);
173 * htable_nextval - find another candidate for a given hash value
174 * @htable: the hashtable
175 * @i: the struct htable_iter to initialize
176 * @hash: the hash value
178 * You'll need to check the value is what you want; returns NULL if no more.
180 #define htable_nextval(htable, i, hash) \
181 htable_nextval_(htable_debug(htable, HTABLE_LOC), i, hash)
182 void *htable_nextval_(const struct htable *htable,
183 struct htable_iter *i, size_t hash);
186 * htable_get - find an entry in the hash table
188 * @h: the hash value of the entry
189 * @cmp: the comparison function
190 * @ptr: the pointer to hand to the comparison function.
192 * Convenient inline wrapper for htable_firstval/htable_nextval loop.
194 static inline void *htable_get(const struct htable *ht,
196 bool (*cmp)(const void *candidate, void *ptr),
199 struct htable_iter i;
202 for (c = htable_firstval(ht,&i,h); c; c = htable_nextval(ht,&i,h)) {
203 if (cmp(c, (void *)ptr))
210 * htable_first - find an entry in the hash table
212 * @i: the struct htable_iter to initialize
214 * Get an entry in the hashtable; NULL if empty.
216 #define htable_first(htable, i) \
217 htable_first_(htable_debug(htable, HTABLE_LOC), i)
218 void *htable_first_(const struct htable *htable, struct htable_iter *i);
221 * htable_next - find another entry in the hash table
223 * @i: the struct htable_iter to use
225 * Get another entry in the hashtable; NULL if all done.
226 * This is usually used after htable_first or prior non-NULL htable_next.
228 #define htable_next(htable, i) \
229 htable_next_(htable_debug(htable, HTABLE_LOC), i)
230 void *htable_next_(const struct htable *htable, struct htable_iter *i);
233 * htable_prev - find the previous entry in the hash table
235 * @i: the struct htable_iter to use
237 * Get previous entry in the hashtable; NULL if all done.
239 * "previous" here only means the item that would have been returned by
240 * htable_next() before the item it returned most recently.
242 * This is usually used in the middle of (or after) a htable_next iteration and
243 * to "unwind" actions taken.
245 #define htable_prev(htable, i) \
246 htable_prev_(htable_debug(htable, HTABLE_LOC), i)
247 void *htable_prev_(const struct htable *htable, struct htable_iter *i);
250 * htable_delval - remove an iterated pointer from a hash table
252 * @i: the htable_iter
254 * Usually used to delete a hash entry after it has been found with
255 * htable_firstval etc.
257 #define htable_delval(htable, i) \
258 htable_delval_(htable_debug(htable, HTABLE_LOC), i)
259 void htable_delval_(struct htable *ht, struct htable_iter *i);
262 * htable_set_allocator - set calloc/free functions.
263 * @alloc: allocator to use, must zero memory!
264 * @free: unallocator to use (@p is NULL or a return from @alloc)
266 void htable_set_allocator(void *(*alloc)(struct htable *, size_t len),
267 void (*free)(struct htable *, void *p));
268 #endif /* CCAN_HTABLE_H */