1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2020 Intel Corporation
5 #ifndef __DLB2_OSDEP_LIST_H
6 #define __DLB2_OSDEP_LIST_H
10 struct dlb2_list_entry {
11 TAILQ_ENTRY(dlb2_list_entry) node;
14 /* Dummy - just a struct definition */
15 TAILQ_HEAD(dlb2_list_head, dlb2_list_entry);
22 #ifndef TAILQ_FOREACH_ENTRY
23 #define TAILQ_FOREACH_ENTRY(ptr, head, name, iter) \
24 for ((iter) = TAILQ_FIRST(&head); \
26 && (ptr = container_of(iter, typeof(*(ptr)), name)); \
27 (iter) = TAILQ_NEXT((iter), node))
30 #ifndef TAILQ_FOREACH_ENTRY_SAFE
31 #define TAILQ_FOREACH_ENTRY_SAFE(ptr, head, name, iter, tvar) \
32 for ((iter) = TAILQ_FIRST(&head); \
34 (ptr = container_of(iter, typeof(*(ptr)), name)) &&\
35 ((tvar) = TAILQ_NEXT((iter), node), 1); \
39 /***********************/
40 /*** List operations ***/
41 /***********************/
44 * dlb2_list_init_head() - initialize the head of a list
47 static inline void dlb2_list_init_head(struct dlb2_list_head *head)
53 * dlb2_list_add() - add an entry to a list
55 * @entry: new list entry
58 dlb2_list_add(struct dlb2_list_head *head, struct dlb2_list_entry *entry)
60 TAILQ_INSERT_TAIL(head, entry, node);
64 * dlb2_list_del() - delete an entry from a list
68 static inline void dlb2_list_del(struct dlb2_list_head *head,
69 struct dlb2_list_entry *entry)
71 TAILQ_REMOVE(head, entry, node);
75 * dlb2_list_empty() - check if a list is empty
79 * Returns 1 if empty, 0 if not.
81 static inline int dlb2_list_empty(struct dlb2_list_head *head)
83 return TAILQ_EMPTY(head);
87 * dlb2_list_splice() - splice a list
88 * @src_head: list to be added
89 * @ head: where src_head will be inserted
91 static inline void dlb2_list_splice(struct dlb2_list_head *src_head,
92 struct dlb2_list_head *head)
94 TAILQ_CONCAT(head, src_head, node);
98 * DLB2_LIST_HEAD() - retrieve the head of the list
100 * @type: type of the list variable
101 * @name: name of the list field within the containing struct
103 #define DLB2_LIST_HEAD(head, type, name) \
104 (TAILQ_FIRST(&head) ? \
105 container_of(TAILQ_FIRST(&head), type, name) : \
109 * DLB2_LIST_FOR_EACH() - iterate over a list
111 * @ptr: pointer to struct containing a struct list
112 * @name: name of the list field within the containing struct
113 * @iter: iterator variable
115 #define DLB2_LIST_FOR_EACH(head, ptr, name, tmp_iter) \
116 TAILQ_FOREACH_ENTRY(ptr, head, name, tmp_iter)
119 * DLB2_LIST_FOR_EACH_SAFE() - iterate over a list. This loop works even if
120 * an element is removed from the list while processing it.
121 * @ptr: pointer to struct containing a struct list
122 * @ptr_tmp: pointer to struct containing a struct list (temporary)
124 * @name: name of the list field within the containing struct
125 * @iter: iterator variable
126 * @iter_tmp: iterator variable (temporary)
128 #define DLB2_LIST_FOR_EACH_SAFE(head, ptr, ptr_tmp, name, tmp_iter, saf_itr) \
129 TAILQ_FOREACH_ENTRY_SAFE(ptr, head, name, tmp_iter, saf_itr)
131 #endif /* __DLB2_OSDEP_LIST_H */