SPM: Add logic for unidirectional linked list

Add operations for unidirectional list: Initialize a head node,
insert a node and iterate the list.
Also change bi-list "next" and "prev" to "bnext" and "bprev" to
avoid misuse between unidirectional list and bi-list operations.

The unidirectional linked list is prepared for linking the
partitions and linking the services.

Change-Id: I7c230f14b8ae78942efd5b1e524f4a05df364f7d
Signed-off-by: Ken Liu <ken.liu@arm.com>
Co-authored-by: Mingyang Sun <mingyang.sun@arm.com>
diff --git a/secure_fw/spm/include/lists.h b/secure_fw/spm/include/lists.h
index 41c540a..c630ff7 100644
--- a/secure_fw/spm/include/lists.h
+++ b/secure_fw/spm/include/lists.h
@@ -7,48 +7,76 @@
 #ifndef __LISTS_H__
 #define __LISTS_H__
 
+/********* Bi-directional list operations ********/
 /* Bi-directional list structure */
 struct bi_list_node_t {
-    struct bi_list_node_t *prev;
-    struct bi_list_node_t *next;
+    struct bi_list_node_t *bprev;
+    struct bi_list_node_t *bnext;
 };
 
 /* Init an empty node. */
 #define BI_LIST_INIT_NODE(node) do {              \
-    (node)->next = node;                          \
-    (node)->prev = node;                          \
-} while(0)
+    (node)->bnext = node;                         \
+    (node)->bprev = node;                         \
+} while (0)
 
-/* Insert a new node after (next) current. */
+/* Insert a new node after current node: (bnext) of current. */
 #define BI_LIST_INSERT_AFTER(curr, node) do {     \
-    (node)->next = (curr)->next;                  \
-    (node)->prev = curr;                          \
-    (curr)->next->prev = node;                    \
-    (curr)->next = node;                          \
-} while(0)
+    (node)->bnext = (curr)->bnext;                \
+    (node)->bprev = curr;                         \
+    (curr)->bnext->bprev = node;                  \
+    (curr)->bnext = node;                         \
+} while (0)
 
-/* Add one node into list as the tail (prev) of head. */
+/* Add one node into list as the tail: (bprev) of head. */
 #define BI_LIST_INSERT_BEFORE(curr, node) do {    \
-    (curr)->prev->next = node;                    \
-    (node)->prev = (curr)->prev;                  \
-    (curr)->prev = node;                          \
-    (node)->next = curr;                          \
-} while(0)
+    (curr)->bprev->bnext = node;                  \
+    (node)->bprev = (curr)->bprev;                \
+    (curr)->bprev = node;                         \
+    (node)->bnext = curr;                         \
+} while (0)
 
 /* Remove one node from the list. */
-#define BI_LIST_REMOVE_NODE(node) do       {      \
-    (node)->prev->next = (node)->next;            \
-    (node)->next->prev = (node)->prev;            \
-} while(0)
+#define BI_LIST_REMOVE_NODE(node) do {            \
+    (node)->bprev->bnext = (node)->bnext;         \
+    (node)->bnext->bprev = (node)->bprev;         \
+} while (0)
 
 /* Is the head empty? */
-#define BI_LIST_IS_EMPTY(head)      ((head)->next == (head))
+#define BI_LIST_IS_EMPTY(head)      ((head)->bnext == (head))
 
 /* The node's next node */
-#define BI_LIST_NEXT_NODE(node)     ((node)->next)
+#define BI_LIST_NEXT_NODE(node)     ((node)->bnext)
 
 /* Go through each node of a list */
 #define BI_LIST_FOR_EACH(node, head)              \
-    for (node = (head)->next; node != head; node = (node)->next)
+    for (node = (head)->bnext; node != head; node = (node)->bnext)
+
+/********* Uni-directional list operations ********/
+/*
+ * To use these single linked list operations, a head node must have been
+ * defined already, and the "next" pointer initialized to "NULL". Like:
+ * struct head_t {
+ *      uint32_t data;
+ *      User_Type *next;
+ * } head;
+ */
+
+/* Initialize the head node */
+#define UNI_LISI_INIT_HEAD(head) do {             \
+    if (head) {                                   \
+        (head)->next = NULL;                      \
+    }                                             \
+} while (0)
+
+/* Insert a node after current node */
+#define UNI_LIST_INSERT_AFTER(curr, node) do {    \
+    (node)->next = (curr)->next;                  \
+    (curr)->next = node;                          \
+} while (0)
+
+/* Go through each node of a list */
+#define UNI_LIST_FOR_EACH(node, head)             \
+    for (node = (head)->next; node != NULL; node = (node)->next)
 
 #endif /* __LISTS_H__ */