Pfp Cybersecurity (Aka Power Fingerprinting, Inc.) / Mbed OS pfp-emon-nxp

Dependencies:   FXAS21002 FXOS8700Q

Committer:
vithyat
Date:
Wed Aug 28 19:24:56 2019 +0000
Revision:
0:977e87915078
init

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vithyat 0:977e87915078 1 /*
vithyat 0:977e87915078 2 * Copyright (c) 2014-2015 ARM Limited. All rights reserved.
vithyat 0:977e87915078 3 * SPDX-License-Identifier: Apache-2.0
vithyat 0:977e87915078 4 * Licensed under the Apache License, Version 2.0 (the License); you may
vithyat 0:977e87915078 5 * not use this file except in compliance with the License.
vithyat 0:977e87915078 6 * You may obtain a copy of the License at
vithyat 0:977e87915078 7 *
vithyat 0:977e87915078 8 * http://www.apache.org/licenses/LICENSE-2.0
vithyat 0:977e87915078 9 *
vithyat 0:977e87915078 10 * Unless required by applicable law or agreed to in writing, software
vithyat 0:977e87915078 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
vithyat 0:977e87915078 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
vithyat 0:977e87915078 13 * See the License for the specific language governing permissions and
vithyat 0:977e87915078 14 * limitations under the License.
vithyat 0:977e87915078 15 */
vithyat 0:977e87915078 16
vithyat 0:977e87915078 17 #ifndef NS_LIST_H_
vithyat 0:977e87915078 18 #define NS_LIST_H_
vithyat 0:977e87915078 19
vithyat 0:977e87915078 20 #include "ns_types.h"
vithyat 0:977e87915078 21
vithyat 0:977e87915078 22 #ifdef __cplusplus
vithyat 0:977e87915078 23 extern "C" {
vithyat 0:977e87915078 24 #endif
vithyat 0:977e87915078 25
vithyat 0:977e87915078 26 /** \file
vithyat 0:977e87915078 27 * \brief Linked list support library
vithyat 0:977e87915078 28 *
vithyat 0:977e87915078 29 * The ns_list.h file provides a doubly-linked list/queue, providing O(1)
vithyat 0:977e87915078 30 * performance for all insertion/removal operations, and access to either
vithyat 0:977e87915078 31 * end of the list.
vithyat 0:977e87915078 32 *
vithyat 0:977e87915078 33 * Memory footprint is two pointers for the list head, and two pointers in each
vithyat 0:977e87915078 34 * list entry. It is similar in concept to BSD's TAILQ.
vithyat 0:977e87915078 35 *
vithyat 0:977e87915078 36 * Although the API is symmetrical and O(1) in both directions, due to internal
vithyat 0:977e87915078 37 * pointer design, it is *slightly* more efficient to insert at the end when
vithyat 0:977e87915078 38 * used as a queue, and to iterate forwards rather than backwards.
vithyat 0:977e87915078 39 *
vithyat 0:977e87915078 40 * Example of an entry type that can be stored to this list.
vithyat 0:977e87915078 41 * ~~~
vithyat 0:977e87915078 42 * typedef struct example_entry
vithyat 0:977e87915078 43 * {
vithyat 0:977e87915078 44 * uint8_t *data;
vithyat 0:977e87915078 45 * uint32_t data_count;
vithyat 0:977e87915078 46 * ns_list_link_t link;
vithyat 0:977e87915078 47 * }
vithyat 0:977e87915078 48 * example_entry_t;
vithyat 0:977e87915078 49 *
vithyat 0:977e87915078 50 * static NS_LIST_HEAD(example_entry_t, link) my_list;
vithyat 0:977e87915078 51 * ns_list_init(&my_list);
vithyat 0:977e87915078 52 * ~~~
vithyat 0:977e87915078 53 * OR
vithyat 0:977e87915078 54 * ~~~
vithyat 0:977e87915078 55 * NS_LIST_HEAD(example_entry_t, link) my_list = NS_LIST_INIT(my_list);
vithyat 0:977e87915078 56 * ~~~
vithyat 0:977e87915078 57 * OR
vithyat 0:977e87915078 58 * ~~~
vithyat 0:977e87915078 59 * static NS_LIST_DEFINE(my_list, example_entry_t, link);
vithyat 0:977e87915078 60 * ~~~
vithyat 0:977e87915078 61 * OR
vithyat 0:977e87915078 62 * ~~~
vithyat 0:977e87915078 63 * typedef NS_LIST_HEAD(example_entry_t, link) example_list_t;
vithyat 0:977e87915078 64 * example_list_t NS_LIST_NAME_INIT(my_list);
vithyat 0:977e87915078 65 * ~~~
vithyat 0:977e87915078 66 * NOTE: the link field SHALL NOT be accessed by the user.
vithyat 0:977e87915078 67 *
vithyat 0:977e87915078 68 * An entry can exist on multiple lists by having multiple link fields.
vithyat 0:977e87915078 69 *
vithyat 0:977e87915078 70 * All the list operations are implemented as macros, most of which are backed
vithyat 0:977e87915078 71 * by optionally-inline functions. The macros do not evaluate any arguments more
vithyat 0:977e87915078 72 * than once, unless documented.
vithyat 0:977e87915078 73 *
vithyat 0:977e87915078 74 * In macro documentation, `list_t` refers to a list type defined using
vithyat 0:977e87915078 75 * NS_LIST_HEAD(), and `entry_t` to the entry type that was passed to it.
vithyat 0:977e87915078 76 */
vithyat 0:977e87915078 77
vithyat 0:977e87915078 78 /** \brief Underlying generic linked list head.
vithyat 0:977e87915078 79 *
vithyat 0:977e87915078 80 * Users should not use this type directly, but use the NS_LIST_HEAD() macro.
vithyat 0:977e87915078 81 */
vithyat 0:977e87915078 82 typedef struct ns_list {
vithyat 0:977e87915078 83 void *first_entry; ///< Pointer to first entry, or NULL if list is empty
vithyat 0:977e87915078 84 void **last_nextptr; ///< Pointer to last entry's `next` pointer, or
vithyat 0:977e87915078 85 ///< to head's `first_entry` pointer if list is empty
vithyat 0:977e87915078 86 } ns_list_t;
vithyat 0:977e87915078 87
vithyat 0:977e87915078 88 /** \brief Declare a list head type
vithyat 0:977e87915078 89 *
vithyat 0:977e87915078 90 * This union stores the real list head, and also encodes as compile-time type
vithyat 0:977e87915078 91 * information the offset of the link pointer, and the type of the entry.
vithyat 0:977e87915078 92 *
vithyat 0:977e87915078 93 * Note that type information is compiler-dependent; this means
vithyat 0:977e87915078 94 * ns_list_get_first() could return either `void *`, or a pointer to the actual
vithyat 0:977e87915078 95 * entry type. So `ns_list_get_first()->data` is not a portable construct -
vithyat 0:977e87915078 96 * always assign returned entry pointers to a properly typed pointer variable.
vithyat 0:977e87915078 97 * This assignment will be then type-checked where the compiler supports it, and
vithyat 0:977e87915078 98 * will dereference correctly on compilers that don't support this extension.
vithyat 0:977e87915078 99 * ~~~
vithyat 0:977e87915078 100 * NS_LIST_HEAD(example_entry_t, link) my_list;
vithyat 0:977e87915078 101 *
vithyat 0:977e87915078 102 * example_entry_t *entry = ns_list_get_first(&my_list);
vithyat 0:977e87915078 103 * do_something(entry->data);
vithyat 0:977e87915078 104 * ~~~
vithyat 0:977e87915078 105 * Each use of this macro generates a new anonymous union, so these two lists
vithyat 0:977e87915078 106 * have different types:
vithyat 0:977e87915078 107 * ~~~
vithyat 0:977e87915078 108 * NS_LIST_HEAD(example_entry_t, link) my_list1;
vithyat 0:977e87915078 109 * NS_LIST_HEAD(example_entry_t, link) my_list2;
vithyat 0:977e87915078 110 * ~~~
vithyat 0:977e87915078 111 * If you need to use a list type in multiple places, eg as a function
vithyat 0:977e87915078 112 * parameter, use typedef:
vithyat 0:977e87915078 113 * ~~~
vithyat 0:977e87915078 114 * typedef NS_LIST_HEAD(example_entry_t, link) example_list_t;
vithyat 0:977e87915078 115 *
vithyat 0:977e87915078 116 * void example_function(example_list_t *);
vithyat 0:977e87915078 117 * ~~~
vithyat 0:977e87915078 118 */
vithyat 0:977e87915078 119 #define NS_LIST_HEAD(entry_type, field) \
vithyat 0:977e87915078 120 NS_LIST_HEAD_BY_OFFSET_(entry_type, offsetof(entry_type, field))
vithyat 0:977e87915078 121
vithyat 0:977e87915078 122 /** \brief Declare a list head type for an incomplete entry type.
vithyat 0:977e87915078 123 *
vithyat 0:977e87915078 124 * This declares a list head, similarly to NS_LIST_HEAD(), but unlike that
vithyat 0:977e87915078 125 * this can be used in contexts where the entry type may be incomplete.
vithyat 0:977e87915078 126 *
vithyat 0:977e87915078 127 * To use this, the link pointer must be the first member in the
vithyat 0:977e87915078 128 * actual complete structure. This is NOT checked - the definition of the
vithyat 0:977e87915078 129 * element should probably test NS_STATIC_ASSERT(offsetof(type, link) == 0)
vithyat 0:977e87915078 130 * if outside users are known to be using NS_LIST_HEAD_INCOMPLETE().
vithyat 0:977e87915078 131 * ~~~
vithyat 0:977e87915078 132 * struct opaque;
vithyat 0:977e87915078 133 * NS_LIST_HEAD_INCOMPLETE(struct opaque) opaque_list;
vithyat 0:977e87915078 134 * ~~~
vithyat 0:977e87915078 135 */
vithyat 0:977e87915078 136 #define NS_LIST_HEAD_INCOMPLETE(entry_type) \
vithyat 0:977e87915078 137 NS_LIST_HEAD_BY_OFFSET_(entry_type, 0)
vithyat 0:977e87915078 138
vithyat 0:977e87915078 139 /// \privatesection
vithyat 0:977e87915078 140 /** \brief Internal macro defining a list head, given the offset to the link pointer
vithyat 0:977e87915078 141 * The +1 allows for link_offset being 0 - we can't declare a 0-size array
vithyat 0:977e87915078 142 */
vithyat 0:977e87915078 143 #define NS_LIST_HEAD_BY_OFFSET_(entry_type, link_offset) \
vithyat 0:977e87915078 144 union \
vithyat 0:977e87915078 145 { \
vithyat 0:977e87915078 146 ns_list_t slist; \
vithyat 0:977e87915078 147 NS_FUNNY_COMPARE_OK \
vithyat 0:977e87915078 148 NS_STATIC_ASSERT(link_offset <= UINT_FAST8_MAX, "link offset too large") \
vithyat 0:977e87915078 149 NS_FUNNY_COMPARE_RESTORE \
vithyat 0:977e87915078 150 char (*offset)[link_offset + 1]; \
vithyat 0:977e87915078 151 entry_type *type; \
vithyat 0:977e87915078 152 }
vithyat 0:977e87915078 153
vithyat 0:977e87915078 154 /** \brief Get offset of link field in entry.
vithyat 0:977e87915078 155 * \return `(ns_list_offset_t)` The offset of the link field for entries on the specified list
vithyat 0:977e87915078 156 */
vithyat 0:977e87915078 157 #define NS_LIST_OFFSET_(list) ((ns_list_offset_t) (sizeof *(list)->offset - 1))
vithyat 0:977e87915078 158
vithyat 0:977e87915078 159 /** \brief Get the entry pointer type.
vithyat 0:977e87915078 160 * \def NS_LIST_PTR_TYPE_
vithyat 0:977e87915078 161 *
vithyat 0:977e87915078 162 * \return An unqualified pointer type to an entry on the specified list.
vithyat 0:977e87915078 163 *
vithyat 0:977e87915078 164 * Only available if the compiler provides a "typeof" operator.
vithyat 0:977e87915078 165 */
vithyat 0:977e87915078 166 #if defined __cplusplus && __cplusplus >= 201103L
vithyat 0:977e87915078 167 #define NS_LIST_PTR_TYPE_(list) decltype((list)->type)
vithyat 0:977e87915078 168 #elif defined __GNUC__
vithyat 0:977e87915078 169 #define NS_LIST_PTR_TYPE_(list) __typeof__((list)->type)
vithyat 0:977e87915078 170 #endif
vithyat 0:977e87915078 171
vithyat 0:977e87915078 172 /** \brief Check for compatible pointer types
vithyat 0:977e87915078 173 *
vithyat 0:977e87915078 174 * This test will produce a diagnostic about a pointer mismatch on
vithyat 0:977e87915078 175 * the == inside the sizeof operator. For example ARM/Norcroft C gives the error:
vithyat 0:977e87915078 176 *
vithyat 0:977e87915078 177 * operand types are incompatible ("entry_t *" and "other_t *")
vithyat 0:977e87915078 178 */
vithyat 0:977e87915078 179 #ifdef CPPCHECK
vithyat 0:977e87915078 180 #define NS_PTR_MATCH_(a, b, str) ((void) 0)
vithyat 0:977e87915078 181 #else
vithyat 0:977e87915078 182 #define NS_PTR_MATCH_(a, b, str) ((void) sizeof ((a) == (b)))
vithyat 0:977e87915078 183 #endif
vithyat 0:977e87915078 184
vithyat 0:977e87915078 185 /** \brief Internal macro to cast returned entry pointers to correct type.
vithyat 0:977e87915078 186 *
vithyat 0:977e87915078 187 * Not portable in C, alas. With GCC or C++11, the "get entry" macros return
vithyat 0:977e87915078 188 * correctly-typed pointers. Otherwise, the macros return `void *`.
vithyat 0:977e87915078 189 *
vithyat 0:977e87915078 190 * The attempt at a portable version would work if the C `?:` operator wasn't
vithyat 0:977e87915078 191 * broken - `x ? (t *) : (void *)` should really have type `(t *)` in C, but
vithyat 0:977e87915078 192 * it has type `(void *)`, which only makes sense for C++. The `?:` is left in,
vithyat 0:977e87915078 193 * in case some day it works. Some compilers may still warn if this is
vithyat 0:977e87915078 194 * assigned to a different type.
vithyat 0:977e87915078 195 */
vithyat 0:977e87915078 196 #ifdef NS_LIST_PTR_TYPE_
vithyat 0:977e87915078 197 #define NS_LIST_TYPECAST_(list, val) ((NS_LIST_PTR_TYPE_(list)) (val))
vithyat 0:977e87915078 198 #else
vithyat 0:977e87915078 199 #define NS_LIST_TYPECAST_(list, val) (0 ? (list)->type : (val))
vithyat 0:977e87915078 200 #endif
vithyat 0:977e87915078 201
vithyat 0:977e87915078 202 /** \brief Internal macro to check types of input entry pointer. */
vithyat 0:977e87915078 203 #define NS_LIST_TYPECHECK_(list, entry) \
vithyat 0:977e87915078 204 (NS_PTR_MATCH_((list)->type, (entry), "incorrect entry type for list"), (entry))
vithyat 0:977e87915078 205
vithyat 0:977e87915078 206 /** \brief Type used to pass link offset to underlying functions
vithyat 0:977e87915078 207 *
vithyat 0:977e87915078 208 * We could use size_t, but it would be unnecessarily large on 8-bit systems,
vithyat 0:977e87915078 209 * where we can be (pretty) confident we won't have next pointers more than
vithyat 0:977e87915078 210 * 256 bytes into a structure.
vithyat 0:977e87915078 211 */
vithyat 0:977e87915078 212 typedef uint_fast8_t ns_list_offset_t;
vithyat 0:977e87915078 213
vithyat 0:977e87915078 214 /// \publicsection
vithyat 0:977e87915078 215 /** \brief The type for the link member in the user's entry structure.
vithyat 0:977e87915078 216 *
vithyat 0:977e87915078 217 * Users should not access this member directly - just pass its name to the
vithyat 0:977e87915078 218 * list head macros. The funny prev pointer simplifies common operations
vithyat 0:977e87915078 219 * (eg insertion, removal), at the expense of complicating rare reverse iteration.
vithyat 0:977e87915078 220 *
vithyat 0:977e87915078 221 * NB - the list implementation relies on next being the first member.
vithyat 0:977e87915078 222 */
vithyat 0:977e87915078 223 typedef struct ns_list_link {
vithyat 0:977e87915078 224 void *next; ///< Pointer to next entry, or NULL if none
vithyat 0:977e87915078 225 void **prev; ///< Pointer to previous entry's (or head's) next pointer
vithyat 0:977e87915078 226 } ns_list_link_t;
vithyat 0:977e87915078 227
vithyat 0:977e87915078 228 /** \brief "Poison" value placed in unattached entries' link pointers.
vithyat 0:977e87915078 229 * \internal What are good values for this? Platform dependent, maybe just NULL
vithyat 0:977e87915078 230 */
vithyat 0:977e87915078 231 #define NS_LIST_POISON ((void *) 0xDEADBEEF)
vithyat 0:977e87915078 232
vithyat 0:977e87915078 233 /** \brief Initialiser for an entry's link member
vithyat 0:977e87915078 234 *
vithyat 0:977e87915078 235 * This initialiser is not required by the library, but a user may want an
vithyat 0:977e87915078 236 * initialiser to include in their own entry initialiser. See
vithyat 0:977e87915078 237 * ns_list_link_init() for more discussion.
vithyat 0:977e87915078 238 */
vithyat 0:977e87915078 239 #define NS_LIST_LINK_INIT(name) \
vithyat 0:977e87915078 240 NS_FUNNY_INTPTR_OK \
vithyat 0:977e87915078 241 { NS_LIST_POISON, NS_LIST_POISON } \
vithyat 0:977e87915078 242 NS_FUNNY_INTPTR_RESTORE
vithyat 0:977e87915078 243
vithyat 0:977e87915078 244 /** \hideinitializer \brief Initialise an entry's list link
vithyat 0:977e87915078 245 *
vithyat 0:977e87915078 246 * This "initialises" an unattached entry's link by filling the fields with
vithyat 0:977e87915078 247 * poison. This is optional, as unattached entries field pointers are not
vithyat 0:977e87915078 248 * meaningful, and it is not valid to call ns_list_get_next or similar on
vithyat 0:977e87915078 249 * an unattached entry.
vithyat 0:977e87915078 250 *
vithyat 0:977e87915078 251 * \param entry Pointer to an entry
vithyat 0:977e87915078 252 * \param field The name of the link member to initialise
vithyat 0:977e87915078 253 */
vithyat 0:977e87915078 254 #define ns_list_link_init(entry, field) ns_list_link_init_(&(entry)->field)
vithyat 0:977e87915078 255
vithyat 0:977e87915078 256 /** \hideinitializer \brief Initialise a list
vithyat 0:977e87915078 257 *
vithyat 0:977e87915078 258 * Initialise a list head before use. A list head must be initialised using this
vithyat 0:977e87915078 259 * function or one of the NS_LIST_INIT()-type macros before use. A zero-initialised
vithyat 0:977e87915078 260 * list head is *not* valid.
vithyat 0:977e87915078 261 *
vithyat 0:977e87915078 262 * If used on a list containing existing entries, those entries will
vithyat 0:977e87915078 263 * become detached. (They are not modified, but their links are now effectively
vithyat 0:977e87915078 264 * undefined).
vithyat 0:977e87915078 265 *
vithyat 0:977e87915078 266 * \param list Pointer to a NS_LIST_HEAD() structure.
vithyat 0:977e87915078 267 */
vithyat 0:977e87915078 268 #define ns_list_init(list) ns_list_init_(&(list)->slist)
vithyat 0:977e87915078 269
vithyat 0:977e87915078 270 /** \brief Initialiser for an empty list
vithyat 0:977e87915078 271 *
vithyat 0:977e87915078 272 * Usage in an enclosing initialiser:
vithyat 0:977e87915078 273 * ~~~
vithyat 0:977e87915078 274 * static my_type_including_list_t x = {
vithyat 0:977e87915078 275 * "Something",
vithyat 0:977e87915078 276 * 23,
vithyat 0:977e87915078 277 * NS_LIST_INIT(x),
vithyat 0:977e87915078 278 * };
vithyat 0:977e87915078 279 * ~~~
vithyat 0:977e87915078 280 * NS_LIST_DEFINE() or NS_LIST_NAME_INIT() may provide a shorter alternative
vithyat 0:977e87915078 281 * in simpler cases.
vithyat 0:977e87915078 282 */
vithyat 0:977e87915078 283 #define NS_LIST_INIT(name) { { NULL, &(name).slist.first_entry } }
vithyat 0:977e87915078 284
vithyat 0:977e87915078 285 /** \brief Name and initialiser for an empty list
vithyat 0:977e87915078 286 *
vithyat 0:977e87915078 287 * Usage:
vithyat 0:977e87915078 288 * ~~~
vithyat 0:977e87915078 289 * list_t NS_LIST_NAME_INIT(foo);
vithyat 0:977e87915078 290 * ~~~
vithyat 0:977e87915078 291 * acts as
vithyat 0:977e87915078 292 * ~~~
vithyat 0:977e87915078 293 * list_t foo = { empty list };
vithyat 0:977e87915078 294 * ~~~
vithyat 0:977e87915078 295 * Also useful with designated initialisers:
vithyat 0:977e87915078 296 * ~~~
vithyat 0:977e87915078 297 * .NS_LIST_NAME_INIT(foo),
vithyat 0:977e87915078 298 * ~~~
vithyat 0:977e87915078 299 * acts as
vithyat 0:977e87915078 300 * ~~~
vithyat 0:977e87915078 301 * .foo = { empty list },
vithyat 0:977e87915078 302 * ~~~
vithyat 0:977e87915078 303 */
vithyat 0:977e87915078 304 #define NS_LIST_NAME_INIT(name) name = NS_LIST_INIT(name)
vithyat 0:977e87915078 305
vithyat 0:977e87915078 306 /** \brief Define a list, and initialise to empty.
vithyat 0:977e87915078 307 *
vithyat 0:977e87915078 308 * Usage:
vithyat 0:977e87915078 309 * ~~~
vithyat 0:977e87915078 310 * static NS_LIST_DEFINE(my_list, entry_t, link);
vithyat 0:977e87915078 311 * ~~~
vithyat 0:977e87915078 312 * acts as
vithyat 0:977e87915078 313 * ~~~
vithyat 0:977e87915078 314 * static list_type my_list = { empty list };
vithyat 0:977e87915078 315 * ~~~
vithyat 0:977e87915078 316 */
vithyat 0:977e87915078 317 #define NS_LIST_DEFINE(name, type, field) \
vithyat 0:977e87915078 318 NS_LIST_HEAD(type, field) NS_LIST_NAME_INIT(name)
vithyat 0:977e87915078 319
vithyat 0:977e87915078 320 /** \hideinitializer \brief Add an entry to the start of the linked list.
vithyat 0:977e87915078 321 *
vithyat 0:977e87915078 322 * ns_list_add_to_end() is *slightly* more efficient than ns_list_add_to_start().
vithyat 0:977e87915078 323 *
vithyat 0:977e87915078 324 * \param list `(list_t *)` Pointer to list.
vithyat 0:977e87915078 325 * \param entry `(entry_t * restrict)` Pointer to new entry to add.
vithyat 0:977e87915078 326 */
vithyat 0:977e87915078 327 #define ns_list_add_to_start(list, entry) \
vithyat 0:977e87915078 328 ns_list_add_to_start_(&(list)->slist, NS_LIST_OFFSET_(list), NS_LIST_TYPECHECK_(list, entry))
vithyat 0:977e87915078 329
vithyat 0:977e87915078 330 /** \hideinitializer \brief Add an entry to the end of the linked list.
vithyat 0:977e87915078 331 *
vithyat 0:977e87915078 332 * \param list `(list_t *)` Pointer to list.
vithyat 0:977e87915078 333 * \param entry `(entry_t * restrict)` Pointer to new entry to add.
vithyat 0:977e87915078 334 */
vithyat 0:977e87915078 335 #define ns_list_add_to_end(list, entry) \
vithyat 0:977e87915078 336 ns_list_add_to_end_(&(list)->slist, NS_LIST_OFFSET_(list), NS_LIST_TYPECHECK_(list, entry))
vithyat 0:977e87915078 337
vithyat 0:977e87915078 338 /** \hideinitializer \brief Add an entry before a specified entry.
vithyat 0:977e87915078 339 *
vithyat 0:977e87915078 340 * \param list `(list_t *)` Pointer to list.
vithyat 0:977e87915078 341 * \param before `(entry_t *)` Existing entry before which to place the new entry.
vithyat 0:977e87915078 342 * \param entry `(entry_t * restrict)` Pointer to new entry to add.
vithyat 0:977e87915078 343 */
vithyat 0:977e87915078 344 #define ns_list_add_before(list, before, entry) \
vithyat 0:977e87915078 345 ns_list_add_before_(NS_LIST_OFFSET_(list), NS_LIST_TYPECHECK_(list, before), NS_LIST_TYPECHECK_(list, entry))
vithyat 0:977e87915078 346
vithyat 0:977e87915078 347 /** \hideinitializer \brief Add an entry after a specified entry.
vithyat 0:977e87915078 348 *
vithyat 0:977e87915078 349 * ns_list_add_before() is *slightly* more efficient than ns_list_add_after().
vithyat 0:977e87915078 350 *
vithyat 0:977e87915078 351 * \param list `(list_t *)` Pointer to list.
vithyat 0:977e87915078 352 * \param after `(entry_t *)` Existing entry after which to place the new entry.
vithyat 0:977e87915078 353 * \param entry `(entry_t * restrict)` Pointer to new entry to add.
vithyat 0:977e87915078 354 */
vithyat 0:977e87915078 355 #define ns_list_add_after(list, after, entry) \
vithyat 0:977e87915078 356 ns_list_add_after_(&(list)->slist, NS_LIST_OFFSET_(list), NS_LIST_TYPECHECK_(list, after), NS_LIST_TYPECHECK_(list, entry))
vithyat 0:977e87915078 357
vithyat 0:977e87915078 358 /** \brief Check if a list is empty.
vithyat 0:977e87915078 359 *
vithyat 0:977e87915078 360 * \param list `(const list_t *)` Pointer to list.
vithyat 0:977e87915078 361 *
vithyat 0:977e87915078 362 * \return `(bool)` true if the list is empty.
vithyat 0:977e87915078 363 */
vithyat 0:977e87915078 364 #define ns_list_is_empty(list) ((bool) ((list)->slist.first_entry == NULL))
vithyat 0:977e87915078 365
vithyat 0:977e87915078 366 /** \brief Get the first entry.
vithyat 0:977e87915078 367 *
vithyat 0:977e87915078 368 * \param list `(const list_t *)` Pointer to list.
vithyat 0:977e87915078 369 *
vithyat 0:977e87915078 370 * \return `(entry_t *)` Pointer to first entry.
vithyat 0:977e87915078 371 * \return NULL if list is empty.
vithyat 0:977e87915078 372 */
vithyat 0:977e87915078 373 #define ns_list_get_first(list) NS_LIST_TYPECAST_(list, (list)->slist.first_entry)
vithyat 0:977e87915078 374
vithyat 0:977e87915078 375 /** \hideinitializer \brief Get the previous entry.
vithyat 0:977e87915078 376 *
vithyat 0:977e87915078 377 * \param list `(const list_t *)` Pointer to list.
vithyat 0:977e87915078 378 * \param current `(const entry_t *)` Pointer to current entry.
vithyat 0:977e87915078 379 *
vithyat 0:977e87915078 380 * \return `(entry_t *)` Pointer to previous entry.
vithyat 0:977e87915078 381 * \return NULL if current entry is first.
vithyat 0:977e87915078 382 */
vithyat 0:977e87915078 383 #define ns_list_get_previous(list, current) \
vithyat 0:977e87915078 384 NS_LIST_TYPECAST_(list, ns_list_get_previous_(&(list)->slist, NS_LIST_OFFSET_(list), NS_LIST_TYPECHECK_(list, current)))
vithyat 0:977e87915078 385
vithyat 0:977e87915078 386 /** \hideinitializer \brief Get the next entry.
vithyat 0:977e87915078 387 *
vithyat 0:977e87915078 388 * \param list `(const list_t *)` Pointer to list.
vithyat 0:977e87915078 389 * \param current `(const entry_t *)` Pointer to current entry.
vithyat 0:977e87915078 390 *
vithyat 0:977e87915078 391 * \return `(entry_t *)` Pointer to next entry.
vithyat 0:977e87915078 392 * \return NULL if current entry is last.
vithyat 0:977e87915078 393 */
vithyat 0:977e87915078 394 #define ns_list_get_next(list, current) \
vithyat 0:977e87915078 395 NS_LIST_TYPECAST_(list, ns_list_get_next_(NS_LIST_OFFSET_(list), NS_LIST_TYPECHECK_(list, current)))
vithyat 0:977e87915078 396
vithyat 0:977e87915078 397 /** \hideinitializer \brief Get the last entry.
vithyat 0:977e87915078 398 *
vithyat 0:977e87915078 399 * \param list `(const list_t *)` Pointer to list.
vithyat 0:977e87915078 400 *
vithyat 0:977e87915078 401 * \return `(entry_t *)` Pointer to last entry.
vithyat 0:977e87915078 402 * \return NULL if list is empty.
vithyat 0:977e87915078 403 */
vithyat 0:977e87915078 404 #define ns_list_get_last(list) \
vithyat 0:977e87915078 405 NS_LIST_TYPECAST_(list, ns_list_get_last_(&(list)->slist, NS_LIST_OFFSET_(list)))
vithyat 0:977e87915078 406
vithyat 0:977e87915078 407 /** \hideinitializer \brief Remove an entry.
vithyat 0:977e87915078 408 *
vithyat 0:977e87915078 409 * \param list `(list_t *)` Pointer to list.
vithyat 0:977e87915078 410 * \param entry `(entry_t *)` Entry on list to be removed.
vithyat 0:977e87915078 411 */
vithyat 0:977e87915078 412 #define ns_list_remove(list, entry) \
vithyat 0:977e87915078 413 ns_list_remove_(&(list)->slist, NS_LIST_OFFSET_(list), NS_LIST_TYPECHECK_(list, entry))
vithyat 0:977e87915078 414
vithyat 0:977e87915078 415 /** \hideinitializer \brief Replace an entry.
vithyat 0:977e87915078 416 *
vithyat 0:977e87915078 417 * \param list `(list_t *)` Pointer to list.
vithyat 0:977e87915078 418 * \param current `(entry_t *)` Existing entry on list to be replaced.
vithyat 0:977e87915078 419 * \param replacement `(entry_t * restrict)` New entry to be the replacement.
vithyat 0:977e87915078 420 */
vithyat 0:977e87915078 421 #define ns_list_replace(list, current, replacement) \
vithyat 0:977e87915078 422 ns_list_replace_(&(list)->slist, NS_LIST_OFFSET_(list), NS_LIST_TYPECHECK_(list, current), NS_LIST_TYPECHECK_(list, replacement))
vithyat 0:977e87915078 423
vithyat 0:977e87915078 424 /** \hideinitializer \brief Concatenate two lists.
vithyat 0:977e87915078 425 *
vithyat 0:977e87915078 426 * Attach the entries on the source list to the end of the destination
vithyat 0:977e87915078 427 * list, leaving the source list empty.
vithyat 0:977e87915078 428 *
vithyat 0:977e87915078 429 * \param dst `(list_t *)` Pointer to destination list.
vithyat 0:977e87915078 430 * \param src `(list_t *)` Pointer to source list.
vithyat 0:977e87915078 431 *
vithyat 0:977e87915078 432 */
vithyat 0:977e87915078 433 #define ns_list_concatenate(dst, src) \
vithyat 0:977e87915078 434 (NS_PTR_MATCH_(dst, src, "concatenating different list types"), \
vithyat 0:977e87915078 435 ns_list_concatenate_(&(dst)->slist, &(src)->slist, NS_LIST_OFFSET_(src)))
vithyat 0:977e87915078 436
vithyat 0:977e87915078 437 /** \brief Iterate forwards over a list.
vithyat 0:977e87915078 438 *
vithyat 0:977e87915078 439 * Example:
vithyat 0:977e87915078 440 * ~~~
vithyat 0:977e87915078 441 * ns_list_foreach(const my_entry_t, cur, &my_list)
vithyat 0:977e87915078 442 * {
vithyat 0:977e87915078 443 * printf("%s\n", cur->name);
vithyat 0:977e87915078 444 * }
vithyat 0:977e87915078 445 * ~~~
vithyat 0:977e87915078 446 * Deletion of the current entry is not permitted as its next is checked after
vithyat 0:977e87915078 447 * running user code.
vithyat 0:977e87915078 448 *
vithyat 0:977e87915078 449 * The iteration pointer is declared inside the loop, using C99/C++, so it
vithyat 0:977e87915078 450 * is not accessible after the loop. This encourages good code style, and
vithyat 0:977e87915078 451 * matches the semantics of C++11's "ranged for", which only provides the
vithyat 0:977e87915078 452 * declaration form:
vithyat 0:977e87915078 453 * ~~~
vithyat 0:977e87915078 454 * for (const my_entry_t cur : my_list)
vithyat 0:977e87915078 455 * ~~~
vithyat 0:977e87915078 456 * If you need to see the value of the iteration pointer after a `break`,
vithyat 0:977e87915078 457 * you will need to assign it to a variable declared outside the loop before
vithyat 0:977e87915078 458 * breaking:
vithyat 0:977e87915078 459 * ~~~
vithyat 0:977e87915078 460 * my_entry_t *match = NULL;
vithyat 0:977e87915078 461 * ns_list_foreach(my_entry_t, cur, &my_list)
vithyat 0:977e87915078 462 * {
vithyat 0:977e87915078 463 * if (cur->id == id)
vithyat 0:977e87915078 464 * {
vithyat 0:977e87915078 465 * match = cur;
vithyat 0:977e87915078 466 * break;
vithyat 0:977e87915078 467 * }
vithyat 0:977e87915078 468 * }
vithyat 0:977e87915078 469 * ~~~
vithyat 0:977e87915078 470 *
vithyat 0:977e87915078 471 * The user has to specify the entry type for the pointer definition, as type
vithyat 0:977e87915078 472 * extraction from the list argument isn't portable. On the other hand, this
vithyat 0:977e87915078 473 * also permits const qualifiers, as in the example above, and serves as
vithyat 0:977e87915078 474 * documentation. The entry type will be checked against the list type where the
vithyat 0:977e87915078 475 * compiler supports it.
vithyat 0:977e87915078 476 *
vithyat 0:977e87915078 477 * \param type Entry type `([const] entry_t)`.
vithyat 0:977e87915078 478 * \param e Name for iteration pointer to be defined
vithyat 0:977e87915078 479 * inside the loop.
vithyat 0:977e87915078 480 * \param list `(const list_t *)` Pointer to list - evaluated multiple times.
vithyat 0:977e87915078 481 */
vithyat 0:977e87915078 482 #define ns_list_foreach(type, e, list) \
vithyat 0:977e87915078 483 for (type *e = ns_list_get_first(list); e; e = ns_list_get_next(list, e))
vithyat 0:977e87915078 484
vithyat 0:977e87915078 485 /** \brief Iterate forwards over a list, where user may delete.
vithyat 0:977e87915078 486 *
vithyat 0:977e87915078 487 * As ns_list_foreach(), but deletion of current entry is permitted as its
vithyat 0:977e87915078 488 * next pointer is recorded before running user code.
vithyat 0:977e87915078 489 *
vithyat 0:977e87915078 490 * Example:
vithyat 0:977e87915078 491 * ~~~
vithyat 0:977e87915078 492 * ns_list_foreach_safe(my_entry_t, cur, &my_list)
vithyat 0:977e87915078 493 * {
vithyat 0:977e87915078 494 * ns_list_remove(cur);
vithyat 0:977e87915078 495 * }
vithyat 0:977e87915078 496 * ~~~
vithyat 0:977e87915078 497 * \param type Entry type `(entry_t)`.
vithyat 0:977e87915078 498 * \param e Name for iteration pointer to be defined
vithyat 0:977e87915078 499 * inside the loop.
vithyat 0:977e87915078 500 * \param list `(list_t *)` Pointer to list - evaluated multiple times.
vithyat 0:977e87915078 501 */
vithyat 0:977e87915078 502 #define ns_list_foreach_safe(type, e, list) \
vithyat 0:977e87915078 503 for (type *e = ns_list_get_first(list), *_next##e; \
vithyat 0:977e87915078 504 e && (_next##e = ns_list_get_next(list, e), true); e = _next##e)
vithyat 0:977e87915078 505
vithyat 0:977e87915078 506 /** \brief Iterate backwards over a list.
vithyat 0:977e87915078 507 *
vithyat 0:977e87915078 508 * As ns_list_foreach(), but going backwards - see its documentation.
vithyat 0:977e87915078 509 * Iterating forwards is *slightly* more efficient.
vithyat 0:977e87915078 510 */
vithyat 0:977e87915078 511 #define ns_list_foreach_reverse(type, e, list) \
vithyat 0:977e87915078 512 for (type *e = ns_list_get_last(list); e; e = ns_list_get_previous(list, e))
vithyat 0:977e87915078 513
vithyat 0:977e87915078 514 /** \brief Iterate backwards over a list, where user may delete.
vithyat 0:977e87915078 515 *
vithyat 0:977e87915078 516 * As ns_list_foreach_safe(), but going backwards - see its documentation.
vithyat 0:977e87915078 517 * Iterating forwards is *slightly* more efficient.
vithyat 0:977e87915078 518 */
vithyat 0:977e87915078 519 #define ns_list_foreach_reverse_safe(type, e, list) \
vithyat 0:977e87915078 520 for (type *e = ns_list_get_last(list), *_next##e; \
vithyat 0:977e87915078 521 e && (_next##e = ns_list_get_previous(list, e), true); e = _next##e)
vithyat 0:977e87915078 522
vithyat 0:977e87915078 523 /** \hideinitializer \brief Count entries on a list
vithyat 0:977e87915078 524 *
vithyat 0:977e87915078 525 * Unlike other operations, this is O(n). Note: if list might contain over
vithyat 0:977e87915078 526 * 65535 entries, this function **must not** be used to get the entry count.
vithyat 0:977e87915078 527 *
vithyat 0:977e87915078 528 * \param list `(const list_t *)` Pointer to list.
vithyat 0:977e87915078 529
vithyat 0:977e87915078 530 * \return `(uint_fast16_t)` Number of entries that are stored in list.
vithyat 0:977e87915078 531 */
vithyat 0:977e87915078 532 #define ns_list_count(list) ns_list_count_(&(list)->slist, NS_LIST_OFFSET_(list))
vithyat 0:977e87915078 533
vithyat 0:977e87915078 534 /** \privatesection
vithyat 0:977e87915078 535 * Internal functions - designed to be accessed using corresponding macros above
vithyat 0:977e87915078 536 */
vithyat 0:977e87915078 537 NS_INLINE void ns_list_init_(ns_list_t *list);
vithyat 0:977e87915078 538 NS_INLINE void ns_list_link_init_(ns_list_link_t *link);
vithyat 0:977e87915078 539 NS_INLINE void ns_list_add_to_start_(ns_list_t *list, ns_list_offset_t link_offset, void *restrict entry);
vithyat 0:977e87915078 540 NS_INLINE void ns_list_add_to_end_(ns_list_t *list, ns_list_offset_t link_offset, void *restrict entry);
vithyat 0:977e87915078 541 NS_INLINE void ns_list_add_before_(ns_list_offset_t link_offset, void *before, void *restrict entry);
vithyat 0:977e87915078 542 NS_INLINE void ns_list_add_after_(ns_list_t *list, ns_list_offset_t link_offset, void *after, void *restrict entry);
vithyat 0:977e87915078 543 NS_INLINE void *ns_list_get_next_(ns_list_offset_t link_offset, const void *current);
vithyat 0:977e87915078 544 NS_INLINE void *ns_list_get_previous_(const ns_list_t *list, ns_list_offset_t link_offset, const void *current);
vithyat 0:977e87915078 545 NS_INLINE void *ns_list_get_last_(const ns_list_t *list, ns_list_offset_t offset);
vithyat 0:977e87915078 546 NS_INLINE void ns_list_remove_(ns_list_t *list, ns_list_offset_t link_offset, void *entry);
vithyat 0:977e87915078 547 NS_INLINE void ns_list_replace_(ns_list_t *list, ns_list_offset_t link_offset, void *current, void *restrict replacement);
vithyat 0:977e87915078 548 NS_INLINE void ns_list_concatenate_(ns_list_t *dst, ns_list_t *src, ns_list_offset_t offset);
vithyat 0:977e87915078 549 NS_INLINE uint_fast16_t ns_list_count_(const ns_list_t *list, ns_list_offset_t link_offset);
vithyat 0:977e87915078 550
vithyat 0:977e87915078 551 /* Provide definitions, either for inlining, or for ns_list.c */
vithyat 0:977e87915078 552 #if defined NS_ALLOW_INLINING || defined NS_LIST_FN
vithyat 0:977e87915078 553 #ifndef NS_LIST_FN
vithyat 0:977e87915078 554 #define NS_LIST_FN NS_INLINE
vithyat 0:977e87915078 555 #endif
vithyat 0:977e87915078 556
vithyat 0:977e87915078 557 /* Pointer to the link member in entry e */
vithyat 0:977e87915078 558 #define NS_LIST_LINK_(e, offset) ((ns_list_link_t *)((char *)(e) + offset))
vithyat 0:977e87915078 559
vithyat 0:977e87915078 560 /* Lvalue of the next link pointer in entry e */
vithyat 0:977e87915078 561 #define NS_LIST_NEXT_(e, offset) (NS_LIST_LINK_(e, offset)->next)
vithyat 0:977e87915078 562
vithyat 0:977e87915078 563 /* Lvalue of the prev link pointer in entry e */
vithyat 0:977e87915078 564 #define NS_LIST_PREV_(e, offset) (NS_LIST_LINK_(e, offset)->prev)
vithyat 0:977e87915078 565
vithyat 0:977e87915078 566 /* Convert a pointer to a link member back to the entry;
vithyat 0:977e87915078 567 * works for linkptr either being a ns_list_link_t pointer, or its next pointer,
vithyat 0:977e87915078 568 * as the next pointer is first in the ns_list_link_t */
vithyat 0:977e87915078 569 #define NS_LIST_ENTRY_(linkptr, offset) ((void *)((char *)(linkptr) - offset))
vithyat 0:977e87915078 570
vithyat 0:977e87915078 571 NS_LIST_FN void ns_list_init_(ns_list_t *list)
vithyat 0:977e87915078 572 {
vithyat 0:977e87915078 573 list->first_entry = NULL;
vithyat 0:977e87915078 574 list->last_nextptr = &list->first_entry;
vithyat 0:977e87915078 575 }
vithyat 0:977e87915078 576
vithyat 0:977e87915078 577 NS_LIST_FN void ns_list_link_init_(ns_list_link_t *link)
vithyat 0:977e87915078 578 {
vithyat 0:977e87915078 579 NS_FUNNY_INTPTR_OK
vithyat 0:977e87915078 580 link->next = NS_LIST_POISON;
vithyat 0:977e87915078 581 link->prev = NS_LIST_POISON;
vithyat 0:977e87915078 582 NS_FUNNY_INTPTR_RESTORE
vithyat 0:977e87915078 583 }
vithyat 0:977e87915078 584
vithyat 0:977e87915078 585 NS_LIST_FN void ns_list_add_to_start_(ns_list_t *list, ns_list_offset_t offset, void *restrict entry)
vithyat 0:977e87915078 586 {
vithyat 0:977e87915078 587 void *next;
vithyat 0:977e87915078 588
vithyat 0:977e87915078 589 NS_LIST_PREV_(entry, offset) = &list->first_entry;
vithyat 0:977e87915078 590 NS_LIST_NEXT_(entry, offset) = next = list->first_entry;
vithyat 0:977e87915078 591
vithyat 0:977e87915078 592 if (next) {
vithyat 0:977e87915078 593 NS_LIST_PREV_(next, offset) = &NS_LIST_NEXT_(entry, offset);
vithyat 0:977e87915078 594 } else {
vithyat 0:977e87915078 595 list->last_nextptr = &NS_LIST_NEXT_(entry, offset);
vithyat 0:977e87915078 596 }
vithyat 0:977e87915078 597
vithyat 0:977e87915078 598 list->first_entry = entry;
vithyat 0:977e87915078 599 }
vithyat 0:977e87915078 600
vithyat 0:977e87915078 601 NS_LIST_FN void ns_list_add_after_(ns_list_t *list, ns_list_offset_t offset, void *current, void *restrict entry)
vithyat 0:977e87915078 602 {
vithyat 0:977e87915078 603 void *next;
vithyat 0:977e87915078 604
vithyat 0:977e87915078 605 NS_LIST_PREV_(entry, offset) = &NS_LIST_NEXT_(current, offset);
vithyat 0:977e87915078 606 NS_LIST_NEXT_(entry, offset) = next = NS_LIST_NEXT_(current, offset);
vithyat 0:977e87915078 607
vithyat 0:977e87915078 608 if (next) {
vithyat 0:977e87915078 609 NS_LIST_PREV_(next, offset) = &NS_LIST_NEXT_(entry, offset);
vithyat 0:977e87915078 610 } else {
vithyat 0:977e87915078 611 list->last_nextptr = &NS_LIST_NEXT_(entry, offset);
vithyat 0:977e87915078 612 }
vithyat 0:977e87915078 613
vithyat 0:977e87915078 614 NS_LIST_NEXT_(current, offset) = entry;
vithyat 0:977e87915078 615 }
vithyat 0:977e87915078 616
vithyat 0:977e87915078 617 NS_LIST_FN void ns_list_add_before_(ns_list_offset_t offset, void *current, void *restrict entry)
vithyat 0:977e87915078 618 {
vithyat 0:977e87915078 619 void **prev_nextptr;
vithyat 0:977e87915078 620
vithyat 0:977e87915078 621 NS_LIST_NEXT_(entry, offset) = current;
vithyat 0:977e87915078 622 NS_LIST_PREV_(entry, offset) = prev_nextptr = NS_LIST_PREV_(current, offset);
vithyat 0:977e87915078 623 *prev_nextptr = entry;
vithyat 0:977e87915078 624 NS_LIST_PREV_(current, offset) = &NS_LIST_NEXT_(entry, offset);
vithyat 0:977e87915078 625 }
vithyat 0:977e87915078 626
vithyat 0:977e87915078 627 NS_LIST_FN void ns_list_add_to_end_(ns_list_t *list, ns_list_offset_t offset, void *restrict entry)
vithyat 0:977e87915078 628 {
vithyat 0:977e87915078 629 void **prev_nextptr;
vithyat 0:977e87915078 630
vithyat 0:977e87915078 631 NS_LIST_NEXT_(entry, offset) = NULL;
vithyat 0:977e87915078 632 NS_LIST_PREV_(entry, offset) = prev_nextptr = list->last_nextptr;
vithyat 0:977e87915078 633 *prev_nextptr = entry;
vithyat 0:977e87915078 634 list->last_nextptr = &NS_LIST_NEXT_(entry, offset);
vithyat 0:977e87915078 635 }
vithyat 0:977e87915078 636
vithyat 0:977e87915078 637 NS_LIST_FN void *ns_list_get_next_(ns_list_offset_t offset, const void *current)
vithyat 0:977e87915078 638 {
vithyat 0:977e87915078 639 return NS_LIST_NEXT_(current, offset);
vithyat 0:977e87915078 640 }
vithyat 0:977e87915078 641
vithyat 0:977e87915078 642 NS_LIST_FN void *ns_list_get_previous_(const ns_list_t *list, ns_list_offset_t offset, const void *current)
vithyat 0:977e87915078 643 {
vithyat 0:977e87915078 644 if (current == list->first_entry) {
vithyat 0:977e87915078 645 return NULL;
vithyat 0:977e87915078 646 }
vithyat 0:977e87915078 647
vithyat 0:977e87915078 648 // Tricky. We don't have a direct previous pointer, but a pointer to the
vithyat 0:977e87915078 649 // pointer that points to us - ie &head->first_entry OR &{prev}->next.
vithyat 0:977e87915078 650 // This makes life easier on insertion and removal, but this is where we
vithyat 0:977e87915078 651 // pay the price.
vithyat 0:977e87915078 652
vithyat 0:977e87915078 653 // We have to check manually for being the first entry above, so we know it's
vithyat 0:977e87915078 654 // a real link's next pointer. Then next is the first field of
vithyat 0:977e87915078 655 // ns_list_link_t, so we can use the normal offset value.
vithyat 0:977e87915078 656
vithyat 0:977e87915078 657 return NS_LIST_ENTRY_(NS_LIST_PREV_(current, offset), offset);
vithyat 0:977e87915078 658 }
vithyat 0:977e87915078 659
vithyat 0:977e87915078 660 NS_LIST_FN void *ns_list_get_last_(const ns_list_t *list, ns_list_offset_t offset)
vithyat 0:977e87915078 661 {
vithyat 0:977e87915078 662 if (!list->first_entry) {
vithyat 0:977e87915078 663 return NULL;
vithyat 0:977e87915078 664 }
vithyat 0:977e87915078 665
vithyat 0:977e87915078 666 // See comments in ns_list_get_previous_()
vithyat 0:977e87915078 667 return NS_LIST_ENTRY_(list->last_nextptr, offset);
vithyat 0:977e87915078 668 }
vithyat 0:977e87915078 669
vithyat 0:977e87915078 670 NS_LIST_FN void ns_list_remove_(ns_list_t *list, ns_list_offset_t offset, void *removed)
vithyat 0:977e87915078 671 {
vithyat 0:977e87915078 672 void *next;
vithyat 0:977e87915078 673 void **prev_nextptr;
vithyat 0:977e87915078 674
vithyat 0:977e87915078 675 next = NS_LIST_NEXT_(removed, offset);
vithyat 0:977e87915078 676 prev_nextptr = NS_LIST_PREV_(removed, offset);
vithyat 0:977e87915078 677 if (next) {
vithyat 0:977e87915078 678 NS_LIST_PREV_(next, offset) = prev_nextptr;
vithyat 0:977e87915078 679 } else {
vithyat 0:977e87915078 680 list->last_nextptr = prev_nextptr;
vithyat 0:977e87915078 681 }
vithyat 0:977e87915078 682 *prev_nextptr = next;
vithyat 0:977e87915078 683
vithyat 0:977e87915078 684 ns_list_link_init_(NS_LIST_LINK_(removed, offset));
vithyat 0:977e87915078 685 }
vithyat 0:977e87915078 686
vithyat 0:977e87915078 687 NS_LIST_FN void ns_list_replace_(ns_list_t *list, ns_list_offset_t offset, void *current, void *restrict replacement)
vithyat 0:977e87915078 688 {
vithyat 0:977e87915078 689 void *next;
vithyat 0:977e87915078 690 void **prev_nextptr;
vithyat 0:977e87915078 691
vithyat 0:977e87915078 692 NS_LIST_PREV_(replacement, offset) = prev_nextptr = NS_LIST_PREV_(current, offset);
vithyat 0:977e87915078 693 NS_LIST_NEXT_(replacement, offset) = next = NS_LIST_NEXT_(current, offset);
vithyat 0:977e87915078 694
vithyat 0:977e87915078 695 if (next) {
vithyat 0:977e87915078 696 NS_LIST_PREV_(next, offset) = &NS_LIST_NEXT_(replacement, offset);
vithyat 0:977e87915078 697 } else {
vithyat 0:977e87915078 698 list->last_nextptr = &NS_LIST_NEXT_(replacement, offset);
vithyat 0:977e87915078 699 }
vithyat 0:977e87915078 700 *prev_nextptr = replacement;
vithyat 0:977e87915078 701
vithyat 0:977e87915078 702 ns_list_link_init_(NS_LIST_LINK_(current, offset));
vithyat 0:977e87915078 703 }
vithyat 0:977e87915078 704
vithyat 0:977e87915078 705 NS_LIST_FN void ns_list_concatenate_(ns_list_t *dst, ns_list_t *src, ns_list_offset_t offset)
vithyat 0:977e87915078 706 {
vithyat 0:977e87915078 707 ns_list_link_t *src_first;
vithyat 0:977e87915078 708
vithyat 0:977e87915078 709 src_first = src->first_entry;
vithyat 0:977e87915078 710 if (!src_first) {
vithyat 0:977e87915078 711 return;
vithyat 0:977e87915078 712 }
vithyat 0:977e87915078 713
vithyat 0:977e87915078 714 *dst->last_nextptr = src_first;
vithyat 0:977e87915078 715 NS_LIST_PREV_(src_first, offset) = dst->last_nextptr;
vithyat 0:977e87915078 716 dst->last_nextptr = src->last_nextptr;
vithyat 0:977e87915078 717
vithyat 0:977e87915078 718 ns_list_init_(src);
vithyat 0:977e87915078 719 }
vithyat 0:977e87915078 720
vithyat 0:977e87915078 721 NS_LIST_FN uint_fast16_t ns_list_count_(const ns_list_t *list, ns_list_offset_t offset)
vithyat 0:977e87915078 722 {
vithyat 0:977e87915078 723 uint_fast16_t count = 0;
vithyat 0:977e87915078 724
vithyat 0:977e87915078 725 for (void *p = list->first_entry; p; p = NS_LIST_NEXT_(p, offset)) {
vithyat 0:977e87915078 726 count++;
vithyat 0:977e87915078 727 }
vithyat 0:977e87915078 728
vithyat 0:977e87915078 729 return count;
vithyat 0:977e87915078 730 }
vithyat 0:977e87915078 731 #endif /* defined NS_ALLOW_INLINING || defined NS_LIST_FN */
vithyat 0:977e87915078 732
vithyat 0:977e87915078 733 #ifdef __cplusplus
vithyat 0:977e87915078 734 }
vithyat 0:977e87915078 735 #endif
vithyat 0:977e87915078 736
vithyat 0:977e87915078 737 #endif /* NS_LIST_H_ */
vithyat 0:977e87915078 738