[f761f1eb] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
[55b77d9] | 3 | * Copyright (c) 2011 Jiri Svoboda
|
---|
[f761f1eb] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[06e1e95] | 30 | /** @addtogroup genericadt
|
---|
[b45c443] | 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /** @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
[06e1e95] | 36 | #ifndef KERN_LIST_H_
|
---|
| 37 | #define KERN_LIST_H_
|
---|
[f761f1eb] | 38 |
|
---|
[7ac426e] | 39 | #include <typedefs.h>
|
---|
[7a0359b] | 40 | #include <trace.h>
|
---|
[f761f1eb] | 41 |
|
---|
[55b77d9] | 42 | /** Doubly linked list link. */
|
---|
[b3f8fb7] | 43 | typedef struct link {
|
---|
[0a02653] | 44 | struct link *prev; /**< Pointer to the previous item in the list. */
|
---|
| 45 | struct link *next; /**< Pointer to the next item in the list. */
|
---|
[b3f8fb7] | 46 | } link_t;
|
---|
[f761f1eb] | 47 |
|
---|
[55b77d9] | 48 | /** Doubly linked list. */
|
---|
| 49 | typedef struct list {
|
---|
| 50 | link_t head; /**< List head. Does not have any data. */
|
---|
| 51 | } list_t;
|
---|
| 52 |
|
---|
[7dd2561] | 53 | /** Declare and initialize statically allocated list.
|
---|
| 54 | *
|
---|
| 55 | * @param name Name of the new statically allocated list.
|
---|
[0a02653] | 56 | *
|
---|
[7dd2561] | 57 | */
|
---|
[80bcaed] | 58 | #define LIST_INITIALIZE(name) \
|
---|
[55b77d9] | 59 | list_t name = { \
|
---|
| 60 | .head = { \
|
---|
| 61 | .prev = &(name).head, \
|
---|
| 62 | .next = &(name).head \
|
---|
| 63 | } \
|
---|
[0a02653] | 64 | }
|
---|
| 65 |
|
---|
| 66 | #define list_get_instance(link, type, member) \
|
---|
| 67 | ((type *) (((void *)(link)) - ((void *) &(((type *) NULL)->member))))
|
---|
| 68 |
|
---|
| 69 | #define list_foreach(list, iterator) \
|
---|
[55b77d9] | 70 | for (link_t *iterator = (list).head.next; \
|
---|
| 71 | iterator != &(list).head; iterator = iterator->next)
|
---|
[7dd2561] | 72 |
|
---|
[b72efe8] | 73 | #define assert_link_not_used(link) \
|
---|
[74464e8] | 74 | ASSERT((link)->prev == NULL && (link)->next == NULL)
|
---|
[b72efe8] | 75 |
|
---|
[40a468a] | 76 | /** Initialize doubly-linked circular list link
|
---|
| 77 | *
|
---|
| 78 | * Initialize doubly-linked list link.
|
---|
| 79 | *
|
---|
| 80 | * @param link Pointer to link_t structure to be initialized.
|
---|
[0a02653] | 81 | *
|
---|
[40a468a] | 82 | */
|
---|
[7a0359b] | 83 | NO_TRACE static inline void link_initialize(link_t *link)
|
---|
[c0a91d1] | 84 | {
|
---|
| 85 | link->prev = NULL;
|
---|
| 86 | link->next = NULL;
|
---|
[f761f1eb] | 87 | }
|
---|
| 88 |
|
---|
[40a468a] | 89 | /** Initialize doubly-linked circular list
|
---|
| 90 | *
|
---|
| 91 | * Initialize doubly-linked circular list.
|
---|
| 92 | *
|
---|
[55b77d9] | 93 | * @param list Pointer to list_t structure.
|
---|
[0a02653] | 94 | *
|
---|
[40a468a] | 95 | */
|
---|
[55b77d9] | 96 | NO_TRACE static inline void list_initialize(list_t *list)
|
---|
[c0a91d1] | 97 | {
|
---|
[55b77d9] | 98 | list->head.prev = &list->head;
|
---|
| 99 | list->head.next = &list->head;
|
---|
[f761f1eb] | 100 | }
|
---|
| 101 |
|
---|
[55b77d9] | 102 | /** Insert item before another item in doubly-linked circular list.
|
---|
[0a02653] | 103 | *
|
---|
[40a468a] | 104 | */
|
---|
[55b77d9] | 105 | static inline void list_insert_before(link_t *lnew, link_t *lold)
|
---|
[c0a91d1] | 106 | {
|
---|
[55b77d9] | 107 | lnew->next = lold;
|
---|
| 108 | lnew->prev = lold->prev;
|
---|
| 109 | lold->prev->next = lnew;
|
---|
| 110 | lold->prev = lnew;
|
---|
[f761f1eb] | 111 | }
|
---|
| 112 |
|
---|
[55b77d9] | 113 | /** Insert item after another item in doubly-linked circular list.
|
---|
[0a02653] | 114 | *
|
---|
[40a468a] | 115 | */
|
---|
[55b77d9] | 116 | static inline void list_insert_after(link_t *lnew, link_t *lold)
|
---|
[c0a91d1] | 117 | {
|
---|
[55b77d9] | 118 | lnew->prev = lold;
|
---|
| 119 | lnew->next = lold->next;
|
---|
| 120 | lold->next->prev = lnew;
|
---|
| 121 | lold->next = lnew;
|
---|
[0a02653] | 122 | }
|
---|
| 123 |
|
---|
[55b77d9] | 124 | /** Add item to the beginning of doubly-linked circular list
|
---|
| 125 | *
|
---|
| 126 | * Add item to the beginning of doubly-linked circular list.
|
---|
| 127 | *
|
---|
| 128 | * @param link Pointer to link_t structure to be added.
|
---|
| 129 | * @param list Pointer to list_t structure.
|
---|
[0a02653] | 130 | *
|
---|
| 131 | */
|
---|
[55b77d9] | 132 | NO_TRACE static inline void list_prepend(link_t *link, list_t *list)
|
---|
[0a02653] | 133 | {
|
---|
[55b77d9] | 134 | list_insert_after(link, &list->head);
|
---|
[0a02653] | 135 | }
|
---|
| 136 |
|
---|
[55b77d9] | 137 | /** Add item to the end of doubly-linked circular list
|
---|
| 138 | *
|
---|
| 139 | * Add item to the end of doubly-linked circular list.
|
---|
| 140 | *
|
---|
| 141 | * @param link Pointer to link_t structure to be added.
|
---|
| 142 | * @param list Pointer to list_t structure.
|
---|
[0a02653] | 143 | *
|
---|
| 144 | */
|
---|
[55b77d9] | 145 | NO_TRACE static inline void list_append(link_t *link, list_t *list)
|
---|
[0a02653] | 146 | {
|
---|
[55b77d9] | 147 | list_insert_before(link, &list->head);
|
---|
[f761f1eb] | 148 | }
|
---|
| 149 |
|
---|
[40a468a] | 150 | /** Remove item from doubly-linked circular list
|
---|
| 151 | *
|
---|
| 152 | * Remove item from doubly-linked circular list.
|
---|
| 153 | *
|
---|
[0a02653] | 154 | * @param link Pointer to link_t structure to be removed from the list
|
---|
| 155 | * it is contained in.
|
---|
| 156 | *
|
---|
[40a468a] | 157 | */
|
---|
[7a0359b] | 158 | NO_TRACE static inline void list_remove(link_t *link)
|
---|
[c0a91d1] | 159 | {
|
---|
| 160 | link->next->prev = link->prev;
|
---|
| 161 | link->prev->next = link->next;
|
---|
| 162 | link_initialize(link);
|
---|
[f761f1eb] | 163 | }
|
---|
| 164 |
|
---|
[40a468a] | 165 | /** Query emptiness of doubly-linked circular list
|
---|
| 166 | *
|
---|
| 167 | * Query emptiness of doubly-linked circular list.
|
---|
| 168 | *
|
---|
[55b77d9] | 169 | * @param list Pointer to lins_t structure.
|
---|
[0a02653] | 170 | *
|
---|
[40a468a] | 171 | */
|
---|
[55b77d9] | 172 | NO_TRACE static inline int list_empty(list_t *list)
|
---|
[40a468a] | 173 | {
|
---|
[55b77d9] | 174 | return (list->head.next == &list->head);
|
---|
[40a468a] | 175 | }
|
---|
| 176 |
|
---|
[55b77d9] | 177 | /** Get first item in list.
|
---|
[0a02653] | 178 | *
|
---|
[55b77d9] | 179 | * @param list Pointer to list_t structure.
|
---|
| 180 | *
|
---|
| 181 | * @return Head item of the list.
|
---|
| 182 | * @return NULL if the list is empty.
|
---|
| 183 | *
|
---|
| 184 | */
|
---|
| 185 | static inline link_t *list_first(list_t *list)
|
---|
| 186 | {
|
---|
| 187 | return ((list->head.next == &list->head) ? NULL : list->head.next);
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | /** Get last item in list.
|
---|
| 191 | *
|
---|
| 192 | * @param list Pointer to list_t structure.
|
---|
[0a02653] | 193 | *
|
---|
| 194 | * @return Head item of the list.
|
---|
| 195 | * @return NULL if the list is empty.
|
---|
| 196 | *
|
---|
| 197 | */
|
---|
[55b77d9] | 198 | static inline link_t *list_last(list_t *list)
|
---|
[0a02653] | 199 | {
|
---|
[55b77d9] | 200 | return ((list->head.prev == &list->head) ? NULL : list->head.prev);
|
---|
[0a02653] | 201 | }
|
---|
[40a468a] | 202 |
|
---|
| 203 | /** Split or concatenate headless doubly-linked circular list
|
---|
| 204 | *
|
---|
| 205 | * Split or concatenate headless doubly-linked circular list.
|
---|
| 206 | *
|
---|
| 207 | * Note that the algorithm works both directions:
|
---|
| 208 | * concatenates splitted lists and splits concatenated lists.
|
---|
| 209 | *
|
---|
[0a02653] | 210 | * @param part1 Pointer to link_t structure leading the first
|
---|
| 211 | * (half of the headless) list.
|
---|
| 212 | * @param part2 Pointer to link_t structure leading the second
|
---|
| 213 | * (half of the headless) list.
|
---|
| 214 | *
|
---|
[40a468a] | 215 | */
|
---|
[7a0359b] | 216 | NO_TRACE static inline void headless_list_split_or_concat(link_t *part1, link_t *part2)
|
---|
[40a468a] | 217 | {
|
---|
| 218 | part1->prev->next = part2;
|
---|
[0a02653] | 219 | part2->prev->next = part1;
|
---|
| 220 |
|
---|
| 221 | link_t *hlp = part1->prev;
|
---|
| 222 |
|
---|
[40a468a] | 223 | part1->prev = part2->prev;
|
---|
| 224 | part2->prev = hlp;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | /** Split headless doubly-linked circular list
|
---|
| 228 | *
|
---|
| 229 | * Split headless doubly-linked circular list.
|
---|
| 230 | *
|
---|
[0a02653] | 231 | * @param part1 Pointer to link_t structure leading
|
---|
| 232 | * the first half of the headless list.
|
---|
| 233 | * @param part2 Pointer to link_t structure leading
|
---|
| 234 | * the second half of the headless list.
|
---|
| 235 | *
|
---|
[40a468a] | 236 | */
|
---|
[7a0359b] | 237 | NO_TRACE static inline void headless_list_split(link_t *part1, link_t *part2)
|
---|
[40a468a] | 238 | {
|
---|
| 239 | headless_list_split_or_concat(part1, part2);
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | /** Concatenate two headless doubly-linked circular lists
|
---|
| 243 | *
|
---|
| 244 | * Concatenate two headless doubly-linked circular lists.
|
---|
| 245 | *
|
---|
[0a02653] | 246 | * @param part1 Pointer to link_t structure leading
|
---|
| 247 | * the first headless list.
|
---|
| 248 | * @param part2 Pointer to link_t structure leading
|
---|
| 249 | * the second headless list.
|
---|
| 250 | *
|
---|
[40a468a] | 251 | */
|
---|
[7a0359b] | 252 | NO_TRACE static inline void headless_list_concat(link_t *part1, link_t *part2)
|
---|
[40a468a] | 253 | {
|
---|
| 254 | headless_list_split_or_concat(part1, part2);
|
---|
| 255 | }
|
---|
[f761f1eb] | 256 |
|
---|
[55b77d9] | 257 | /** Get n-th item in a list.
|
---|
[0a02653] | 258 | *
|
---|
| 259 | * @param list Pointer to link_t structure representing the list.
|
---|
| 260 | * @param n Item number (indexed from zero).
|
---|
| 261 | *
|
---|
| 262 | * @return n-th item of the list.
|
---|
| 263 | * @return NULL if no n-th item found.
|
---|
| 264 | *
|
---|
| 265 | */
|
---|
[55b77d9] | 266 | static inline link_t *list_nth(list_t *list, unsigned int n)
|
---|
[0a02653] | 267 | {
|
---|
| 268 | unsigned int cnt = 0;
|
---|
| 269 |
|
---|
| 270 | list_foreach(*list, link) {
|
---|
| 271 | if (cnt == n)
|
---|
| 272 | return link;
|
---|
| 273 |
|
---|
| 274 | cnt++;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | return NULL;
|
---|
| 278 | }
|
---|
[f761f1eb] | 279 |
|
---|
[55b77d9] | 280 | extern int list_member(const link_t *, const list_t *);
|
---|
| 281 | extern void list_concat(list_t *, list_t *);
|
---|
| 282 | extern unsigned int list_count(const list_t *);
|
---|
[f761f1eb] | 283 |
|
---|
| 284 | #endif
|
---|
[b45c443] | 285 |
|
---|
[06e1e95] | 286 | /** @}
|
---|
[b45c443] | 287 | */
|
---|