1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2020 Intel Corporation
5 #ifndef __DLB_OSDEP_LIST_H__
6 #define __DLB_OSDEP_LIST_H__
10 struct dlb_list_entry {
11 TAILQ_ENTRY(dlb_list_entry) node;
14 /* Dummy - just a struct definition */
15 TAILQ_HEAD(dlb_list_head, dlb_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); \
45 * dlb_list_init_head() - initialize the head of a list
48 static inline void dlb_list_init_head(struct dlb_list_head *head)
54 * dlb_list_add() - add an entry to a list
55 * @head: new entry will be added after this list header
56 * @entry: new list entry to be added
58 static inline void dlb_list_add(struct dlb_list_head *head,
59 struct dlb_list_entry *entry)
61 TAILQ_INSERT_TAIL(head, entry, node);
66 * @entry: list entry to be deleted
68 static inline void dlb_list_del(struct dlb_list_head *head,
69 struct dlb_list_entry *entry)
71 TAILQ_REMOVE(head, entry, node);
75 * dlb_list_empty() - check if a list is empty
79 * Returns 1 if empty, 0 if not.
81 static inline bool dlb_list_empty(struct dlb_list_head *head)
83 return TAILQ_EMPTY(head);
87 * dlb_list_empty() - check if a list is empty
88 * @src_head: list to be added
89 * @ head: where src_head will be inserted
91 static inline void dlb_list_splice(struct dlb_list_head *src_head,
92 struct dlb_list_head *head)
94 TAILQ_CONCAT(head, src_head, node);
98 * DLB_LIST_HEAD() - retrieve the head of the list
100 * @type: type of the list variable
101 * @name: name of the dlb_list within the struct
103 #define DLB_LIST_HEAD(head, type, name) \
104 (TAILQ_FIRST(&head) ? \
105 container_of(TAILQ_FIRST(&head), type, name) : \
109 * DLB_LIST_FOR_EACH() - iterate over a list
111 * @ptr: pointer to struct containing a struct dlb_list_entry
112 * @name: name of the dlb_list_entry field within the containing struct
113 * @iter: iterator variable
115 #define DLB_LIST_FOR_EACH(head, ptr, name, tmp_iter) \
116 TAILQ_FOREACH_ENTRY(ptr, head, name, tmp_iter)
119 * DLB_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 dlb_list_entry
122 * @ptr_tmp: pointer to struct containing a struct dlb_list_entry (temporary)
124 * @name: name of the dlb_list_entry field within the containing struct
125 * @iter: iterator variable
126 * @iter_tmp: iterator variable (temporary)
128 #define DLB_LIST_FOR_EACH_SAFE(head, ptr, ptr_tmp, name, tmp_iter, saf_iter) \
129 TAILQ_FOREACH_ENTRY_SAFE(ptr, head, name, tmp_iter, saf_iter)
131 #endif /* __DLB_OSDEP_LIST_H__ */