Commit 9ca74a8e authored by Rusty Russell's avatar Rusty Russell

Many list funcs should work with constant lists.

parent 6e1450e6
......@@ -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;
}
......@@ -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;
......
#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;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment