]>
git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/ocf/env/posix/ocf_env_list.h
2 * Copyright(c) 2019 Intel Corporation
3 * SPDX-License-Identifier: BSD-3-Clause-Clear
6 #ifndef __OCF_ENV_LIST__
7 #define __OCF_ENV_LIST__
9 #define LIST_POISON1 ((void *)0x101)
10 #define LIST_POISON2 ((void *)0x202)
13 * List entry structure mimicking linux kernel based one.
16 struct list_head
*next
;
17 struct list_head
*prev
;
23 #define INIT_LIST_HEAD(l) { (l)->prev = l; (l)->next = l; }
26 * Add item to list head.
27 * @param it list entry to be added
28 * @param l1 list main node (head)
30 static inline void list_add(struct list_head
*it
, struct list_head
*l1
)
40 * Add item it to tail.
41 * @param it list entry to be added
42 * @param l1 list main node (head)
44 static inline void list_add_tail(struct list_head
*it
, struct list_head
*l1
)
54 * check if a list is empty (return true)
55 * @param l1 list main node (head)
57 static inline int list_empty(struct list_head
*l1
)
59 return l1
->next
== l1
;
63 * delete an entry from a list
64 * @param it list entry to be deleted
66 static inline void list_del(struct list_head
*it
)
68 it
->next
->prev
= it
->prev
;
69 it
->prev
->next
= it
->next
;
73 * Move element to list head.
74 * @param it list entry to be moved
75 * @param l1 list main node (head)
77 static inline void list_move(struct list_head
*it
, struct list_head
*l1
)
84 * Move element to list tail.
85 * @param it list entry to be moved
86 * @param l1 list main node (head)
88 static inline void list_move_tail(struct list_head
*it
, struct list_head
*l1
)
91 list_add_tail(it
, l1
);
96 * @param list_head_i list head item, from which entry is extracted
97 * @param item_type type (struct) of list entry
98 * @param field_name name of list_head field within item_type
100 #define list_entry(list_head_i, item_type, field_name) \
101 (item_type *)(((void*)(list_head_i)) - offsetof(item_type, field_name))
103 #define list_first_entry(list_head_i, item_type, field_name) \
104 list_entry((list_head_i)->next, item_type, field_name)
107 * @param iterator uninitialized list_head pointer, to be used as iterator
108 * @param plist list head (main node)
110 #define list_for_each(iterator, plist) \
111 for (iterator = (plist)->next; \
112 (iterator)->next != (plist)->next; \
113 iterator = (iterator)->next)
116 * Safe version of list_for_each which works even if entries are deleted during
118 * @param iterator uninitialized list_head pointer, to be used as iterator
119 * @param q another uninitialized list_head, used as helper
120 * @param plist list head (main node)
123 * Algorithm handles situation, where q is deleted.
124 * consider in example 3 element list with header h:
126 * h -> 1 -> 2 -> 3 ->
133 #define list_for_each_safe(iterator, q, plist) \
134 for (iterator = (q = (plist)->next->next)->prev; \
135 (q) != (plist)->next; \
136 iterator = (q = (q)->next)->prev)
138 #define _list_entry_helper(item, head, field_name) \
139 list_entry(head, typeof(*item), field_name)
142 * Iterate over list entries.
143 * @param list pointer to list item (iterator)
144 * @param plist pointer to list_head item
145 * @param field_name name of list_head field in list entry
147 #define list_for_each_entry(item, plist, field_name) \
148 for (item = _list_entry_helper(item, (plist)->next, field_name); \
149 _list_entry_helper(item, (item)->field_name.next, field_name) !=\
150 _list_entry_helper(item, (plist)->next, field_name); \
151 item = _list_entry_helper(item, (item)->field_name.next, field_name))
154 * Safe version of list_for_each_entry which works even if entries are deleted
156 * @param list pointer to list item (iterator)
157 * @param q another pointer to list item, used as helper
158 * @param plist pointer to list_head item
159 * @param field_name name of list_head field in list entry
161 #define list_for_each_entry_safe(item, q, plist, field_name) \
162 for (item = _list_entry_helper(item, (plist)->next, field_name), \
163 q = _list_entry_helper(item, (item)->field_name.next, field_name); \
164 _list_entry_helper(item, (item)->field_name.next, field_name) != \
165 _list_entry_helper(item, (plist)->next, field_name); \
166 item = q, q = _list_entry_helper(q, (q)->field_name.next, field_name))
168 #endif // __OCF_ENV_LIST__