X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Flist%2Flist.c;h=d876f21e7b06b0bd06a38fdd89b5b35ff6d62d31;hp=b72f8f67d87705c7af25af874ec08269c6080e41;hb=a94286219597d8123188aaff0aaa55dba5104a05;hpb=650c775ff00cccd03fc84e7789a03c51d9839004 diff --git a/ccan/list/list.c b/ccan/list/list.c index b72f8f67..d876f21e 100644 --- a/ccan/list/list.c +++ b/ccan/list/list.c @@ -1,33 +1,42 @@ #include #include -#include "list/list.h" +#include "list.h" -struct list_head *list_check(struct list_head *h, const char *abortstr) +static void *corrupt(const char *abortstr, + const struct list_node *head, + const struct list_node *node, + unsigned int count) { - struct list_node *n, *p; - int count = 0; - - if (h->n.next == &h->n) { - if (h->n.prev != &h->n) { - if (!abortstr) - return NULL; - fprintf(stderr, "%s: prev corrupt in empty %p\n", - abortstr, h); - abort(); - } - return h; + if (abortstr) { + fprintf(stderr, + "%s: prev corrupt in node %p (%u) of %p\n", + abortstr, node, count, head); + abort(); } + return NULL; +} + +struct list_node *list_check_node(const struct list_node *node, + const char *abortstr) +{ + const struct list_node *p, *n; + int count = 0; - for (p = &h->n, n = h->n.next; n != &h->n; p = n, n = n->next) { + for (p = node, n = node->next; n != node; p = n, n = n->next) { count++; - if (n->prev != p) { - if (!abortstr) - return NULL; - fprintf(stderr, - "%s: prev corrupt in node %p (%u) of %p\n", - abortstr, n, count, h); - abort(); - } + if (n->prev != p) + return corrupt(abortstr, node, n, count); } - return h; + /* Check prev on head node. */ + if (node->prev != p) + return corrupt(abortstr, node, node, 0); + + return (struct list_node *)node; +} + +struct list_head *list_check(const struct list_head *h, const char *abortstr) +{ + if (!list_check_node(&h->n, abortstr)) + return NULL; + return (struct list_head *)h; }