Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed Socket lwip-eth lwip-sys lwip
Fork of 6_songs-from-the-cloud by
ns_list.h
00001 /* 00002 * Copyright (c) 2014-2015 ARM Limited. All rights reserved. 00003 * SPDX-License-Identifier: Apache-2.0 00004 * Licensed under the Apache License, Version 2.0 (the License); you may 00005 * not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an AS IS BASIS, WITHOUT 00012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef NS_LIST_H_ 00018 #define NS_LIST_H_ 00019 00020 #include "ns_types.h" 00021 00022 #ifdef __cplusplus 00023 extern "C" { 00024 #endif 00025 00026 /** \file 00027 * \brief Linked list support library 00028 * 00029 * The ns_list.h file provides a doubly-linked list/queue, providing O(1) 00030 * performance for all insertion/removal operations, and access to either 00031 * end of the list. 00032 * 00033 * Memory footprint is two pointers for the list head, and two pointers in each 00034 * list entry. It is similar in concept to BSD's TAILQ. 00035 * 00036 * Although the API is symmetrical and O(1) in both directions, due to internal 00037 * pointer design, it is *slightly* more efficient to insert at the end when 00038 * used as a queue, and to iterate forwards rather than backwards. 00039 * 00040 * Example of an entry type that can be stored to this list. 00041 * ~~~ 00042 * typedef struct example_entry 00043 * { 00044 * uint8_t *data; 00045 * uint32_t data_count; 00046 * ns_list_link_t link; 00047 * } 00048 * example_entry_t; 00049 * 00050 * static NS_LIST_HEAD(example_entry_t, link) my_list; 00051 * ns_list_init(&my_list); 00052 * ~~~ 00053 * OR 00054 * ~~~ 00055 * NS_LIST_HEAD(example_entry_t, link) my_list = NS_LIST_INIT(my_list); 00056 * ~~~ 00057 * OR 00058 * ~~~ 00059 * static NS_LIST_DEFINE(my_list, example_entry_t, link); 00060 * ~~~ 00061 * OR 00062 * ~~~ 00063 * typedef NS_LIST_HEAD(example_entry_t, link) example_list_t; 00064 * example_list_t NS_LIST_NAME_INIT(my_list); 00065 * ~~~ 00066 * NOTE: the link field SHALL NOT be accessed by the user. 00067 * 00068 * An entry can exist on multiple lists by having multiple link fields. 00069 * 00070 * All the list operations are implemented as macros, most of which are backed 00071 * by optionally-inline functions. The macros do not evaluate any arguments more 00072 * than once, unless documented. 00073 * 00074 * In macro documentation, `list_t` refers to a list type defined using 00075 * NS_LIST_HEAD(), and `entry_t` to the entry type that was passed to it. 00076 */ 00077 00078 /** \brief Underlying generic linked list head. 00079 * 00080 * Users should not use this type directly, but use the NS_LIST_HEAD() macro. 00081 */ 00082 typedef struct ns_list { 00083 void *first_entry; ///< Pointer to first entry, or NULL if list is empty 00084 void **last_nextptr; ///< Pointer to last entry's `next` pointer, or 00085 ///< to head's `first_entry` pointer if list is empty 00086 } ns_list_t; 00087 00088 /** \brief Declare a list head type 00089 * 00090 * This union stores the real list head, and also encodes as compile-time type 00091 * information the offset of the link pointer, and the type of the entry. 00092 * 00093 * Note that type information is compiler-dependent; this means 00094 * ns_list_get_first() could return either `void *`, or a pointer to the actual 00095 * entry type. So `ns_list_get_first()->data` is not a portable construct - 00096 * always assign returned entry pointers to a properly typed pointer variable. 00097 * This assignment will be then type-checked where the compiler supports it, and 00098 * will dereference correctly on compilers that don't support this extension. 00099 * ~~~ 00100 * NS_LIST_HEAD(example_entry_t, link) my_list; 00101 * 00102 * example_entry_t *entry = ns_list_get_first(&my_list); 00103 * do_something(entry->data); 00104 * ~~~ 00105 * Each use of this macro generates a new anonymous union, so these two lists 00106 * have different types: 00107 * ~~~ 00108 * NS_LIST_HEAD(example_entry_t, link) my_list1; 00109 * NS_LIST_HEAD(example_entry_t, link) my_list2; 00110 * ~~~ 00111 * If you need to use a list type in multiple places, eg as a function 00112 * parameter, use typedef: 00113 * ~~~ 00114 * typedef NS_LIST_HEAD(example_entry_t, link) example_list_t; 00115 * 00116 * void example_function(example_list_t *); 00117 * ~~~ 00118 */ 00119 #define NS_LIST_HEAD(entry_type, field) \ 00120 NS_LIST_HEAD_BY_OFFSET_(entry_type, offsetof(entry_type, field)) 00121 00122 /** \brief Declare a list head type for an incomplete entry type. 00123 * 00124 * This declares a list head, similarly to NS_LIST_HEAD(), but unlike that 00125 * this can be used in contexts where the entry type may be incomplete. 00126 * 00127 * To use this, the link pointer must be the first member in the 00128 * actual complete structure. This is NOT checked - the definition of the 00129 * element should probably test NS_STATIC_ASSERT(offsetof(type, link) == 0) 00130 * if outside users are known to be using NS_LIST_HEAD_INCOMPLETE(). 00131 * ~~~ 00132 * struct opaque; 00133 * NS_LIST_HEAD_INCOMPLETE(struct opaque) opaque_list; 00134 * ~~~ 00135 */ 00136 #define NS_LIST_HEAD_INCOMPLETE(entry_type) \ 00137 NS_LIST_HEAD_BY_OFFSET_(entry_type, 0) 00138 00139 /// \privatesection 00140 /** \brief Internal macro defining a list head, given the offset to the link pointer 00141 * The +1 allows for link_offset being 0 - we can't declare a 0-size array 00142 */ 00143 #define NS_LIST_HEAD_BY_OFFSET_(entry_type, link_offset) \ 00144 union \ 00145 { \ 00146 ns_list_t slist; \ 00147 NS_STATIC_ASSERT(link_offset <= UINT_FAST8_MAX, "link offset too large") \ 00148 char (*offset)[link_offset + 1]; \ 00149 entry_type *type; \ 00150 } 00151 00152 /** \brief Get offset of link field in entry. 00153 * \return `(ns_list_offset_t)` The offset of the link field for entries on the specified list 00154 */ 00155 #define NS_LIST_OFFSET_(list) ((ns_list_offset_t) (sizeof *(list)->offset - 1)) 00156 00157 /** \brief Get the entry pointer type. 00158 * \def NS_LIST_PTR_TYPE_ 00159 * 00160 * \return An unqualified pointer type to an entry on the specified list. 00161 * 00162 * Only available if the compiler provides a "typeof" operator. 00163 */ 00164 #if defined __cplusplus && __cplusplus >= 201103L 00165 #define NS_LIST_PTR_TYPE_(list) decltype((list)->type) 00166 #elif defined __GNUC__ 00167 #define NS_LIST_PTR_TYPE_(list) __typeof__((list)->type) 00168 #endif 00169 00170 /** \brief Check for compatible pointer types 00171 * 00172 * This test will produce a diagnostic about a pointer mismatch on 00173 * the == inside the sizeof operator. For example ARM/Norcroft C gives the error: 00174 * 00175 * operand types are incompatible ("entry_t *" and "other_t *") 00176 */ 00177 #ifdef CPPCHECK 00178 #define NS_PTR_MATCH_(a, b, str) ((void) 0) 00179 #else 00180 #define NS_PTR_MATCH_(a, b, str) ((void) sizeof ((a) == (b))) 00181 #endif 00182 00183 /** \brief Internal macro to cast returned entry pointers to correct type. 00184 * 00185 * Not portable in C, alas. With GCC or C++11, the "get entry" macros return 00186 * correctly-typed pointers. Otherwise, the macros return `void *`. 00187 * 00188 * The attempt at a portable version would work if the C `?:` operator wasn't 00189 * broken - `x ? (t *) : (void *)` should really have type `(t *)` in C, but 00190 * it has type `(void *)`, which only makes sense for C++. The `?:` is left in, 00191 * in case some day it works. Some compilers may still warn if this is 00192 * assigned to a different type. 00193 */ 00194 #ifdef NS_LIST_PTR_TYPE_ 00195 #define NS_LIST_TYPECAST_(list, val) ((NS_LIST_PTR_TYPE_(list)) (val)) 00196 #else 00197 #define NS_LIST_TYPECAST_(list, val) (0 ? (list)->type : (val)) 00198 #endif 00199 00200 /** \brief Internal macro to check types of input entry pointer. */ 00201 #define NS_LIST_TYPECHECK_(list, entry) \ 00202 (NS_PTR_MATCH_((list)->type, (entry), "incorrect entry type for list"), (entry)) 00203 00204 /** \brief Type used to pass link offset to underlying functions 00205 * 00206 * We could use size_t, but it would be unnecessarily large on 8-bit systems, 00207 * where we can be (pretty) confident we won't have next pointers more than 00208 * 256 bytes into a structure. 00209 */ 00210 typedef uint_fast8_t ns_list_offset_t; 00211 00212 /// \publicsection 00213 /** \brief The type for the link member in the user's entry structure. 00214 * 00215 * Users should not access this member directly - just pass its name to the 00216 * list head macros. The funny prev pointer simplifies common operations 00217 * (eg insertion, removal), at the expense of complicating rare reverse iteration. 00218 * 00219 * NB - the list implementation relies on next being the first member. 00220 */ 00221 typedef struct ns_list_link { 00222 void *next; ///< Pointer to next entry, or NULL if none 00223 void **prev; ///< Pointer to previous entry's (or head's) next pointer 00224 } ns_list_link_t; 00225 00226 /** \brief "Poison" value placed in unattached entries' link pointers. 00227 * \internal What are good values for this? Platform dependent, maybe just NULL 00228 */ 00229 #define NS_LIST_POISON ((void *) 0xDEADBEEF) 00230 00231 /** \brief Initialiser for an entry's link member 00232 * 00233 * This initialiser is not required by the library, but a user may want an 00234 * initialiser to include in their own entry initialiser. See 00235 * ns_list_link_init() for more discussion. 00236 */ 00237 #define NS_LIST_LINK_INIT(name) \ 00238 NS_FUNNY_INTPTR_OK \ 00239 { NS_LIST_POISON, NS_LIST_POISON } \ 00240 NS_FUNNY_INTPTR_RESTORE 00241 00242 /** \hideinitializer \brief Initialise an entry's list link 00243 * 00244 * This "initialises" an unattached entry's link by filling the fields with 00245 * poison. This is optional, as unattached entries field pointers are not 00246 * meaningful, and it is not valid to call ns_list_get_next or similar on 00247 * an unattached entry. 00248 * 00249 * \param entry Pointer to an entry 00250 * \param field The name of the link member to initialise 00251 */ 00252 #define ns_list_link_init(entry, field) ns_list_link_init_(&(entry)->field) 00253 00254 /** \hideinitializer \brief Initialise a list 00255 * 00256 * Initialise a list head before use. A list head must be initialised using this 00257 * function or one of the NS_LIST_INIT()-type macros before use. A zero-initialised 00258 * list head is *not* valid. 00259 * 00260 * If used on a list containing existing entries, those entries will 00261 * become detached. (They are not modified, but their links are now effectively 00262 * undefined). 00263 * 00264 * \param list Pointer to a NS_LIST_HEAD() structure. 00265 */ 00266 #define ns_list_init(list) ns_list_init_(&(list)->slist) 00267 00268 /** \brief Initialiser for an empty list 00269 * 00270 * Usage in an enclosing initialiser: 00271 * ~~~ 00272 * static my_type_including_list_t x = { 00273 * "Something", 00274 * 23, 00275 * NS_LIST_INIT(x), 00276 * }; 00277 * ~~~ 00278 * NS_LIST_DEFINE() or NS_LIST_NAME_INIT() may provide a shorter alternative 00279 * in simpler cases. 00280 */ 00281 #define NS_LIST_INIT(name) { { NULL, &(name).slist.first_entry } } 00282 00283 /** \brief Name and initialiser for an empty list 00284 * 00285 * Usage: 00286 * ~~~ 00287 * list_t NS_LIST_NAME_INIT(foo); 00288 * ~~~ 00289 * acts as 00290 * ~~~ 00291 * list_t foo = { empty list }; 00292 * ~~~ 00293 * Also useful with designated initialisers: 00294 * ~~~ 00295 * .NS_LIST_NAME_INIT(foo), 00296 * ~~~ 00297 * acts as 00298 * ~~~ 00299 * .foo = { empty list }, 00300 * ~~~ 00301 */ 00302 #define NS_LIST_NAME_INIT(name) name = NS_LIST_INIT(name) 00303 00304 /** \brief Define a list, and initialise to empty. 00305 * 00306 * Usage: 00307 * ~~~ 00308 * static NS_LIST_DEFINE(my_list, entry_t, link); 00309 * ~~~ 00310 * acts as 00311 * ~~~ 00312 * static list_type my_list = { empty list }; 00313 * ~~~ 00314 */ 00315 #define NS_LIST_DEFINE(name, type, field) \ 00316 NS_LIST_HEAD(type, field) NS_LIST_NAME_INIT(name) 00317 00318 /** \hideinitializer \brief Add an entry to the start of the linked list. 00319 * 00320 * ns_list_add_to_end() is *slightly* more efficient than ns_list_add_to_start(). 00321 * 00322 * \param list `(list_t *)` Pointer to list. 00323 * \param entry `(entry_t * restrict)` Pointer to new entry to add. 00324 */ 00325 #define ns_list_add_to_start(list, entry) \ 00326 ns_list_add_to_start_(&(list)->slist, NS_LIST_OFFSET_(list), NS_LIST_TYPECHECK_(list, entry)) 00327 00328 /** \hideinitializer \brief Add an entry to the end of the linked list. 00329 * 00330 * \param list `(list_t *)` Pointer to list. 00331 * \param entry `(entry_t * restrict)` Pointer to new entry to add. 00332 */ 00333 #define ns_list_add_to_end(list, entry) \ 00334 ns_list_add_to_end_(&(list)->slist, NS_LIST_OFFSET_(list), NS_LIST_TYPECHECK_(list, entry)) 00335 00336 /** \hideinitializer \brief Add an entry before a specified entry. 00337 * 00338 * \param list `(list_t *)` Pointer to list. 00339 * \param before `(entry_t *)` Existing entry before which to place the new entry. 00340 * \param entry `(entry_t * restrict)` Pointer to new entry to add. 00341 */ 00342 #define ns_list_add_before(list, before, entry) \ 00343 ns_list_add_before_(NS_LIST_OFFSET_(list), NS_LIST_TYPECHECK_(list, before), NS_LIST_TYPECHECK_(list, entry)) 00344 00345 /** \hideinitializer \brief Add an entry after a specified entry. 00346 * 00347 * ns_list_add_before() is *slightly* more efficient than ns_list_add_after(). 00348 * 00349 * \param list `(list_t *)` Pointer to list. 00350 * \param after `(entry_t *)` Existing entry after which to place the new entry. 00351 * \param entry `(entry_t * restrict)` Pointer to new entry to add. 00352 */ 00353 #define ns_list_add_after(list, after, entry) \ 00354 ns_list_add_after_(&(list)->slist, NS_LIST_OFFSET_(list), NS_LIST_TYPECHECK_(list, after), NS_LIST_TYPECHECK_(list, entry)) 00355 00356 /** \brief Check if a list is empty. 00357 * 00358 * \param list `(const list_t *)` Pointer to list. 00359 * 00360 * \return `(bool)` true if the list is empty. 00361 */ 00362 #define ns_list_is_empty(list) ((bool) ((list)->slist.first_entry == NULL)) 00363 00364 /** \brief Get the first entry. 00365 * 00366 * \param list `(const list_t *)` Pointer to list. 00367 * 00368 * \return `(entry_t *)` Pointer to first entry. 00369 * \return NULL if list is empty. 00370 */ 00371 #define ns_list_get_first(list) NS_LIST_TYPECAST_(list, (list)->slist.first_entry) 00372 00373 /** \hideinitializer \brief Get the previous entry. 00374 * 00375 * \param list `(const list_t *)` Pointer to list. 00376 * \param current `(const entry_t *)` Pointer to current entry. 00377 * 00378 * \return `(entry_t *)` Pointer to previous entry. 00379 * \return NULL if current entry is first. 00380 */ 00381 #define ns_list_get_previous(list, current) \ 00382 NS_LIST_TYPECAST_(list, ns_list_get_previous_(&(list)->slist, NS_LIST_OFFSET_(list), NS_LIST_TYPECHECK_(list, current))) 00383 00384 /** \hideinitializer \brief Get the next entry. 00385 * 00386 * \param list `(const list_t *)` Pointer to list. 00387 * \param current `(const entry_t *)` Pointer to current entry. 00388 * 00389 * \return `(entry_t *)` Pointer to next entry. 00390 * \return NULL if current entry is last. 00391 */ 00392 #define ns_list_get_next(list, current) \ 00393 NS_LIST_TYPECAST_(list, ns_list_get_next_(NS_LIST_OFFSET_(list), NS_LIST_TYPECHECK_(list, current))) 00394 00395 /** \hideinitializer \brief Get the last entry. 00396 * 00397 * \param list `(const list_t *)` Pointer to list. 00398 * 00399 * \return `(entry_t *)` Pointer to last entry. 00400 * \return NULL if list is empty. 00401 */ 00402 #define ns_list_get_last(list) \ 00403 NS_LIST_TYPECAST_(list, ns_list_get_last_(&(list)->slist, NS_LIST_OFFSET_(list))) 00404 00405 /** \hideinitializer \brief Remove an entry. 00406 * 00407 * \param list `(list_t *)` Pointer to list. 00408 * \param entry `(entry_t *)` Entry on list to be removed. 00409 */ 00410 #define ns_list_remove(list, entry) \ 00411 ns_list_remove_(&(list)->slist, NS_LIST_OFFSET_(list), NS_LIST_TYPECHECK_(list, entry)) 00412 00413 /** \hideinitializer \brief Replace an entry. 00414 * 00415 * \param list `(list_t *)` Pointer to list. 00416 * \param current `(entry_t *)` Existing entry on list to be replaced. 00417 * \param replacement `(entry_t * restrict)` New entry to be the replacement. 00418 */ 00419 #define ns_list_replace(list, current, replacement) \ 00420 ns_list_replace_(&(list)->slist, NS_LIST_OFFSET_(list), NS_LIST_TYPECHECK_(list, current), NS_LIST_TYPECHECK_(list, replacement)) 00421 00422 /** \hideinitializer \brief Concatenate two lists. 00423 * 00424 * Attach the entries on the source list to the end of the destination 00425 * list, leaving the source list empty. 00426 * 00427 * \param dst `(list_t *)` Pointer to destination list. 00428 * \param src `(list_t *)` Pointer to source list. 00429 * 00430 */ 00431 #define ns_list_concatenate(dst, src) \ 00432 (NS_PTR_MATCH_(dst, src, "concatenating different list types"), \ 00433 ns_list_concatenate_(&(dst)->slist, &(src)->slist, NS_LIST_OFFSET_(src))) 00434 00435 /** \brief Iterate forwards over a list. 00436 * 00437 * Example: 00438 * ~~~ 00439 * ns_list_foreach(const my_entry_t, cur, &my_list) 00440 * { 00441 * printf("%s\n", cur->name); 00442 * } 00443 * ~~~ 00444 * Deletion of the current entry is not permitted as its next is checked after 00445 * running user code. 00446 * 00447 * The iteration pointer is declared inside the loop, using C99/C++, so it 00448 * is not accessible after the loop. This encourages good code style, and 00449 * matches the semantics of C++11's "ranged for", which only provides the 00450 * declaration form: 00451 * ~~~ 00452 * for (const my_entry_t cur : my_list) 00453 * ~~~ 00454 * If you need to see the value of the iteration pointer after a `break`, 00455 * you will need to assign it to a variable declared outside the loop before 00456 * breaking: 00457 * ~~~ 00458 * my_entry_t *match = NULL; 00459 * ns_list_foreach(my_entry_t, cur, &my_list) 00460 * { 00461 * if (cur->id == id) 00462 * { 00463 * match = cur; 00464 * break; 00465 * } 00466 * } 00467 * ~~~ 00468 * 00469 * The user has to specify the entry type for the pointer definition, as type 00470 * extraction from the list argument isn't portable. On the other hand, this 00471 * also permits const qualifiers, as in the example above, and serves as 00472 * documentation. The entry type will be checked against the list type where the 00473 * compiler supports it. 00474 * 00475 * \param type Entry type `([const] entry_t)`. 00476 * \param e Name for iteration pointer to be defined 00477 * inside the loop. 00478 * \param list `(const list_t *)` Pointer to list - evaluated multiple times. 00479 */ 00480 #define ns_list_foreach(type, e, list) \ 00481 for (type *e = ns_list_get_first(list); e; e = ns_list_get_next(list, e)) 00482 00483 /** \brief Iterate forwards over a list, where user may delete. 00484 * 00485 * As ns_list_foreach(), but deletion of current entry is permitted as its 00486 * next pointer is recorded before running user code. 00487 * 00488 * Example: 00489 * ~~~ 00490 * ns_list_foreach_safe(my_entry_t, cur, &my_list) 00491 * { 00492 * ns_list_remove(cur); 00493 * } 00494 * ~~~ 00495 * \param type Entry type `(entry_t)`. 00496 * \param e Name for iteration pointer to be defined 00497 * inside the loop. 00498 * \param list `(list_t *)` Pointer to list - evaluated multiple times. 00499 */ 00500 #define ns_list_foreach_safe(type, e, list) \ 00501 for (type *e = ns_list_get_first(list), *_next; \ 00502 e && (_next = ns_list_get_next(list, e), true); e = _next) 00503 00504 /** \brief Iterate backwards over a list. 00505 * 00506 * As ns_list_foreach(), but going backwards - see its documentation. 00507 * Iterating forwards is *slightly* more efficient. 00508 */ 00509 #define ns_list_foreach_reverse(type, e, list) \ 00510 for (type *e = ns_list_get_last(list); e; e = ns_list_get_previous(list, e)) 00511 00512 /** \brief Iterate backwards over a list, where user may delete. 00513 * 00514 * As ns_list_foreach_safe(), but going backwards - see its documentation. 00515 * Iterating forwards is *slightly* more efficient. 00516 */ 00517 #define ns_list_foreach_reverse_safe(type, e, list) \ 00518 for (type *e = ns_list_get_last(list), *_next; \ 00519 e && (_next = ns_list_get_previous(list, e), true); e = _next) 00520 00521 /** \hideinitializer \brief Count entries on a list 00522 * 00523 * Unlike other operations, this is O(n). Note: if list might contain over 00524 * 65535 entries, this function **must not** be used to get the entry count. 00525 * 00526 * \param list `(const list_t *)` Pointer to list. 00527 00528 * \return `(uint_fast16_t)` Number of entries that are stored in list. 00529 */ 00530 #define ns_list_count(list) ns_list_count_(&(list)->slist, NS_LIST_OFFSET_(list)) 00531 00532 /** \privatesection 00533 * Internal functions - designed to be accessed using corresponding macros above 00534 */ 00535 NS_INLINE void ns_list_init_(ns_list_t *list); 00536 NS_INLINE void ns_list_link_init_(ns_list_link_t *link); 00537 NS_INLINE void ns_list_add_to_start_(ns_list_t *list, ns_list_offset_t link_offset, void *restrict entry); 00538 NS_INLINE void ns_list_add_to_end_(ns_list_t *list, ns_list_offset_t link_offset, void *restrict entry); 00539 NS_INLINE void ns_list_add_before_(ns_list_offset_t link_offset, void *before, void *restrict entry); 00540 NS_INLINE void ns_list_add_after_(ns_list_t *list, ns_list_offset_t link_offset, void *after, void *restrict entry); 00541 NS_INLINE void *ns_list_get_next_(ns_list_offset_t link_offset, const void *current); 00542 NS_INLINE void *ns_list_get_previous_(const ns_list_t *list, ns_list_offset_t link_offset, const void *current); 00543 NS_INLINE void *ns_list_get_last_(const ns_list_t *list, ns_list_offset_t offset); 00544 NS_INLINE void ns_list_remove_(ns_list_t *list, ns_list_offset_t link_offset, void *entry); 00545 NS_INLINE void ns_list_replace_(ns_list_t *list, ns_list_offset_t link_offset, void *current, void *restrict replacement); 00546 NS_INLINE void ns_list_concatenate_(ns_list_t *dst, ns_list_t *src, ns_list_offset_t offset); 00547 NS_INLINE uint_fast16_t ns_list_count_(const ns_list_t *list, ns_list_offset_t link_offset); 00548 00549 /* Provide definitions, either for inlining, or for ns_list.c */ 00550 #if defined NS_ALLOW_INLINING || defined NS_LIST_FN 00551 #ifndef NS_LIST_FN 00552 #define NS_LIST_FN NS_INLINE 00553 #endif 00554 00555 /* Pointer to the link member in entry e */ 00556 #define NS_LIST_LINK_(e, offset) ((ns_list_link_t *)((char *)(e) + offset)) 00557 00558 /* Lvalue of the next link pointer in entry e */ 00559 #define NS_LIST_NEXT_(e, offset) (NS_LIST_LINK_(e, offset)->next) 00560 00561 /* Lvalue of the prev link pointer in entry e */ 00562 #define NS_LIST_PREV_(e, offset) (NS_LIST_LINK_(e, offset)->prev) 00563 00564 /* Convert a pointer to a link member back to the entry; 00565 * works for linkptr either being a ns_list_link_t pointer, or its next pointer, 00566 * as the next pointer is first in the ns_list_link_t */ 00567 #define NS_LIST_ENTRY_(linkptr, offset) ((void *)((char *)(linkptr) - offset)) 00568 00569 NS_LIST_FN void ns_list_init_(ns_list_t *list) 00570 { 00571 list->first_entry = NULL; 00572 list->last_nextptr = &list->first_entry; 00573 } 00574 00575 NS_LIST_FN void ns_list_link_init_(ns_list_link_t *link) 00576 { 00577 NS_FUNNY_INTPTR_OK 00578 link->next = NS_LIST_POISON; 00579 link->prev = NS_LIST_POISON; 00580 NS_FUNNY_INTPTR_RESTORE 00581 } 00582 00583 NS_LIST_FN void ns_list_add_to_start_(ns_list_t *list, ns_list_offset_t offset, void *restrict entry) 00584 { 00585 void *next; 00586 00587 NS_LIST_PREV_(entry, offset) = &list->first_entry; 00588 NS_LIST_NEXT_(entry, offset) = next = list->first_entry; 00589 00590 if (next) { 00591 NS_LIST_PREV_(next, offset) = &NS_LIST_NEXT_(entry, offset); 00592 } else { 00593 list->last_nextptr = &NS_LIST_NEXT_(entry, offset); 00594 } 00595 00596 list->first_entry = entry; 00597 } 00598 00599 NS_LIST_FN void ns_list_add_after_(ns_list_t *list, ns_list_offset_t offset, void *current, void *restrict entry) 00600 { 00601 void *next; 00602 00603 NS_LIST_PREV_(entry, offset) = &NS_LIST_NEXT_(current, offset); 00604 NS_LIST_NEXT_(entry, offset) = next = NS_LIST_NEXT_(current, offset); 00605 00606 if (next) { 00607 NS_LIST_PREV_(next, offset) = &NS_LIST_NEXT_(entry, offset); 00608 } else { 00609 list->last_nextptr = &NS_LIST_NEXT_(entry, offset); 00610 } 00611 00612 NS_LIST_NEXT_(current, offset) = entry; 00613 } 00614 00615 NS_LIST_FN void ns_list_add_before_(ns_list_offset_t offset, void *current, void *restrict entry) 00616 { 00617 void **prev_nextptr; 00618 00619 NS_LIST_NEXT_(entry, offset) = current; 00620 NS_LIST_PREV_(entry, offset) = prev_nextptr = NS_LIST_PREV_(current, offset); 00621 *prev_nextptr = entry; 00622 NS_LIST_PREV_(current, offset) = &NS_LIST_NEXT_(entry, offset); 00623 } 00624 00625 NS_LIST_FN void ns_list_add_to_end_(ns_list_t *list, ns_list_offset_t offset, void *restrict entry) 00626 { 00627 void **prev_nextptr; 00628 00629 NS_LIST_NEXT_(entry, offset) = NULL; 00630 NS_LIST_PREV_(entry, offset) = prev_nextptr = list->last_nextptr; 00631 *prev_nextptr = entry; 00632 list->last_nextptr = &NS_LIST_NEXT_(entry, offset); 00633 } 00634 00635 NS_LIST_FN void *ns_list_get_next_(ns_list_offset_t offset, const void *current) 00636 { 00637 return NS_LIST_NEXT_(current, offset); 00638 } 00639 00640 NS_LIST_FN void *ns_list_get_previous_(const ns_list_t *list, ns_list_offset_t offset, const void *current) 00641 { 00642 if (current == list->first_entry) { 00643 return NULL; 00644 } 00645 00646 // Tricky. We don't have a direct previous pointer, but a pointer to the 00647 // pointer that points to us - ie &head->first_entry OR &{prev}->next. 00648 // This makes life easier on insertion and removal, but this is where we 00649 // pay the price. 00650 00651 // We have to check manually for being the first entry above, so we know it's 00652 // a real link's next pointer. Then next is the first field of 00653 // ns_list_link_t, so we can use the normal offset value. 00654 00655 return NS_LIST_ENTRY_(NS_LIST_PREV_(current, offset), offset); 00656 } 00657 00658 NS_LIST_FN void *ns_list_get_last_(const ns_list_t *list, ns_list_offset_t offset) 00659 { 00660 if (!list->first_entry) { 00661 return NULL; 00662 } 00663 00664 // See comments in ns_list_get_previous_() 00665 return NS_LIST_ENTRY_(list->last_nextptr, offset); 00666 } 00667 00668 NS_LIST_FN void ns_list_remove_(ns_list_t *list, ns_list_offset_t offset, void *removed) 00669 { 00670 void *next; 00671 void **prev_nextptr; 00672 00673 next = NS_LIST_NEXT_(removed, offset); 00674 prev_nextptr = NS_LIST_PREV_(removed, offset); 00675 if (next) { 00676 NS_LIST_PREV_(next, offset) = prev_nextptr; 00677 } else { 00678 list->last_nextptr = prev_nextptr; 00679 } 00680 *prev_nextptr = next; 00681 00682 ns_list_link_init_(NS_LIST_LINK_(removed, offset)); 00683 } 00684 00685 NS_LIST_FN void ns_list_replace_(ns_list_t *list, ns_list_offset_t offset, void *current, void *restrict replacement) 00686 { 00687 void *next; 00688 void **prev_nextptr; 00689 00690 NS_LIST_PREV_(replacement, offset) = prev_nextptr = NS_LIST_PREV_(current, offset); 00691 NS_LIST_NEXT_(replacement, offset) = next = NS_LIST_NEXT_(current, offset); 00692 00693 if (next) { 00694 NS_LIST_PREV_(next, offset) = &NS_LIST_NEXT_(replacement, offset); 00695 } else { 00696 list->last_nextptr = &NS_LIST_NEXT_(replacement, offset); 00697 } 00698 *prev_nextptr = replacement; 00699 00700 ns_list_link_init_(NS_LIST_LINK_(current, offset)); 00701 } 00702 00703 NS_LIST_FN void ns_list_concatenate_(ns_list_t *dst, ns_list_t *src, ns_list_offset_t offset) 00704 { 00705 ns_list_link_t *src_first; 00706 00707 src_first = src->first_entry; 00708 if (!src_first) { 00709 return; 00710 } 00711 00712 *dst->last_nextptr = src_first; 00713 NS_LIST_PREV_(src_first, offset) = dst->last_nextptr; 00714 dst->last_nextptr = src->last_nextptr; 00715 00716 ns_list_init_(src); 00717 } 00718 00719 NS_LIST_FN uint_fast16_t ns_list_count_(const ns_list_t *list, ns_list_offset_t offset) 00720 { 00721 uint_fast16_t count = 0; 00722 00723 for (void *p = list->first_entry; p; p = NS_LIST_NEXT_(p, offset)) { 00724 count++; 00725 } 00726 00727 return count; 00728 } 00729 #endif /* defined NS_ALLOW_INLINING || defined NS_LIST_FN */ 00730 00731 #ifdef __cplusplus 00732 } 00733 #endif 00734 00735 #endif /* NS_LIST_H_ */ 00736
Generated on Tue Jul 12 2022 12:47:48 by
