chiark / gitweb /
list: add macro for iterating through a list an item is in, skipping the item
[elogind.git] / src / shared / list.h
index e55b91cea4ad9c0184ee07390ba78a11c9c81a0a..7ed63188ba8e6da903ba5457c9385da9e4095058 100644 (file)
                 *_head = _item;                                         \
         } while(false)
 
+/* Append an item to the list */
+#define LIST_APPEND(name,head,item)                                     \
+        do {                                                            \
+                typeof(*(head)) *_tail;                                 \
+                LIST_FIND_TAIL(name,head,_tail);                        \
+                LIST_INSERT_AFTER(name,head,_tail,item);                \
+        } while(false)
+
 /* Remove an item from the list */
 #define LIST_REMOVE(name,head,item)                                     \
         do {                                                            \
 #define LIST_FIND_HEAD(name,item,head)                                  \
         do {                                                            \
                 typeof(*(item)) *_item = (item);                        \
-                assert(_item);                                          \
-                while (_item->name##_prev)                              \
-                       _item = _item->name##_prev;                      \
-                (head) = _item;                                         \
+                if (!_item)                                             \
+                        (head) = NULL;                                  \
+                else {                                                  \
+                        while (_item->name##_prev)                      \
+                                _item = _item->name##_prev;             \
+                        (head) = _item;                                 \
+                }                                                       \
         } while (false)
 
 /* Find the tail of the list */
 #define LIST_FIND_TAIL(name,item,tail)                                  \
         do {                                                            \
                 typeof(*(item)) *_item = (item);                        \
-                assert(_item);                                          \
-                while (_item->name##_next)                              \
-                        _item = _item->name##_next;                     \
-                (tail) = _item;                                         \
+                if (!_item)                                             \
+                        (tail) = NULL;                                  \
+                else {                                                  \
+                        while (_item->name##_next)                      \
+                                _item = _item->name##_next;             \
+                        (tail) = _item;                                 \
+                }                                                       \
         } while (false)
 
 /* Insert an item after another one (a = where, b = what) */
 #define LIST_FOREACH_AFTER(name,i,p)                                    \
         for ((i) = (p)->name##_next; (i); (i) = (i)->name##_next)
 
+/* Iterate through all the members of the list p is included in, but skip over p */
+#define LIST_FOREACH_OTHERS(name,i,p)                                   \
+        for (({                                                         \
+                (i) = (p);                                              \
+                while ((i) && (i)->name##_prev)                         \
+                        (i) = (i)->name##_prev;                         \
+             });                                                        \
+             (i);                                                       \
+             (i) = (i)->name##_next == (p) ? (p)->name##_next : (i)->name##_next)
+
 /* Loop starting from p->next until p->prev.
    p can be adjusted meanwhile. */
 #define LIST_LOOP_BUT_ONE(name,i,head,p)                                \