}
#endif
-#define foreach_in_list(__type, __inst, __list) \
- for (__type *__inst = (__type *)(__list)->head_sentinel.next; \
- !(__inst)->is_tail_sentinel(); \
- (__inst) = (__type *)(__inst)->next)
+#define exec_node_typed_forward(__node, __type) \
+ (!exec_node_is_tail_sentinel(__node) ? (__type) (__node) : NULL)
-#define foreach_in_list_reverse(__type, __inst, __list) \
- for (__type *__inst = (__type *)(__list)->tail_sentinel.prev; \
- !(__inst)->is_head_sentinel(); \
- (__inst) = (__type *)(__inst)->prev)
+#define exec_node_typed_backward(__node, __type) \
+ (!exec_node_is_head_sentinel(__node) ? (__type) (__node) : NULL)
+
+#define foreach_in_list(__type, __inst, __list) \
+ for (__type *__inst = exec_node_typed_forward((__list)->head_sentinel.next, __type *); \
+ (__inst) != NULL; \
+ (__inst) = exec_node_typed_forward((__inst)->next, __type *))
+
+#define foreach_in_list_reverse(__type, __inst, __list) \
+ for (__type *__inst = exec_node_typed_backward((__list)->tail_sentinel.prev, __type *); \
+ (__inst) != NULL; \
+ (__inst) = exec_node_typed_backward((__inst)->prev, __type *))
/**
* This version is safe even if the current node is removed.
- */
-#define foreach_in_list_safe(__type, __node, __list) \
- for (__type *__node = (__type *)(__list)->head_sentinel.next, \
- *__next = (__type *)__node->next; \
- __next != NULL; \
- __node = __next, __next = (__type *)__next->next)
-
-#define foreach_in_list_reverse_safe(__type, __node, __list) \
- for (__type *__node = (__type *)(__list)->tail_sentinel.prev, \
- *__prev = (__type *)__node->prev; \
- __prev != NULL; \
- __node = __prev, __prev = (__type *)__prev->prev)
-
-#define foreach_in_list_use_after(__type, __inst, __list) \
- __type *__inst; \
- for ((__inst) = (__type *)(__list)->head_sentinel.next; \
- !(__inst)->is_tail_sentinel(); \
- (__inst) = (__type *)(__inst)->next)
+ */
+
+#define foreach_in_list_safe(__type, __node, __list) \
+ for (__type *__node = exec_node_typed_forward((__list)->head_sentinel.next, __type *), \
+ *__next = (__node) ? exec_node_typed_forward((__list)->head_sentinel.next->next, __type *) : NULL; \
+ (__node) != NULL; \
+ (__node) = __next, __next = __next ? exec_node_typed_forward(__next->next, __type *) : NULL)
+
+#define foreach_in_list_reverse_safe(__type, __node, __list) \
+ for (__type *__node = exec_node_typed_backward((__list)->tail_sentinel.prev, __type *), \
+ *__prev = (__node) ? exec_node_typed_backward((__list)->tail_sentinel.prev->prev, __type *) : NULL; \
+ (__node) != NULL; \
+ (__node) = __prev, __prev = __prev ? exec_node_typed_backward(__prev->prev, __type *) : NULL)
+
+#define foreach_in_list_use_after(__type, __inst, __list) \
+ __type *__inst; \
+ for ((__inst) = exec_node_typed_forward((__list)->head_sentinel.next, __type *); \
+ (__inst) != NULL; \
+ (__inst) = exec_node_typed_forward((__inst)->next, __type *))
+
/**
* Iterate through two lists at once. Stops at the end of the shorter list.
*
__next1 = __next1->next, \
__next2 = __next2->next)
-#define foreach_list_typed(__type, __node, __field, __list) \
- for (__type * __node = \
- exec_node_data(__type, (__list)->head_sentinel.next, __field); \
- (__node)->__field.next != NULL; \
- (__node) = exec_node_data(__type, (__node)->__field.next, __field))
-
-#define foreach_list_typed_from(__type, __node, __field, __list, __start) \
- for (__type * __node = exec_node_data(__type, (__start), __field); \
- (__node)->__field.next != NULL; \
- (__node) = exec_node_data(__type, (__node)->__field.next, __field))
-
-#define foreach_list_typed_reverse(__type, __node, __field, __list) \
- for (__type * __node = \
- exec_node_data(__type, (__list)->tail_sentinel.prev, __field); \
- (__node)->__field.prev != NULL; \
- (__node) = exec_node_data(__type, (__node)->__field.prev, __field))
-
-#define foreach_list_typed_safe(__type, __node, __field, __list) \
- for (__type * __node = \
- exec_node_data(__type, (__list)->head_sentinel.next, __field), \
- * __next = \
- exec_node_data(__type, (__node)->__field.next, __field); \
- (__node)->__field.next != NULL; \
- __node = __next, __next = \
- exec_node_data(__type, (__next)->__field.next, __field))
-
-#define foreach_list_typed_reverse_safe(__type, __node, __field, __list) \
- for (__type * __node = \
- exec_node_data(__type, (__list)->tail_sentinel.prev, __field), \
- * __prev = \
- exec_node_data(__type, (__node)->__field.prev, __field); \
- (__node)->__field.prev != NULL; \
- __node = __prev, __prev = \
- exec_node_data(__type, (__prev)->__field.prev, __field))
+#define exec_node_data_forward(type, node, field) \
+ (!exec_node_is_tail_sentinel(node) ? exec_node_data(type, node, field) : NULL)
+
+#define exec_node_data_backward(type, node, field) \
+ (!exec_node_is_head_sentinel(node) ? exec_node_data(type, node, field) : NULL)
+
+#define foreach_list_typed(__type, __node, __field, __list) \
+ for (__type * __node = \
+ exec_node_data_forward(__type, (__list)->head_sentinel.next, __field); \
+ (__node) != NULL; \
+ (__node) = exec_node_data_forward(__type, (__node)->__field.next, __field))
+
+#define foreach_list_typed_from(__type, __node, __field, __list, __start) \
+ for (__type * __node = exec_node_data_forward(__type, (__start), __field); \
+ (__node) != NULL; \
+ (__node) = exec_node_data_forward(__type, (__node)->__field.next, __field))
+
+#define foreach_list_typed_reverse(__type, __node, __field, __list) \
+ for (__type * __node = \
+ exec_node_data_backward(__type, (__list)->tail_sentinel.prev, __field); \
+ (__node) != NULL; \
+ (__node) = exec_node_data_backward(__type, (__node)->__field.prev, __field))
+
+#define foreach_list_typed_safe(__type, __node, __field, __list) \
+ for (__type * __node = \
+ exec_node_data_forward(__type, (__list)->head_sentinel.next, __field), \
+ * __next = (__node) ? \
+ exec_node_data_forward(__type, (__node)->__field.next, __field) : NULL; \
+ (__node) != NULL; \
+ (__node) = __next, __next = (__next && (__next)->__field.next) ? \
+ exec_node_data_forward(__type, (__next)->__field.next, __field) : NULL)
+
+#define foreach_list_typed_reverse_safe(__type, __node, __field, __list) \
+ for (__type * __node = \
+ exec_node_data_backward(__type, (__list)->tail_sentinel.prev, __field), \
+ * __prev = (__node) ? \
+ exec_node_data_backward(__type, (__node)->__field.prev, __field) : NULL; \
+ (__node) != NULL; \
+ (__node) = __prev, __prev = (__prev && (__prev)->__field.prev) ? \
+ exec_node_data_backward(__type, (__prev)->__field.prev, __field) : NULL)
#endif /* LIST_CONTAINER_H */