summaryrefslogtreecommitdiff
path: root/libcr_ipc/_linkedlist.c
blob: b27dba8bd5193e33a4d28bc88f1a3eb75c58daa5 (plain)
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
/* libcr_ipc/_linkedlist.c - Common low-level linked lists for use in libcr_ipc
 *
 * Copyright (C) 2024-2025  Luke T. Shumaker <lukeshu@lukeshu.com>
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

#include <stddef.h> /* for NULL */

#include "_linkedlist.h"

/* singly linked list *********************************************************/

void cr_ipc_sll_push_to_rear(_cr_ipc_sll_root *root, cr_ipc_sll_node *node) {
	assert(root);
	node->rear = NULL;
	if (root->rear)
		root->rear->rear = node;
	else
		root->front = node;
	root->rear = node;
}

void cr_ipc_sll_pop_from_front(_cr_ipc_sll_root *root) {
	assert(root);
	assert(root->front);
	root->front = root->front->rear;
	if (!root->front)
		root->rear = NULL;
}

/* doubly linked list *********************************************************/

void cr_ipc_dll_push_to_rear(_cr_ipc_dll_root *root, cr_ipc_dll_node *node) {
	assert(root);
	assert(node);
	node->front = root->rear;
	node->rear = NULL;
	if (root->rear)
		root->rear->rear = node;
	else
		root->front = node;
	root->rear = node;
}

void cr_ipc_dll_remove(_cr_ipc_dll_root *root, cr_ipc_dll_node *node) {
	assert(root);
	assert(node);
	if (node->front)
		node->front->rear = node->rear;
	else
		root->front = node->rear;
	if (node->rear)
		node->rear->front = node->front;
	else
		root->rear = node->front;
}

void cr_ipc_dll_pop_from_front(_cr_ipc_dll_root *root) {
	assert(root);
	assert(root->front);
	cr_ipc_dll_remove(root, root->front);
}