PatchworkOS  19e446b
A non-POSIX operating system.
Loading...
Searching...
No Matches
Doubly linked list

Doubly linked list header. More...

Collaboration diagram for Doubly linked list:

Detailed Description

Doubly linked list header.

The sys/list.h header implements a intrusive doubly linked list.

Given a entry within a structure, the CONTAINER_OF() macro can be used to get a pointer to the structure from the list entry pointer.

Warning
If a list is protected with RCU, the list_*_rcu() functions must be used.

Data Structures

struct  list_entry_t
 A entry in a doubly linked list. More...
 
struct  list_t
 A doubly linked list. More...
 

Macros

#define LIST_FOR_EACH(elem, list, member)
 Iterates over a list.
 
#define LIST_FOR_EACH_SAFE(elem, temp, list, member)
 Safely iterates over a list, allowing for element removal during iteration.
 
#define LIST_FOR_EACH_REVERSE(elem, list, member)
 Iterates over a list in reverse.
 
#define LIST_FOR_EACH_FROM(elem, start, list, member)
 Iterates over a list starting from a specific element.
 
#define LIST_FOR_EACH_FROM_REVERSE(elem, start, list, member)
 Iterates over a list in reverse order starting from a specific element.
 
#define LIST_FOR_EACH_TO(elem, end, list, member)
 Iterates over a list up to a specific element.
 
#define LIST_FOR_EACH_TO_REVERSE(elem, end, list, member)
 Iterates over a list in reverse order up to a specific element.
 
#define LIST_ENTRY_CREATE(name)
 Creates a list entry initializer.
 
#define LIST_CREATE(name)
 Creates a list initializer.
 

Typedefs

typedef struct list list_t
 

Functions

static void list_entry_init (list_entry_t *entry)
 Initializes a list entry.
 
static void list_init (list_t *list)
 Initializes a list.
 
static bool list_entry_in_list (list_entry_t *entry)
 Check if an entry is in a list.
 
static bool list_is_empty (list_t *list)
 Checks if a list is empty.
 
static void list_add (list_entry_t *prev, list_entry_t *next, list_entry_t *entry)
 Adds a new element between two existing list entries.
 
static void list_add_rcu (list_entry_t *prev, list_entry_t *next, list_entry_t *entry)
 Adds a new element between two existing list entries in a RCU-safe manner.
 
static void list_append (list_entry_t *prev, list_entry_t *entry)
 Appends an entry to the list.
 
static void list_prepend (list_entry_t *head, list_entry_t *entry)
 Prepends an entry to the list.
 
static void list_remove (list_entry_t *entry)
 Removes a list entry from its current list.
 
static void list_remove_rcu (list_entry_t *entry)
 Removes a list entry from its current list in a RCU-safe manner.
 
static void list_push_back (list_t *list, list_entry_t *entry)
 Pushes an entry to the end of the list.
 
static void list_push_back_rcu (list_t *list, list_entry_t *entry)
 Pushes an entry to the end of the list in a RCU-safe manner.
 
static void list_push_front (list_t *list, list_entry_t *entry)
 Pushes an entry to the front of the list.
 
static list_entry_tlist_pop_front (list_t *list)
 Pops the first entry from the list.
 
static list_entry_tlist_pop_back (list_t *list)
 Pops the last entry from the list.
 
static list_entry_tlist_first (list_t *list)
 Gets the first entry in the list without removing it.
 
static list_entry_tlist_last (list_t *list)
 Gets the last entry in the list without removing it.
 
static list_entry_tlist_next (list_t *list, list_entry_t *entry)
 Gets the next entry in the list relative to a given entry.
 
static list_entry_tlist_prev (list_t *list, list_entry_t *entry)
 Gets the previous entry in the list relative to a given entry.
 
static uint64_t list_size (list_t *list)
 Gets the size of the list.
 

Macro Definition Documentation

◆ LIST_FOR_EACH

#define LIST_FOR_EACH (   elem,
  list,
  member 
)
Value:
for ((elem) = CONTAINER_OF((list)->head.next, typeof(*elem), member); &(elem)->member != &((list)->head); \
(elem) = CONTAINER_OF((elem)->member.next, typeof(*elem), member))
#define CONTAINER_OF(ptr, type, member)
Container of macro.

