1 /*****************************************************************************
2 *
3 * Linux kernel header adapted for user-mode
4 * The 2.6.17-rt1 version was used.
5 *
6 * Original copyright holders of this code are unknown, they were not
7 * mentioned in the original file.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 *****************************************************************************/
23
24 #ifndef LINUX_LIST_H_INCLUDED
25 #define LINUX_LIST_H_INCLUDED
26
27 /* This file is from Linux Kernel (include/linux/list.h)
28 * and modified by removing hardware prefetching of list items
29 * and added list_split_tail* functions.
30 *
31 * Here by copyright, credits attributed to wherever they belong.
32 * Filipe Coelho (aka falkTX <falktx@falktx.com>)
33 */
34
35 #ifdef __cplusplus
36 # include <cstddef>
37 #else
38 # include <stddef.h>
39 #endif
40
41 #ifndef offsetof
42 # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
43 #endif
44
45 /**
46 * container_of - cast a member of a structure out to the containing structure
47 * @param ptr: the pointer to the member.
48 * @param type: the type of the container struct this is embedded in.
49 * @param member the name of the member within the struct.
50 *
51 */
52 #define container_of(ptr, type, member) ({ \
53 typeof( ((type *)0)->member ) *__mptr = (ptr); \
54 (type *)( (char *)__mptr - offsetof(type, member) );})
55
56 #define container_of_const(ptr, type, member) ({ \
57 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
58 (const type *)( (const char *)__mptr - offsetof(type, member) );})
59
60 #define prefetch(x) (x = x)
61
62 /*
63 * These are non-NULL pointers that will result in page faults
64 * under normal circumstances, used to verify that nobody uses
65 * non-initialized list entries.
66 */
67 #define LIST_POISON1 ((void *) 0x00100100)
68 #define LIST_POISON2 ((void *) 0x00200200)
69
70 /*
71 * Simple doubly linked list implementation.
72 *
73 * Some of the internal functions ("__xxx") are useful when
74 * manipulating whole lists rather than single entries, as
75 * sometimes we already know the next/prev entries and we can
76 * generate better code by using them directly rather than
77 * using the generic single-entry routines.
78 */
79
80 struct list_head {
81 struct list_head *next, *prev;
82 };
83
84 #define LIST_HEAD_INIT(name) { &(name), &(name) }
85
86 #define LIST_HEAD(name) \
87 struct list_head name = LIST_HEAD_INIT(name)
88
INIT_LIST_HEAD(struct list_head * list)89 static inline void INIT_LIST_HEAD(struct list_head *list)
90 {
91 list->next = list;
92 list->prev = list;
93 }
94
95 /*
96 * Insert a new entry between two known consecutive entries.
97 *
98 * This is only for internal list manipulation where we know
99 * the prev/next entries already!
100 */
__list_add(struct list_head * new_,struct list_head * prev,struct list_head * next)101 static inline void __list_add(struct list_head *new_, struct list_head *prev, struct list_head *next)
102 {
103 next->prev = new_;
104 new_->next = next;
105 new_->prev = prev;
106 prev->next = new_;
107 }
108
109 /**
110 * list_add - add a new entry
111 * @param new_ new entry to be added
112 * @param head list head to add it after
113 *
114 * Insert a new entry after the specified head.
115 * This is good for implementing stacks.
116 */
list_add(struct list_head * new_,struct list_head * head)117 static inline void list_add(struct list_head *new_, struct list_head *head)
118 {
119 __list_add(new_, head, head->next);
120 }
121
122 /**
123 * list_add_tail - add a new entry
124 * @param new_ new entry to be added
125 * @param head list head to add it before
126 *
127 * Insert a new entry before the specified head.
128 * This is useful for implementing queues.
129 */
list_add_tail(struct list_head * new_,struct list_head * head)130 static inline void list_add_tail(struct list_head *new_, struct list_head *head)
131 {
132 __list_add(new_, head->prev, head);
133 }
134
135 /*
136 * Delete a list entry by making the prev/next entries
137 * point to each other.
138 *
139 * This is only for internal list manipulation where we know
140 * the prev/next entries already!
141 */
__list_del(struct list_head * prev,struct list_head * next)142 static inline void __list_del(struct list_head *prev, struct list_head *next)
143 {
144 next->prev = prev;
145 prev->next = next;
146 }
147
148 /**
149 * list_del - deletes entry from list.
150 * @param entry the element to delete from the list.
151 * Note: list_empty on entry does not return true after this, the entry is
152 * in an undefined state.
153 */
list_del(struct list_head * entry)154 static inline void list_del(struct list_head *entry)
155 {
156 __list_del(entry->prev, entry->next);
157 entry->next = (struct list_head*)LIST_POISON1;
158 entry->prev = (struct list_head*)LIST_POISON2;
159 }
160
161 /**
162 * list_del_init - deletes entry from list and reinitialize it.
163 * @param entry the element to delete from the list.
164 */
list_del_init(struct list_head * entry)165 static inline void list_del_init(struct list_head *entry)
166 {
167 __list_del(entry->prev, entry->next);
168 INIT_LIST_HEAD(entry);
169 }
170
171 /**
172 * list_move - delete from one list and add as another's head
173 * @param list the entry to move
174 * @param head the head that will precede our entry
175 */
list_move(struct list_head * list,struct list_head * head)176 static inline void list_move(struct list_head *list, struct list_head *head)
177 {
178 __list_del(list->prev, list->next);
179 list_add(list, head);
180 }
181
182 /**
183 * list_move_tail - delete from one list and add as another's tail
184 * @param list the entry to move
185 * @param head the head that will follow our entry
186 */
list_move_tail(struct list_head * list,struct list_head * head)187 static inline void list_move_tail(struct list_head *list, struct list_head *head)
188 {
189 __list_del(list->prev, list->next);
190 list_add_tail(list, head);
191 }
192
193 /**
194 * list_empty - tests whether a list is empty
195 * @param head the list to test.
196 */
list_empty(const struct list_head * head)197 static inline int list_empty(const struct list_head *head)
198 {
199 return head->next == head;
200 }
201
202 /**
203 * list_empty_careful - tests whether a list is
204 * empty _and_ checks that no other CPU might be
205 * in the process of still modifying either member
206 *
207 * NOTE: using list_empty_careful() without synchronization
208 * can only be safe if the only activity that can happen
209 * to the list entry is list_del_init(). Eg. it cannot be used
210 * if another CPU could re-list_add() it.
211 *
212 * @param head the list to test.
213 */
list_empty_careful(const struct list_head * head)214 static inline int list_empty_careful(const struct list_head *head)
215 {
216 struct list_head *next = head->next;
217 return (next == head) && (next == head->prev);
218 }
219
__list_splice(struct list_head * list,struct list_head * head)220 static inline void __list_splice(struct list_head *list, struct list_head *head)
221 {
222 struct list_head *first = list->next;
223 struct list_head *last = list->prev;
224 struct list_head *at = head->next;
225
226 first->prev = head;
227 head->next = first;
228
229 last->next = at;
230 at->prev = last;
231 }
232
__list_splice_tail(struct list_head * list,struct list_head * head)233 static inline void __list_splice_tail(struct list_head *list, struct list_head *head)
234 {
235 struct list_head *first = list->next;
236 struct list_head *last = list->prev;
237 struct list_head *at = head->prev;
238
239 first->prev = at;
240 at->next = first;
241
242 last->next = head;
243 head->prev = last;
244 }
245
246 /**
247 * list_splice - join two lists
248 * @param list the new list to add.
249 * @param head the place to add it in the first list.
250 */
list_splice(struct list_head * list,struct list_head * head)251 static inline void list_splice(struct list_head *list, struct list_head *head)
252 {
253 if (!list_empty(list))
254 __list_splice(list, head);
255 }
256
257 /**
258 * list_splice_tail - join two lists
259 * @param list the new list to add.
260 * @param head the place to add it in the first list.
261 *
262 * @a list goes to the end (at head->prev)
263 */
list_splice_tail(struct list_head * list,struct list_head * head)264 static inline void list_splice_tail(struct list_head *list, struct list_head *head)
265 {
266 if (!list_empty(list))
267 __list_splice_tail(list, head);
268 }
269
270 /**
271 * list_splice_init - join two lists and reinitialise the emptied list.
272 * @param list the new list to add.
273 * @param head the place to add it in the first list.
274 *
275 * The list at @a list is reinitialised
276 */
list_splice_init(struct list_head * list,struct list_head * head)277 static inline void list_splice_init(struct list_head *list, struct list_head *head)
278 {
279 if (!list_empty(list)) {
280 __list_splice(list, head);
281 INIT_LIST_HEAD(list);
282 }
283 }
284
285 /**
286 * list_splice_tail_init - join two lists and reinitialise the emptied list.
287 * @param list the new list to add.
288 * @param head the place to add it in the first list.
289 *
290 * The list @a list is reinitialised
291 * @a list goes to the end (at head->prev)
292 */
list_splice_tail_init(struct list_head * list,struct list_head * head)293 static inline void list_splice_tail_init(struct list_head *list, struct list_head *head)
294 {
295 if (!list_empty(list)) {
296 __list_splice_tail(list, head);
297 INIT_LIST_HEAD(list);
298 }
299 }
300
301 /**
302 * list_entry - get the struct for this entry
303 * @param ptr: the &struct list_head pointer.
304 * @param type: the type of the struct this is embedded in.
305 * @param member the name of the list_struct within the struct.
306 */
307 #if (defined(__GNUC__) || defined(__clang__)) && ! defined(__STRICT_ANSI__)
308 # define list_entry(ptr, type, member) \
309 container_of(ptr, type, member)
310 # define list_entry_const(ptr, type, member) \
311 container_of_const(ptr, type, member)
312 #else
313 # define list_entry(ptr, type, member) \
314 ((type *)((char *)(ptr)-offsetof(type, member)))
315 # define list_entry_const(ptr, type, member) \
316 ((const type *)((const char *)(ptr)-offsetof(type, member)))
317 #endif
318
319 /**
320 * list_for_each - iterate over a list
321 * @param pos the &struct list_head to use as a loop counter.
322 * @param head the head for your list.
323 */
324 #define list_for_each(pos, head) \
325 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
326 pos = pos->next)
327
328 /**
329 * __list_for_each - iterate over a list
330 * @param pos the &struct list_head to use as a loop counter.
331 * @param head the head for your list.
332 *
333 * This variant differs from list_for_each() in that it's the
334 * simplest possible list iteration code, no prefetching is done.
335 * Use this for code that knows the list to be very short (empty
336 * or 1 entry) most of the time.
337 */
338 #define __list_for_each(pos, head) \
339 for (pos = (head)->next; pos != (head); pos = pos->next)
340
341 /**
342 * list_for_each_prev - iterate over a list backwards
343 * @param pos the &struct list_head to use as a loop counter.
344 * @param head the head for your list.
345 */
346 #define list_for_each_prev(pos, head) \
347 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
348 pos = pos->prev)
349
350 /**
351 * list_for_each_safe - iterate over a list safe against removal of list entry
352 * @param pos the &struct list_head to use as a loop counter.
353 * @param n another &struct list_head to use as temporary storage
354 * @param head the head for your list.
355 */
356 #define list_for_each_safe(pos, n, head) \
357 for (pos = (head)->next, n = pos->next; pos != (head); \
358 pos = n, n = pos->next)
359
360 /**
361 * list_for_each_entry - iterate over list of given type
362 * @param pos the type * to use as a loop counter.
363 * @param head the head for your list.
364 * @param member the name of the list_struct within the struct.
365 */
366 #define list_for_each_entry(pos, head, member) \
367 for (pos = list_entry((head)->next, typeof(*pos), member); \
368 prefetch(pos->member.next), &pos->member != (head); \
369 pos = list_entry(pos->member.next, typeof(*pos), member))
370
371 /**
372 * list_for_each_entry_reverse - iterate backwards over list of given type.
373 * @param pos the type * to use as a loop counter.
374 * @param head the head for your list.
375 * @param member the name of the list_struct within the struct.
376 */
377 #define list_for_each_entry_reverse(pos, head, member) \
378 for (pos = list_entry((head)->prev, typeof(*pos), member); \
379 prefetch(pos->member.prev), &pos->member != (head); \
380 pos = list_entry(pos->member.prev, typeof(*pos), member))
381
382 /**
383 * list_prepare_entry - prepare a pos entry for use as a start point in list_for_each_entry_continue
384 * @param pos the type * to use as a start point
385 * @param head the head of the list
386 * @param member the name of the list_struct within the struct.
387 */
388 #define list_prepare_entry(pos, head, member) \
389 ((pos) ? : list_entry(head, typeof(*pos), member))
390
391 /**
392 * list_for_each_entry_continue - iterate over list of given type continuing after existing point
393 * @param pos the type * to use as a loop counter.
394 * @param head the head for your list.
395 * @param member the name of the list_struct within the struct.
396 */
397 #define list_for_each_entry_continue(pos, head, member) \
398 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
399 prefetch(pos->member.next), &pos->member != (head); \
400 pos = list_entry(pos->member.next, typeof(*pos), member))
401
402 /**
403 * list_for_each_entry_from - iterate over list of given type continuing from existing point
404 * @param pos the type * to use as a loop counter.
405 * @param head the head for your list.
406 * @param member the name of the list_struct within the struct.
407 */
408 #define list_for_each_entry_from(pos, head, member) \
409 for (; prefetch(pos->member.next), &pos->member != (head); \
410 pos = list_entry(pos->member.next, typeof(*pos), member))
411
412 /**
413 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
414 * @param pos the type * to use as a loop counter.
415 * @param n another type * to use as temporary storage
416 * @param head the head for your list.
417 * @param member the name of the list_struct within the struct.
418 */
419 #define list_for_each_entry_safe(pos, n, head, member) \
420 for (pos = list_entry((head)->next, typeof(*pos), member), \
421 n = list_entry(pos->member.next, typeof(*pos), member); \
422 &pos->member != (head); \
423 pos = n, n = list_entry(n->member.next, typeof(*n), member))
424
425 /**
426 * list_for_each_entry_safe_continue - iterate over list of given type continuing after existing point safe against removal of list entry
427 * @param pos the type * to use as a loop counter.
428 * @param n another type * to use as temporary storage
429 * @param head the head for your list.
430 * @param member the name of the list_struct within the struct.
431 */
432 #define list_for_each_entry_safe_continue(pos, n, head, member) \
433 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
434 n = list_entry(pos->member.next, typeof(*pos), member); \
435 &pos->member != (head); \
436 pos = n, n = list_entry(n->member.next, typeof(*n), member))
437
438 /**
439 * list_for_each_entry_safe_from - iterate over list of given type from existing point safe against removal of list entry
440 * @param pos the type * to use as a loop counter.
441 * @param n another type * to use as temporary storage
442 * @param head the head for your list.
443 * @param member the name of the list_struct within the struct.
444 */
445 #define list_for_each_entry_safe_from(pos, n, head, member) \
446 for (n = list_entry(pos->member.next, typeof(*pos), member); \
447 &pos->member != (head); \
448 pos = n, n = list_entry(n->member.next, typeof(*n), member))
449
450 /**
451 * list_for_each_entry_safe_reverse - iterate backwards over list of given type safe against removal of list entry
452 * @param pos the type * to use as a loop counter.
453 * @param n another type * to use as temporary storage
454 * @param head the head for your list.
455 * @param member the name of the list_struct within the struct.
456 */
457 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
458 for (pos = list_entry((head)->prev, typeof(*pos), member), \
459 n = list_entry(pos->member.prev, typeof(*pos), member); \
460 &pos->member != (head); \
461 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
462
463 #endif // LINUX_LIST_H_INCLUDED
464