PipeWire  0.3.59
list.h
Go to the documentation of this file.
1 /* Simple Plugin API
2  *
3  * Copyright © 2018 Wim Taymans
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 
25 #ifndef SPA_LIST_H
26 #define SPA_LIST_H
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
42 struct spa_list {
43  struct spa_list *next;
44  struct spa_list *prev;
45 };
46 
47 #define SPA_LIST_INIT(list) ((struct spa_list){ (list), (list) })
48 
49 static inline void spa_list_init(struct spa_list *list)
50 {
51  *list = SPA_LIST_INIT(list);
52 }
53 
54 static inline int spa_list_is_initialized(struct spa_list *list)
55 {
56  return !!list->prev;
57 }
58 
59 #define spa_list_is_empty(l) ((l)->next == (l))
60 
61 static inline void spa_list_insert(struct spa_list *list, struct spa_list *elem)
62 {
63  elem->prev = list;
64  elem->next = list->next;
65  list->next = elem;
66  elem->next->prev = elem;
67 }
68 
69 static inline void spa_list_insert_list(struct spa_list *list, struct spa_list *other)
70 {
71  if (spa_list_is_empty(other))
72  return;
73  other->next->prev = list;
74  other->prev->next = list->next;
75  list->next->prev = other->prev;
76  list->next = other->next;
77 }
78 
79 static inline void spa_list_remove(struct spa_list *elem)
80 {
81  elem->prev->next = elem->next;
82  elem->next->prev = elem->prev;
83 }
84 
85 #define spa_list_first(head, type, member) \
86  SPA_CONTAINER_OF((head)->next, type, member)
87 
88 #define spa_list_last(head, type, member) \
89  SPA_CONTAINER_OF((head)->prev, type, member)
90 
91 #define spa_list_append(list, item) \
92  spa_list_insert((list)->prev, item)
93 
94 #define spa_list_prepend(list, item) \
95  spa_list_insert(list, item)
96 
97 #define spa_list_is_end(pos, head, member) \
98  (&(pos)->member == (head))
99 
100 #define spa_list_next(pos, member) \
101  SPA_CONTAINER_OF((pos)->member.next, __typeof__(*(pos)), member)
102 
103 #define spa_list_prev(pos, member) \
104  SPA_CONTAINER_OF((pos)->member.prev, __typeof__(*(pos)), member)
105 
106 #define spa_list_consume(pos, head, member) \
107  for ((pos) = spa_list_first(head, __typeof__(*(pos)), member); \
108  !spa_list_is_empty(head); \
109  (pos) = spa_list_first(head, __typeof__(*(pos)), member))
110 
111 #define spa_list_for_each_next(pos, head, curr, member) \
112  for ((pos) = spa_list_first(curr, __typeof__(*(pos)), member); \
113  !spa_list_is_end(pos, head, member); \
114  (pos) = spa_list_next(pos, member))
115 
116 #define spa_list_for_each_prev(pos, head, curr, member) \
117  for ((pos) = spa_list_last(curr, __typeof__(*(pos)), member); \
118  !spa_list_is_end(pos, head, member); \
119  (pos) = spa_list_prev(pos, member))
120 
121 #define spa_list_for_each(pos, head, member) \
122  spa_list_for_each_next(pos, head, head, member)
123 
124 #define spa_list_for_each_reverse(pos, head, member) \
125  spa_list_for_each_prev(pos, head, head, member)
126 
127 #define spa_list_for_each_safe_next(pos, tmp, head, curr, member) \
128  for ((pos) = spa_list_first(curr, __typeof__(*(pos)), member); \
129  (tmp) = spa_list_next(pos, member), \
130  !spa_list_is_end(pos, head, member); \
131  (pos) = (tmp))
132 
133 #define spa_list_for_each_safe_prev(pos, tmp, head, curr, member) \
134  for ((pos) = spa_list_last(curr, __typeof__(*(pos)), member); \
135  (tmp) = spa_list_prev(pos, member), \
136  !spa_list_is_end(pos, head, member); \
137  (pos) = (tmp))
138 
139 #define spa_list_for_each_safe(pos, tmp, head, member) \
140  spa_list_for_each_safe_next(pos, tmp, head, head, member)
141 
142 #define spa_list_for_each_safe_reverse(pos, tmp, head, member) \
143  spa_list_for_each_safe_prev(pos, tmp, head, head, member)
144 
145 #define spa_list_cursor_start(cursor, head, member) \
146  spa_list_prepend(head, &(cursor).member)
147 
148 #define spa_list_for_each_cursor(pos, cursor, head, member) \
149  for((pos) = spa_list_first(&(cursor).member, __typeof__(*(pos)), member); \
150  spa_list_remove(&(pos)->member), \
151  spa_list_append(&(cursor).member, &(pos)->member), \
152  !spa_list_is_end(pos, head, member); \
153  (pos) = spa_list_next(&(cursor), member))
154 
155 #define spa_list_cursor_end(cursor, member) \
156  spa_list_remove(&(cursor).member)
157 
162 #ifdef __cplusplus
163 } /* extern "C" */
164 #endif
165 
166 #endif /* SPA_LIST_H */
static void spa_list_insert(struct spa_list *list, struct spa_list *elem)
Definition: list.h:67
static int spa_list_is_initialized(struct spa_list *list)
Definition: list.h:60
static void spa_list_init(struct spa_list *list)
Definition: list.h:55
static void spa_list_remove(struct spa_list *elem)
Definition: list.h:85
#define spa_list_is_empty(l)
Definition: list.h:65
#define SPA_LIST_INIT(list)
Definition: list.h:53
static void spa_list_insert_list(struct spa_list *list, struct spa_list *other)
Definition: list.h:75
Definition: list.h:47
struct spa_list * prev
Definition: list.h:49
struct spa_list * next
Definition: list.h:48