summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2011-12-05 16:42:48 +1030
committerRusty Russell <rusty@rustcorp.com.au>2011-12-05 16:42:48 +1030
commite1374049678842f5086f174d97ae916345686f87 (patch)
treee00cc49a635530c28c5d03d1cc4ca060ab57c2e9 /lib
parent0ac7deefbf190e11d38cc47807e0f5f6cfb1775e (diff)
downloadsamba-e1374049678842f5086f174d97ae916345686f87.tar.gz
samba-e1374049678842f5086f174d97ae916345686f87.tar.bz2
samba-e1374049678842f5086f174d97ae916345686f87.zip
lib/ccan/htable: HTABLE_INITIALIZER() for static initialization.
Unfortunately it's a bit of a pain to use for typed hashtables, but it works. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> (Imported from CCAN commit 60cc720d0797fc49325437ea36a9ffd909c75ed0)
Diffstat (limited to 'lib')
-rw-r--r--lib/ccan/htable/htable.c12
-rw-r--r--lib/ccan/htable/htable.h29
-rw-r--r--lib/ccan/htable/htable_type.h3
3 files changed, 29 insertions, 15 deletions
diff --git a/lib/ccan/htable/htable.c b/lib/ccan/htable/htable.c
index f3e529211d..0a01ead897 100644
--- a/lib/ccan/htable/htable.c
+++ b/lib/ccan/htable/htable.c
@@ -45,18 +45,10 @@ static inline uintptr_t get_hash_ptr_bits(const struct htable *ht,
void htable_init(struct htable *ht,
size_t (*rehash)(const void *elem, void *priv), void *priv)
{
- ht->bits = 0;
+ struct htable empty = HTABLE_INITIALIZER(empty, NULL, NULL);
+ *ht = empty;
ht->rehash = rehash;
ht->priv = priv;
- ht->elems = 0;
- ht->deleted = 0;
- ht->max = 0;
- ht->max_with_deleted = 0;
- /* This guarantees we enter update_common first add. */
- ht->common_mask = -1;
- ht->common_bits = 0;
- ht->perfect_bit = 0;
- /* Dummy table until first insert. */
ht->table = &ht->perfect_bit;
}
diff --git a/lib/ccan/htable/htable.h b/lib/ccan/htable/htable.h
index b3962ac050..ed668e7405 100644
--- a/lib/ccan/htable/htable.h
+++ b/lib/ccan/htable/htable.h
@@ -24,7 +24,26 @@ struct htable {
};
/**
- * htable_init - initialize an empty hash tree.
+ * 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_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.
@@ -33,7 +52,7 @@ void htable_init(struct htable *ht,
size_t (*rehash)(const void *elem, void *priv), void *priv);
/**
- * htable_clear - empty 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.
@@ -48,7 +67,7 @@ void htable_clear(struct htable *ht);
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
@@ -59,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
@@ -145,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
*
diff --git a/lib/ccan/htable/htable_type.h b/lib/ccan/htable/htable_type.h
index 3b555cde4a..03cc46fc58 100644
--- a/lib/ccan/htable/htable_type.h
+++ b/lib/ccan/htable/htable_type.h
@@ -38,6 +38,9 @@
*
* It's currently safe to iterate over a changing hashtable, but you might
* miss an element. Iteration isn't very efficient, either.
+ *
+ * You can use HTABLE_INITIALIZER like so:
+ * struct <name> ht = { HTABLE_INITIALIZER(ht.raw, <name>_hash, NULL) };
*/
#define HTABLE_DEFINE_TYPE(type, keyof, hashfn, eqfn, name) \
struct name { struct htable raw; }; \