[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 |
|
---|
[c14762e] | 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 |
|
---|
[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) \
|
---|
[55b77d9] | 65 | list_t name = { \
|
---|
| 66 | .head = { \
|
---|
| 67 | .prev = &(name).head, \
|
---|
| 68 | .next = &(name).head \
|
---|
| 69 | } \
|
---|
[0a02653] | 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) \
|
---|
[55b77d9] | 76 | for (link_t *iterator = (list).head.next; \
|
---|
| 77 | iterator != &(list).head; iterator = iterator->next)
|
---|
[7dd2561] | 78 |
|
---|
[ef1603b] | 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 |
|
---|
[b72efe8] | 111 | #define assert_link_not_used(link) \
|
---|
[14a60e3] | 112 | ASSERT(((link)->prev == NULL) && ((link)->next == NULL))
|
---|
[b72efe8] | 113 |
|
---|
[40a468a] | 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.
|
---|
[0a02653] | 119 | *
|
---|
[40a468a] | 120 | */
|
---|
[7a0359b] | 121 | NO_TRACE static inline void link_initialize(link_t *link)
|
---|
[c0a91d1] | 122 | {
|
---|
| 123 | link->prev = NULL;
|
---|
| 124 | link->next = NULL;
|
---|
[f761f1eb] | 125 | }
|
---|
| 126 |
|
---|
[ef1603b] | 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 |
|
---|
[40a468a] | 138 | /** Initialize doubly-linked circular list
|
---|
| 139 | *
|
---|
| 140 | * Initialize doubly-linked circular list.
|
---|
| 141 | *
|
---|
[55b77d9] | 142 | * @param list Pointer to list_t structure.
|
---|
[0a02653] | 143 | *
|
---|
[40a468a] | 144 | */
|
---|
[55b77d9] | 145 | NO_TRACE static inline void list_initialize(list_t *list)
|
---|
[c0a91d1] | 146 | {
|
---|
[55b77d9] | 147 | list->head.prev = &list->head;
|
---|
| 148 | list->head.next = &list->head;
|
---|
[f761f1eb] | 149 | }
|
---|
| 150 |
|
---|
[55b77d9] | 151 | /** Insert item before another item in doubly-linked circular list.
|
---|
[0a02653] | 152 | *
|
---|
[40a468a] | 153 | */
|
---|
[55b77d9] | 154 | static inline void list_insert_before(link_t *lnew, link_t *lold)
|
---|
[c0a91d1] | 155 | {
|
---|
[55b77d9] | 156 | lnew->next = lold;
|
---|
| 157 | lnew->prev = lold->prev;
|
---|
| 158 | lold->prev->next = lnew;
|
---|
| 159 | lold->prev = lnew;
|
---|
[f761f1eb] | 160 | }
|
---|
| 161 |
|
---|
[55b77d9] | 162 | /** Insert item after another item in doubly-linked circular list.
|
---|
[0a02653] | 163 | *
|
---|
[40a468a] | 164 | */
|
---|
[55b77d9] | 165 | static inline void list_insert_after(link_t *lnew, link_t *lold)
|
---|
[c0a91d1] | 166 | {
|
---|
[55b77d9] | 167 | lnew->prev = lold;
|
---|
| 168 | lnew->next = lold->next;
|
---|
| 169 | lold->next->prev = lnew;
|
---|
| 170 | lold->next = lnew;
|
---|
[0a02653] | 171 | }
|
---|
| 172 |
|
---|
[55b77d9] | 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.
|
---|
[0a02653] | 179 | *
|
---|
| 180 | */
|
---|
[55b77d9] | 181 | NO_TRACE static inline void list_prepend(link_t *link, list_t *list)
|
---|
[0a02653] | 182 | {
|
---|
[55b77d9] | 183 | list_insert_after(link, &list->head);
|
---|
[0a02653] | 184 | }
|
---|
| 185 |
|
---|
[55b77d9] | 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.
|
---|
[0a02653] | 192 | *
|
---|
| 193 | */
|
---|
[55b77d9] | 194 | NO_TRACE static inline void list_append(link_t *link, list_t *list)
|
---|
[0a02653] | 195 | {
|
---|
[55b77d9] | 196 | list_insert_before(link, &list->head);
|
---|
[f761f1eb] | 197 | }
|
---|
| 198 |
|
---|
[40a468a] | 199 | /** Remove item from doubly-linked circular list
|
---|
| 200 | *
|
---|
| 201 | * Remove item from doubly-linked circular list.
|
---|
| 202 | *
|
---|
[0a02653] | 203 | * @param link Pointer to link_t structure to be removed from the list
|
---|
| 204 | * it is contained in.
|
---|
| 205 | *
|
---|
[40a468a] | 206 | */
|
---|
[7a0359b] | 207 | NO_TRACE static inline void list_remove(link_t *link)
|
---|
[c0a91d1] | 208 | {
|
---|
[14a60e3] | 209 | if ((link->prev != NULL) && (link->next != NULL)) {
|
---|
| 210 | link->next->prev = link->prev;
|
---|
| 211 | link->prev->next = link->next;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[c0a91d1] | 214 | link_initialize(link);
|
---|
[f761f1eb] | 215 | }
|
---|
| 216 |
|
---|
[40a468a] | 217 | /** Query emptiness of doubly-linked circular list
|
---|
| 218 | *
|
---|
| 219 | * Query emptiness of doubly-linked circular list.
|
---|
| 220 | *
|
---|
[55b77d9] | 221 | * @param list Pointer to lins_t structure.
|
---|
[0a02653] | 222 | *
|
---|
[40a468a] | 223 | */
|
---|
[4748038] | 224 | NO_TRACE static inline int list_empty(const list_t *list)
|
---|
[40a468a] | 225 | {
|
---|
[55b77d9] | 226 | return (list->head.next == &list->head);
|
---|
[40a468a] | 227 | }
|
---|
| 228 |
|
---|
[55b77d9] | 229 | /** Get first item in list.
|
---|
[0a02653] | 230 | *
|
---|
[55b77d9] | 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 | */
|
---|
[4748038] | 237 | static inline link_t *list_first(const list_t *list)
|
---|
[55b77d9] | 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.
|
---|
[0a02653] | 245 | *
|
---|
| 246 | * @return Head item of the list.
|
---|
| 247 | * @return NULL if the list is empty.
|
---|
| 248 | *
|
---|
| 249 | */
|
---|
[55b77d9] | 250 | static inline link_t *list_last(list_t *list)
|
---|
[0a02653] | 251 | {
|
---|
[55b77d9] | 252 | return ((list->head.prev == &list->head) ? NULL : list->head.prev);
|
---|
[0a02653] | 253 | }
|
---|
[40a468a] | 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 | *
|
---|
[0a02653] | 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 | *
|
---|
[40a468a] | 267 | */
|
---|
[7a0359b] | 268 | NO_TRACE static inline void headless_list_split_or_concat(link_t *part1, link_t *part2)
|
---|
[40a468a] | 269 | {
|
---|
| 270 | part1->prev->next = part2;
|
---|
[0a02653] | 271 | part2->prev->next = part1;
|
---|
| 272 |
|
---|
| 273 | link_t *hlp = part1->prev;
|
---|
| 274 |
|
---|
[40a468a] | 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 | *
|
---|
[0a02653] | 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 | *
|
---|
[40a468a] | 288 | */
|
---|
[7a0359b] | 289 | NO_TRACE static inline void headless_list_split(link_t *part1, link_t *part2)
|
---|
[40a468a] | 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 | *
|
---|
[0a02653] | 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 | *
|
---|
[40a468a] | 303 | */
|
---|
[7a0359b] | 304 | NO_TRACE static inline void headless_list_concat(link_t *part1, link_t *part2)
|
---|
[40a468a] | 305 | {
|
---|
| 306 | headless_list_split_or_concat(part1, part2);
|
---|
| 307 | }
|
---|
[f761f1eb] | 308 |
|
---|
[c14762e] | 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 | *
|
---|
[ff90f5f] | 318 | */
|
---|
[c14762e] | 319 | NO_TRACE static inline void list_concat(list_t *list1, list_t *list2)
|
---|
[ff90f5f] | 320 | {
|
---|
[c14762e] | 321 | list_splice(list2, list1->head.prev);
|
---|
[ff90f5f] | 322 | }
|
---|
| 323 |
|
---|
[55b77d9] | 324 | /** Get n-th item in a list.
|
---|
[0a02653] | 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 | */
|
---|
[55b77d9] | 333 | static inline link_t *list_nth(list_t *list, unsigned int n)
|
---|
[0a02653] | 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 | }
|
---|
[f761f1eb] | 346 |
|
---|
| 347 | #endif
|
---|
[b45c443] | 348 |
|
---|
[06e1e95] | 349 | /** @}
|
---|
[b45c443] | 350 | */
|
---|