1 /******************************************************************************
2 *
3 * Copyright(c) 2019 Realtek Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 *****************************************************************************/
15 #ifndef _PHL_LIST_H_
16 #define _PHL_LIST_H_
17
18 /*
19 * Copied from include/linux/...
20 */
21 #ifdef PHL_PLATFORM_WINDOWS
22
23 #define phl_container_of(_ptr, _type, _member) container_of(_ptr, _type, _member)
24 #define phl_list_for_loop(_pos, _type, _head, _member) \
25 list_for_each_entry(_pos, _type, _head, _member)
26 #define phl_list_for_loop_safe(_pos, _n, _type, _head, _member) \
27 list_for_each_entry_safe(_pos, _n, _type, _head, _member)
28
29
30 #elif defined(PHL_PLATFORM_LINUX)
31
32 #define phl_container_of(_ptr, _type, _member) container_of(_ptr, _type, _member)
33 #define phl_list_for_loop(_pos, _type, _head, _member) \
34 list_for_each_entry(_pos, _head, _member)
35 #define phl_list_for_loop_safe(_pos, _n, _type, _head, _member) \
36 list_for_each_entry_safe(_pos, _n, _head, _member)
37
38 #elif defined(PHL_PLATFORM_MACOS)
39
40 #define phl_container_of(_ptr, _type, _member) container_of(_ptr, _type, _member)
41 #define phl_list_for_loop(_pos, _type, _head, _member) \
42 list_for_each_entry(_pos, _type, _head, _member)
43 #define phl_list_for_loop_safe(_pos, _n, _type, _head, _member) \
44 list_for_each_entry_safe(_pos, _n, _type, _head, _member)
45
46 #else /* os free */
47 #undef offsetof
48 #define offsetof(_type, _member) ((size_t) &((_type *)0)->_member)
49 #define phl_container_of(_ptr, _type, _member) container_of(_ptr, _type, _member)
50 #define phl_list_for_loop(_pos, _type, _head, _member) \
51 list_for_each_entry(_pos, _type, _head, _member)
52 #define phl_list_for_loop_safe(_pos, _n, _type, _head, _member) \
53 list_for_each_entry_safe(_pos, _n, _type, _head, _member)
54
55 #endif
56
57 enum list_pos {
58 _first,
59 _tail
60 };
61
62 #ifndef PHL_PLATFORM_LINUX
63 /**
64 * container_of - cast a member of a structure out to the containing structure
65 * @ptr: the pointer to the member.
66 * @type: the type of the container struct this is embedded in.
67 * @member: the name of the member within the struct.
68 *
69 */
70 #define container_of(_ptr, _type, _member) \
71 ((_type*)((char*)(_ptr) - (char*)&(((_type*)0)->_member)))
72
73
74 struct list_head {
75 struct list_head *next, *prev;
76 };
77
78
79 #define LIST_HEAD_INIT(name) { &(name), &(name) }
80
81 #define LIST_HEAD(name) \
82 struct list_head name = LIST_HEAD_INIT(name)
83
INIT_LIST_HEAD(struct list_head * list)84 static __inline void INIT_LIST_HEAD(struct list_head *list)
85 {
86 list->next = list;
87 list->prev = list;
88 }
89
90
91 /**
92 * list_entry - get the struct for this entry
93 * @ptr: the &struct list_head pointer.
94 * @type: the type of the struct this is embedded in.
95 * @member: the name of the list_head within the struct.
96 */
97 #define list_entry(ptr, type, member) \
98 container_of(ptr, type, member)
99
100 /**
101 * list_first_entry - get the first element from a list
102 * @ptr: the list head to take the element from.
103 * @type: the type of the struct this is embedded in.
104 * @member: the name of the list_head within the struct.
105 *
106 * Note, that list is expected to be not empty.
107 */
108 #define list_first_entry(ptr, type, member) \
109 list_entry((ptr)->next, type, member)
110
111 /**
112 * list_last_entry - get the last element from a list
113 * @ptr: the list head to take the element from.
114 * @type: the type of the struct this is embedded in.
115 * @member: the name of the list_head within the struct.
116 *
117 * Note, that list is expected to be not empty.
118 */
119 #define list_last_entry(ptr, type, member) \
120 list_entry((ptr)->prev, type, member)
121
122 /**
123 * list_for_each_entry - iterate over list of given type
124 * @pos: the type * to use as a loop cursor.
125 * @type: the type of the struct this is embedded in.
126 * @head: the head for your list.
127 * @member: the name of the list_head within the struct.
128 */
129 #define list_for_each_entry(pos, type, head, member) \
130 for (pos = list_entry((head)->next, type, member); \
131 &pos->member != (head); \
132 pos = list_entry(pos->member.next, type, member))
133
134 /**
135 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
136 * @pos: the type * to use as a loop cursor.
137 * @n: another type * to use as temporary storage
138 * @type: the type of the struct this is embedded in
139 * @head: the head for your list.
140 * @member: the name of the list_head within the struct.
141 */
142 #define list_for_each_entry_safe(pos, n, type, head, member) \
143 for (pos = list_entry((head)->next, type, member), \
144 n = list_entry(pos->member.next, type, member); \
145 &pos->member != (head); \
146 pos = n, n = list_entry(n->member.next, type, member))
147
148 /**
149 * list_empty - tests whether a list is empty
150 * @head: the list to test.
151 */
list_empty(const struct list_head * head)152 static __inline int list_empty(const struct list_head *head)
153 {
154 return head->next == head;
155 }
156
157 /*
158 * Insert a new entry between two known consecutive entries.
159 *
160 * This is only for internal list manipulation where we know
161 * the prev/next entries already!
162 */
__list_add(struct list_head * _new,struct list_head * prev,struct list_head * next)163 static __inline void __list_add(struct list_head *_new,
164 struct list_head *prev,
165 struct list_head *next)
166 {
167 next->prev = _new;
168 _new->next = next;
169 _new->prev = prev;
170 prev->next = _new;
171 }
172
173 /**
174 * list_add - add a new entry
175 * @new: new entry to be added
176 * @head: list head to add it after
177 *
178 * Insert a new entry after the specified head.
179 * This is good for implementing stacks.
180 */
list_add(struct list_head * _new,struct list_head * head)181 static __inline void list_add(struct list_head *_new, struct list_head *head)
182 {
183 __list_add(_new, head, head->next);
184 }
185
186
187 /**
188 * list_add_tail - add a new entry
189 * @new: new entry to be added
190 * @head: list head to add it before
191 *
192 * Insert a new entry before the specified head.
193 * This is useful for implementing queues.
194 */
list_add_tail(struct list_head * _new,struct list_head * head)195 static __inline void list_add_tail(struct list_head *_new, struct list_head *head)
196 {
197 __list_add(_new, head->prev, head);
198 }
199
200 /*
201 * Delete a list entry by making the prev/next entries
202 * point to each other.
203 *
204 * This is only for internal list manipulation where we know
205 * the prev/next entries already!
206 */
__list_del(struct list_head * prev,struct list_head * next)207 static __inline void __list_del(struct list_head *prev, struct list_head *next)
208 {
209 next->prev = prev;
210 prev->next = next;
211 }
212
213 #define LIST_POISON1 ((void *) 0x00100100)
214 #define LIST_POISON2 ((void *) 0x00200200)
215 /**
216 * list_del - deletes entry from list.
217 * @entry: the element to delete from the list.
218 * Note: list_empty() on entry does not return true after this, the entry is
219 * in an undefined state.
220 */
list_del(struct list_head * entry)221 static __inline void list_del(struct list_head *entry)
222 {
223 __list_del(entry->prev, entry->next);
224 entry->next = (struct list_head*)LIST_POISON1;
225 entry->prev = (struct list_head*)LIST_POISON2;
226 }
227
228 #endif
229 #endif /*_PHL_LIST_H_*/
230