Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a66ee69

Browse files
committed
Embedded list interface
Provide a common implementation of embedded singly-linked and doubly-linked lists. "Embedded" in the sense that the nodes' next/previous pointers exist within some larger struct; this design choice reduces memory allocation overhead. Most of the implementation uses inlineable functions (where supported), for performance. Some existing uses of both types of lists have been converted to the new code, for demonstration purposes. Other uses can (and probably will) be converted in the future. Since dllist.c is unused after this conversion, it has been removed. Author: Andres Freund Some tweaks by me Reviewed by Tom Lane, Peter Geoghegan
1 parent f862a32 commit a66ee69

File tree

9 files changed

+1079
-530
lines changed

9 files changed

+1079
-530
lines changed

src/backend/lib/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ subdir = src/backend/lib
1212
top_builddir = ../../..
1313
include $(top_builddir)/src/Makefile.global
1414

15-
OBJS = dllist.o stringinfo.o
15+
OBJS = ilist.o stringinfo.o
1616

1717
include $(top_srcdir)/src/backend/common.mk

src/backend/lib/dllist.c

-214
This file was deleted.

src/backend/lib/ilist.c

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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

Comments
 (0)