summaryrefslogtreecommitdiff
path: root/libcr_ipc/_linkedlist.h
blob: ab6d89e57c23a3a96b430ffe52fcf3f4e67656ae (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
/* libcr_ipc/_linkedlist.h - 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
 */

#ifndef _LIBCR_IPC__LINKEDLIST_H_
#define _LIBCR_IPC__LINKEDLIST_H_

#include <libmisc/assert.h>

#include <libcr_ipc/_linkedlist_pub.h>

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

typedef struct _cr_ipc_sll_node {
	struct _cr_ipc_sll_node *rear;
} cr_ipc_sll_node;

#define cr_ipc_sll_node_cast(node_typ, node_ptr)                                    \
	({                                                                          \
		static_assert(_Generic(node_ptr, cr_ipc_sll_node *: 1, default: 0), \
		              "typeof("#node_ptr") != cr_ipc_sll_node *");          \
		assert(node_ptr);                                                   \
		static_assert(offsetof(node_typ, cr_ipc_sll_node) == 0);            \
		((node_typ*)(node_ptr));                                            \
	})

void cr_ipc_sll_push_to_rear(_cr_ipc_sll_root *root, cr_ipc_sll_node *node);
void cr_ipc_sll_pop_from_front(_cr_ipc_sll_root *root);

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

typedef struct _cr_ipc_dll_node {
	struct _cr_ipc_dll_node *front, *rear;
} cr_ipc_dll_node;

#define cr_ipc_dll_node_cast(node_typ, node_ptr)                                    \
	({                                                                          \
		static_assert(_Generic(node_ptr, cr_ipc_dll_node *: 1, default: 0), \
		              "typeof("#node_ptr") != cr_ipc_dll_node *");          \
		assert(node_ptr);                                                   \
		static_assert(offsetof(node_typ, cr_ipc_dll_node) == 0);            \
		((node_typ*)(node_ptr));                                            \
	})

void cr_ipc_dll_push_to_rear(_cr_ipc_dll_root *root, cr_ipc_dll_node *node);
void cr_ipc_dll_remove(_cr_ipc_dll_root *root, cr_ipc_dll_node *node);
void cr_ipc_dll_pop_from_front(_cr_ipc_dll_root *root);

#endif /* _LIBCR_IPC__LINKEDLIST_H_ */