#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) {
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) {
abort();
}
}
- return h;
+ return (struct list_head *)h;
}
* 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__)
* 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;
--- /dev/null
+#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;
+}