|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * ilist.c |
| 4 | + * support for integrated/inline doubly- and singly- linked lists |
| 5 | + * |
| 6 | + * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group |
| 7 | + * Portions Copyright (c) 1994, Regents of the University of California |
| 8 | + * |
| 9 | + * |
| 10 | + * IDENTIFICATION |
| 11 | + * src/backend/lib/ilist.c |
| 12 | + * |
| 13 | + * NOTES |
| 14 | + * This file only contains functions that are too big to be considered |
| 15 | + * for inlining. See ilist.h for most of the goodies. |
| 16 | + * |
| 17 | + *------------------------------------------------------------------------- |
| 18 | + */ |
| 19 | +#include "postgres.h" |
| 20 | + |
| 21 | +/* See ilist.h */ |
| 22 | +#define ILIST_INCLUDE_DEFINITIONS |
| 23 | + |
| 24 | +#include "lib/ilist.h" |
| 25 | + |
| 26 | +/* |
| 27 | + * removes a node from a list |
| 28 | + * |
| 29 | + * Attention: O(n) |
| 30 | + */ |
| 31 | +void |
| 32 | +slist_delete(slist_head *head, slist_node *node) |
| 33 | +{ |
| 34 | + slist_node *last = &head->head; |
| 35 | + slist_node *cur; |
| 36 | + bool found PG_USED_FOR_ASSERTS_ONLY = false; |
| 37 | + |
| 38 | + while ((cur = last->next) != NULL) |
| 39 | + { |
| 40 | + if (cur == node) |
| 41 | + { |
| 42 | + last->next = cur->next; |
| 43 | +#ifdef USE_ASSERT_CHECKING |
| 44 | + found = true; |
| 45 | +#endif |
| 46 | + break; |
| 47 | + } |
| 48 | + last = cur; |
| 49 | + } |
| 50 | + |
| 51 | + slist_check(head); |
| 52 | + Assert(found); |
| 53 | +} |
| 54 | + |
| 55 | +#ifdef ILIST_DEBUG |
| 56 | +/* |
| 57 | + * Verify integrity of a doubly linked list |
| 58 | + */ |
| 59 | +void |
| 60 | +dlist_check(dlist_head *head) |
| 61 | +{ |
| 62 | + dlist_node *cur; |
| 63 | + |
| 64 | + if (head == NULL || !(&head->head)) |
| 65 | + elog(ERROR, "doubly linked list head is not properly initialized"); |
| 66 | + |
| 67 | + /* iterate in forward direction */ |
| 68 | + for (cur = head->head.next; cur != &head->head; cur = cur->next) |
| 69 | + { |
| 70 | + if (cur == NULL || |
| 71 | + cur->next == NULL || |
| 72 | + cur->prev == NULL || |
| 73 | + cur->prev->next != cur || |
| 74 | + cur->next->prev != cur) |
| 75 | + elog(ERROR, "doubly linked list is corrupted"); |
| 76 | + } |
| 77 | + |
| 78 | + /* iterate in backward direction */ |
| 79 | + for (cur = head->head.prev; cur != &head->head; cur = cur->prev) |
| 80 | + { |
| 81 | + if (cur == NULL || |
| 82 | + cur->next == NULL || |
| 83 | + cur->prev == NULL || |
| 84 | + cur->prev->next != cur || |
| 85 | + cur->next->prev != cur) |
| 86 | + elog(ERROR, "doubly linked list is corrupted"); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/* |
| 91 | + * Verify integrity of a singly linked list |
| 92 | + */ |
| 93 | +void |
| 94 | +slist_check(slist_head *head) |
| 95 | +{ |
| 96 | + slist_node *cur; |
| 97 | + |
| 98 | + if (head == NULL) |
| 99 | + elog(ERROR, "singly linked is NULL"); |
| 100 | + |
| 101 | + /* |
| 102 | + * there isn't much we can test in a singly linked list other that it |
| 103 | + * actually ends sometime, i.e. hasn't introduced a cycle or similar |
| 104 | + */ |
| 105 | + for (cur = head->head.next; cur != NULL; cur = cur->next) |
| 106 | + ; |
| 107 | +} |
| 108 | + |
| 109 | +#endif /* ILIST_DEBUG */ |
0 commit comments