1 /* $NetBSD: list.h,v 1.2 2004/05/20 19:51:55 christos Exp $ */ 2 3 /* 4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 5 * Copyright (c) 1997,1999 by Internet Software Consortium. 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 #ifndef LIST_H 21 #define LIST_H 1 22 #include <isc/assertions.h> 23 24 #define LIST(type) struct { type *head, *tail; } 25 #define INIT_LIST(list) \ 26 do { (list).head = NULL; (list).tail = NULL; } while (/*CONSTCOND*/0) 27 28 #define LINK(type) struct { type *prev, *next; } 29 #define INIT_LINK_TYPE(elt, link, type) \ 30 do { \ 31 (elt)->link.prev = (type *)(-1); \ 32 (elt)->link.next = (type *)(-1); \ 33 } while (/*CONSTCOND*/0) 34 #define INIT_LINK(elt, link) \ 35 INIT_LINK_TYPE(elt, link, void) 36 #define LINKED(elt, link) ((void *)((elt)->link.prev) != (void *)(-1)) 37 38 #define HEAD(list) ((list).head) 39 #define TAIL(list) ((list).tail) 40 #define EMPTY(list) ((list).head == NULL) 41 42 #define PREPEND(list, elt, link) \ 43 do { \ 44 INSIST(!LINKED(elt, link));\ 45 if ((list).head != NULL) \ 46 (list).head->link.prev = (elt); \ 47 else \ 48 (list).tail = (elt); \ 49 (elt)->link.prev = NULL; \ 50 (elt)->link.next = (list).head; \ 51 (list).head = (elt); \ 52 } while (/*CONSTCOND*/0) 53 54 #define APPEND(list, elt, link) \ 55 do { \ 56 INSIST(!LINKED(elt, link));\ 57 if ((list).tail != NULL) \ 58 (list).tail->link.next = (elt); \ 59 else \ 60 (list).head = (elt); \ 61 (elt)->link.prev = (list).tail; \ 62 (elt)->link.next = NULL; \ 63 (list).tail = (elt); \ 64 } while (/*CONSTCOND*/0) 65 66 #define UNLINK_TYPE(list, elt, link, type) \ 67 do { \ 68 INSIST(LINKED(elt, link));\ 69 if ((elt)->link.next != NULL) \ 70 (elt)->link.next->link.prev = (elt)->link.prev; \ 71 else \ 72 (list).tail = (elt)->link.prev; \ 73 if ((elt)->link.prev != NULL) \ 74 (elt)->link.prev->link.next = (elt)->link.next; \ 75 else \ 76 (list).head = (elt)->link.next; \ 77 INIT_LINK_TYPE(elt, link, type); \ 78 } while (/*CONSTCOND*/0) 79 #define UNLINK(list, elt, link) \ 80 UNLINK_TYPE(list, elt, link, void) 81 82 #define PREV(elt, link) ((elt)->link.prev) 83 #define NEXT(elt, link) ((elt)->link.next) 84 85 #define INSERT_BEFORE(list, before, elt, link) \ 86 do { \ 87 INSIST(!LINKED(elt, link));\ 88 if ((before)->link.prev == NULL) \ 89 PREPEND(list, elt, link); \ 90 else { \ 91 (elt)->link.prev = (before)->link.prev; \ 92 (before)->link.prev = (elt); \ 93 (elt)->link.prev->link.next = (elt); \ 94 (elt)->link.next = (before); \ 95 } \ 96 } while (/*CONSTCOND*/0) 97 98 #define INSERT_AFTER(list, after, elt, link) \ 99 do { \ 100 INSIST(!LINKED(elt, link));\ 101 if ((after)->link.next == NULL) \ 102 APPEND(list, elt, link); \ 103 else { \ 104 (elt)->link.next = (after)->link.next; \ 105 (after)->link.next = (elt); \ 106 (elt)->link.next->link.prev = (elt); \ 107 (elt)->link.prev = (after); \ 108 } \ 109 } while (/*CONSTCOND*/0) 110 111 #define ENQUEUE(list, elt, link) APPEND(list, elt, link) 112 #define DEQUEUE(list, elt, link) UNLINK(list, elt, link) 113 114 #endif /* LIST_H */ 115