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 <stdlib.h>
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
24 struct metal_list {
25  struct metal_list *next, *prev;
26 };
27 
28 /*
29  * METAL_INIT_LIST - used for initializing an list elmenet in a static struct
30  * or global
31  */
32 #define METAL_INIT_LIST(name) { .next = &name, .prev = &name }
33 /*
34  * METAL_DECLARE_LIST - used for defining and initializing a global or
35  * static singleton list
36  */
37 #define METAL_DECLARE_LIST(name) \
38  struct metal_list name = METAL_INIT_LIST(name)
39 
40 static inline void metal_list_init(struct metal_list *list)
41 {
42  list->next = list->prev = list;
43 }
44 
45 static inline void metal_list_add_before(struct metal_list *node,
46  struct metal_list *new_node)
47 {
48  new_node->prev = node->prev;
49  new_node->next = node;
50  new_node->next->prev = new_node;
51  new_node->prev->next = new_node;
52 }
53 
54 static inline void metal_list_add_after(struct metal_list *node,
55  struct metal_list *new_node)
56 {
57  new_node->prev = node;
58  new_node->next = node->next;
59  new_node->next->prev = new_node;
60  new_node->prev->next = new_node;
61 }
62 
63 static inline void metal_list_add_head(struct metal_list *list,
64  struct metal_list *node)
65 {
66  metal_list_add_after(list, node);
67 }
68 
69 static inline void metal_list_add_tail(struct metal_list *list,
70  struct metal_list *node)
71 {
72  metal_list_add_before(list, node);
73 }
74 
75 static inline int metal_list_is_empty(struct metal_list *list)
76 {
77  return list->next == list;
78 }
79 
80 static inline void metal_list_del(struct metal_list *node)
81 {
82  node->next->prev = node->prev;
83  node->prev->next = node->next;
84  node->next = node->prev = node;
85 }
86 
87 static inline struct metal_list *metal_list_first(struct metal_list *list)
88 {
89  return metal_list_is_empty(list) ? NULL : list->next;
90 }
91 
92 #define metal_list_for_each(list, node) \
93  for ((node) = (list)->next; \
94  (node) != (list); \
95  (node) = (node)->next)
96 
98 #ifdef __cplusplus
99 }
100 #endif
101 
102 #endif /* __METAL_LIST__H__ */
struct metal_list * next
Definition: list.h:25
struct metal_list * prev
Definition: list.h:25
static void metal_list_add_before(struct metal_list *node, struct metal_list *new_node)
Definition: list.h:45
Definition: list.h:24
static struct metal_list * metal_list_first(struct metal_list *list)
Definition: list.h:87
static void metal_list_add_tail(struct metal_list *list, struct metal_list *node)
Definition: list.h:69
static void metal_list_init(struct metal_list *list)
Definition: list.h:40
static int metal_list_is_empty(struct metal_list *list)
Definition: list.h:75
static void metal_list_del(struct metal_list *node)
Definition: list.h:80
static void metal_list_add_head(struct metal_list *list, struct metal_list *node)
Definition: list.h:63
static void metal_list_add_after(struct metal_list *node, struct metal_list *new_node)
Definition: list.h:54