xref: /onnv-gate/usr/src/lib/libresolv2/include/isc/list.h (revision 11038:74b12212b8a2)
10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
30Sstevel@tonic-gate  * Copyright (c) 1997,1999 by Internet Software Consortium.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
60Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
70Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
80Sstevel@tonic-gate  *
9*11038SRao.Shoaib@Sun.COM  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*11038SRao.Shoaib@Sun.COM  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*11038SRao.Shoaib@Sun.COM  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*11038SRao.Shoaib@Sun.COM  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*11038SRao.Shoaib@Sun.COM  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*11038SRao.Shoaib@Sun.COM  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*11038SRao.Shoaib@Sun.COM  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
160Sstevel@tonic-gate  */
170Sstevel@tonic-gate 
180Sstevel@tonic-gate #ifndef LIST_H
190Sstevel@tonic-gate #define LIST_H 1
200Sstevel@tonic-gate #include <isc/assertions.h>
210Sstevel@tonic-gate 
220Sstevel@tonic-gate #define LIST(type) struct { type *head, *tail; }
230Sstevel@tonic-gate #define INIT_LIST(list) \
240Sstevel@tonic-gate 	do { (list).head = NULL; (list).tail = NULL; } while (0)
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #define LINK(type) struct { type *prev, *next; }
270Sstevel@tonic-gate #define INIT_LINK_TYPE(elt, link, type) \
280Sstevel@tonic-gate 	do { \
290Sstevel@tonic-gate 		(elt)->link.prev = (type *)(-1); \
300Sstevel@tonic-gate 		(elt)->link.next = (type *)(-1); \
310Sstevel@tonic-gate 	} while (0)
320Sstevel@tonic-gate #define INIT_LINK(elt, link) \
330Sstevel@tonic-gate 	INIT_LINK_TYPE(elt, link, void)
34*11038SRao.Shoaib@Sun.COM #define LINKED(elt, link) ((void *)((elt)->link.prev) != (void *)(-1) && \
35*11038SRao.Shoaib@Sun.COM 			   (void *)((elt)->link.next) != (void *)(-1))
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #define HEAD(list) ((list).head)
380Sstevel@tonic-gate #define TAIL(list) ((list).tail)
390Sstevel@tonic-gate #define EMPTY(list) ((list).head == NULL)
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #define PREPEND(list, elt, link) \
420Sstevel@tonic-gate 	do { \
430Sstevel@tonic-gate 		INSIST(!LINKED(elt, link));\
440Sstevel@tonic-gate 		if ((list).head != NULL) \
450Sstevel@tonic-gate 			(list).head->link.prev = (elt); \
460Sstevel@tonic-gate 		else \
470Sstevel@tonic-gate 			(list).tail = (elt); \
480Sstevel@tonic-gate 		(elt)->link.prev = NULL; \
490Sstevel@tonic-gate 		(elt)->link.next = (list).head; \
500Sstevel@tonic-gate 		(list).head = (elt); \
510Sstevel@tonic-gate 	} while (0)
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #define APPEND(list, elt, link) \
540Sstevel@tonic-gate 	do { \
550Sstevel@tonic-gate 		INSIST(!LINKED(elt, link));\
560Sstevel@tonic-gate 		if ((list).tail != NULL) \
570Sstevel@tonic-gate 			(list).tail->link.next = (elt); \
580Sstevel@tonic-gate 		else \
590Sstevel@tonic-gate 			(list).head = (elt); \
600Sstevel@tonic-gate 		(elt)->link.prev = (list).tail; \
610Sstevel@tonic-gate 		(elt)->link.next = NULL; \
620Sstevel@tonic-gate 		(list).tail = (elt); \
630Sstevel@tonic-gate 	} while (0)
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #define UNLINK_TYPE(list, elt, link, type) \
660Sstevel@tonic-gate 	do { \
670Sstevel@tonic-gate 		INSIST(LINKED(elt, link));\
680Sstevel@tonic-gate 		if ((elt)->link.next != NULL) \
690Sstevel@tonic-gate 			(elt)->link.next->link.prev = (elt)->link.prev; \
70*11038SRao.Shoaib@Sun.COM 		else { \
71*11038SRao.Shoaib@Sun.COM 			INSIST((list).tail == (elt)); \
720Sstevel@tonic-gate 			(list).tail = (elt)->link.prev; \
73*11038SRao.Shoaib@Sun.COM 		} \
740Sstevel@tonic-gate 		if ((elt)->link.prev != NULL) \
750Sstevel@tonic-gate 			(elt)->link.prev->link.next = (elt)->link.next; \
76*11038SRao.Shoaib@Sun.COM 		else { \
77*11038SRao.Shoaib@Sun.COM 			INSIST((list).head == (elt)); \
780Sstevel@tonic-gate 			(list).head = (elt)->link.next; \
79*11038SRao.Shoaib@Sun.COM 		} \
800Sstevel@tonic-gate 		INIT_LINK_TYPE(elt, link, type); \
810Sstevel@tonic-gate 	} while (0)
820Sstevel@tonic-gate #define UNLINK(list, elt, link) \
830Sstevel@tonic-gate 	UNLINK_TYPE(list, elt, link, void)
840Sstevel@tonic-gate 
850Sstevel@tonic-gate #define PREV(elt, link) ((elt)->link.prev)
860Sstevel@tonic-gate #define NEXT(elt, link) ((elt)->link.next)
870Sstevel@tonic-gate 
880Sstevel@tonic-gate #define INSERT_BEFORE(list, before, elt, link) \
890Sstevel@tonic-gate 	do { \
900Sstevel@tonic-gate 		INSIST(!LINKED(elt, link));\
910Sstevel@tonic-gate 		if ((before)->link.prev == NULL) \
920Sstevel@tonic-gate 			PREPEND(list, elt, link); \
930Sstevel@tonic-gate 		else { \
940Sstevel@tonic-gate 			(elt)->link.prev = (before)->link.prev; \
950Sstevel@tonic-gate 			(before)->link.prev = (elt); \
960Sstevel@tonic-gate 			(elt)->link.prev->link.next = (elt); \
970Sstevel@tonic-gate 			(elt)->link.next = (before); \
980Sstevel@tonic-gate 		} \
990Sstevel@tonic-gate 	} while (0)
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate #define INSERT_AFTER(list, after, elt, link) \
1020Sstevel@tonic-gate 	do { \
1030Sstevel@tonic-gate 		INSIST(!LINKED(elt, link));\
1040Sstevel@tonic-gate 		if ((after)->link.next == NULL) \
1050Sstevel@tonic-gate 			APPEND(list, elt, link); \
1060Sstevel@tonic-gate 		else { \
1070Sstevel@tonic-gate 			(elt)->link.next = (after)->link.next; \
1080Sstevel@tonic-gate 			(after)->link.next = (elt); \
1090Sstevel@tonic-gate 			(elt)->link.next->link.prev = (elt); \
1100Sstevel@tonic-gate 			(elt)->link.prev = (after); \
1110Sstevel@tonic-gate 		} \
1120Sstevel@tonic-gate 	} while (0)
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate #define ENQUEUE(list, elt, link) APPEND(list, elt, link)
1150Sstevel@tonic-gate #define DEQUEUE(list, elt, link) UNLINK(list, elt, link)
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate #endif /* LIST_H */
118*11038SRao.Shoaib@Sun.COM /*! \file */
119