1 #ifndef CCAN_HASHTABLE_H
2 #define CCAN_HASHTABLE_H
5 #include <ccan/typesafe_cb/typesafe_cb.h>
10 * hashtable_new - allocate a hash tree.
11 * @rehash: hash function to use for rehashing.
12 * @priv: private argument to @rehash function.
14 struct hashtable *hashtable_new(unsigned long (*rehash)(const void *elem,
19 * hashtable_free - dellocate a hash tree.
21 * This doesn't do anything to any pointers left in it.
23 void hashtable_free(struct hashtable *);
26 * hashtable_find - look for an object in a hash tree.
28 * @hash: the hash value of the object you are seeking
29 * @cmp: a comparison function: returns true if the object is found
30 * @cmpdata: the data to hand as second arg to @cmp
32 * Note that you can do all the work inside the cmp function if you want to:
33 * hashtable_find will return @htelem if @cmp returns true.
35 void *hashtable_find(struct hashtable *ht,
37 bool (*cmp)(const void *htelem, void *cmpdata),
41 * hashtable_add - add an (aligned) pointer into a hash tree.
43 * @hash: the hash value of the object
44 * @p: the non-NULL pointer, usually from malloc or equiv.
46 * Note that this implementation (ab)uses the lower bits of the pointer, so
47 * it will abort if the pointer is not aligned to 8 bytes.
49 * Also note that this can only fail due to allocation failure. Otherwise, it
52 bool hashtable_add(struct hashtable *ht, unsigned long hash, const void *p);
55 * hashtable_del - remove a pointer from a hash tree
57 * @hash: the hash value of the object
60 * Returns true if the pointer was found (and deleted).
62 bool hashtable_del(struct hashtable *ht, unsigned long hash, const void *p);
65 * hashtable_traverse - call a function on every pointer in hash tree
67 * @type: the type of the element in the hashtable.
68 * @cb: the callback: returns true to abort traversal.
69 * @cbarg: the argument to the callback
71 * Note that your traversal callback may delete any entry (it won't crash),
72 * but it may make the traverse unreliable.
74 #define hashtable_traverse(ht, type, cb, cbarg) \
75 _hashtable_traverse(ht, cast_if_type(bool (*)(void *, void *), \
76 cast_if_any(bool (*)(void *, \
78 bool (*)(const type *, \
79 const typeof(*cbarg) *), \
81 const typeof(*cbarg) *), \
82 bool (*)(const type *, \
88 void _hashtable_traverse(struct hashtable *ht,
89 bool (*cb)(void *p, void *cbarg), void *cbarg);
90 #endif /* CCAN_HASHTABLE_H */