1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
#ifndef LL_H__
#define LL_H__
/*
* Copyright 2022 Embedded Artistry LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <stdint.h>
#include <stdlib.h> //size_t, NULL
/** @defgroup linkedlist-C C Linked List Interface
* A linked list library for C modules
*
* @ingroup FrameworkUtils
* @{
*/
/**
* Define offsetof if we don't have it already
*/
#ifndef offsetof
#ifdef __compiler_offsetof
#define offsetof(TYPE, MEMBER) __compiler_offsetof(TYPE, MEMBER)
#else
#define offsetof(TYPE, MEMBER) ((size_t) & ((TYPE*)0)->MEMBER)
#endif
#endif // offsetof
/**
* Define container_of if we don't have it already
*/
#ifndef container_of
#ifdef __GNUC__
#ifndef __clang__
// Isolate the GNU-specific expression
#define container_of(ptr, type, member) \
({ \
const __typeof__(((type*)0)->member)* __mptr = (ptr); \
(type*)((uintptr_t)__mptr - offsetof(type, member)); \
})
#else // we are clang - avoid GNU expression
#define container_of(ptr, type, member) ((type*)((uintptr_t)(ptr)-offsetof(type, member)))
#endif // GNU and not clang
#else
#define container_of(ptr, type, member) ((type*)((uintptr_t)(ptr)-offsetof(type, member)))
#endif // not GNU
#endif // container_of
#ifdef __cplusplus
extern "C" {
#endif //__cplusplus
/** Linked list struct
*
* This is a doubly linked list structure.
* The ll_t structure should be embedded in a container structure that you want to list.
*
* Example:
*
* @code
* typedef struct
* {
* ll_t node;
* size_t size;
* char* block;
* } alloc_node_t;
* @endcode
*/
typedef struct ll_head
{
/// Pointer to the next element in the list.
struct ll_head* next;
/// Pointer to the previous element in the list.
struct ll_head* prev;
} ll_t;
/// @name Get Containers
/// @{
/** Get the container for a list entry
*
* @param[in] ptr The pointer to the target ll_t node.
* @param[in] type The struct type which contains the ll_t node. For this example struct,
* type would refer to alloc_node_t:
* @code
* typedef struct
* {
* ll_t node;
* size_t size;
* char* block;
* } alloc_node_t;
* @endcode
*
* @param[in] member The member which corresponds to the member name of the ll_t entry. For this
* example struct, member would refer to `node`.
* @code
* typedef struct
* {
* ll_t node;
* size_t size;
* char* block;
* } alloc_node_t;
* @endcode
*
* @returns a pointer to the struct containing the linked list node at `ptr`, cast to type `type`.
*/
#define list_entry(ptr, type, member) container_of(ptr, type, member)
/** Get the container for the first item in the list
*
* @param[in] head The pointer to the head of the list.
* @param[in] type The struct type which contains the ll_t node. For this example struct,
* type would refer to alloc_node_t:
* @code
* typedef struct
* {
* ll_t node;
* size_t size;
* char* block;
* } alloc_node_t;
* @endcode
* @param[in] member The member which corresponds to the member name of the ll_t entry. For this
* example struct, member would refer to `node`.
* @code
* typedef struct
* {
* ll_t node;
* size_t size;
* char* block;
* } alloc_node_t;
* @endcode
*
* @returns a pointer to the struct containing the linked list node at `ptr`, cast to type `type`.
*/
#define list_first_entry(head, type, member) list_entry((head)->next, type, member)
/// @}
// Get containers
/// @name Foreach Operations
/// @{
/** Declare a foreach loop which iterates over the list
*
* list_for_each() will run as long as the current object's next pointer is not equal to the
* head of the list. It's possible for a malformed list to loop forever.
*
* @param[in] pos The variable which will hold the current iteration's position value.
* This variable must be a pointer and should be pre-declared before instantiating the loop.
* @code
* ll_t *b;
* list_for_each(b, &free_list)
* {
* ...
* }
* @endcode
* @param[in] head The head of the linked list. Input should be a pointer.
*/
#define list_for_each(pos, head) for(pos = (head)->next; pos != (head); pos = pos->next)
/** Declare a foreach loop which iterates over the list, copy current node pointer.
*
* list_for_each_safe() will run as long as the current object's next pointer is not equal to the
* head of the list. It's possible for a malformed list to loop forever.
*
* The list_for_each_safe() variant makes a copy of the current node pointer, enabling the loop
* to get to the next pointer if there is a deletion.
*
* @param[in] pos The variable which will hold the current iteration's position value.
* This variable must be a pointer should be pre-declared before instantiating the loop.
* @code
* ll_t *b, *t;
* list_for_each_safe(b, t, &free_list)
* {
* ...
* }
* @endcode
* @param[in] n The variable which will hold the current iteration's position value **copy**.
* This variable must be a pointer and should be pre-declared before instantiating the loop.
* @code
* alloc_node_t *b, *t;
* list_for_each_safe(b, t, &free_list)
* {
* ...
* }
* @endcode
* @param[in] head The head of the linked list. Input should be a pointer.
*/
#define list_for_each_safe(pos, n, head) \
for(pos = (head)->next, n = pos->next; pos != (head); pos = n, n = pos->next)
/** Declare a for loop which operates on each node in the list using the container value.
*
* @param[in] pos The variable which will hold the current iteration's position value.
* This variable must be a pointer and should be pre-declared before instantiating the loop.
* The `pos` variable must be the container type.
* @code
* alloc_node_t *b, *t;
* list_for_each_entry(b, &free_list, node)
* {
* ...
* }
* @endcode
*
* @param[in] head The head of the linked list. Input should be a pointer.
*
* @param[in] member The member which corresponds to the member name of the ll_t entry. For this
* example struct, member would refer to `node`.
* @code
* typedef struct
* {
* ll_t node;
* size_t size;
* char* block;
* } alloc_node_t;
* @endcode
*/
#define list_for_each_entry(pos, head, member) \
for(pos = list_entry((head)->next, __typeof__(*pos), member); &pos->member != (head); \
pos = list_entry(pos->member.next, __typeof__(*pos), member))
/** Declare a for loop which operates on each node in the list using a copy of the container value.
*
* @param[in] pos The variable which will hold the current iteration's position value.
* This variable must be a pointer and should be pre-declared before instantiating the loop.
* The `pos` variable must be the container type.
* @code
* alloc_node_t *b, *t;
* list_for_each_entry(b, &free_list, node)
* {
* ...
* }
* @endcode
* @param[in] n The variable which will hold the current iteration's position value **copy**.
* This variable must be a pointer and should be pre-declared before instantiating the loop.
* The `n` variable must be the container type.
* @code
* typedef struct
* {
* ll_t node;
* size_t size;
* char* block;
* } alloc_node_t;
*
* alloc_node_t *b, *t;
* list_for_each_entrysafe(b, t, &free_list, node)
* {
* ...
* }
* @endcode
* @param[in] head The head of the linked list. Input should be a pointer.
* @param[in] member The member which corresponds to the member name of the ll_t entry. For this
* example struct, member would refer to `node`.
* @code
* typedef struct
* {
* ll_t node;
* size_t size;
* char* block;
* } alloc_node_t;
* @endcode
*/
#define list_for_each_entry_safe(pos, n, head, member) \
for(pos = list_entry((head)->next, __typeof__(*pos), member), \
n = list_entry(pos->member.next, __typeof__(*pos), member); \
&pos->member != (head); pos = n, n = list_entry(n->member.next, __typeof__(*n), member))
/// @}
// End foreach
/// @name Initialization
/// @{
/// Initialize a linked list so it points to itself
/// @param[in] name of the linked list object
#define ll_head_INIT(name) \
{ \
&(name), &(name) \
}
// Added by BenC
static inline void INIT_LIST_HEAD(ll_t *list)
{
list->next = list;
list->prev = list;
}
/** Initialize a linked list
*
* @code
* // This macro declares and initializes our linked list
* static LIST_INIT(free_list);
* @endcode
* @param[in] name The name of the linked list object to declare
*/
#define LIST_INIT(name) struct ll_head name = ll_head_INIT(name)
/// @}
/// @name Addition
/// @{
/// Insert a new element between two existing elements.
/// @param[in] n The node to add to the list.
/// @param[in] prev The pointer to the node before where the new node will be inserted.
/// @param[in] next The pointer to the new node after where the new node will be inserted.
static inline void list_insert(struct ll_head* n, struct ll_head* prev, struct ll_head* next)
{
next->prev = n;
n->next = next;
n->prev = prev;
prev->next = n;
}
/// Add a node to the front of the list
/// @param[in] n The node to add to the list.
/// @param[in] head The head of the list.
static inline void list_add(struct ll_head* n, struct ll_head* head)
{
list_insert(n, head, head->next);
}
/// Add a node to the end of the list
/// @param[in] n The node to add to the list.
/// @param[in] head The head of the list.
static inline void list_add_tail(struct ll_head* n, struct ll_head* head)
{
list_insert(n, head->prev, head);
}
/// @}
/// @name Deletion
/// @{
/// Remove the node between two element pointers.
///
/// Joins the `prev` and `next` elements together, effectively removing
/// the element in the middle.
///
/// @param[in] prev The previous element in the list, which will now be joined to next.
/// @param[in] next The next element in the list, which will now be joined to prev.
static inline void list_join_nodes(struct ll_head* prev, struct ll_head* next)
{
next->prev = prev;
prev->next = next;
}
/// Remove an entry from the list
/// @param[in] entry The pointer to the entry to remove from the list.
static inline void list_del(struct ll_head* entry)
{
list_join_nodes(entry->prev, entry->next);
entry->next = NULL;
entry->prev = NULL;
}
/// @}
/// @}
// end group
#ifdef __cplusplus
}
#endif //__cplusplus
#endif // LL_H__
|