Iterates over a list.

Parameters
elemThe loop variable, a pointer to the structure containing the list entry.
listA pointer to the list_t structure to iterate over.
memberThe name of the list_entry_t member within the structure elem.

Definition at line 58 of file list.h.

◆ LIST_FOR_EACH_SAFE

#define LIST_FOR_EACH_SAFE (   elem,
  temp,
  list,
  member 
)
Value:
for ((elem) = CONTAINER_OF((list)->head.next, typeof(*elem), member), \
(temp) = CONTAINER_OF((elem)->member.next, typeof(*elem), member); \
&(elem)->member != &((list)->head); \
(elem) = (temp), (temp) = CONTAINER_OF((elem)->member.next, typeof(*elem), member))

Safely iterates over a list, allowing for element removal during iteration.

The LIST_FOR_EACH_SAFE() macro is similar to LIST_FOR_EACH() but uses a temporary variable to store the next element, making it safe to remove the current element during iteration.

Parameters
elemThe loop variable, a pointer to the structure containing the list entry.
tempA temporary loop variable, a pointer to the structure containing the next list entry.
listA pointer to the list_t structure to iterate over.
memberThe name of the list_entry_t member within the structure elem.

Definition at line 73 of file list.h.

◆ LIST_FOR_EACH_REVERSE

#define LIST_FOR_EACH_REVERSE (   elem,
  list,
  member 
)
Value:
for ((elem) = CONTAINER_OF((list)->head.prev, typeof(*elem), member); &(elem)->member != &((list)->head); \
(elem) = CONTAINER_OF((elem)->member.prev, typeof(*elem), member))

Iterates over a list in reverse.

Parameters
elemThe loop variable, a pointer to the structure containing the list entry.
listA pointer to the list_t structure to iterate over.
memberThe name of the list_entry_t member within the structure elem.

Definition at line 86 of file list.h.

◆ LIST_FOR_EACH_FROM

#define LIST_FOR_EACH_FROM (   elem,
  start,
  list,
  member 
)
Value:
for ((elem) = CONTAINER_OF(start, typeof(*elem), member); &(elem)->member != &((list)->head); \
(elem) = CONTAINER_OF((elem)->member.next, typeof(*elem), member))
static void start()
Definition main.c:542

Iterates over a list starting from a specific element.

The LIST_FOR_EACH_FROM() macro iterates from a specific element, inclusive, until the end of the list.

Parameters
elemThe loop variable, a pointer to the structure containing the list entry.
startA pointer to the list_entry_t from which to start iteration.
listA pointer to the list_t structure to iterate over.
memberThe name of the list_entry_t member within the structure elem.

Definition at line 100 of file list.h.

◆ LIST_FOR_EACH_FROM_REVERSE

#define LIST_FOR_EACH_FROM_REVERSE (   elem,
  start,
  list,
  member 
)
Value:
for ((elem) = CONTAINER_OF(start, typeof(*elem), member); &(elem)->member != &((list)->head); \
(elem) = CONTAINER_OF((elem)->member.prev, typeof(*elem), member))

Iterates over a list in reverse order starting from a specific element.

Parameters
elemThe loop variable, a pointer to the structure containing the list entry.
startA pointer to the list_entry_t from which to start reverse iteration.
listA pointer to the list_t structure to iterate over.
memberThe name of the list_entry_t member within the structure elem.

Definition at line 112 of file list.h.

◆ LIST_FOR_EACH_TO

#define LIST_FOR_EACH_TO (   elem,
  end,
  list,
  member 
)
Value:
for ((elem) = CONTAINER_OF((list)->head.next, typeof(*elem), member); \
&(elem)->member != &((list)->head) && &(elem)->member != (end); \
(elem) = CONTAINER_OF((elem)->member.next, typeof(*elem), member))

Iterates over a list up to a specific element.

The LIST_FOR_EACH_TO() macro iterates from the start of the list, inclusive, until a specified element, not inclusive.

