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