In preparation for the error-inject command, add anew helper to
ccan/list for adding a list element in the middle of a list.

Cc: Dan Williams <[email protected]>
Signed-off-by: Vishal Verma <[email protected]>
---
 ccan/list/list.h | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/ccan/list/list.h b/ccan/list/list.h
index 4d1d34e..e6ecfa3 100644
--- a/ccan/list/list.h
+++ b/ccan/list/list.h
@@ -193,6 +193,38 @@ static inline void list_add_tail_(struct list_head *h,
 }
 
 /**
+ * list_add_after - add an entry after the given node in the linked list.
+ * @h: the list_head to add the node to
+ * @l: the list_node after which to add to
+ * @n: the list_node to add to the list.
+ *
+ * The list_node does not need to be initialized; it will be overwritten.
+ * Example:
+ *     struct child *child = malloc(sizeof(*child));
+ *
+ *     child->name = "geoffrey";
+ *     list_add_after(&parent->children, &child1->list, &child->list);
+ *     parent->num_children++;
+ */
+#define list_add_after(h, l, n) list_add_after_(h, l, n, LIST_LOC)
+static inline void list_add_after_(struct list_head *h,
+                                  struct list_node *l,
+                                  struct list_node *n,
+                                  const char *abortstr)
+{
+       if (l->next == &h->n) {
+               /* l is the last element, this becomes a list_add_tail */
+               list_add_tail(h, n);
+               return;
+       }
+       n->next = l->next;
+       n->prev = l;
+       l->next->prev = n;
+       l->next = n;
+       (void)list_debug(h, abortstr);
+}
+
+/**
  * list_empty - is a list empty?
  * @h: the list_head
  *
-- 
2.9.5

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm

Reply via email to