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