17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
37c478bd9Sstevel@tonic-gate  * Copyright (c) 1997,1999 by Internet Software Consortium.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
67c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate  *
9*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*9525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*9525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*9525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*9525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*9525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*9525b14bSRao Shoaib  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #ifndef LIST_H
197c478bd9Sstevel@tonic-gate #define LIST_H 1
207c478bd9Sstevel@tonic-gate #include <isc/assertions.h>
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate #define LIST(type) struct { type *head, *tail; }
237c478bd9Sstevel@tonic-gate #define INIT_LIST(list) \
247c478bd9Sstevel@tonic-gate 	do { (list).head = NULL; (list).tail = NULL; } while (0)
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #define LINK(type) struct { type *prev, *next; }
277c478bd9Sstevel@tonic-gate #define INIT_LINK_TYPE(elt, link, type) \
287c478bd9Sstevel@tonic-gate 	do { \
297c478bd9Sstevel@tonic-gate 		(elt)->link.prev = (type *)(-1); \
307c478bd9Sstevel@tonic-gate 		(elt)->link.next = (type *)(-1); \
317c478bd9Sstevel@tonic-gate 	} while (0)
327c478bd9Sstevel@tonic-gate #define INIT_LINK(elt, link) \
337c478bd9Sstevel@tonic-gate 	INIT_LINK_TYPE(elt, link, void)
34*9525b14bSRao Shoaib #define LINKED(elt, link) ((void *)((elt)->link.prev) != (void *)(-1) && \
35*9525b14bSRao Shoaib 			   (void *)((elt)->link.next) != (void *)(-1))
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #define HEAD(list) ((list).head)
387c478bd9Sstevel@tonic-gate #define TAIL(list) ((list).tail)
397c478bd9Sstevel@tonic-gate #define EMPTY(list) ((list).head == NULL)
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #define PREPEND(list, elt, link) \
427c478bd9Sstevel@tonic-gate 	do { \
437c478bd9Sstevel@tonic-gate 		INSIST(!LINKED(elt, link));\
447c478bd9Sstevel@tonic-gate 		if ((list).head != NULL) \
457c478bd9Sstevel@tonic-gate 			(list).head->link.prev = (elt); \
467c478bd9Sstevel@tonic-gate 		else \
477c478bd9Sstevel@tonic-gate 			(list).tail = (elt); \
487c478bd9Sstevel@tonic-gate 		(elt)->link.prev = NULL; \
497c478bd9Sstevel@tonic-gate 		(elt)->link.next = (list).head; \
507c478bd9Sstevel@tonic-gate 		(list).head = (elt); \
517c478bd9Sstevel@tonic-gate 	} while (0)
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #define APPEND(list, elt, link) \
547c478bd9Sstevel@tonic-gate 	do { \
557c478bd9Sstevel@tonic-gate 		INSIST(!LINKED(elt, link));\
567c478bd9Sstevel@tonic-gate 		if ((list).tail != NULL) \
577c478bd9Sstevel@tonic-gate 			(list).tail->link.next = (elt); \
587c478bd9Sstevel@tonic-gate 		else \
597c478bd9Sstevel@tonic-gate 			(list).head = (elt); \
607c478bd9Sstevel@tonic-gate 		(elt)->link.prev = (list).tail; \
617c478bd9Sstevel@tonic-gate 		(elt)->link.next = NULL; \
627c478bd9Sstevel@tonic-gate 		(list).tail = (elt); \
637c478bd9Sstevel@tonic-gate 	} while (0)
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #define UNLINK_TYPE(list, elt, link, type) \
667c478bd9Sstevel@tonic-gate 	do { \
677c478bd9Sstevel@tonic-gate 		INSIST(LINKED(elt, link));\
687c478bd9Sstevel@tonic-gate 		if ((elt)->link.next != NULL) \
697c478bd9Sstevel@tonic-gate 			(elt)->link.next->link.prev = (elt)->link.prev; \
70*9525b14bSRao Shoaib 		else { \
71*9525b14bSRao Shoaib 			INSIST((list).tail == (elt)); \
727c478bd9Sstevel@tonic-gate 			(list).tail = (elt)->link.prev; \
73*9525b14bSRao Shoaib 		} \
747c478bd9Sstevel@tonic-gate 		if ((elt)->link.prev != NULL) \
757c478bd9Sstevel@tonic-gate 			(elt)->link.prev->link.next = (elt)->link.next; \
76*9525b14bSRao Shoaib 		else { \
77*9525b14bSRao Shoaib 			INSIST((list).head == (elt)); \
787c478bd9Sstevel@tonic-gate 			(list).head = (elt)->link.next; \
79*9525b14bSRao Shoaib 		} \
807c478bd9Sstevel@tonic-gate 		INIT_LINK_TYPE(elt, link, type); \
817c478bd9Sstevel@tonic-gate 	} while (0)
827c478bd9Sstevel@tonic-gate #define UNLINK(list, elt, link) \
837c478bd9Sstevel@tonic-gate 	UNLINK_TYPE(list, elt, link, void)
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #define PREV(elt, link) ((elt)->link.prev)
867c478bd9Sstevel@tonic-gate #define NEXT(elt, link) ((elt)->link.next)
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #define INSERT_BEFORE(list, before, elt, link) \
897c478bd9Sstevel@tonic-gate 	do { \
907c478bd9Sstevel@tonic-gate 		INSIST(!LINKED(elt, link));\
917c478bd9Sstevel@tonic-gate 		if ((before)->link.prev == NULL) \
927c478bd9Sstevel@tonic-gate 			PREPEND(list, elt, link); \
937c478bd9Sstevel@tonic-gate 		else { \
947c478bd9Sstevel@tonic-gate 			(elt)->link.prev = (before)->link.prev; \
957c478bd9Sstevel@tonic-gate 			(before)->link.prev = (elt); \
967c478bd9Sstevel@tonic-gate 			(elt)->link.prev->link.next = (elt); \
977c478bd9Sstevel@tonic-gate 			(elt)->link.next = (before); \
987c478bd9Sstevel@tonic-gate 		} \
997c478bd9Sstevel@tonic-gate 	} while (0)
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate #define INSERT_AFTER(list, after, elt, link) \
1027c478bd9Sstevel@tonic-gate 	do { \
1037c478bd9Sstevel@tonic-gate 		INSIST(!LINKED(elt, link));\
1047c478bd9Sstevel@tonic-gate 		if ((after)->link.next == NULL) \
1057c478bd9Sstevel@tonic-gate 			APPEND(list, elt, link); \
1067c478bd9Sstevel@tonic-gate 		else { \
1077c478bd9Sstevel@tonic-gate 			(elt)->link.next = (after)->link.next; \
1087c478bd9Sstevel@tonic-gate 			(after)->link.next = (elt); \
1097c478bd9Sstevel@tonic-gate 			(elt)->link.next->link.prev = (elt); \
1107c478bd9Sstevel@tonic-gate 			(elt)->link.prev = (after); \
1117c478bd9Sstevel@tonic-gate 		} \
1127c478bd9Sstevel@tonic-gate 	} while (0)
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate #define ENQUEUE(list, elt, link) APPEND(list, elt, link)
1157c478bd9Sstevel@tonic-gate #define DEQUEUE(list, elt, link) UNLINK(list, elt, link)
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate #endif /* LIST_H */
118*9525b14bSRao Shoaib /*! \file */
119