CAPS Universe documentation  1.0.4
All you need to know to be successful
Macros
list.h File Reference

Double linked list implementation in a single header file. More...

Go to the source code of this file.

Macros

#define LIST_HEAD(t, name)
 
#define LIST_FIELDS(t, name)
 
#define LIST_HEAD_INIT(head)
 
#define LIST_INIT(name, item)
 
#define LIST_PREPEND(name, head, item)
 
#define LIST_APPEND(name, head, item)
 
#define LIST_REMOVE(name, head, item)
 
#define LIST_FIND_HEAD(name, item, head)
 
#define LIST_FIND_TAIL(name, item, tail)
 
#define LIST_INSERT_AFTER(name, head, a, b)
 
#define LIST_INSERT_BEFORE(name, head, a, b)
 
#define LIST_JUST_US(name, item)
 
#define LIST_FOREACH(name, i, head)
 
#define LIST_FOREACH_SAFE(name, i, n, head)
 
#define LIST_FOREACH_BEFORE(name, i, p)
 
#define LIST_FOREACH_AFTER(name, i, p)
 
#define LIST_FOREACH_OTHERS(name, i, p)
 
#define LIST_LOOP_BUT_ONE(name, i, head, p)
 
#define LIST_IS_EMPTY(head)
 

Detailed Description

Stolen from the systemd project

Macro Definition Documentation

◆ LIST_HEAD

#define LIST_HEAD (   t,
  name 
)

The head of the linked list. Use this in the structure that shall contain the head of the linked list

◆ LIST_FIELDS

#define LIST_FIELDS (   t,
  name 
)

The pointers in the linked list's items. Use this in the item structure

◆ LIST_HEAD_INIT

#define LIST_HEAD_INIT (   head)

Initialize the list's head

Parameters
[in,out]headLinked list begin variable

◆ LIST_INIT

#define LIST_INIT (   name,
  item 
)

Initialize a list item

Parameters
[in]nameThe name of the linked list member in the linked structure
[in,out]itemThe to be initialized item

◆ LIST_PREPEND

#define LIST_PREPEND (   name,
  head,
  item 
)

Prepend an item to the list

Parameters
[in]nameThe name of the linked list member in the linked structure
[in,out]headLinked list begin variable
[in,out]itemThe to be prepended item

◆ LIST_APPEND

#define LIST_APPEND (   name,
  head,
  item 
)

Append an item to the list

Parameters
[in]nameThe name of the linked list member in the linked structure
[in,out]headLinked list begin variable
[in,out]itemThe to be appended item

If the linked list is empty, head gets modified.

◆ LIST_REMOVE

#define LIST_REMOVE (   name,
  head,
  item 
)

Remove an item from the list

Parameters
[in]nameThe name of the linked list member in the linked structure
[in,out]headLinked list begin variable
[in]itemThe to be removed member

◆ LIST_FIND_HEAD

#define LIST_FIND_HEAD (   name,
  item,
  head 
)

Find the head of a list

Parameters
[in]nameThe name of the linked list member in the linked structure
[in]itemMember to start the search
[in]headWhere to store the pointer to the head element
const struct A *list_head;
struct A *some_element_in_the_list;
LIST_FIND_HEAD(list, some_element_in_the_list, list_head)
#define LIST_FIND_HEAD(name, item, head)
Definition: list.h:113

◆ LIST_FIND_TAIL

#define LIST_FIND_TAIL (   name,
  item,
  tail 
)

Find the tail of a list

Parameters
[in]nameThe name of the linked list member in the linked structure
[in]itemMember to start the search
[in]tailWhere to store the pointer to the tail element
const struct A *list_tail;
struct A *some_element_in_the_list;
LIST_FIND_HEAD(list, some_element_in_the_list, list_tail);

◆ LIST_INSERT_AFTER

#define LIST_INSERT_AFTER (   name,
  head,
  a,
 
)

Insert an item after another one (a = where, b = what)

◆ LIST_INSERT_BEFORE

#define LIST_INSERT_BEFORE (   name,
  head,
  a,
 
)

Insert an item before another one (a = where, b = what)

◆ LIST_JUST_US

#define LIST_JUST_US (   name,
  item 
)

Check if the given linked list member is the only member

Parameters
[in]nameThe name of the linked list member in the linked structure
[in]itemThe known member
Return values
trueIf this member is the only member
falseThere are more than this member in the linked list

◆ LIST_FOREACH

#define LIST_FOREACH (   name,
  i,
  head 
)

Iterate over all elements of a linked list beginning with head

Parameters
[in]nameThe name of the linked list member in the linked structure
[out]iA variable to receive a pointer to the current linked structure
[in]headWhere everything starts
Attention
This macro creates code to iterate over the list in a read-only manner, e.g. you cannot change the current element. If you want to, refer LIST_FOREACH_SAFE() instead

Example: struct A is the linked list. It has one member defined via LIST_FIELDS, LIST_FIELDS(struct A, list);

To iterate over this list:

const struct A *current;
LIST_FOREACH(list, current, some_first_element_var)
{ read from current; }
#define LIST_FOREACH(name, i, head)
Definition: list.h:229

◆ LIST_FOREACH_SAFE

#define LIST_FOREACH_SAFE (   name,
  i,
  n,
  head 
)

Iterate safely over all elements of a linked list beginning with head

Parameters
[in]nameThe name of the linked list member in the linked structure
[out]iA variable to receive a pointer to the current linked structure
[out]nA variable to receive a pointer to the next linked structure
[in]headWhere everything starts
Note
Safely means you can modify the current element (deleting for example) and this loop will still continue

Example: struct A is the linked list. It has one member defined via LIST_FIELDS, LIST_FIELDS(struct A, list);

To iterate over this list:

struct A *current;
struct A *next;
LIST_FOREACH_SAFE(list, current, next, some_first_element_var)
{ remove current from list; }
#define LIST_FOREACH_SAFE(name, i, n, head)
Definition: list.h:253

◆ LIST_FOREACH_BEFORE

#define LIST_FOREACH_BEFORE (   name,
  i,
 
)

Iterate backwards over the list, beginning with the given member

Parameters
[in]nameThe name of the linked list member in the linked structure
[out]iA variable to receive a pointer to the current linked structure
[in]pA member in the middle of the list
Attention
This macro creates code to iterate over the list in a read-only manner, e.g. you cannot change the current element.

◆ LIST_FOREACH_AFTER

#define LIST_FOREACH_AFTER (   name,
  i,
 
)

Iterate forwards over the list, beginning with the given member

Parameters
[in]nameThe name of the linked list member in the linked structure
[out]iA variable to receive a pointer to the current linked structure
[in]pA member in the middle of the list
Attention
This macro creates code to iterate over the list in a read-only manner, e.g. you cannot change the current element.

◆ LIST_FOREACH_OTHERS

#define LIST_FOREACH_OTHERS (   name,
  i,
 
)

Iterate through all the members of the list p is included in, but skip over p

Parameters
[in]nameThe name of the linked list member in the linked structure
[out]iA variable to receive a pointer to the current linked structure
[in]pA member in the middle of the list

◆ LIST_LOOP_BUT_ONE

#define LIST_LOOP_BUT_ONE (   name,
  i,
  head,
 
)

Loop starting from p->next until p->prev. p can be adjusted meanwhile.

◆ LIST_IS_EMPTY

#define LIST_IS_EMPTY (   head)

Check if the linked list is empty

Parameters
[in]headStart of the list
Return values
trueLinked list is empty
falseLinked list is filled