xref: /illumos-gate/usr/src/common/list/list.c (revision 3c112a2b)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
531fd60d3Sahrens  * Common Development and Distribution License (the "License").
631fd60d3Sahrens  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*3c112a2bSEric Taylor  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
267c478bd9Sstevel@tonic-gate  * Generic doubly-linked list implementation
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/list.h>
307c478bd9Sstevel@tonic-gate #include <sys/list_impl.h>
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
33*3c112a2bSEric Taylor #ifdef _KERNEL
347c478bd9Sstevel@tonic-gate #include <sys/debug.h>
35*3c112a2bSEric Taylor #else
36*3c112a2bSEric Taylor #include <assert.h>
37*3c112a2bSEric Taylor #define	ASSERT(a)	assert(a)
38*3c112a2bSEric Taylor #endif
397c478bd9Sstevel@tonic-gate 
40*3c112a2bSEric Taylor #ifdef lint
41*3c112a2bSEric Taylor extern list_node_t *list_d2l(list_t *list, void *obj);
42*3c112a2bSEric Taylor #else
437c478bd9Sstevel@tonic-gate #define	list_d2l(a, obj) ((list_node_t *)(((char *)obj) + (a)->list_offset))
44*3c112a2bSEric Taylor #endif
457c478bd9Sstevel@tonic-gate #define	list_object(a, node) ((void *)(((char *)node) - (a)->list_offset))
467c478bd9Sstevel@tonic-gate #define	list_empty(a) ((a)->list_head.list_next == &(a)->list_head)
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #define	list_insert_after_node(list, node, object) {	\
497c478bd9Sstevel@tonic-gate 	list_node_t *lnew = list_d2l(list, object);	\
50b5fca8f8Stomee 	lnew->list_prev = (node);			\
51b5fca8f8Stomee 	lnew->list_next = (node)->list_next;		\
52b5fca8f8Stomee 	(node)->list_next->list_prev = lnew;		\
53b5fca8f8Stomee 	(node)->list_next = lnew;			\
547c478bd9Sstevel@tonic-gate }
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #define	list_insert_before_node(list, node, object) {	\
577c478bd9Sstevel@tonic-gate 	list_node_t *lnew = list_d2l(list, object);	\
58b5fca8f8Stomee 	lnew->list_next = (node);			\
59b5fca8f8Stomee 	lnew->list_prev = (node)->list_prev;		\
60b5fca8f8Stomee 	(node)->list_prev->list_next = lnew;		\
61b5fca8f8Stomee 	(node)->list_prev = lnew;			\
627c478bd9Sstevel@tonic-gate }
637c478bd9Sstevel@tonic-gate 
64b5fca8f8Stomee #define	list_remove_node(node)					\
65b5fca8f8Stomee 	(node)->list_prev->list_next = (node)->list_next;	\
66b5fca8f8Stomee 	(node)->list_next->list_prev = (node)->list_prev;	\
67b5fca8f8Stomee 	(node)->list_next = (node)->list_prev = NULL
68b5fca8f8Stomee 
697c478bd9Sstevel@tonic-gate void
list_create(list_t * list,size_t size,size_t offset)707c478bd9Sstevel@tonic-gate list_create(list_t *list, size_t size, size_t offset)
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate 	ASSERT(list);
737c478bd9Sstevel@tonic-gate 	ASSERT(size > 0);
747c478bd9Sstevel@tonic-gate 	ASSERT(size >= offset + sizeof (list_node_t));
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	list->list_size = size;
777c478bd9Sstevel@tonic-gate 	list->list_offset = offset;
787c478bd9Sstevel@tonic-gate 	list->list_head.list_next = list->list_head.list_prev =
797c478bd9Sstevel@tonic-gate 	    &list->list_head;
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate void
list_destroy(list_t * list)837c478bd9Sstevel@tonic-gate list_destroy(list_t *list)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	list_node_t *node = &list->list_head;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	ASSERT(list);
887c478bd9Sstevel@tonic-gate 	ASSERT(list->list_head.list_next == node);
897c478bd9Sstevel@tonic-gate 	ASSERT(list->list_head.list_prev == node);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	node->list_next = node->list_prev = NULL;
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate void
list_insert_after(list_t * list,void * object,void * nobject)957c478bd9Sstevel@tonic-gate list_insert_after(list_t *list, void *object, void *nobject)
967c478bd9Sstevel@tonic-gate {
97b5fca8f8Stomee 	if (object == NULL) {
98b5fca8f8Stomee 		list_insert_head(list, nobject);
99b5fca8f8Stomee 	} else {
100b5fca8f8Stomee 		list_node_t *lold = list_d2l(list, object);
101b5fca8f8Stomee 		list_insert_after_node(list, lold, nobject);
102b5fca8f8Stomee 	}
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate void
list_insert_before(list_t * list,void * object,void * nobject)1067c478bd9Sstevel@tonic-gate list_insert_before(list_t *list, void *object, void *nobject)
1077c478bd9Sstevel@tonic-gate {
108b5fca8f8Stomee 	if (object == NULL) {
109b5fca8f8Stomee 		list_insert_tail(list, nobject);
110b5fca8f8Stomee 	} else {
111b5fca8f8Stomee 		list_node_t *lold = list_d2l(list, object);
112b5fca8f8Stomee 		list_insert_before_node(list, lold, nobject);
113b5fca8f8Stomee 	}
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate void
list_insert_head(list_t * list,void * object)1177c478bd9Sstevel@tonic-gate list_insert_head(list_t *list, void *object)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	list_node_t *lold = &list->list_head;
1207c478bd9Sstevel@tonic-gate 	list_insert_after_node(list, lold, object);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate void
list_insert_tail(list_t * list,void * object)1247c478bd9Sstevel@tonic-gate list_insert_tail(list_t *list, void *object)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	list_node_t *lold = &list->list_head;
1277c478bd9Sstevel@tonic-gate 	list_insert_before_node(list, lold, object);
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate void
list_remove(list_t * list,void * object)1317c478bd9Sstevel@tonic-gate list_remove(list_t *list, void *object)
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate 	list_node_t *lold = list_d2l(list, object);
1347c478bd9Sstevel@tonic-gate 	ASSERT(!list_empty(list));
1351934e92fSmaybee 	ASSERT(lold->list_next != NULL);
136b5fca8f8Stomee 	list_remove_node(lold);
137b5fca8f8Stomee }
138b5fca8f8Stomee 
139b5fca8f8Stomee void *
list_remove_head(list_t * list)140b5fca8f8Stomee list_remove_head(list_t *list)
141b5fca8f8Stomee {
142b5fca8f8Stomee 	list_node_t *head = list->list_head.list_next;
143b5fca8f8Stomee 	if (head == &list->list_head)
144b5fca8f8Stomee 		return (NULL);
145b5fca8f8Stomee 	list_remove_node(head);
146b5fca8f8Stomee 	return (list_object(list, head));
147b5fca8f8Stomee }
148b5fca8f8Stomee 
149b5fca8f8Stomee void *
list_remove_tail(list_t * list)150b5fca8f8Stomee list_remove_tail(list_t *list)
151b5fca8f8Stomee {
152b5fca8f8Stomee 	list_node_t *tail = list->list_head.list_prev;
153b5fca8f8Stomee 	if (tail == &list->list_head)
154b5fca8f8Stomee 		return (NULL);
155b5fca8f8Stomee 	list_remove_node(tail);
156b5fca8f8Stomee 	return (list_object(list, tail));
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate void *
list_head(list_t * list)1607c478bd9Sstevel@tonic-gate list_head(list_t *list)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	if (list_empty(list))
1637c478bd9Sstevel@tonic-gate 		return (NULL);
1647c478bd9Sstevel@tonic-gate 	return (list_object(list, list->list_head.list_next));
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate void *
list_tail(list_t * list)1687c478bd9Sstevel@tonic-gate list_tail(list_t *list)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	if (list_empty(list))
1717c478bd9Sstevel@tonic-gate 		return (NULL);
1727c478bd9Sstevel@tonic-gate 	return (list_object(list, list->list_head.list_prev));
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate void *
list_next(list_t * list,void * object)1767c478bd9Sstevel@tonic-gate list_next(list_t *list, void *object)
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate 	list_node_t *node = list_d2l(list, object);
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	if (node->list_next != &list->list_head)
1817c478bd9Sstevel@tonic-gate 		return (list_object(list, node->list_next));
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	return (NULL);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate void *
list_prev(list_t * list,void * object)1877c478bd9Sstevel@tonic-gate list_prev(list_t *list, void *object)
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate 	list_node_t *node = list_d2l(list, object);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	if (node->list_prev != &list->list_head)
1927c478bd9Sstevel@tonic-gate 		return (list_object(list, node->list_prev));
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	return (NULL);
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate /*
1987c478bd9Sstevel@tonic-gate  *  Insert src list after dst list. Empty src list thereafter.
1997c478bd9Sstevel@tonic-gate  */
2007c478bd9Sstevel@tonic-gate void
list_move_tail(list_t * dst,list_t * src)2017c478bd9Sstevel@tonic-gate list_move_tail(list_t *dst, list_t *src)
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	list_node_t *dstnode = &dst->list_head;
2047c478bd9Sstevel@tonic-gate 	list_node_t *srcnode = &src->list_head;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	ASSERT(dst->list_size == src->list_size);
2077c478bd9Sstevel@tonic-gate 	ASSERT(dst->list_offset == src->list_offset);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	if (list_empty(src))
2107c478bd9Sstevel@tonic-gate 		return;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	dstnode->list_prev->list_next = srcnode->list_next;
2137c478bd9Sstevel@tonic-gate 	srcnode->list_next->list_prev = dstnode->list_prev;
2147c478bd9Sstevel@tonic-gate 	dstnode->list_prev = srcnode->list_prev;
2157c478bd9Sstevel@tonic-gate 	srcnode->list_prev->list_next = dstnode;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	/* empty src list */
2187c478bd9Sstevel@tonic-gate 	srcnode->list_next = srcnode->list_prev = srcnode;
2197c478bd9Sstevel@tonic-gate }
220fa9e4066Sahrens 
221b5fca8f8Stomee void
list_link_replace(list_node_t * lold,list_node_t * lnew)222b5fca8f8Stomee list_link_replace(list_node_t *lold, list_node_t *lnew)
223b5fca8f8Stomee {
224b5fca8f8Stomee 	ASSERT(list_link_active(lold));
225b5fca8f8Stomee 	ASSERT(!list_link_active(lnew));
226b5fca8f8Stomee 
227b5fca8f8Stomee 	lnew->list_next = lold->list_next;
228b5fca8f8Stomee 	lnew->list_prev = lold->list_prev;
229b5fca8f8Stomee 	lold->list_prev->list_next = lnew;
230b5fca8f8Stomee 	lold->list_next->list_prev = lnew;
231b5fca8f8Stomee 	lold->list_next = lold->list_prev = NULL;
232b5fca8f8Stomee }
233b5fca8f8Stomee 
234b5fca8f8Stomee void
list_link_init(list_node_t * link)235b5fca8f8Stomee list_link_init(list_node_t *link)
236b5fca8f8Stomee {
237b5fca8f8Stomee 	link->list_next = NULL;
238b5fca8f8Stomee 	link->list_prev = NULL;
239b5fca8f8Stomee }
240b5fca8f8Stomee 
241fa9e4066Sahrens int
list_link_active(list_node_t * link)242fa9e4066Sahrens list_link_active(list_node_t *link)
243fa9e4066Sahrens {
244fa9e4066Sahrens 	return (link->list_next != NULL);
245fa9e4066Sahrens }
246fa9e4066Sahrens 
247fa9e4066Sahrens int
list_is_empty(list_t * list)248fa9e4066Sahrens list_is_empty(list_t *list)
249fa9e4066Sahrens {
250fa9e4066Sahrens 	return (list_empty(list));
251fa9e4066Sahrens }
252