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