]> git.ozlabs.org Git - petitboot/blobdiff - lib/list/list.h
lib/list: Fix handling of empty lists
[petitboot] / lib / list / list.h
index 7f9955f93115e2761fbe1b0ec94398480d40e8a8..0174b90e21b281edf4adba6c8bd7ef3d36b2f3e0 100644 (file)
@@ -40,14 +40,23 @@ struct list {
 #define list_for_each_entry_continue(_list, _pos, _member) \
        for (; _pos; _pos = list_next_entry(_list, _pos, _member))
 
+#define list_for_each_entry_safe(_list, _pos, _tmp, _member) \
+       for (_pos = container_of((_list)->head.next, typeof(*_pos), _member), \
+               _tmp = container_of(_pos->_member.next, typeof(*_pos), \
+                               _member); \
+            &_pos->_member != &(_list)->head; \
+            _pos = _tmp, \
+            _tmp = container_of(_tmp->_member.next, typeof(*_pos), _member))
 
-#define STATIC_LIST(_list) static struct list _list = { \
+#define DEFINE_LIST(_list) struct list _list = { \
        .head = { \
                .next = &_list.head, \
                .prev = &_list.head \
        } \
 }
 
+#define STATIC_LIST(_list) static DEFINE_LIST(_list)
+
 void list_init(struct list *list);
 void list_insert_before(struct list_item *next, struct list_item *item);
 void list_insert_after(struct list_item *prev, struct list_item *item);