Parameters
elemThe loop variable, a pointer to the structure containing the list entry.
endA pointer to the list_entry_t at which to stop iteration (exclusive).
listA pointer to the list_t structure to iterate over.
memberThe name of the list_entry_t member within the structure elem.

Definition at line 127 of file list.h.

◆ LIST_FOR_EACH_TO_REVERSE

#define LIST_FOR_EACH_TO_REVERSE (   elem,
  end,
  list,
  member 
)
Value:
for ((elem) = CONTAINER_OF((list)->head.prev, typeof(*elem), member); \
&(elem)->member != &((list)->head) && &(elem)->member != (end); \
(elem) = CONTAINER_OF((elem)->member.prev, typeof(*elem), member))

Iterates over a list in reverse order up to a specific element.

Parameters
elemThe loop variable, a pointer to the structure containing the list entry.
endA pointer to the list_entry_t at which to stop reverse iteration (exclusive).
listA pointer to the list_t structure to iterate over.
memberThe name of the list_entry_t member within the structure elem.

Definition at line 140 of file list.h.

◆ LIST_ENTRY_CREATE

#define LIST_ENTRY_CREATE (   name)
Value:
{ \
.prev = &(name), .next = &(name), \
}
static atomic_long next
Definition main.c:12
A entry in a doubly linked list.
Definition list.h:37

Creates a list entry initializer.

Parameters
nameThe name of the entry variable to initialize.
Returns
A list_entry_t initializer for the specified entry variable.

Definition at line 151 of file list.h.

◆ LIST_CREATE

#define LIST_CREATE (   name)
Value:
{ \
.head = {.prev = &(name).head, .next = &(name).head } \
}

Creates a list initializer.

Parameters
nameThe name of the list variable to initialize.
Returns
A list_t initializer for the specified list variable.

Definition at line 163 of file list.h.

Typedef Documentation

◆ list_t

typedef struct list list_t

Definition at line 27 of file list.h.

Function Documentation

◆ list_entry_init()

static void list_entry_init ( list_entry_t entry)
inlinestatic

Initializes a list entry.

Parameters
entryA pointer to the list_entry_t to initialize.

Definition at line 173 of file list.h.

Here is the caller graph for this function:

◆ list_init()

static void list_init ( list_t list)
inlinestatic

Initializes a list.

Parameters
listA pointer to the list_t to initialize.

Definition at line 185 of file list.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ list_entry_in_list()

static bool list_entry_in_list ( list_entry_t entry)
inlinestatic

Check if an entry is in a list.

Parameters
entryA pointer to the list_entry_t to check.
Returns
true if the entry is in a list, false otherwise.

Definition at line 197 of file list.h.

◆ list_is_empty()

static bool list_is_empty ( list_t list)
inlinestatic

Checks if a list is empty.

Parameters
listA pointer to the list_t to check.
Returns
true if the list is empty, false otherwise.

Definition at line 210 of file list.h.

Here is the caller graph for this function:

◆ list_add()

static void list_add ( list_entry_t prev,
list_entry_t next,
list_entry_t entry 
)
inlinestatic

Adds a new element between two existing list entries.

Parameters
prevA pointer to the list entry that will precede the new element.
nextA pointer to the list entry that will follow the new element.
elemA pointer to the list_entry_t to add.

Definition at line 224 of file list.h.

Here is the caller graph for this function:

◆ list_add_rcu()

static void list_add_rcu ( list_entry_t prev,
list_entry_t next,
list_entry_t entry 
)
inlinestatic

Adds a new element between two existing list entries in a RCU-safe manner.

Parameters
prevA pointer to the list entry that will precede the new element.
nextA pointer to the list entry that will follow the new element.
elemA pointer to the list_entry_t to add.

Definition at line 245 of file list.h.

Here is the caller graph for this function:

◆ list_append()

static void list_append ( list_entry_t prev,
list_entry_t entry 
)
inlinestatic

Appends an entry to the list.

Parameters
prevA pointer to the list entry after which the new entry will be appended.
entryA pointer to the list_entry_t to append.

Definition at line 268 of file list.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ list_prepend()

