doc: Fix section of functions age(xid) and mxid_age(xid)
[pgsql.git] / src / backend / lib / ilist.c
blobce6306f0c233b2e5f313da3549b80b2274f3bab7
1 /*-------------------------------------------------------------------------
3 * ilist.c
4 * support for integrated/inline doubly- and singly- linked lists
6 * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * IDENTIFICATION
11 * src/backend/lib/ilist.c
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.
17 *-------------------------------------------------------------------------
19 #include "postgres.h"
21 #include "lib/ilist.h"
24 * Delete 'node' from list.
26 * It is not allowed to delete a 'node' which is not in the list 'head'
28 * Caution: this is O(n); consider using slist_delete_current() instead.
30 void
31 slist_delete(slist_head *head, const slist_node *node)
33 slist_node *last = &head->head;
34 slist_node *cur;
35 bool found PG_USED_FOR_ASSERTS_ONLY = false;
37 while ((cur = last->next) != NULL)
39 if (cur == node)
41 last->next = cur->next;
42 #ifdef USE_ASSERT_CHECKING
43 found = true;
44 #endif
45 break;
47 last = cur;
49 Assert(found);
51 slist_check(head);
54 #ifdef ILIST_DEBUG
56 * dlist_member_check
57 * Validate that 'node' is a member of 'head'
59 void
60 dlist_member_check(const dlist_head *head, const dlist_node *node)
62 const dlist_node *cur;
64 /* iteration open-coded to due to the use of const */
65 for (cur = head->head.next; cur != &head->head; cur = cur->next)
67 if (cur == node)
68 return;
70 elog(ERROR, "double linked list member check failure");
74 * Verify integrity of a doubly linked list
76 void
77 dlist_check(const dlist_head *head)
79 dlist_node *cur;
81 if (head == NULL)
82 elog(ERROR, "doubly linked list head address is NULL");
84 if (head->head.next == NULL && head->head.prev == NULL)
85 return; /* OK, initialized as zeroes */
87 /* iterate in forward direction */
88 for (cur = head->head.next; cur != &head->head; cur = cur->next)
90 if (cur == NULL ||
91 cur->next == NULL ||
92 cur->prev == NULL ||
93 cur->prev->next != cur ||
94 cur->next->prev != cur)
95 elog(ERROR, "doubly linked list is corrupted");
98 /* iterate in backward direction */
99 for (cur = head->head.prev; cur != &head->head; cur = cur->prev)
101 if (cur == NULL ||
102 cur->next == NULL ||
103 cur->prev == NULL ||
104 cur->prev->next != cur ||
105 cur->next->prev != cur)
106 elog(ERROR, "doubly linked list is corrupted");
111 * Verify integrity of a singly linked list
113 void
114 slist_check(const slist_head *head)
116 slist_node *cur;
118 if (head == NULL)
119 elog(ERROR, "singly linked list head address is NULL");
122 * there isn't much we can test in a singly linked list except that it
123 * actually ends sometime, i.e. hasn't introduced a cycle or similar
125 for (cur = head->head.next; cur != NULL; cur = cur->next)
129 #endif /* ILIST_DEBUG */