util/list: Add list_empty and list_length functions
authorJason Ekstrand <jason.ekstrand@intel.com>
Tue, 28 Apr 2015 03:39:37 +0000 (20:39 -0700)
committerJason Ekstrand <jason.ekstrand@intel.com>
Sat, 9 May 2015 00:16:13 +0000 (17:16 -0700)
v2: Don't use C99 when iterating over the list

Acked-by: Connor Abbott <cwabbott0@gmail.com>
Reviewed-by: Rob Clark <robclark@freedesktop.org>
src/util/list.h

index 287a4946dc6ea3e5ac18264803a0f7092c95a7ae..73b6272ca195b7646bb9acd23321e5d872023d21 100644 (file)
@@ -38,6 +38,7 @@
 #define _UTIL_LIST_H_
 
 
 #define _UTIL_LIST_H_
 
 
+#include <stdbool.h>
 #include <stddef.h>
 
 
 #include <stddef.h>
 
 
@@ -92,6 +93,20 @@ static inline void list_delinit(struct list_head *item)
     item->prev = item;
 }
 
     item->prev = item;
 }
 
+static inline bool list_empty(struct list_head *list)
+{
+   return list->next == list;
+}
+
+static inline unsigned list_length(struct list_head *list)
+{
+   struct list_head *node;
+   unsigned length = 0;
+   for (node = list->next; node != list; node = node->next)
+      length++;
+   return length;
+}
+
 #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)
 #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)