]> git.ozlabs.org Git - ccan/blobdiff - ccan/list/list.h
Fix sequence logic bug, and satisfies() logic fix/cleanup.
[ccan] / ccan / list / list.h
index c664d83426af36d78304ae98b33f8dd04f98c21e..d47e13253b56596d83fac64cf7fef1b907b9dc40 100644 (file)
@@ -1,7 +1,7 @@
 #ifndef CCAN_LIST_H
 #define CCAN_LIST_H
 #include <stdbool.h>
-#include "container_of/container_of.h"
+#include <ccan/container_of/container_of.h>
 
 /**
  * struct list_node - an entry in a doubly-linked list
@@ -62,7 +62,7 @@ struct list_head
  *                     printf(" -> %s\n", c->name);
  *     }
  */
-struct list_head *list_check(struct list_head *h, const char *abortstr);
+struct list_head *list_check(const struct list_head *h, const char *abortstr);
 
 #ifdef CCAN_LIST_DEBUG
 #define debug_list(h) list_check((h), __func__)
@@ -166,7 +166,7 @@ static inline void list_del(struct list_node *n)
  * Example:
  *     assert(list_empty(&parent->children) == (parent->num_children == 0));
  */
-static inline bool list_empty(struct list_head *h)
+static inline bool list_empty(const struct list_head *h)
 {
        (void)debug_list(h);
        return h->n.next == &h->n;
@@ -198,15 +198,22 @@ static inline bool list_empty(struct list_head *h)
  *     first = list_top(&parent->children, struct child, list);
  */
 #define list_top(h, type, member) \
-       list_entry(_list_top(h), type, member)  
+       (list_empty(h) ? NULL : list_entry((h)->n.next, type, member))
 
-static inline struct list_node *_list_top(struct list_head *h)
-{
-       (void)debug_list(h);
-       if (list_empty(h))
-               return NULL;
-       return h->n.next;
-}
+/**
+ * list_tail - get the last entry in a list
+ * @h: the list_head
+ * @type: the type of the entry
+ * @member: the list_node member of the type
+ *
+ * If the list is empty, returns NULL.
+ *
+ * Example:
+ *     struct child *last;
+ *     last = list_tail(&parent->children, struct child, list);
+ */
+#define list_tail(h, type, member) \
+       (list_empty(h) ? NULL : list_entry((h)->n.prev, type, member))
 
 /**
  * list_for_each - iterate through a list.