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