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