]> git.ozlabs.org Git - ccan/commitdiff
Many list funcs should work with constant lists.
authorRusty Russell <rusty@rustcorp.com.au>
Fri, 5 Jun 2009 07:24:04 +0000 (16:54 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Fri, 5 Jun 2009 07:24:04 +0000 (16:54 +0930)
ccan/list/list.c
ccan/list/list.h
ccan/list/test/compile_ok-constant.c [new file with mode: 0644]

index 0a9e4e4d2d5d092ef1449b82c4a72781033fadc9..3d5811011bcc1e034dc1571a622162e8bbfeede6 100644 (file)
@@ -2,9 +2,9 @@
 #include <stdlib.h>
 #include "list.h"
 
-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)
 {
-       struct list_node *n, *p;
+       const struct list_node *n, *p;
        int count = 0;
 
        if (h->n.next == &h->n) {
@@ -15,7 +15,7 @@ struct list_head *list_check(struct list_head *h, const char *abortstr)
                                abortstr, h);
                        abort();
                }
-               return h;
+               return (struct list_head *)h;
        }
 
        for (p = &h->n, n = h->n.next; n != &h->n; p = n, n = n->next) {
@@ -29,5 +29,5 @@ struct list_head *list_check(struct list_head *h, const char *abortstr)
                        abort();
                }
        }
-       return h;
+       return (struct list_head *)h;
 }
index e1f07a0e043ae7f460d2650e2acdf39d468ab5aa..d47e13253b56596d83fac64cf7fef1b907b9dc40 100644 (file)
@@ -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;
diff --git a/ccan/list/test/compile_ok-constant.c b/ccan/list/test/compile_ok-constant.c
new file mode 100644 (file)
index 0000000..edb3cb0
--- /dev/null
@@ -0,0 +1,49 @@
+#include "list/list.h"
+#include "tap/tap.h"
+#include "list/list.c"
+#include <stdbool.h>
+#include <stdio.h>
+
+struct child {
+       const char *name;
+       struct list_node list;
+};
+
+static bool children(const struct list_head *list)
+{
+       return !list_empty(list);
+}
+
+static const struct child *first_child(const struct list_head *list)
+{
+       return list_top(list, struct child, list);
+}
+
+static const struct child *last_child(const struct list_head *list)
+{
+       return list_tail(list, struct child, list);
+}
+
+static void check_children(const struct list_head *list)
+{
+       list_check(list, "bad child list");
+}
+
+static void print_children(const struct list_head *list)
+{
+       const struct child *c;
+       list_for_each(list, c, list)
+               printf("%s\n", c->name);
+}
+
+int main(void)
+{
+       LIST_HEAD(h);
+
+       children(&h);
+       first_child(&h);
+       last_child(&h);
+       check_children(&h);
+       print_children(&h);
+       return 0;
+}