X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fhtable%2Fhtable.h;h=ed668e7405ed84e819cec49b85f8186b85287933;hp=73f4da847a0864cd8912d5e8a624f7d4bb8a5a50;hb=926996e88c32445c874ff9c4f47f159db6b45995;hpb=74257cee33ae3033f961d5f22a0313b8cb1b18d4 diff --git a/ccan/htable/htable.h b/ccan/htable/htable.h index 73f4da84..ed668e74 100644 --- a/ccan/htable/htable.h +++ b/ccan/htable/htable.h @@ -2,25 +2,62 @@ #ifndef CCAN_HTABLE_H #define CCAN_HTABLE_H #include "config.h" +#include #include #include -struct htable; +/** + * struct htable - private definition of a htable. + * + * It's exposed here so you can put it in your structures and so we can + * supply inline functions. + */ +struct htable { + size_t (*rehash)(const void *elem, void *priv); + void *priv; + unsigned int bits; + size_t elems, deleted, max, max_with_deleted; + /* These are the bits which are the same in all pointers. */ + uintptr_t common_mask, common_bits; + uintptr_t perfect_bit; + uintptr_t *table; +}; + +/** + * HTABLE_INITIALIZER - static initialization for a hash table. + * @name: name of this htable. + * @rehash: hash function to use for rehashing. + * @priv: private argument to @rehash function. + * + * This is useful for setting up static and global hash tables. + * + * Example: + * // For simplicity's sake, say hash value is contents of elem. + * static size_t rehash(const void *elem, void *unused) + * { + * return *(size_t *)elem; + * } + * static struct htable ht = HTABLE_INITIALIZER(ht, rehash, NULL); + */ +#define HTABLE_INITIALIZER(name, rehash, priv) \ + { rehash, priv, 0, 0, 0, 0, 0, -1, 0, 0, &name.perfect_bit } /** - * htable_new - allocate a hash tree. + * htable_init - initialize an empty hash table. + * @ht: the hash table to initialize * @rehash: hash function to use for rehashing. * @priv: private argument to @rehash function. */ -struct htable *htable_new(size_t (*hash)(const void *elem, void *priv), - void *priv); +void htable_init(struct htable *ht, + size_t (*rehash)(const void *elem, void *priv), void *priv); /** - * htable_free - dellocate a hash tree. + * htable_clear - empty a hash table. + * @ht: the hash table to clear * * This doesn't do anything to any pointers left in it. */ -void htable_free(const struct htable *); +void htable_clear(struct htable *ht); /** * htable_rehash - use a hashtree's rehash function @@ -30,7 +67,7 @@ void htable_free(const struct htable *); size_t htable_rehash(const void *elem); /** - * htable_add - add a pointer into a hash tree. + * htable_add - add a pointer into a hash table. * @ht: the htable * @hash: the hash value of the object * @p: the non-NULL pointer @@ -41,7 +78,7 @@ size_t htable_rehash(const void *elem); bool htable_add(struct htable *ht, size_t hash, const void *p); /** - * htable_del - remove a pointer from a hash tree + * htable_del - remove a pointer from a hash table * @ht: the htable * @hash: the hash value of the object * @p: the pointer @@ -127,7 +164,7 @@ void *htable_first(const struct htable *htable, struct htable_iter *i); void *htable_next(const struct htable *htable, struct htable_iter *i); /** - * htable_delval - remove an iterated pointer from a hash tree + * htable_delval - remove an iterated pointer from a hash table * @ht: the htable * @i: the htable_iter *