reduce the size of ppage by 8 bytes using signly linked list
[lunaix-os.git] / lunaix-os / includes / lunaix / ds / llist.h
index 79fa99173003797fe8dfa6f0a805576a4d506151..88e25233e718eddc335ced9c326b3c9cfa742dbd 100644 (file)
@@ -12,7 +12,7 @@
 #ifndef __LUNAIX_LLIST_H
 #define __LUNAIX_LLIST_H
 
-#include <lunaix/common.h>
+#include <lunaix/types.h>
 
 struct llist_header
 {
@@ -41,13 +41,19 @@ llist_init_head(struct llist_header* head)
 static inline void
 llist_append(struct llist_header* head, struct llist_header* elem)
 {
-    __llist_add(elem, head, head->next);
+    __llist_add(elem, head->prev, head);
 }
 
 static inline void
 llist_prepend(struct llist_header* head, struct llist_header* elem)
 {
-    __llist_add(elem, head->prev, head);
+    __llist_add(elem, head, head->next);
+}
+
+static inline void
+llist_insert_after(struct llist_header* head, struct llist_header* elem)
+{
+    __llist_add(elem, head, head->next);
 }
 
 static inline void
@@ -57,16 +63,22 @@ llist_delete(struct llist_header* elem)
     elem->next->prev = elem->prev;
 
     // make elem orphaned
-    // elem->prev = elem;
-    // elem->next = elem;
+    elem->prev = elem;
+    elem->next = elem;
 }
 
 static inline int
 llist_empty(struct llist_header* elem)
 {
-    return elem->next == elem;
+    return elem->next == elem && elem->prev == elem;
 }
 
+#define DEFINE_LLIST(name)                                                     \
+    struct llist_header name = (struct llist_header)                           \
+    {                                                                          \
+        .prev = &name, .next = &name                                           \
+    }
+
 /**
  * list_entry - get the struct for this entry
  * @ptr:       the &struct list_head pointer.
@@ -75,6 +87,22 @@ llist_empty(struct llist_header* elem)
  */
 #define list_entry(ptr, type, member) container_of(ptr, type, member)
 
+/**
+ * list_next - get the struct for next entry
+ * @ptr:       the &struct list_head pointer.
+ * @type:      the type of the struct this is embedded in.
+ * @member:    the name of the list_struct within the struct.
+ */
+#define list_next(current, type, member) container_of(current->member.next, type, member)
+
+/**
+ * list_prev - get the struct for prev entry
+ * @ptr:       the &struct list_head pointer.
+ * @type:      the type of the struct this is embedded in.
+ * @member:    the name of the list_struct within the struct.
+ */
+#define list_prev(current, type, member) container_of(current->member.prev, type, member)
+
 /**
  * list_for_each_entry -       iterate over list of given type
  * @pos:       the type * to use as a loop counter.
@@ -87,4 +115,35 @@ llist_empty(struct llist_header* elem)
          &pos->member != (head);                                               \
          pos = n, n = list_entry(n->member.next, typeof(*n), member))
 
+struct hlist_node
+{
+    struct hlist_node *next, **pprev;
+};
+
+static inline void
+hlist_delete(struct hlist_node* node)
+{
+    if (!node->pprev)
+        return;
+
+    if (node->next) {
+        node->next->pprev = node->pprev;
+    }
+    
+    *node->pprev = node->next;
+
+    node->next = 0;
+    node->pprev = 0;
+}
+
+static inline void
+hlist_add(struct hlist_node** head, struct hlist_node* node)
+{
+    node->next = *head;
+    if (*head) {
+        (*head)->pprev = &node->next;
+    }
+    node->pprev = head;
+    *head = node;
+}
 #endif /* __LUNAIX_LLIST_H */