summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKenneth Graunke <[email protected]>2010-07-08 13:06:22 -0700
committerIan Romanick <[email protected]>2010-07-09 09:46:07 -0700
commitf3290e950cd78a423d380b7e0a7aa18eb7718e27 (patch)
tree9f134837e59bc40d35c9202805134596627519df
parentdfd30ca6a95a7d95835dad78ffe1fba4d1f4ef69 (diff)
glsl2: Add foreach_list_safe which works even when mutating the list.
In particular, with foreach_list_safe, one can remove and free the current node without crashes; if new nodes are added after the current node, they will be properly visited as well. Signed-off-by: Ian Romanick <[email protected]>
-rw-r--r--src/glsl/list.h14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/glsl/list.h b/src/glsl/list.h
index b5a413dc511..48502fb4c8c 100644
--- a/src/glsl/list.h
+++ b/src/glsl/list.h
@@ -421,6 +421,20 @@ struct exec_list {
#endif
};
+/**
+ * This version is safe even if the current node is removed. If you insert
+ * new nodes before the current node, they will be processed next.
+ *
+ * \note
+ * The extra test for \c __node being \c NULL is required because after the
+ * iteration \c __prev coupld be the last node in the list. The loop increment
+ * then causes \c __prev to point to the sentinal and \c __node to be \c NULL.
+ */
+#define foreach_list_safe(__node, __list) \
+ for (exec_node * __prev = (exec_node *) (__list), * __node = (__list)->head \
+ ; __node != NULL && (__node)->next != NULL \
+ ; __prev = (__prev)->next, __node = (__prev)->next)
+
#define foreach_list(__node, __list) \
for (exec_node * __node = (__list)->head \
; (__node)->next != NULL \