diff mbox

[ndctl,4/8] ccan/list: add a list_add_after helper

Message ID 20171006015405.29908-5-vishal.l.verma@intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

Verma, Vishal L Oct. 6, 2017, 1:54 a.m. UTC
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 <dan.j.williams@intel.com>
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
---
 ccan/list/list.h | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

Comments

Dan Williams Oct. 6, 2017, 5:39 p.m. UTC | #1
On Thu, Oct 5, 2017 at 6:54 PM, Vishal Verma <vishal.l.verma@intel.com> wrote:
> 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 <dan.j.williams@intel.com>
> Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
> ---
>  ccan/list/list.h | 32 ++++++++++++++++++++++++++++++++
>  1 file changed, 32 insertions(+)

Let's not touch the ccan files since these should always match what is
in upstream ccan. Create a local util/list.h to add new routines. It
would also be nice to rename this to list_splice() to bring us closer
in line with kernel list primitives.
diff mbox

Patch

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
  *