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