]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/ocf/tests/unit/tests/ocf_env/ocf_env_list.h
import 15.2.0 Octopus source
[ceph.git] / ceph / src / spdk / ocf / tests / unit / tests / ocf_env / ocf_env_list.h
1 /*
2 * Copyright(c) 2012-2018 Intel Corporation
3 * SPDX-License-Identifier: BSD-3-Clause-Clear
4 */
5
6 #ifndef __OCF_LIST_H__
7 #define __OCF_LIST_H__
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 * Extract an entry.
74 * @param list_head_i list head item, from which entry is extracted
75 * @param item_type type (struct) of list entry
76 * @param field_name name of list_head field within item_type
77 */
78 #define list_entry(list_head_i, item_type, field_name) \
79 (item_type *)(((void*)(list_head_i)) - offsetof(item_type, field_name))
80
81 #define list_first_entry(list_head_i, item_type, field_name) \
82 list_entry((list_head_i)->next, item_type, field_name)
83
84 /**
85 * @param iterator uninitialized list_head pointer, to be used as iterator
86 * @param plist list head (main node)
87 */
88 #define list_for_each(iterator, plist) \
89 for (iterator = (plist)->next; \
90 (iterator)->next != (plist)->next; \
91 iterator = (iterator)->next)
92
93 /**
94 * Safe version of list_for_each which works even if entries are deleted during
95 * loop.
96 * @param iterator uninitialized list_head pointer, to be used as iterator
97 * @param q another uninitialized list_head, used as helper
98 * @param plist list head (main node)
99 */
100 /*
101 * Algorithm handles situation, where q is deleted.
102 * consider in example 3 element list with header h:
103 *
104 * h -> 1 -> 2 -> 3 ->
105 *1. i q
106 *
107 *2. i q
108 *
109 *3. q i
110 */
111 #define list_for_each_safe(iterator, q, plist) \
112 for (iterator = (q = (plist)->next->next)->prev; \
113 (q) != (plist)->next; \
114 iterator = (q = (q)->next)->prev)
115
116 #define _list_entry_helper(item, head, field_name) \
117 list_entry(head, typeof(*item), field_name)
118
119 /**
120 * Iterate over list entries.
121 * @param list pointer to list item (iterator)
122 * @param plist pointer to list_head item
123 * @param field_name name of list_head field in list entry
124 */
125 #define list_for_each_entry(item, plist, field_name) \
126 for (item = _list_entry_helper(item, (plist)->next, field_name); \
127 _list_entry_helper(item, (item)->field_name.next, field_name) !=\
128 _list_entry_helper(item, (plist)->next, field_name); \
129 item = _list_entry_helper(item, (item)->field_name.next, field_name))
130
131 /**
132 * Safe version of list_for_each_entry which works even if entries are deleted
133 * during loop.
134 * @param list pointer to list item (iterator)
135 * @param q another pointer to list item, used as helper
136 * @param plist pointer to list_head item
137 * @param field_name name of list_head field in list entry
138 */
139 #define list_for_each_entry_safe(item, q, plist, field_name) \
140 for (item = _list_entry_helper(item, (plist)->next, field_name), \
141 q = _list_entry_helper(item, (item)->field_name.next, field_name); \
142 _list_entry_helper(item, (item)->field_name.next, field_name) != \
143 _list_entry_helper(item, (plist)->next, field_name); \
144 item = q, q = _list_entry_helper(q, (q)->field_name.next, field_name))
145
146 #endif