autotools: library-dependency when no sse and 32-bit
[mesa.git] / src / util / list.h
index 73b6272ca195b7646bb9acd23321e5d872023d21..09d1b4cae64c2aef2db3e018cc5dc7ec10ac1769 100644 (file)
@@ -40,6 +40,8 @@
 
 #include <stdbool.h>
 #include <stddef.h>
+#include <assert.h>
+#include "c99_compat.h"
 
 
 struct list_head
@@ -70,12 +72,18 @@ static inline void list_addtail(struct list_head *item, struct list_head *list)
     list->prev = item;
 }
 
+static inline bool list_empty(const struct list_head *list);
+
 static inline void list_replace(struct list_head *from, struct list_head *to)
 {
-    to->prev = from->prev;
-    to->next = from->next;
-    from->next->prev = to;
-    from->prev->next = to;
+    if (list_empty(from)) {
+        list_inithead(to);
+    } else {
+        to->prev = from->prev;
+        to->next = from->next;
+        from->next->prev = to;
+        from->prev->next = to;
+    }
 }
 
 static inline void list_del(struct list_head *item)
@@ -93,12 +101,20 @@ static inline void list_delinit(struct list_head *item)
     item->prev = item;
 }
 
-static inline bool list_empty(struct list_head *list)
+static inline bool list_empty(const struct list_head *list)
 {
    return list->next == list;
 }
 
-static inline unsigned list_length(struct list_head *list)
+/**
+ * Returns whether the list has exactly one element.
+ */
+static inline bool list_is_singular(const struct list_head *list)
+{
+   return list->next != NULL && list->next != list && list->next->next == list;
+}
+
+static inline unsigned list_length(const struct list_head *list)
 {
    struct list_head *node;
    unsigned length = 0;
@@ -107,6 +123,36 @@ static inline unsigned list_length(struct list_head *list)
    return length;
 }
 
+static inline void list_splice(struct list_head *src, struct list_head *dst)
+{
+   if (list_empty(src))
+      return;
+
+   src->next->prev = dst;
+   src->prev->next = dst->next;
+   dst->next->prev = src->prev;
+   dst->next = src->next;
+}
+
+static inline void list_splicetail(struct list_head *src, struct list_head *dst)
+{
+   if (list_empty(src))
+      return;
+
+   src->prev->next = dst;
+   src->next->prev = dst->prev;
+   dst->prev->next = src->next;
+   dst->prev = src->prev;
+}
+
+static inline void list_validate(const struct list_head *list)
+{
+   struct list_head *node;
+   assert(list->next->prev == list && list->prev->next == list);
+   for (node = list->next; node != list; node = node->next)
+      assert(node->next->prev == node && node->prev->next == node);
+}
+
 #define LIST_INITHEAD(__item) list_inithead(__item)
 #define LIST_ADD(__item, __list) list_add(__item, __list)
 #define LIST_ADDTAIL(__item, __list) list_addtail(__item, __list)
@@ -131,6 +177,13 @@ static inline unsigned list_length(struct list_head *list)
             - ((char *)&(sample)->member - (char *)(sample)))
 #endif
 
+#define list_first_entry(ptr, type, member) \
+        LIST_ENTRY(type, (ptr)->next, member)
+
+#define list_last_entry(ptr, type, member) \
+        LIST_ENTRY(type, (ptr)->prev, member)
+
+
 #define LIST_FOR_EACH_ENTRY(pos, head, member)                         \
    for (pos = NULL, pos = container_of((head)->next, pos, member);     \
        &pos->member != (head);                                         \