lib/list: Add list_for_each_entry_safe
authorJeremy Kerr <jk@ozlabs.org>
Tue, 16 Apr 2013 03:39:59 +0000 (11:39 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Tue, 16 Apr 2013 04:57:15 +0000 (12:57 +0800)
Add a list iterator that is safe against deletion of the current
element.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
lib/list/list.h

index cd9cd105911e2316e8bbbfec87037287253e4242..97fb615cdb9fd1bc8361c876c2e063fb11a41b32 100644 (file)
@@ -40,6 +40,14 @@ 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 = list_entry(_tmp->_member.next, typeof(*_pos), \
+                               _member, _list))
 
 #define DEFINE_LIST(_list) struct list _list = { \
        .head = { \