]> git.ozlabs.org Git - ccan/blob - ccan/htable/htable.h
938b43afc89ce5a6603c45761da17d26b748475e
[ccan] / ccan / htable / htable.h
1 /* Licensed under LGPLv2+ - see LICENSE file for details */
2 #ifndef CCAN_HTABLE_H
3 #define CCAN_HTABLE_H
4 #include "config.h"
5 #include <ccan/str/str.h>
6 #include <stdint.h>
7 #include <stdbool.h>
8 #include <stdlib.h>
9
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)
14 #else
15 #define htable_debug(h, loc) ((void)loc, h)
16 #endif
17
18 /**
19  * struct htable - private definition of a htable.
20  *
21  * It's exposed here so you can put it in your structures and so we can
22  * supply inline functions.
23  */
24 struct htable {
25         size_t (*rehash)(const void *elem, void *priv);
26         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;
31         uintptr_t *table;
32 };
33
34 /**
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.
39  *
40  * This is useful for setting up static and global hash tables.
41  *
42  * Example:
43  *      // For simplicity's sake, say hash value is contents of elem.
44  *      static size_t rehash(const void *elem, void *unused)
45  *      {
46  *              (void)unused;
47  *              return *(size_t *)elem;
48  *      }
49  *      static struct htable ht = HTABLE_INITIALIZER(ht, rehash, NULL);
50  */
51 #define HTABLE_INITIALIZER(name, rehash, priv)                          \
52         { rehash, priv, 0, 0, 0, 0, -1, 0, &name.common_bits }
53
54 /**
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.
59  */
60 void htable_init(struct htable *ht,
61                  size_t (*rehash)(const void *elem, void *priv), void *priv);
62
63 /**
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.
69  *
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
72  * @size htable_adds.
73  */
74 bool htable_init_sized(struct htable *ht,
75                        size_t (*rehash)(const void *elem, void *priv),
76                        void *priv, size_t size);
77
78 /**
79  * htable_clear - empty a hash table.
80  * @ht: the hash table to clear
81  *
82  * This doesn't do anything to any pointers left in it.
83  */
84 void htable_clear(struct htable *ht);
85
86
87 /**
88  * htable_check - check hash table for consistency
89  * @ht: the htable
90  * @abortstr: the location to print on aborting, or NULL.
91  *
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.
96  *
97  * Returns the htable if it is consistent, NULL if not (it can never return
98  * NULL if @abortstr is set).
99  */
100 struct htable *htable_check(const struct htable *ht, const char *abortstr);
101
102 /**
103  * htable_copy - duplicate a hash table.
104  * @dst: the hash table to overwrite
105  * @src: the hash table to copy
106  *
107  * Only fails on out-of-memory.
108  *
109  * Equivalent to (but faster than):
110  *    if (!htable_init_sized(dst, src->rehash, src->priv, 1U << src->bits))
111  *         return false;
112  *    v = htable_first(src, &i);
113  *    while (v) {
114  *              htable_add(dst, v);
115  *              v = htable_next(src, i);
116  *    }
117  *    return true;
118  */
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);
121
122 /**
123  * htable_add - add a pointer into a hash table.
124  * @ht: the htable
125  * @hash: the hash value of the object
126  * @p: the non-NULL pointer
127  *
128  * Also note that this can only fail due to allocation failure.  Otherwise, it
129  * returns true.
130  */
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);
134
135 /**
136  * htable_del - remove a pointer from a hash table
137  * @ht: the htable
138  * @hash: the hash value of the object
139  * @p: the pointer
140  *
141  * Returns true if the pointer was found (and deleted).
142  */
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);
146
147 /**
148  * struct htable_iter - iterator or htable_first or htable_firstval etc.
149  *
150  * This refers to a location inside the hashtable.
151  */
152 struct htable_iter {
153         size_t off;
154 };
155
156 /**
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
161  *
162  * You'll need to check the value is what you want; returns NULL if none.
163  * See Also:
164  *      htable_delval()
165  */
166 #define htable_firstval(htable, i, hash) \
167         htable_firstval_(htable_debug(htable, HTABLE_LOC), i, hash)
168
169 void *htable_firstval_(const struct htable *htable,
170                        struct htable_iter *i, size_t hash);
171
172 /**
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
177  *
178  * You'll need to check the value is what you want; returns NULL if no more.
179  */
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);
184
185 /**
186  * htable_get - find an entry in the hash table
187  * @ht: the hashtable
188  * @h: the hash value of the entry
189  * @cmp: the comparison function
190  * @ptr: the pointer to hand to the comparison function.
191  *
192  * Convenient inline wrapper for htable_firstval/htable_nextval loop.
193  */
194 static inline void *htable_get(const struct htable *ht,
195                                size_t h,
196                                bool (*cmp)(const void *candidate, void *ptr),
197                                const void *ptr)
198 {
199         struct htable_iter i;
200         void *c;
201
202         for (c = htable_firstval(ht,&i,h); c; c = htable_nextval(ht,&i,h)) {
203                 if (cmp(c, (void *)ptr))
204                         return c;
205         }
206         return NULL;
207 }
208
209 /**
210  * htable_first - find an entry in the hash table
211  * @ht: the hashtable
212  * @i: the struct htable_iter to initialize
213  *
214  * Get an entry in the hashtable; NULL if empty.
215  */
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);
219
220 /**
221  * htable_next - find another entry in the hash table
222  * @ht: the hashtable
223  * @i: the struct htable_iter to use
224  *
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.
227  */
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);
231
232 /**
233  * htable_prev - find the previous entry in the hash table
234  * @ht: the hashtable
235  * @i: the struct htable_iter to use
236  *
237  * Get previous entry in the hashtable; NULL if all done.
238  *
239  * "previous" here only means the item that would have been returned by
240  * htable_next() before the item it returned most recently.
241  *
242  * This is usually used in the middle of (or after) a htable_next iteration and
243  * to "unwind" actions taken.
244  */
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);
248
249 /**
250  * htable_delval - remove an iterated pointer from a hash table
251  * @ht: the htable
252  * @i: the htable_iter
253  *
254  * Usually used to delete a hash entry after it has been found with
255  * htable_firstval etc.
256  */
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);
260
261 /**
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)
265  */
266 void htable_set_allocator(void *(*alloc)(struct htable *, size_t len),
267                           void (*free)(struct htable *, void *p));
268 #endif /* CCAN_HTABLE_H */