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