libmetal
list.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, Xilinx Inc. and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7/*
8 * @file list.h
9 * @brief List primitives for libmetal.
10 */
11
12#ifndef __METAL_LIST__H__
13#define __METAL_LIST__H__
14
15#include <stdbool.h>
16#include <stdlib.h>
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
26struct metal_list {
27 struct metal_list *next, *prev;
28};
29
30/*
31 * METAL_INIT_LIST - used for initializing an list element in a static struct
32 * or global
33 */
34#define METAL_INIT_LIST(name) { .next = &name, .prev = &name }
35/*
36 * METAL_DECLARE_LIST - used for defining and initializing a global or
37 * static singleton list
38 */
39#define METAL_DECLARE_LIST(name) \
40 struct metal_list name = METAL_INIT_LIST(name)
41
42static inline void metal_list_init(struct metal_list *list)
43{
44 list->prev = list;
45 list->next = list;
46}
47
48static inline void metal_list_add_before(struct metal_list *node,
49 struct metal_list *new_node)
50{
51 new_node->prev = node->prev;
52 new_node->next = node;
53 new_node->next->prev = new_node;
54 new_node->prev->next = new_node;
55}
56
57static inline void metal_list_add_after(struct metal_list *node,
58 struct metal_list *new_node)
59{
60 new_node->prev = node;
61 new_node->next = node->next;
62 new_node->next->prev = new_node;
63 new_node->prev->next = new_node;
64}
65
66static inline void metal_list_add_head(struct metal_list *list,
67 struct metal_list *node)
68{
69 metal_list_add_after(list, node);
70}
71
72static inline void metal_list_add_tail(struct metal_list *list,
73 struct metal_list *node)
74{
75 metal_list_add_before(list, node);
76}
77
78static inline int metal_list_is_empty(struct metal_list *list)
79{
80 return list->next == list;
81}
82
83static inline void metal_list_del(struct metal_list *node)
84{
85 node->next->prev = node->prev;
86 node->prev->next = node->next;
87 node->prev = node;
88 node->next = node;
89}
90
91static inline struct metal_list *metal_list_first(struct metal_list *list)
92{
93 return metal_list_is_empty(list) ? NULL : list->next;
94}
95
96#define metal_list_for_each(list, node) \
97 for ((node) = (list)->next; \
98 (node) != (list); \
99 (node) = (node)->next)
100
101static inline bool metal_list_find_node(struct metal_list *list,
102 struct metal_list *node)
103{
104 struct metal_list *n;
105
106 metal_list_for_each(list, n) {
107 if (n == node)
108 return true;
109 }
110 return false;
111}
114#ifdef __cplusplus
115}
116#endif
117
118#endif /* __METAL_LIST__H__ */
static void metal_list_add_tail(struct metal_list *list, struct metal_list *node)
Definition: list.h:72
static void metal_list_add_after(struct metal_list *node, struct metal_list *new_node)
Definition: list.h:57
static int metal_list_is_empty(struct metal_list *list)
Definition: list.h:78
static void metal_list_add_head(struct metal_list *list, struct metal_list *node)
Definition: list.h:66
static struct metal_list * metal_list_first(struct metal_list *list)
Definition: list.h:91
#define metal_list_for_each(list, node)
Definition: list.h:96
static void metal_list_del(struct metal_list *node)
Definition: list.h:83
static void metal_list_add_before(struct metal_list *node, struct metal_list *new_node)
Definition: list.h:48
static void metal_list_init(struct metal_list *list)
Definition: list.h:42
static bool metal_list_find_node(struct metal_list *list, struct metal_list *node)
Definition: list.h:101
Definition: list.h:26
struct metal_list * next
Definition: list.h:27
struct metal_list * prev
Definition: list.h:27