1 /*-------------------------------------------------------------------------
4 * integrated/inline doubly- and singly-linked lists
6 * These list types are useful when there are only a predetermined set of
7 * lists that an object could be in. List links are embedded directly into
8 * the objects, and thus no extra memory management overhead is required.
9 * (Of course, if only a small proportion of existing objects are in a list,
10 * the link fields in the remainder would be wasted space. But usually,
11 * it saves space to not have separately-allocated list nodes.)
13 * None of the functions here allocate any memory; they just manipulate
14 * externally managed memory. The APIs for singly and doubly linked lists
15 * are identical as far as capabilities of both allow.
17 * Each list has a list header, which exists even when the list is empty.
18 * An empty singly-linked list has a NULL pointer in its header.
19 * There are two kinds of empty doubly linked lists: those that have been
20 * initialized to NULL, and those that have been initialized to circularity.
21 * (If a dlist is modified and then all its elements are deleted, it will be
22 * in the circular state.) We prefer circular dlists because there are some
23 * operations that can be done without branches (and thus faster) on lists
24 * that use circular representation. However, it is often convenient to
25 * initialize list headers to zeroes rather than setting them up with an
26 * explicit initialization function, so we also allow the other case.
30 * Here's a simple example demonstrating how this can be used. Let's assume
31 * we want to store information about the tables contained in a database.
33 * #include "lib/ilist.h"
35 * // Define struct for the databases including a list header that will be
36 * // used to access the nodes in the table list later on.
37 * typedef struct my_database
44 * // Define struct for the tables. Note the list_node element which stores
45 * // prev/next list links. The list_node element need not be first.
46 * typedef struct my_table
49 * dlist_node list_node;
54 * // create a database
55 * my_database *db = create_database();
57 * // and add a few tables to its table list
58 * dlist_push_head(&db->tables, &create_table(db, "a")->list_node);
60 * dlist_push_head(&db->tables, &create_table(db, "b")->list_node);
63 * To iterate over the table list, we allocate an iterator variable and use
64 * a specialized looping construct. Inside a dlist_foreach, the iterator's
65 * 'cur' field can be used to access the current element. iter.cur points to
66 * a 'dlist_node', but most of the time what we want is the actual table
67 * information; dlist_container() gives us that, like so:
70 * dlist_foreach(iter, &db->tables)
72 * my_table *tbl = dlist_container(my_table, list_node, iter.cur);
73 * printf("we have a table: %s in database %s\n",
74 * tbl->tablename, db->datname);
78 * While a simple iteration is useful, we sometimes also want to manipulate
79 * the list while iterating. There is a different iterator element and looping
80 * construct for that. Suppose we want to delete tables that meet a certain
83 * dlist_mutable_iter miter;
84 * dlist_foreach_modify(miter, &db->tables)
86 * my_table *tbl = dlist_container(my_table, list_node, miter.cur);
88 * if (!tbl->to_be_deleted)
89 * continue; // don't touch this one
91 * // unlink the current table from the linked list
92 * dlist_delete(miter.cur);
93 * // as these lists never manage memory, we can still access the table
94 * // after it's been unlinked
95 * drop_table(db, tbl);
99 * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
100 * Portions Copyright (c) 1994, Regents of the University of California
103 * src/include/lib/ilist.h
104 *-------------------------------------------------------------------------
110 * Enable for extra debugging. This is rather expensive, so it's not enabled by
111 * default even when USE_ASSERT_CHECKING.
113 /* #define ILIST_DEBUG */
116 * Node of a doubly linked list.
118 * Embed this in structs that need to be part of a doubly linked list.
120 typedef struct dlist_node dlist_node
;
128 * Head of a doubly linked list.
130 * Non-empty lists are internally circularly linked. Circular lists have the
131 * advantage of not needing any branches in the most common list manipulations.
132 * An empty list can also be represented as a pair of NULL pointers, making
133 * initialization easier.
135 typedef struct dlist_head
138 * head.next either points to the first element of the list; to &head if
139 * it's a circular empty list; or to NULL if empty and not circular.
141 * head.prev either points to the last element of the list; to &head if
142 * it's a circular empty list; or to NULL if empty and not circular.
149 * Doubly linked list iterator.
151 * Used as state in dlist_foreach() and dlist_reverse_foreach(). To get the
152 * current element of the iteration use the 'cur' member.
154 * Iterations using this are *not* allowed to change the list while iterating!
156 * NB: We use an extra "end" field here to avoid multiple evaluations of
157 * arguments in the dlist_foreach() macro.
159 typedef struct dlist_iter
161 dlist_node
*cur
; /* current element */
162 dlist_node
*end
; /* last node we'll iterate to */
166 * Doubly linked list iterator allowing some modifications while iterating.
168 * Used as state in dlist_foreach_modify(). To get the current element of the
169 * iteration use the 'cur' member.
171 * Iterations using this are only allowed to change the list at the current
172 * point of iteration. It is fine to delete the current node, but it is *not*
173 * fine to insert or delete adjacent nodes.
175 * NB: We need a separate type for mutable iterations so that we can store
176 * the 'next' node of the current node in case it gets deleted or modified.
178 typedef struct dlist_mutable_iter
180 dlist_node
*cur
; /* current element */
181 dlist_node
*next
; /* next node we'll iterate to */
182 dlist_node
*end
; /* last node we'll iterate to */
183 } dlist_mutable_iter
;
186 * Node of a singly linked list.
188 * Embed this in structs that need to be part of a singly linked list.
190 typedef struct slist_node slist_node
;
197 * Head of a singly linked list.
199 * Singly linked lists are not circularly linked, in contrast to doubly linked
200 * lists; we just set head.next to NULL if empty. This doesn't incur any
201 * additional branches in the usual manipulations.
203 typedef struct slist_head
209 * Singly linked list iterator.
211 * Used as state in slist_foreach(). To get the current element of the
212 * iteration use the 'cur' member.
214 * It's allowed to modify the list while iterating, with the exception of
215 * deleting the iterator's current node; deletion of that node requires
216 * care if the iteration is to be continued afterward. (Doing so and also
217 * deleting or inserting adjacent list elements might misbehave; also, if
218 * the user frees the current node's storage, continuing the iteration is
221 * NB: this wouldn't really need to be an extra struct, we could use an
222 * slist_node * directly. We prefer a separate type for consistency.
224 typedef struct slist_iter
230 * Singly linked list iterator allowing some modifications while iterating.
232 * Used as state in slist_foreach_modify(). To get the current element of the
233 * iteration use the 'cur' member.
235 * The only list modification allowed while iterating is to remove the current
236 * node via slist_delete_current() (*not* slist_delete()). Insertion or
237 * deletion of nodes adjacent to the current node would misbehave.
239 typedef struct slist_mutable_iter
241 slist_node
*cur
; /* current element */
242 slist_node
*next
; /* next node we'll iterate to */
243 slist_node
*prev
; /* prev node, for deletions */
244 } slist_mutable_iter
;
247 /* Static initializers */
248 #define DLIST_STATIC_INIT(name) {{&(name).head, &(name).head}}
249 #define SLIST_STATIC_INIT(name) {{NULL}}
252 /* Prototypes for functions too big to be inline */
254 /* Caution: this is O(n); consider using slist_delete_current() instead */
255 extern void slist_delete(slist_head
*head
, slist_node
*node
);
258 extern void dlist_check(dlist_head
*head
);
259 extern void slist_check(slist_head
*head
);
262 * These seemingly useless casts to void are here to keep the compiler quiet
263 * about the argument being unused in many functions in a non-debug compile,
264 * in which functions the only point of passing the list head pointer is to be
265 * able to run these checks.
267 #define dlist_check(head) ((void) (head))
268 #define slist_check(head) ((void) (head))
269 #endif /* ILIST_DEBUG */
271 /* doubly linked list implementation */
274 * Initialize a doubly linked list.
275 * Previous state will be thrown away without any cleanup.
278 dlist_init(dlist_head
*head
)
280 head
->head
.next
= head
->head
.prev
= &head
->head
;
286 * An empty list has either its first 'next' pointer set to NULL, or to itself.
289 dlist_is_empty(dlist_head
*head
)
293 return head
->head
.next
== NULL
|| head
->head
.next
== &(head
->head
);
297 * Insert a node at the beginning of the list.
300 dlist_push_head(dlist_head
*head
, dlist_node
*node
)
302 if (head
->head
.next
== NULL
) /* convert NULL header to circular */
305 node
->next
= head
->head
.next
;
306 node
->prev
= &head
->head
;
307 node
->next
->prev
= node
;
308 head
->head
.next
= node
;
314 * Insert a node at the end of the list.
317 dlist_push_tail(dlist_head
*head
, dlist_node
*node
)
319 if (head
->head
.next
== NULL
) /* convert NULL header to circular */
322 node
->next
= &head
->head
;
323 node
->prev
= head
->head
.prev
;
324 node
->prev
->next
= node
;
325 head
->head
.prev
= node
;
331 * Insert a node after another *in the same list*
334 dlist_insert_after(dlist_node
*after
, dlist_node
*node
)
337 node
->next
= after
->next
;
339 node
->next
->prev
= node
;
343 * Insert a node before another *in the same list*
346 dlist_insert_before(dlist_node
*before
, dlist_node
*node
)
348 node
->prev
= before
->prev
;
351 node
->prev
->next
= node
;
355 * Delete 'node' from its list (it must be in one).
358 dlist_delete(dlist_node
*node
)
360 node
->prev
->next
= node
->next
;
361 node
->next
->prev
= node
->prev
;
365 * Remove and return the first node from a list (there must be one).
367 static inline dlist_node
*
368 dlist_pop_head_node(dlist_head
*head
)
372 Assert(!dlist_is_empty(head
));
373 node
= head
->head
.next
;
379 * Move element from its current position in the list to the head position in
382 * Undefined behaviour if 'node' is not already part of the list.
385 dlist_move_head(dlist_head
*head
, dlist_node
*node
)
387 /* fast path if it's already at the head */
388 if (head
->head
.next
== node
)
392 dlist_push_head(head
, node
);
398 * Check whether 'node' has a following node.
399 * Caution: unreliable if 'node' is not in the list.
402 dlist_has_next(dlist_head
*head
, dlist_node
*node
)
404 return node
->next
!= &head
->head
;
408 * Check whether 'node' has a preceding node.
409 * Caution: unreliable if 'node' is not in the list.
412 dlist_has_prev(dlist_head
*head
, dlist_node
*node
)
414 return node
->prev
!= &head
->head
;
418 * Return the next node in the list (there must be one).
420 static inline dlist_node
*
421 dlist_next_node(dlist_head
*head
, dlist_node
*node
)
423 Assert(dlist_has_next(head
, node
));
428 * Return previous node in the list (there must be one).
430 static inline dlist_node
*
431 dlist_prev_node(dlist_head
*head
, dlist_node
*node
)
433 Assert(dlist_has_prev(head
, node
));
437 /* internal support function to get address of head element's struct */
439 dlist_head_element_off(dlist_head
*head
, size_t off
)
441 Assert(!dlist_is_empty(head
));
442 return (char *) head
->head
.next
- off
;
446 * Return the first node in the list (there must be one).
448 static inline dlist_node
*
449 dlist_head_node(dlist_head
*head
)
451 return (dlist_node
*) dlist_head_element_off(head
, 0);
454 /* internal support function to get address of tail element's struct */
456 dlist_tail_element_off(dlist_head
*head
, size_t off
)
458 Assert(!dlist_is_empty(head
));
459 return (char *) head
->head
.prev
- off
;
463 * Return the last node in the list (there must be one).
465 static inline dlist_node
*
466 dlist_tail_node(dlist_head
*head
)
468 return (dlist_node
*) dlist_tail_element_off(head
, 0);
472 * Return the containing struct of 'type' where 'membername' is the dlist_node
473 * pointed at by 'ptr'.
475 * This is used to convert a dlist_node * back to its containing struct.
477 #define dlist_container(type, membername, ptr) \
478 (AssertVariableIsOfTypeMacro(ptr, dlist_node *), \
479 AssertVariableIsOfTypeMacro(((type *) NULL)->membername, dlist_node), \
480 ((type *) ((char *) (ptr) - offsetof(type, membername))))
483 * Return the address of the first element in the list.
485 * The list must not be empty.
487 #define dlist_head_element(type, membername, lhead) \
488 (AssertVariableIsOfTypeMacro(((type *) NULL)->membername, dlist_node), \
489 (type *) dlist_head_element_off(lhead, offsetof(type, membername)))
492 * Return the address of the last element in the list.
494 * The list must not be empty.
496 #define dlist_tail_element(type, membername, lhead) \
497 (AssertVariableIsOfTypeMacro(((type *) NULL)->membername, dlist_node), \
498 ((type *) dlist_tail_element_off(lhead, offsetof(type, membername))))
501 * Iterate through the list pointed at by 'lhead' storing the state in 'iter'.
503 * Access the current element with iter.cur.
505 * It is *not* allowed to manipulate the list during iteration.
507 #define dlist_foreach(iter, lhead) \
508 for (AssertVariableIsOfTypeMacro(iter, dlist_iter), \
509 AssertVariableIsOfTypeMacro(lhead, dlist_head *), \
510 (iter).end = &(lhead)->head, \
511 (iter).cur = (iter).end->next ? (iter).end->next : (iter).end; \
512 (iter).cur != (iter).end; \
513 (iter).cur = (iter).cur->next)
516 * Iterate through the list pointed at by 'lhead' storing the state in 'iter'.
518 * Access the current element with iter.cur.
520 * Iterations using this are only allowed to change the list at the current
521 * point of iteration. It is fine to delete the current node, but it is *not*
522 * fine to insert or delete adjacent nodes.
524 #define dlist_foreach_modify(iter, lhead) \
525 for (AssertVariableIsOfTypeMacro(iter, dlist_mutable_iter), \
526 AssertVariableIsOfTypeMacro(lhead, dlist_head *), \
527 (iter).end = &(lhead)->head, \
528 (iter).cur = (iter).end->next ? (iter).end->next : (iter).end, \
529 (iter).next = (iter).cur->next; \
530 (iter).cur != (iter).end; \
531 (iter).cur = (iter).next, (iter).next = (iter).cur->next)
534 * Iterate through the list in reverse order.
536 * It is *not* allowed to manipulate the list during iteration.
538 #define dlist_reverse_foreach(iter, lhead) \
539 for (AssertVariableIsOfTypeMacro(iter, dlist_iter), \
540 AssertVariableIsOfTypeMacro(lhead, dlist_head *), \
541 (iter).end = &(lhead)->head, \
542 (iter).cur = (iter).end->prev ? (iter).end->prev : (iter).end; \
543 (iter).cur != (iter).end; \
544 (iter).cur = (iter).cur->prev)
547 /* singly linked list implementation */
550 * Initialize a singly linked list.
551 * Previous state will be thrown away without any cleanup.
554 slist_init(slist_head
*head
)
556 head
->head
.next
= NULL
;
563 slist_is_empty(slist_head
*head
)
567 return head
->head
.next
== NULL
;
571 * Insert a node at the beginning of the list.
574 slist_push_head(slist_head
*head
, slist_node
*node
)
576 node
->next
= head
->head
.next
;
577 head
->head
.next
= node
;
583 * Insert a node after another *in the same list*
586 slist_insert_after(slist_node
*after
, slist_node
*node
)
588 node
->next
= after
->next
;
593 * Remove and return the first node from a list (there must be one).
595 static inline slist_node
*
596 slist_pop_head_node(slist_head
*head
)
600 Assert(!slist_is_empty(head
));
601 node
= head
->head
.next
;
602 head
->head
.next
= node
->next
;
608 * Check whether 'node' has a following node.
611 slist_has_next(slist_head
*head
, slist_node
*node
)
615 return node
->next
!= NULL
;
619 * Return the next node in the list (there must be one).
621 static inline slist_node
*
622 slist_next_node(slist_head
*head
, slist_node
*node
)
624 Assert(slist_has_next(head
, node
));
628 /* internal support function to get address of head element's struct */
630 slist_head_element_off(slist_head
*head
, size_t off
)
632 Assert(!slist_is_empty(head
));
633 return (char *) head
->head
.next
- off
;
637 * Return the first node in the list (there must be one).
639 static inline slist_node
*
640 slist_head_node(slist_head
*head
)
642 return (slist_node
*) slist_head_element_off(head
, 0);
646 * Delete the list element the iterator currently points to.
648 * Caution: this modifies iter->cur, so don't use that again in the current
652 slist_delete_current(slist_mutable_iter
*iter
)
655 * Update previous element's forward link. If the iteration is at the
656 * first list element, iter->prev will point to the list header's "head"
657 * field, so we don't need a special case for that.
659 iter
->prev
->next
= iter
->next
;
662 * Reset cur to prev, so that prev will continue to point to the prior
663 * valid list element after slist_foreach_modify() advances to the next.
665 iter
->cur
= iter
->prev
;
669 * Return the containing struct of 'type' where 'membername' is the slist_node
670 * pointed at by 'ptr'.
672 * This is used to convert a slist_node * back to its containing struct.
674 #define slist_container(type, membername, ptr) \
675 (AssertVariableIsOfTypeMacro(ptr, slist_node *), \
676 AssertVariableIsOfTypeMacro(((type *) NULL)->membername, slist_node), \
677 ((type *) ((char *) (ptr) - offsetof(type, membername))))
680 * Return the address of the first element in the list.
682 * The list must not be empty.
684 #define slist_head_element(type, membername, lhead) \
685 (AssertVariableIsOfTypeMacro(((type *) NULL)->membername, slist_node), \
686 (type *) slist_head_element_off(lhead, offsetof(type, membername)))
689 * Iterate through the list pointed at by 'lhead' storing the state in 'iter'.
691 * Access the current element with iter.cur.
693 * It's allowed to modify the list while iterating, with the exception of
694 * deleting the iterator's current node; deletion of that node requires
695 * care if the iteration is to be continued afterward. (Doing so and also
696 * deleting or inserting adjacent list elements might misbehave; also, if
697 * the user frees the current node's storage, continuing the iteration is
700 #define slist_foreach(iter, lhead) \
701 for (AssertVariableIsOfTypeMacro(iter, slist_iter), \
702 AssertVariableIsOfTypeMacro(lhead, slist_head *), \
703 (iter).cur = (lhead)->head.next; \
704 (iter).cur != NULL; \
705 (iter).cur = (iter).cur->next)
708 * Iterate through the list pointed at by 'lhead' storing the state in 'iter'.
710 * Access the current element with iter.cur.
712 * The only list modification allowed while iterating is to remove the current
713 * node via slist_delete_current() (*not* slist_delete()). Insertion or
714 * deletion of nodes adjacent to the current node would misbehave.
716 #define slist_foreach_modify(iter, lhead) \
717 for (AssertVariableIsOfTypeMacro(iter, slist_mutable_iter), \
718 AssertVariableIsOfTypeMacro(lhead, slist_head *), \
719 (iter).prev = &(lhead)->head, \
720 (iter).cur = (iter).prev->next, \
721 (iter).next = (iter).cur ? (iter).cur->next : NULL; \
722 (iter).cur != NULL; \
723 (iter).prev = (iter).cur, \
724 (iter).cur = (iter).next, \
725 (iter).next = (iter).next ? (iter).next->next : NULL)