]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/ocf/env/posix/ocf_env_list.h
import 15.2.0 Octopus source
[ceph.git] / ceph / src / spdk / ocf / env / posix / ocf_env_list.h
1 /*
2 * Copyright(c) 2019 Intel Corporation
3 * SPDX-License-Identifier: BSD-3-Clause-Clear
4 */
5
6 #ifndef __OCF_ENV_LIST__
7 #define __OCF_ENV_LIST__
8
9 #define LIST_POISON1 ((void *)0x101)
10 #define LIST_POISON2 ((void *)0x202)
11
12 /**
13 * List entry structure mimicking linux kernel based one.
14 */
15 struct list_head {
16 struct list_head *next;
17 struct list_head *prev;
18 };
19
20 /**
21 * start an empty list
22 */
23 #define INIT_LIST_HEAD(l) { (l)->prev = l; (l)->next = l; }
24
25 /**
26 * Add item to list head.
27 * @param it list entry to be added
28 * @param l1 list main node (head)
29 */
30 static inline void list_add(struct list_head *it, struct list_head *l1)
31 {
32 it->prev = l1;
33 it->next = l1->next;
34
35 l1->next->prev = it;
36 l1->next = it;
37 }
38
39 /**
40 * Add item it to tail.
41 * @param it list entry to be added
42 * @param l1 list main node (head)
43 */
44 static inline void list_add_tail(struct list_head *it, struct list_head *l1)
45 {
46 it->prev = l1->prev;
47 it->next = l1;
48
49 l1->prev->next = it;
50 l1->prev = it;
51 }
52
53 /**
54 * check if a list is empty (return true)
55 * @param l1 list main node (head)
56 */
57 static inline int list_empty(struct list_head *l1)
58 {
59 return l1->next == l1;
60 }
61
62 /**
63 * delete an entry from a list
64 * @param it list entry to be deleted
65 */
66 static inline void list_del(struct list_head *it)
67 {
68 it->next->prev = it->prev;
69 it->prev->next = it->next;
70 }
71
72 /**
73 * Move element to list head.
74 * @param it list entry to be moved
75 * @param l1 list main node (head)
76 */
77 static inline void list_move(struct list_head *it, struct list_head *l1)
78 {
79 list_del(it);
80 list_add(it, l1);
81 }
82
83 /**
84 * Move element to list tail.
85 * @param it list entry to be moved
86 * @param l1 list main node (head)
87 */
88 static inline void list_move_tail(struct list_head *it, struct list_head *l1)
89 {
90 list_del(it);
91 list_add_tail(it, l1);
92 }
93
94 /**
95 * Extract an entry.
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
99 */
100 #define list_entry(list_head_i, item_type, field_name) \
101 (item_type *)(((void*)(list_head_i)) - offsetof(item_type, field_name))
102
103 #define list_first_entry(list_head_i, item_type, field_name) \
104 list_entry((list_head_i)->next, item_type, field_name)
105
106 /**
107 * @param iterator uninitialized list_head pointer, to be used as iterator
108 * @param plist list head (main node)
109 */
110 #define list_for_each(iterator, plist) \
111 for (iterator = (plist)->next; \
112 (iterator)->next != (plist)->next; \
113 iterator = (iterator)->next)
114
115 /**
116 * Safe version of list_for_each which works even if entries are deleted during
117 * loop.
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)
121 */
122 /*
123 * Algorithm handles situation, where q is deleted.
124 * consider in example 3 element list with header h:
125 *
126 * h -> 1 -> 2 -> 3 ->
127 *1. i q
128 *
129 *2. i q
130 *
131 *3. q i
132 */
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)
137
138 #define _list_entry_helper(item, head, field_name) \
139 list_entry(head, typeof(*item), field_name)
140
141 /**
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
146 */
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))
152
153 /**
154 * Safe version of list_for_each_entry which works even if entries are deleted
155 * during loop.
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
160 */
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))
167
168 #endif // __OCF_ENV_LIST__