Add hash table iterator macros
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 9 Nov 2011 21:18:42 +0000 (16:18 -0500)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 9 Nov 2011 21:18:42 +0000 (16:18 -0500)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
tests/test_urcu_hash.c
urcu/rculfhash.h

index 87c4ad13781393a1d3aeb522f0e8c4406e52d037..554f1e914202c48ce7cc296524a1fd4fb4ef1f6c 100644 (file)
@@ -711,14 +711,12 @@ void test_delete_all_nodes(struct cds_lfht *ht)
        struct lfht_test_node *node;
        unsigned long count = 0;
 
-       cds_lfht_first(ht, &iter);
-       while ((node = cds_lfht_iter_get_test_node(&iter)) != NULL) {
+       cds_lfht_for_each_entry(ht, &iter, node, node) {
                int ret;
 
                ret = cds_lfht_del(test_ht, &iter);
                assert(!ret);
                call_rcu(&node->head, free_node_cb);
-               cds_lfht_next(ht, &iter);
                count++;
        }
        printf("deleted %lu nodes.\n", count);
index 6e4f6040094f81eba2736718263eaf66f88ea97e..37eaccf6b153c031ef29865d8ad8ec1a6e008ce5 100644 (file)
@@ -26,6 +26,7 @@
  */
 
 #include <stdint.h>
+#include <urcu/compiler.h>
 #include <urcu-call-rcu.h>
 
 #ifdef __cplusplus
@@ -361,6 +362,43 @@ int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_iter *iter);
  */
 void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size);
 
+/*
+ * Note: cds_lfht_for_each are safe for element removal during
+ * iteration.
+ */
+#define cds_lfht_for_each(ht, iter, node)                              \
+       for (cds_lfht_first(ht, iter),                                  \
+                       node = cds_lfht_iter_get_node(iter);            \
+               node != NULL;                                           \
+               cds_lfht_next(ht, iter),                                \
+                       node = cds_lfht_iter_get_node(iter))
+
+#define cds_lfht_for_each_duplicate(ht, match, hash, key, iter, node)  \
+       for (cds_lfht_lookup(ht, match, hash, key, iter),               \
+                       node = cds_lfht_iter_get_node(iter);            \
+               node != NULL;                                           \
+               cds_lfht_next_duplicate(ht, match, key, iter),          \
+                       node = cds_lfht_iter_get_node(iter))
+
+#define cds_lfht_for_each_entry(ht, iter, pos, member)                 \
+       for (cds_lfht_first(ht, iter),                                  \
+                       pos = caa_container_of(cds_lfht_iter_get_node(iter), \
+                                       typeof(*(pos)), member);        \
+               &pos->member != NULL;                                   \
+               cds_lfht_next(ht, iter),                                \
+                       pos = caa_container_of(cds_lfht_iter_get_node(iter), \
+                                       typeof(*(pos)), member))
+
+#define cds_lfht_for_each_entry_duplicate(ht, match, hash, key,                \
+                               iter, pos, member)                      \
+       for (cds_lfht_lookup(ht, match, hash, key, iter),               \
+                       pos = caa_container_of(cds_lfht_iter_get_node(iter), \
+                                       typeof(*(pos)), member);        \
+               &pos->member != NULL;                                   \
+               cds_lfht_next_duplicate(ht, match, key, iter),          \
+                       pos = caa_container_of(cds_lfht_iter_get_node(iter), \
+                                       typeof(*(pos)), member))
+
 #ifdef __cplusplus
 }
 #endif
This page took 0.026996 seconds and 4 git commands to generate.