data-structures-c
Loading...
Searching...
No Matches
list.h File Reference

Intrusive circular doubly linked list. More...

#include "container_of.h"
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>

Go to the source code of this file.

Classes

struct  list_node
 Intrusive list node structure. More...
 

Macros

#define list_node_entry(ptr, type, member)
 Obtain a pointer to the struct that contains the list node as a member.
 

Functions

static void list_node_init (struct list_node *node_ptr)
 Initialize a list node.
 
static bool list_node_is_first (const struct list_node *node_ptr, const struct list_node *head_ptr)
 Check if a given list node is first in the list (aka after the head).
 
static bool list_node_is_last (const struct list_node *node_ptr, const struct list_node *tail_ptr)
 Check if a given list node is the last of the list (aka before the tail).
 
static bool list_node_is_head (const struct list_node *node_ptr, const struct list_node *head_ptr)
 Check if a given list node is the head of the list.
 
static bool list_node_is_tail (const struct list_node *node_ptr, const struct list_node *tail_ptr)
 Check if a given list node is the tail of the list.
 
static void list_node_add_after (struct list_node *node_ptr, struct list_node *prev_ptr)
 Add a node after the given node.
 
static void list_node_add_before (struct list_node *node_ptr, struct list_node *next_ptr)
 Add a node before the given node.
 
static struct list_nodelist_node_remove (struct list_node *node_ptr)
 Remove a node and deattach it from the list it resides in.
 
static void list_node_replace (struct list_node *old_ptr, struct list_node *new_ptr)
 Replace a given node by a new node.
 

Detailed Description

Intrusive circular doubly linked list.

Inspired by:

Macro Definition Documentation

◆ list_node_entry

#define list_node_entry ( ptr,
type,
member )
Value:
container_of(ptr, type, member)
#define container_of(ptr, type, member)
Obtain a pointer to the struct that contains the member.
Definition container_of.h:34

Obtain a pointer to the struct that contains the list node as a member.

Parameters
[in]ptrNode pointer.
[in]typeContainer type.
[in]memberNode member name.
Returns
A pointer to the struct containing the node member.

Function Documentation

◆ list_node_add_after()

static void list_node_add_after ( struct list_node * node_ptr,
struct list_node * prev_ptr )
inlinestatic

Add a node after the given node.

Note
This can be used to construct a stack.
Parameters
[in]prev_ptrThe prev node pointer.
[in]node_ptrThe node pointer.

◆ list_node_add_before()

static void list_node_add_before ( struct list_node * node_ptr,
struct list_node * next_ptr )
inlinestatic

Add a node before the given node.

Note
This can be used to construct a queue.
Parameters
[in]next_ptrThe next node pointer.
[in]node_ptrThe node pointer.

◆ list_node_init()

static void list_node_init ( struct list_node * node_ptr)
inlinestatic

Initialize a list node.

Parameters
[in]node_ptrThe node pointer.

◆ list_node_is_first()

static bool list_node_is_first ( const struct list_node * node_ptr,
const struct list_node * head_ptr )
inlinestatic

Check if a given list node is first in the list (aka after the head).

Parameters
[in]node_ptrThe node pointer.
[in]head_ptrThe head node pointer.
Returns
Whether the list node is first in the list.

◆ list_node_is_head()

static bool list_node_is_head ( const struct list_node * node_ptr,
const struct list_node * head_ptr )
inlinestatic

Check if a given list node is the head of the list.

Parameters
[in]node_ptrThe node pointer.
[in]head_ptrThe head node pointer.
Returns
Whether the list node is head of the list.

◆ list_node_is_last()

static bool list_node_is_last ( const struct list_node * node_ptr,
const struct list_node * tail_ptr )
inlinestatic

Check if a given list node is the last of the list (aka before the tail).

Parameters
[in]node_ptrThe node pointer.
[in]tail_ptrThe tail node pointer.
Returns
Whether the list node is the last in the list.

◆ list_node_is_tail()

static bool list_node_is_tail ( const struct list_node * node_ptr,
const struct list_node * tail_ptr )
inlinestatic

Check if a given list node is the tail of the list.

Parameters
[in]node_ptrThe node pointer.
[in]tail_ptrThe tail node pointer.
Returns
Whether the list node is the tail of the list.

◆ list_node_remove()

static struct list_node * list_node_remove ( struct list_node * node_ptr)
inlinestatic

Remove a node and deattach it from the list it resides in.

Assumes:

  • node_ptr node is a part of a list.
  • node_ptr node is not the head or tail node.
Parameters
[in]node_ptrThe node pointer.
Returns
Pointer to the removed node

◆ list_node_replace()

static void list_node_replace ( struct list_node * old_ptr,
struct list_node * new_ptr )
inlinestatic

Replace a given node by a new node.

Assumes:

  • The node is a part of a list.
  • The node is not the head or tail node (points to self).
Parameters
[in]old_ptrPointer to old node.
[in]new_ptrPointer to new node.