From: Rusty Russell Date: Fri, 5 Jun 2009 07:24:04 +0000 (+0930) Subject: Many list funcs should work with constant lists. X-Git-Url: http://git.ozlabs.org/?p=ccan;a=commitdiff_plain;h=9ca74a8e616261a3afebcc52472a25f2d551e2b6 Many list funcs should work with constant lists. --- diff --git a/ccan/list/list.c b/ccan/list/list.c index 0a9e4e4d..3d581101 100644 --- a/ccan/list/list.c +++ b/ccan/list/list.c @@ -2,9 +2,9 @@ #include #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; } diff --git a/ccan/list/list.h b/ccan/list/list.h index e1f07a0e..d47e1325 100644 --- a/ccan/list/list.h +++ b/ccan/list/list.h @@ -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 index 00000000..edb3cb01 --- /dev/null +++ b/ccan/list/test/compile_ok-constant.c @@ -0,0 +1,49 @@ +#include "list/list.h" +#include "tap/tap.h" +#include "list/list.c" +#include +#include + +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; +}