list: implement list_check_node to check list from node rather than head
[ccan] / ccan / list / list.c
index 3d5811011bcc1e034dc1571a622162e8bbfeede6..fad40cb80b3e00938ead3ecbe96c415c55a3973d 100644 (file)
@@ -2,32 +2,40 @@
 #include <stdlib.h>
 #include "list.h"
 
-struct list_head *list_check(const struct list_head *h, const char *abortstr)
+struct list_node *list_check_node(const struct list_node *node,
+                                 const char *abortstr)
 {
-       const struct list_node *n, *p;
+       const struct list_node *p, *n;
        int count = 0;
 
-       if (h->n.next == &h->n) {
-               if (h->n.prev != &h->n) {
+       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 empty %p\n",
-                               abortstr, h);
+                       fprintf(stderr,
+                               "%s: prev corrupt in node %p (%u) of %p\n",
+                               abortstr, n, count, node);
                        abort();
                }
-               return (struct list_head *)h;
        }
+       return (struct list_node *)node;
+}
 
-       for (p = &h->n, n = h->n.next; n != &h->n; p = n, n = n->next) {
-               count++;
-               if (n->prev != p) {
+struct list_head *list_check(const struct list_head *h, const char *abortstr)
+{
+       if (h->n.next == &h->n) {
+               if (h->n.prev != &h->n) {
                        if (!abortstr)
                                return NULL;
-                       fprintf(stderr,
-                               "%s: prev corrupt in node %p (%u) of %p\n",
-                               abortstr, n, count, h);
+                       fprintf(stderr, "%s: prev corrupt in empty %p\n",
+                               abortstr, h);
                        abort();
                }
+               return (struct list_head *)h;
        }
+
+       if (!list_check_node(&h->n, abortstr))
+               return NULL;
        return (struct list_head *)h;
 }