]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/list.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 struct lxc_list
*next
;
11 struct lxc_list
*prev
;
14 #define lxc_init_list(l) \
16 .next = l, .prev = l \
20 * Iterate through an lxc list. An example for an idiom would be:
22 * struct lxc_list *iterator;
23 * lxc_list_for_each(iterator, list) {
25 * tmp = iterator->elem;
28 #define lxc_list_for_each(__iterator, __list) \
29 for (__iterator = (__list)->next; __iterator != __list; \
30 __iterator = __iterator->next)
32 /* Iterate safely through an lxc list. An example for an appropriate use case
35 * struct lxc_list *cur, *next;
36 * lxc_list_for_each_safe(cur, list, next) {
41 #define lxc_list_for_each_safe(__iterator, __list, __next) \
42 for (__iterator = (__list)->next, __next = __iterator->next; \
43 __iterator != __list; __iterator = __next, __next = __next->next)
45 /* Initialize list. */
46 static inline void lxc_list_init(struct lxc_list
*list
)
49 list
->next
= list
->prev
= list
;
52 /* Add an element to a list. See lxc_list_add() and lxc_list_add_tail() for an
55 static inline void lxc_list_add_elem(struct lxc_list
*list
, void *elem
)
60 /* Retrieve first element of list. */
61 static inline void *lxc_list_first_elem(const struct lxc_list
*list
)
63 return list
->next
->elem
;
66 /* Retrieve last element of list. */
67 static inline void *lxc_list_last_elem(const struct lxc_list
*list
)
69 return list
->prev
->elem
;
72 /* Determine if list is empty. */
73 static inline int lxc_list_empty(const struct lxc_list
*list
)
75 return list
== list
->next
;
78 /* Workhorse to be called from lxc_list_add() and lxc_list_add_tail(). */
79 static inline void __lxc_list_add(struct lxc_list
*new, struct lxc_list
*prev
,
80 struct lxc_list
*next
)
88 /* Idiom to add an element to the beginning of an lxc list:
90 * struct lxc_list *tmp = malloc(sizeof(*tmp));
93 * lxc_list_add_elem(tmp, elem);
94 * lxc_list_add(list, tmp);
96 static inline void lxc_list_add(struct lxc_list
*head
, struct lxc_list
*list
)
98 __lxc_list_add(list
, head
, head
->next
);
101 /* Idiom to add an element to the end of an lxc list:
103 * struct lxc_list *tmp = malloc(sizeof(*tmp));
106 * lxc_list_add_elem(tmp, elem);
107 * lxc_list_add_tail(list, tmp);
109 static inline void lxc_list_add_tail(struct lxc_list
*head
,
110 struct lxc_list
*list
)
112 __lxc_list_add(list
, head
->prev
, head
);
115 /* Idiom to remove an element from a list:
116 * struct lxc_list *cur, *next;
117 * lxc_list_for_each_safe(cur, list, next) {
123 static inline void lxc_list_del(struct lxc_list
*list
)
125 struct lxc_list
*next
, *prev
;
133 /* Return length of the list. */
134 static inline size_t lxc_list_len(struct lxc_list
*list
)
137 struct lxc_list
*iter
;
139 lxc_list_for_each(iter
, list
) {
146 #endif /* __LXC_LIST_H */