]> git.ozlabs.org Git - ccan/blob - ccan/list/list.c
list: implement list_check_node to check list from node rather than head
[ccan] / ccan / list / list.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "list.h"
4
5 struct list_node *list_check_node(const struct list_node *node,
6                                   const char *abortstr)
7 {
8         const struct list_node *p, *n;
9         int count = 0;
10
11         for (p = node, n = node->next; n != node; p = n, n = n->next) {
12                 count++;
13                 if (n->prev != p) {
14                         if (!abortstr)
15                                 return NULL;
16                         fprintf(stderr,
17                                 "%s: prev corrupt in node %p (%u) of %p\n",
18                                 abortstr, n, count, node);
19                         abort();
20                 }
21         }
22         return (struct list_node *)node;
23 }
24
25 struct list_head *list_check(const struct list_head *h, const char *abortstr)
26 {
27         if (h->n.next == &h->n) {
28                 if (h->n.prev != &h->n) {
29                         if (!abortstr)
30                                 return NULL;
31                         fprintf(stderr, "%s: prev corrupt in empty %p\n",
32                                 abortstr, h);
33                         abort();
34                 }
35                 return (struct list_head *)h;
36         }
37
38         if (!list_check_node(&h->n, abortstr))
39                 return NULL;
40         return (struct list_head *)h;
41 }