| 1 | /*
|
|---|
| 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
|---|
| 3 | * Copyright (c) 2011 Jiri Svoboda
|
|---|
| 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 |
|
|---|
| 30 | /** @addtogroup genericadt
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /** @file
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #ifndef KERN_LIST_H_
|
|---|
| 37 | #define KERN_LIST_H_
|
|---|
| 38 |
|
|---|
| 39 | #include <typedefs.h>
|
|---|
| 40 | #include <trace.h>
|
|---|
| 41 |
|
|---|
| 42 | /** Doubly linked list link. */
|
|---|
| 43 | typedef struct link {
|
|---|
| 44 | struct link *prev; /**< Pointer to the previous item in the list. */
|
|---|
| 45 | struct link *next; /**< Pointer to the next item in the list. */
|
|---|
| 46 | } link_t;
|
|---|
| 47 |
|
|---|
| 48 | /** Doubly linked list. */
|
|---|
| 49 | typedef struct list {
|
|---|
| 50 | link_t head; /**< List head. Does not have any data. */
|
|---|
| 51 | } list_t;
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 | extern int list_member(const link_t *, const list_t *);
|
|---|
| 55 | extern void list_splice(list_t *, link_t *);
|
|---|
| 56 | extern unsigned int list_count(const list_t *);
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 | /** Declare and initialize statically allocated list.
|
|---|
| 60 | *
|
|---|
| 61 | * @param name Name of the new statically allocated list.
|
|---|
| 62 | *
|
|---|
| 63 | */
|
|---|
| 64 | #define LIST_INITIALIZE(name) \
|
|---|
| 65 | list_t name = { \
|
|---|
| 66 | .head = { \
|
|---|
| 67 | .prev = &(name).head, \
|
|---|
| 68 | .next = &(name).head \
|
|---|
| 69 | } \
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | #define list_get_instance(link, type, member) \
|
|---|
| 73 | ((type *) (((void *)(link)) - ((void *) &(((type *) NULL)->member))))
|
|---|
| 74 |
|
|---|
| 75 | #define list_foreach(list, iterator) \
|
|---|
| 76 | for (link_t *iterator = (list).head.next; \
|
|---|
| 77 | iterator != &(list).head; iterator = iterator->next)
|
|---|
| 78 |
|
|---|
| 79 | /** Unlike list_foreach(), allows removing items while traversing a list.
|
|---|
| 80 | *
|
|---|
| 81 | * @code
|
|---|
| 82 | * list_t mylist;
|
|---|
| 83 | * typedef struct item {
|
|---|
| 84 | * int value;
|
|---|
| 85 | * link_t item_link;
|
|---|
| 86 | * } item_t;
|
|---|
| 87 | *
|
|---|
| 88 | * //..
|
|---|
| 89 | *
|
|---|
| 90 | * // Print each list element's value and remove the element from the list.
|
|---|
| 91 | * list_foreach_safe(mylist, cur_link, next_link) {
|
|---|
| 92 | * item_t *cur_item = list_get_instance(cur_link, item_t, item_link);
|
|---|
| 93 | * printf("%d\n", cur_item->value);
|
|---|
| 94 | * list_remove(cur_link);
|
|---|
| 95 | * }
|
|---|
| 96 | * @endcode
|
|---|
| 97 | *
|
|---|
| 98 | * @param list List to traverse.
|
|---|
| 99 | * @param iterator Iterator to the current element of the list.
|
|---|
| 100 | * The item this iterator points may be safely removed
|
|---|
| 101 | * from the list.
|
|---|
| 102 | * @param next_iter Iterator to the next element of the list.
|
|---|
| 103 | */
|
|---|
| 104 | #define list_foreach_safe(list, iterator, next_iter) \
|
|---|
| 105 | for (link_t *iterator = (list).head.next, \
|
|---|
| 106 | *next_iter = iterator->next; \
|
|---|
| 107 | iterator != &(list).head; \
|
|---|
| 108 | iterator = next_iter, next_iter = iterator->next)
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 | #define assert_link_not_used(link) \
|
|---|
| 112 | ASSERT(((link)->prev == NULL) && ((link)->next == NULL))
|
|---|
| 113 |
|
|---|
| 114 | /** Initialize doubly-linked circular list link
|
|---|
| 115 | *
|
|---|
| 116 | * Initialize doubly-linked list link.
|
|---|
| 117 | *
|
|---|
| 118 | * @param link Pointer to link_t structure to be initialized.
|
|---|
| 119 | *
|
|---|
| 120 | */
|
|---|
| 121 | NO_TRACE static inline void link_initialize(link_t *link)
|
|---|
| 122 | {
|
|---|
| 123 | link->prev = NULL;
|
|---|
| 124 | link->next = NULL;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | /** Returns true if the initialized link is already in use by any list.
|
|---|
| 128 | *
|
|---|
| 129 | * @param link Link to examine whether if belongs to a list or not.
|
|---|
| 130 | * @return 1 if the link is part of a list.
|
|---|
| 131 | * @return 0 otherwise.
|
|---|
| 132 | */
|
|---|
| 133 | NO_TRACE static inline int link_used(const link_t *link)
|
|---|
| 134 | {
|
|---|
| 135 | return link->prev != NULL || link->next != NULL;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | /** Initialize doubly-linked circular list
|
|---|
| 139 | *
|
|---|
| 140 | * Initialize doubly-linked circular list.
|
|---|
| 141 | *
|
|---|
| 142 | * @param list Pointer to list_t structure.
|
|---|
| 143 | *
|
|---|
| 144 | */
|
|---|
| 145 | NO_TRACE static inline void list_initialize(list_t *list)
|
|---|
| 146 | {
|
|---|
| 147 | list->head.prev = &list->head;
|
|---|
| 148 | list->head.next = &list->head;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | /** Insert item before another item in doubly-linked circular list.
|
|---|
| 152 | *
|
|---|
| 153 | */
|
|---|
| 154 | static inline void list_insert_before(link_t *lnew, link_t *lold)
|
|---|
| 155 | {
|
|---|
| 156 | lnew->next = lold;
|
|---|
| 157 | lnew->prev = lold->prev;
|
|---|
| 158 | lold->prev->next = lnew;
|
|---|
| 159 | lold->prev = lnew;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | /** Insert item after another item in doubly-linked circular list.
|
|---|
| 163 | *
|
|---|
| 164 | */
|
|---|
| 165 | static inline void list_insert_after(link_t *lnew, link_t *lold)
|
|---|
| 166 | {
|
|---|
| 167 | lnew->prev = lold;
|
|---|
| 168 | lnew->next = lold->next;
|
|---|
| 169 | lold->next->prev = lnew;
|
|---|
| 170 | lold->next = lnew;
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | /** Add item to the beginning of doubly-linked circular list
|
|---|
| 174 | *
|
|---|
| 175 | * Add item to the beginning of doubly-linked circular list.
|
|---|
| 176 | *
|
|---|
| 177 | * @param link Pointer to link_t structure to be added.
|
|---|
| 178 | * @param list Pointer to list_t structure.
|
|---|
| 179 | *
|
|---|
| 180 | */
|
|---|
| 181 | NO_TRACE static inline void list_prepend(link_t *link, list_t *list)
|
|---|
| 182 | {
|
|---|
| 183 | list_insert_after(link, &list->head);
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | /** Add item to the end of doubly-linked circular list
|
|---|
| 187 | *
|
|---|
| 188 | * Add item to the end of doubly-linked circular list.
|
|---|
| 189 | *
|
|---|
| 190 | * @param link Pointer to link_t structure to be added.
|
|---|
| 191 | * @param list Pointer to list_t structure.
|
|---|
| 192 | *
|
|---|
| 193 | */
|
|---|
| 194 | NO_TRACE static inline void list_append(link_t *link, list_t *list)
|
|---|
| 195 | {
|
|---|
| 196 | list_insert_before(link, &list->head);
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | /** Remove item from doubly-linked circular list
|
|---|
| 200 | *
|
|---|
| 201 | * Remove item from doubly-linked circular list.
|
|---|
| 202 | *
|
|---|
| 203 | * @param link Pointer to link_t structure to be removed from the list
|
|---|
| 204 | * it is contained in.
|
|---|
| 205 | *
|
|---|
| 206 | */
|
|---|
| 207 | NO_TRACE static inline void list_remove(link_t *link)
|
|---|
| 208 | {
|
|---|
| 209 | if ((link->prev != NULL) && (link->next != NULL)) {
|
|---|
| 210 | link->next->prev = link->prev;
|
|---|
| 211 | link->prev->next = link->next;
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | link_initialize(link);
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | /** Query emptiness of doubly-linked circular list
|
|---|
| 218 | *
|
|---|
| 219 | * Query emptiness of doubly-linked circular list.
|
|---|
| 220 | *
|
|---|
| 221 | * @param list Pointer to lins_t structure.
|
|---|
| 222 | *
|
|---|
| 223 | */
|
|---|
| 224 | NO_TRACE static inline int list_empty(const list_t *list)
|
|---|
| 225 | {
|
|---|
| 226 | return (list->head.next == &list->head);
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | /** Get first item in list.
|
|---|
| 230 | *
|
|---|
| 231 | * @param list Pointer to list_t structure.
|
|---|
| 232 | *
|
|---|
| 233 | * @return Head item of the list.
|
|---|
| 234 | * @return NULL if the list is empty.
|
|---|
| 235 | *
|
|---|
| 236 | */
|
|---|
| 237 | static inline link_t *list_first(const list_t *list)
|
|---|
| 238 | {
|
|---|
| 239 | return ((list->head.next == &list->head) ? NULL : list->head.next);
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | /** Get last item in list.
|
|---|
| 243 | *
|
|---|
| 244 | * @param list Pointer to list_t structure.
|
|---|
| 245 | *
|
|---|
| 246 | * @return Head item of the list.
|
|---|
| 247 | * @return NULL if the list is empty.
|
|---|
| 248 | *
|
|---|
| 249 | */
|
|---|
| 250 | static inline link_t *list_last(list_t *list)
|
|---|
| 251 | {
|
|---|
| 252 | return ((list->head.prev == &list->head) ? NULL : list->head.prev);
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | /** Split or concatenate headless doubly-linked circular list
|
|---|
| 256 | *
|
|---|
| 257 | * Split or concatenate headless doubly-linked circular list.
|
|---|
| 258 | *
|
|---|
| 259 | * Note that the algorithm works both directions:
|
|---|
| 260 | * concatenates splitted lists and splits concatenated lists.
|
|---|
| 261 | *
|
|---|
| 262 | * @param part1 Pointer to link_t structure leading the first
|
|---|
| 263 | * (half of the headless) list.
|
|---|
| 264 | * @param part2 Pointer to link_t structure leading the second
|
|---|
| 265 | * (half of the headless) list.
|
|---|
| 266 | *
|
|---|
| 267 | */
|
|---|
| 268 | NO_TRACE static inline void headless_list_split_or_concat(link_t *part1, link_t *part2)
|
|---|
| 269 | {
|
|---|
| 270 | part1->prev->next = part2;
|
|---|
| 271 | part2->prev->next = part1;
|
|---|
| 272 |
|
|---|
| 273 | link_t *hlp = part1->prev;
|
|---|
| 274 |
|
|---|
| 275 | part1->prev = part2->prev;
|
|---|
| 276 | part2->prev = hlp;
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | /** Split headless doubly-linked circular list
|
|---|
| 280 | *
|
|---|
| 281 | * Split headless doubly-linked circular list.
|
|---|
| 282 | *
|
|---|
| 283 | * @param part1 Pointer to link_t structure leading
|
|---|
| 284 | * the first half of the headless list.
|
|---|
| 285 | * @param part2 Pointer to link_t structure leading
|
|---|
| 286 | * the second half of the headless list.
|
|---|
| 287 | *
|
|---|
| 288 | */
|
|---|
| 289 | NO_TRACE static inline void headless_list_split(link_t *part1, link_t *part2)
|
|---|
| 290 | {
|
|---|
| 291 | headless_list_split_or_concat(part1, part2);
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | /** Concatenate two headless doubly-linked circular lists
|
|---|
| 295 | *
|
|---|
| 296 | * Concatenate two headless doubly-linked circular lists.
|
|---|
| 297 | *
|
|---|
| 298 | * @param part1 Pointer to link_t structure leading
|
|---|
| 299 | * the first headless list.
|
|---|
| 300 | * @param part2 Pointer to link_t structure leading
|
|---|
| 301 | * the second headless list.
|
|---|
| 302 | *
|
|---|
| 303 | */
|
|---|
| 304 | NO_TRACE static inline void headless_list_concat(link_t *part1, link_t *part2)
|
|---|
| 305 | {
|
|---|
| 306 | headless_list_split_or_concat(part1, part2);
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | /** Concatenate two lists
|
|---|
| 310 | *
|
|---|
| 311 | * Concatenate lists @a list1 and @a list2, producing a single
|
|---|
| 312 | * list @a list1 containing items from both (in @a list1, @a list2
|
|---|
| 313 | * order) and empty list @a list2.
|
|---|
| 314 | *
|
|---|
| 315 | * @param list1 First list and concatenated output
|
|---|
| 316 | * @param list2 Second list and empty output.
|
|---|
| 317 | *
|
|---|
| 318 | */
|
|---|
| 319 | NO_TRACE static inline void list_concat(list_t *list1, list_t *list2)
|
|---|
| 320 | {
|
|---|
| 321 | list_splice(list2, list1->head.prev);
|
|---|
| 322 | }
|
|---|
| 323 |
|
|---|
| 324 | /** Get n-th item in a list.
|
|---|
| 325 | *
|
|---|
| 326 | * @param list Pointer to link_t structure representing the list.
|
|---|
| 327 | * @param n Item number (indexed from zero).
|
|---|
| 328 | *
|
|---|
| 329 | * @return n-th item of the list.
|
|---|
| 330 | * @return NULL if no n-th item found.
|
|---|
| 331 | *
|
|---|
| 332 | */
|
|---|
| 333 | static inline link_t *list_nth(list_t *list, unsigned int n)
|
|---|
| 334 | {
|
|---|
| 335 | unsigned int cnt = 0;
|
|---|
| 336 |
|
|---|
| 337 | list_foreach(*list, link) {
|
|---|
| 338 | if (cnt == n)
|
|---|
| 339 | return link;
|
|---|
| 340 |
|
|---|
| 341 | cnt++;
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| 344 | return NULL;
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | #endif
|
|---|
| 348 |
|
|---|
| 349 | /** @}
|
|---|
| 350 | */
|
|---|