X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fhtable%2Fhtable.h;h=53c447c09b952b198913efa25a48b82d34777242;hb=9d2d2c49f053018724bcc6e37029da10b7c3d60d;hp=ed668e7405ed84e819cec49b85f8186b85287933;hpb=60cc720d0797fc49325437ea36a9ffd909c75ed0;p=ccan diff --git a/ccan/htable/htable.h b/ccan/htable/htable.h index ed668e74..53c447c0 100644 --- a/ccan/htable/htable.h +++ b/ccan/htable/htable.h @@ -2,10 +2,19 @@ #ifndef CCAN_HTABLE_H #define CCAN_HTABLE_H #include "config.h" +#include #include #include #include +/* Define CCAN_HTABLE_DEBUG for expensive debugging checks on each call. */ +#define HTABLE_LOC __FILE__ ":" stringify(__LINE__) +#ifdef CCAN_HTABLE_DEBUG +#define htable_debug(h, loc) htable_check((h), loc) +#else +#define htable_debug(h, loc) ((void)loc, h) +#endif + /** * struct htable - private definition of a htable. * @@ -35,6 +44,7 @@ struct htable { * // For simplicity's sake, say hash value is contents of elem. * static size_t rehash(const void *elem, void *unused) * { + * (void)unused; * return *(size_t *)elem; * } * static struct htable ht = HTABLE_INITIALIZER(ht, rehash, NULL); @@ -51,6 +61,21 @@ struct htable { void htable_init(struct htable *ht, size_t (*rehash)(const void *elem, void *priv), void *priv); +/** + * htable_init_sized - initialize an empty hash table of given size. + * @ht: the hash table to initialize + * @rehash: hash function to use for rehashing. + * @priv: private argument to @rehash function. + * @size: the number of element. + * + * If this returns false, @ht is still usable, but may need to do reallocation + * upon an add. If this returns true, it will not need to reallocate within + * @size htable_adds. + */ +bool htable_init_sized(struct htable *ht, + size_t (*rehash)(const void *elem, void *priv), + void *priv, size_t size); + /** * htable_clear - empty a hash table. * @ht: the hash table to clear @@ -59,12 +84,41 @@ void htable_init(struct htable *ht, */ void htable_clear(struct htable *ht); + /** - * htable_rehash - use a hashtree's rehash function - * @elem: the argument to rehash() + * htable_check - check hash table for consistency + * @ht: the htable + * @abortstr: the location to print on aborting, or NULL. + * + * Because hash tables have redundant information, consistency checking that + * each element is in the correct location can be done. This is useful as a + * debugging check. If @abortstr is non-NULL, that will be printed in a + * diagnostic if the htable is inconsistent, and the function will abort. * + * Returns the htable if it is consistent, NULL if not (it can never return + * NULL if @abortstr is set). */ -size_t htable_rehash(const void *elem); +struct htable *htable_check(const struct htable *ht, const char *abortstr); + +/** + * htable_copy - duplicate a hash table. + * @dst: the hash table to overwrite + * @src: the hash table to copy + * + * Only fails on out-of-memory. + * + * Equivalent to (but faster than): + * if (!htable_init_sized(dst, src->rehash, src->priv, 1U << src->bits)) + * return false; + * v = htable_first(src, &i); + * while (v) { + * htable_add(dst, v); + * v = htable_next(src, i); + * } + * return true; + */ +#define htable_copy(dst, src) htable_copy_(dst, htable_debug(src, HTABLE_LOC)) +bool htable_copy_(struct htable *dst, const struct htable *src); /** * htable_add - add a pointer into a hash table. @@ -75,7 +129,9 @@ size_t htable_rehash(const void *elem); * Also note that this can only fail due to allocation failure. Otherwise, it * returns true. */ -bool htable_add(struct htable *ht, size_t hash, const void *p); +#define htable_add(ht, hash, p) \ + htable_add_(htable_debug(ht, HTABLE_LOC), hash, p) +bool htable_add_(struct htable *ht, size_t hash, const void *p); /** * htable_del - remove a pointer from a hash table @@ -85,7 +141,9 @@ bool htable_add(struct htable *ht, size_t hash, const void *p); * * Returns true if the pointer was found (and deleted). */ -bool htable_del(struct htable *ht, size_t hash, const void *p); +#define htable_del(ht, hash, p) \ + htable_del_(htable_debug(ht, HTABLE_LOC), hash, p) +bool htable_del_(struct htable *ht, size_t hash, const void *p); /** * struct htable_iter - iterator or htable_first or htable_firstval etc. @@ -106,8 +164,11 @@ struct htable_iter { * See Also: * htable_delval() */ -void *htable_firstval(const struct htable *htable, - struct htable_iter *i, size_t hash); +#define htable_firstval(htable, i, hash) \ + htable_firstval_(htable_debug(htable, HTABLE_LOC), i, hash) + +void *htable_firstval_(const struct htable *htable, + struct htable_iter *i, size_t hash); /** * htable_nextval - find another candidate for a given hash value @@ -117,8 +178,10 @@ void *htable_firstval(const struct htable *htable, * * You'll need to check the value is what you want; returns NULL if no more. */ -void *htable_nextval(const struct htable *htable, - struct htable_iter *i, size_t hash); +#define htable_nextval(htable, i, hash) \ + htable_nextval_(htable_debug(htable, HTABLE_LOC), i, hash) +void *htable_nextval_(const struct htable *htable, + struct htable_iter *i, size_t hash); /** * htable_get - find an entry in the hash table @@ -151,7 +214,9 @@ static inline void *htable_get(const struct htable *ht, * * Get an entry in the hashtable; NULL if empty. */ -void *htable_first(const struct htable *htable, struct htable_iter *i); +#define htable_first(htable, i) \ + htable_first_(htable_debug(htable, HTABLE_LOC), i) +void *htable_first_(const struct htable *htable, struct htable_iter *i); /** * htable_next - find another entry in the hash table @@ -161,7 +226,26 @@ void *htable_first(const struct htable *htable, struct htable_iter *i); * Get another entry in the hashtable; NULL if all done. * This is usually used after htable_first or prior non-NULL htable_next. */ -void *htable_next(const struct htable *htable, struct htable_iter *i); +#define htable_next(htable, i) \ + htable_next_(htable_debug(htable, HTABLE_LOC), i) +void *htable_next_(const struct htable *htable, struct htable_iter *i); + +/** + * htable_prev - find the previous entry in the hash table + * @ht: the hashtable + * @i: the struct htable_iter to use + * + * Get previous entry in the hashtable; NULL if all done. + * + * "previous" here only means the item that would have been returned by + * htable_next() before the item it returned most recently. + * + * This is usually used in the middle of (or after) a htable_next iteration and + * to "unwind" actions taken. + */ +#define htable_prev(htable, i) \ + htable_prev_(htable_debug(htable, HTABLE_LOC), i) +void *htable_prev_(const struct htable *htable, struct htable_iter *i); /** * htable_delval - remove an iterated pointer from a hash table @@ -171,6 +255,8 @@ void *htable_next(const struct htable *htable, struct htable_iter *i); * Usually used to delete a hash entry after it has been found with * htable_firstval etc. */ -void htable_delval(struct htable *ht, struct htable_iter *i); +#define htable_delval(htable, i) \ + htable_delval_(htable_debug(htable, HTABLE_LOC), i) +void htable_delval_(struct htable *ht, struct htable_iter *i); #endif /* CCAN_HTABLE_H */