Nut/OS  5.0.5
API Reference
list.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 1997-2002  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00024 #ifndef ISC_LIST_H
00025 #define ISC_LIST_H 1
00026 
00032 
00033 #ifdef ISC_LIST_CHECKINIT
00034 #define ISC_LINK_INSIST(x) ISC_INSIST(x)
00035 #else
00036 #define ISC_LINK_INSIST(x)
00037 #endif
00038 
00039 #define ISC_LIST(type) struct { type *head, *tail; }
00040 #define ISC_LIST_INIT(list) \
00041     do { (list).head = NULL; (list).tail = NULL; } while (0)
00042 
00043 #define ISC_LINK(type) struct { type *prev, *next; }
00044 #define ISC_LINK_INIT_TYPE(elt, link, type) \
00045     do { \
00046         (elt)->link.prev = (type *)(-1); \
00047         (elt)->link.next = (type *)(-1); \
00048     } while (0)
00049 #define ISC_LINK_INIT(elt, link) \
00050     ISC_LINK_INIT_TYPE(elt, link, void)
00051 #define ISC_LINK_LINKED(elt, link) ((void *)((elt)->link.prev) != (void *)(-1))
00052 
00053 #define ISC_LIST_HEAD(list) ((list).head)
00054 #define ISC_LIST_TAIL(list) ((list).tail)
00055 #define ISC_LIST_EMPTY(list) ISC_TF((list).head == NULL)
00056 
00057 #define __ISC_LIST_PREPENDUNSAFE(list, elt, link) \
00058     do { \
00059         if ((list).head != NULL) \
00060             (list).head->link.prev = (elt); \
00061         else \
00062             (list).tail = (elt); \
00063         (elt)->link.prev = NULL; \
00064         (elt)->link.next = (list).head; \
00065         (list).head = (elt); \
00066     } while (0)
00067 
00068 #define ISC_LIST_PREPEND(list, elt, link) \
00069     do { \
00070         ISC_LINK_INSIST(!ISC_LINK_LINKED(elt, link)); \
00071         __ISC_LIST_PREPENDUNSAFE(list, elt, link); \
00072     } while (0)
00073 
00074 #define ISC_LIST_INITANDPREPEND(list, elt, link) \
00075         __ISC_LIST_PREPENDUNSAFE(list, elt, link)
00076 
00077 #define __ISC_LIST_APPENDUNSAFE(list, elt, link) \
00078     do { \
00079         if ((list).tail != NULL) \
00080             (list).tail->link.next = (elt); \
00081         else \
00082             (list).head = (elt); \
00083         (elt)->link.prev = (list).tail; \
00084         (elt)->link.next = NULL; \
00085         (list).tail = (elt); \
00086     } while (0)
00087 
00088 #define ISC_LIST_APPEND(list, elt, link) \
00089     do { \
00090         ISC_LINK_INSIST(!ISC_LINK_LINKED(elt, link)); \
00091         __ISC_LIST_APPENDUNSAFE(list, elt, link); \
00092     } while (0)
00093 
00094 #define ISC_LIST_INITANDAPPEND(list, elt, link) \
00095         __ISC_LIST_APPENDUNSAFE(list, elt, link)
00096 
00097 #define __ISC_LIST_UNLINKUNSAFE_TYPE(list, elt, link, type) \
00098     do { \
00099         if ((elt)->link.next != NULL) \
00100             (elt)->link.next->link.prev = (elt)->link.prev; \
00101         else \
00102             (list).tail = (elt)->link.prev; \
00103         if ((elt)->link.prev != NULL) \
00104             (elt)->link.prev->link.next = (elt)->link.next; \
00105         else \
00106             (list).head = (elt)->link.next; \
00107         (elt)->link.prev = (type *)(-1); \
00108         (elt)->link.next = (type *)(-1); \
00109     } while (0)
00110 
00111 #define __ISC_LIST_UNLINKUNSAFE(list, elt, link) \
00112     __ISC_LIST_UNLINKUNSAFE_TYPE(list, elt, link, void)
00113 
00114 #define ISC_LIST_UNLINK_TYPE(list, elt, link, type) \
00115     do { \
00116         ISC_LINK_INSIST(ISC_LINK_LINKED(elt, link)); \
00117         __ISC_LIST_UNLINKUNSAFE_TYPE(list, elt, link, type); \
00118     } while (0)
00119 #define ISC_LIST_UNLINK(list, elt, link) \
00120     ISC_LIST_UNLINK_TYPE(list, elt, link, void)
00121 
00122 #define ISC_LIST_PREV(elt, link) ((elt)->link.prev)
00123 #define ISC_LIST_NEXT(elt, link) ((elt)->link.next)
00124 
00125 #define __ISC_LIST_INSERTBEFOREUNSAFE(list, before, elt, link) \
00126     do { \
00127         if ((before)->link.prev == NULL) \
00128             ISC_LIST_PREPEND(list, elt, link); \
00129         else { \
00130             (elt)->link.prev = (before)->link.prev; \
00131             (before)->link.prev = (elt); \
00132             (elt)->link.prev->link.next = (elt); \
00133             (elt)->link.next = (before); \
00134         } \
00135     } while (0)
00136 
00137 #define ISC_LIST_INSERTBEFORE(list, before, elt, link) \
00138     do { \
00139         ISC_LINK_INSIST(ISC_LINK_LINKED(before, link)); \
00140         ISC_LINK_INSIST(!ISC_LINK_LINKED(elt, link)); \
00141         __ISC_LIST_INSERTBEFOREUNSAFE(list, before, elt, link); \
00142     } while (0)
00143 
00144 #define __ISC_LIST_INSERTAFTERUNSAFE(list, after, elt, link) \
00145     do { \
00146         if ((after)->link.next == NULL) \
00147             ISC_LIST_APPEND(list, elt, link); \
00148         else { \
00149             (elt)->link.next = (after)->link.next; \
00150             (after)->link.next = (elt); \
00151             (elt)->link.next->link.prev = (elt); \
00152             (elt)->link.prev = (after); \
00153         } \
00154     } while (0)
00155 
00156 #define ISC_LIST_INSERTAFTER(list, after, elt, link) \
00157     do { \
00158         ISC_LINK_INSIST(ISC_LINK_LINKED(after, link)); \
00159         ISC_LINK_INSIST(!ISC_LINK_LINKED(elt, link)); \
00160         __ISC_LIST_INSERTAFTERUNSAFE(list, after, elt, link); \
00161     } while (0)
00162 
00163 #define ISC_LIST_APPENDLIST(list1, list2, link) \
00164     do { \
00165         if (ISC_LIST_EMPTY(list1)) \
00166             (list1) = (list2); \
00167         else if (!ISC_LIST_EMPTY(list2)) { \
00168             (list1).tail->link.next = (list2).head; \
00169             (list2).head->link.prev = (list1).tail; \
00170             (list1).tail = (list2).tail; \
00171         } \
00172         (list2).head = NULL; \
00173         (list2).tail = NULL; \
00174     } while (0)
00175 
00176 #define ISC_LIST_ENQUEUE(list, elt, link) ISC_LIST_APPEND(list, elt, link)
00177 #define __ISC_LIST_ENQUEUEUNSAFE(list, elt, link) \
00178     __ISC_LIST_APPENDUNSAFE(list, elt, link)
00179 #define ISC_LIST_DEQUEUE(list, elt, link) \
00180      ISC_LIST_UNLINK_TYPE(list, elt, link, void)
00181 #define ISC_LIST_DEQUEUE_TYPE(list, elt, link, type) \
00182      ISC_LIST_UNLINK_TYPE(list, elt, link, type)
00183 #define __ISC_LIST_DEQUEUEUNSAFE(list, elt, link) \
00184     __ISC_LIST_UNLINKUNSAFE_TYPE(list, elt, link, void)
00185 #define __ISC_LIST_DEQUEUEUNSAFE_TYPE(list, elt, link, type) \
00186     __ISC_LIST_UNLINKUNSAFE_TYPE(list, elt, link, type)
00187 
00188 #define ISC_LIST_INITIAL_TYPE(type) { (type *)NULL, (type *)NULL }
00189 #define ISC_LIST_INITIAL ISC_LIST_INITIAL_TYPE(void)
00190 
00191 #define ISC_LINK_INITIAL(type) { (type *)-1, (type *)-1 }
00192 
00194 #endif /* ISC_LIST_H */