static void list_prepend ( list_entry_t head,
list_entry_t entry 
)
inlinestatic

Prepends an entry to the list.

Parameters
listA pointer to the list_t to prepend to.
headA pointer to the list entry before which the new entry will be prepended.
entryA pointer to the list_entry_t to prepend.

Definition at line 280 of file list.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ list_remove()

static void list_remove ( list_entry_t entry)
inlinestatic

Removes a list entry from its current list.

Parameters
entryA pointer to the list_entry_t to remove.

Definition at line 290 of file list.h.

Here is the call graph for this function:

◆ list_remove_rcu()

static void list_remove_rcu ( list_entry_t entry)
inlinestatic

Removes a list entry from its current list in a RCU-safe manner.

Warning
After calling this function the entry will still be connected to the list, but iteration over the list will not find it.
Parameters
entryA pointer to the list_entry_t to remove.

Definition at line 307 of file list.h.

Here is the caller graph for this function:

◆ list_push_back()

static void list_push_back ( list_t list,
list_entry_t entry 
)
inlinestatic

Pushes an entry to the end of the list.

Parameters
listA pointer to the list_t to push the entry to.
entryA pointer to the list_entry_t to push.

Definition at line 322 of file list.h.

Here is the call graph for this function:

◆ list_push_back_rcu()

static void list_push_back_rcu ( list_t list,
list_entry_t entry 
)
inlinestatic

Pushes an entry to the end of the list in a RCU-safe manner.

Parameters
listA pointer to the list_t to push the entry to.
entryA pointer to the list_entry_t to push.

Definition at line 337 of file list.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ list_push_front()

static void list_push_front ( list_t list,
list_entry_t entry 
)
inlinestatic

Pushes an entry to the front of the list.

Parameters
listA pointer to the list_t to push the entry to.
entryA pointer to the list_entry_t to push.

Definition at line 351 of file list.h.

Here is the call graph for this function:

◆ list_pop_front()

static list_entry_t * list_pop_front ( list_t list)
inlinestatic

Pops the first entry from the list.

Parameters
listA pointer to the list_t to pop the entry from.
Returns
A pointer to the removed list_entry_t, or NULL if the list is empty.

Definition at line 366 of file list.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ list_pop_back()

static list_entry_t * list_pop_back ( list_t list)
inlinestatic

Pops the last entry from the list.

Parameters
listA pointer to the list_t to pop the entry from.
Returns
A pointer to the removed list_entry_t, or NULL if the list is empty.

Definition at line 386 of file list.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ list_first()

static list_entry_t * list_first ( list_t list)
inlinestatic

Gets the first entry in the list without removing it.

Parameters
listA pointer to the list_t.
Returns
A pointer to the first list_entry_t in the list, or NULL if the list is empty.

Definition at line 406 of file list.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ list_last()

static list_entry_t * list_last ( list_t list)
inlinestatic

Gets the last entry in the list without removing it.

Parameters
listA pointer to the list_t.
Returns
A pointer to the last list_entry_t in the list, or NULL if the list is empty.

Definition at line 423 of file list.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ list_next()

static list_entry_t * list_next ( list_t list,
list_entry_t entry 
)
inlinestatic

Gets the next entry in the list relative to a given entry.

Parameters
listA pointer to the list_t.
entryA pointer to the list_entry_t to get the next entry from.
Returns
A pointer to the next list_entry_t in the list, or NULL if the given entry is the last.

Definition at line 441 of file list.h.

◆ list_prev()

static list_entry_t * list_prev ( list_t list,
list_entry_t entry 
)
inlinestatic

Gets the previous entry in the list relative to a given entry.

Parameters
listA pointer to the list_t.
entryA pointer to the list_entry_t to get the previous entry from.
Returns
A pointer to the previous list_entry_t in the list, or NULL if the given entry is the first.

Definition at line 460 of file list.h.

◆ list_size()

static uint64_t list_size ( list_t list)
inlinestatic

Gets the size of the list.

Parameters
listA pointer to the list_t.
Returns
The number of entries in the list.

Definition at line 478 of file list.h.

Here is the caller graph for this function: