]> git.ozlabs.org Git - ccan/blobdiff - ccan/list/list.h
list: list_swap to exchange elements
[ccan] / ccan / list / list.h
index 5c1159f48159987994341266616620f6f1c915c1..983675b81f9d53877fd5d0dfaa56abc1d742a641 100644 (file)
@@ -368,6 +368,39 @@ static inline void list_del_from(struct list_head *h, struct list_node *n)
        list_del(n);
 }
 
+/**
+ * list_swap - swap out an entry from an (unknown) linked list for a new one.
+ * @o: the list_node to replace from the list.
+ * @n: the list_node to insert in place of the old one.
+ *
+ * Note that this leaves @o in an undefined state; it can be added to
+ * another list, but not deleted/swapped again.
+ *
+ * See also:
+ *     list_del()
+ *
+ * Example:
+ *     struct child x1, x2;
+ *     LIST_HEAD(xh);
+ *
+ *     list_add(&xh, &x1.list);
+ *     list_swap(&x1.list, &x2.list);
+ */
+#define list_swap(o, n) list_swap_(o, n, LIST_LOC)
+static inline void list_swap_(struct list_node *o,
+                             struct list_node *n,
+                             const char* abortstr)
+{
+       (void)list_debug_node(o, abortstr);
+       *n = *o;
+       n->next->prev = n;
+       n->prev->next = n;
+#ifdef CCAN_LIST_DEBUG
+       /* Catch use-after-del. */
+       o->next = o->prev = NULL;
+#endif
+}
+
 /**
  * list_entry - convert a list_node back into the structure containing it.
  * @n: the list_node