aboutsummaryrefslogtreecommitdiff
path: root/lib/chibios-contrib/os/hal/include/usbh/list.h
diff options
context:
space:
mode:
authorAkshay <[email protected]>2022-04-10 12:13:40 +0100
committerAkshay <[email protected]>2022-04-10 12:13:40 +0100
commitdc90387ce7d8ba7b607d9c48540bf6d8b560f14d (patch)
tree4ccb8fa5886b66fa9d480edef74236c27f035e16 /lib/chibios-contrib/os/hal/include/usbh/list.h
Diffstat (limited to 'lib/chibios-contrib/os/hal/include/usbh/list.h')
-rw-r--r--lib/chibios-contrib/os/hal/include/usbh/list.h590
1 files changed, 590 insertions, 0 deletions
diff --git a/lib/chibios-contrib/os/hal/include/usbh/list.h b/lib/chibios-contrib/os/hal/include/usbh/list.h
new file mode 100644
index 000000000..034c6ff17
--- /dev/null
+++ b/lib/chibios-contrib/os/hal/include/usbh/list.h
@@ -0,0 +1,590 @@
1#ifndef USBH_LIST_H_
2#define USBH_LIST_H_
3
4/* TODO: re-write this file; stolen from linux */
5
6#ifndef offsetof
7#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
8#endif
9
10#ifndef container_of
11#define container_of(ptr, type, member) ((type *)(void *)((char *)(ptr) - offsetof(type, member)))
12#endif
13
14/*
15 * Simple doubly linked list implementation.
16 *
17 * Some of the internal functions ("__xxx") are useful when
18 * manipulating whole lists rather than single entries, as
19 * sometimes we already know the next/prev entries and we can
20 * generate better code by using them directly rather than
21 * using the generic single-entry routines.
22 */
23struct list_head {
24 struct list_head *next, *prev;
25};
26
27#define LIST_HEAD_INIT(name) { &(name), &(name) }
28
29#define LIST_HEAD(name) \
30 struct list_head name = LIST_HEAD_INIT(name)
31
32static inline void INIT_LIST_HEAD(struct list_head *list)
33{
34 list->next = list;
35 list->prev = list;
36}
37
38/*
39 * Insert a new entry between two known consecutive entries.
40 *
41 * This is only for internal list manipulation where we know
42 * the prev/next entries already!
43 */
44static inline void __list_add(struct list_head *_new,
45 struct list_head *prev,
46 struct list_head *next)
47{
48 next->prev = _new;
49 _new->next = next;
50 _new->prev = prev;
51 prev->next = _new;
52}
53
54/**
55 * list_add - add a new entry
56 * @new: new entry to be added
57 * @head: list head to add it after
58 *
59 * Insert a new entry after the specified head.
60 * This is good for implementing stacks.
61 */
62static inline void list_add(struct list_head *_new, struct list_head *head)
63{
64 __list_add(_new, head, head->next);
65}
66
67
68/**
69 * list_add_tail - add a new entry
70 * @new: new entry to be added
71 * @head: list head to add it before
72 *
73 * Insert a new entry before the specified head.
74 * This is useful for implementing queues.
75 */
76static inline void list_add_tail(struct list_head *_new, struct list_head *head)
77{
78 __list_add(_new, head->prev, head);
79}
80
81/*
82 * Delete a list entry by making the prev/next entries
83 * point to each other.
84 *
85 * This is only for internal list manipulation where we know
86 * the prev/next entries already!
87 */
88static inline void __list_del(struct list_head * prev, struct list_head * next)
89{
90 next->prev = prev;
91 prev->next = next;
92}
93
94/**
95 * list_del - deletes entry from list.
96 * @entry: the element to delete from the list.
97 * Note: list_empty() on entry does not return true after this, the entry is
98 * in an undefined state.
99 */
100
101static inline void __list_del_entry(struct list_head *entry)
102{
103 __list_del(entry->prev, entry->next);
104}
105
106static inline void list_del(struct list_head *entry)
107{
108 __list_del(entry->prev, entry->next);
109}
110
111/**
112 * list_del_init - deletes entry from list and reinitialize it.
113 * @entry: the element to delete from the list.
114 */
115static inline void list_del_init(struct list_head *entry)
116{
117 __list_del_entry(entry);
118 INIT_LIST_HEAD(entry);
119}
120
121/**
122 * list_move_tail - delete from one list and add as another's tail
123 * @list: the entry to move
124 * @head: the head that will follow our entry
125 */
126static inline void list_move_tail(struct list_head *list,
127 struct list_head *head)
128{
129 __list_del_entry(list);
130 list_add_tail(list, head);
131}
132
133
134/**
135 * list_empty - tests whether a list is empty
136 * @head: the list to test.
137 */
138static inline int list_empty(const struct list_head *head)
139{
140 return head->next == head;
141}
142
143/**
144 * list_entry - get the struct for this entry
145 * @ptr: the &struct list_head pointer.
146 * @type: the type of the struct this is embedded in.
147 * @member: the name of the list_head within the struct.
148 */
149#define list_entry(ptr, type, member) \
150 container_of(ptr, type, member)
151
152/**
153 * list_first_entry - get the first element from a list
154 * @ptr: the list head to take the element from.
155 * @type: the type of the struct this is embedded in.
156 * @member: the name of the list_head within the struct.
157 *
158 * Note, that list is expected to be not empty.
159 */
160#define list_first_entry(ptr, type, member) \
161 list_entry((ptr)->next, type, member)
162
163/**
164 * list_next_entry - get the next element in list
165 * @pos: the type * to cursor
166 * @member: the name of the list_head within the struct.
167 */
168#define list_next_entry(pos, type, member) \
169 list_entry((pos)->member.next, type, member)
170
171/**
172 * list_for_each_entry - iterate over list of given type
173 * @pos: the type * to use as a loop cursor.
174 * @head: the head for your list.
175 * @member: the name of the list_head within the struct.
176 */
177#define list_for_each_entry(pos, type, head, member) \
178 for (pos = list_first_entry(head, type, member); \
179 &pos->member != (head); \
180 pos = list_next_entry(pos, type, member))
181
182
183/**
184 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
185 * @pos: the type * to use as a loop cursor.
186 * @n: another type * to use as temporary storage
187 * @head: the head for your list.
188 * @member: the name of the list_head within the struct.
189 */
190#define list_for_each_entry_safe(pos, type, n, head, member) \
191 for (pos = list_first_entry(head, type, member), \
192 n = list_next_entry(pos, type, member); \
193 &pos->member != (head); \
194 pos = n, n = list_next_entry(n, type, member))
195
196#if 0
197
198/**
199 * list_for_each - iterate over a list
200 * @pos: the &struct list_head to use as a loop cursor.
201 * @head: the head for your list.
202 */
203#define list_for_each(pos, head) \
204 for (pos = (head)->next; pos != (head); pos = pos->next)
205
206/**
207 * list_for_each_safe - iterate over a list safe against removal of list entry
208 * @pos: the &struct list_head to use as a loop cursor.
209 * @n: another &struct list_head to use as temporary storage
210 * @head: the head for your list.
211 */
212#define list_for_each_safe(pos, n, head) \
213 for (pos = (head)->next, n = pos->next; pos != (head); \
214 pos = n, n = pos->next)
215
216/**
217 * list_prev_entry - get the prev element in list
218 * @pos: the type * to cursor
219 * @member: the name of the list_head within the struct.
220 */
221#define list_prev_entry(pos, type, member) \
222 list_entry((pos)->member.prev, type, member)
223
224/**
225 * list_for_each_prev - iterate over a list backwards
226 * @pos: the &struct list_head to use as a loop cursor.
227 * @head: the head for your list.
228 */
229#define list_for_each_prev(pos, head) \
230 for (pos = (head)->prev; pos != (head); pos = pos->prev)
231
232/**
233 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
234 * @pos: the &struct list_head to use as a loop cursor.
235 * @n: another &struct list_head to use as temporary storage
236 * @head: the head for your list.
237 */
238#define list_for_each_prev_safe(pos, n, head) \
239 for (pos = (head)->prev, n = pos->prev; \
240 pos != (head); \
241 pos = n, n = pos->prev)
242
243/**
244 * list_last_entry - get the last element from a list
245 * @ptr: the list head to take the element from.
246 * @type: the type of the struct this is embedded in.
247 * @member: the name of the list_head within the struct.
248 *
249 * Note, that list is expected to be not empty.
250 */
251#define list_last_entry(ptr, type, member) \
252 list_entry((ptr)->prev, type, member)
253
254/**
255 * list_first_entry_or_null - get the first element from a list
256 * @ptr: the list head to take the element from.
257 * @type: the type of the struct this is embedded in.
258 * @member: the name of the list_head within the struct.
259 *
260 * Note that if the list is empty, it returns NULL.
261 */
262#define list_first_entry_or_null(ptr, type, member) \
263 (!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
264
265
266/**
267 * list_move - delete from one list and add as another's head
268 * @list: the entry to move
269 * @head: the head that will precede our entry
270 */
271static inline void list_move(struct list_head *list, struct list_head *head)
272{
273 __list_del_entry(list);
274 list_add(list, head);
275}
276
277/**
278 * list_is_last - tests whether @list is the last entry in list @head
279 * @list: the entry to test
280 * @head: the head of the list
281 */
282static inline int list_is_last(const struct list_head *list,
283 const struct list_head *head)
284{
285 return list->next == head;
286}
287
288/**
289 * list_empty_careful - tests whether a list is empty and not being modified
290 * @head: the list to test
291 *
292 * Description:
293 * tests whether a list is empty _and_ checks that no other CPU might be
294 * in the process of modifying either member (next or prev)
295 *
296 * NOTE: using list_empty_careful() without synchronization
297 * can only be safe if the only activity that can happen
298 * to the list entry is list_del_init(). Eg. it cannot be used
299 * if another CPU could re-list_add() it.
300 */
301static inline int list_empty_careful(const struct list_head *head)
302{
303 struct list_head *next = head->next;
304 return (next == head) && (next == head->prev);
305}
306
307/**
308 * list_rotate_left - rotate the list to the left
309 * @head: the head of the list
310 */
311static inline void list_rotate_left(struct list_head *head)
312{
313 struct list_head *first;
314
315 if (!list_empty(head)) {
316 first = head->next;
317 list_move_tail(first, head);
318 }
319}
320
321/**
322 * list_is_singular - tests whether a list has just one entry.
323 * @head: the list to test.
324 */
325static inline int list_is_singular(const struct list_head *head)
326{
327 return !list_empty(head) && (head->next == head->prev);
328}
329
330static inline void __list_cut_position(struct list_head *list,
331 struct list_head *head, struct list_head *entry)
332{
333 struct list_head *new_first = entry->next;
334 list->next = head->next;
335 list->next->prev = list;
336 list->prev = entry;
337 entry->next = list;
338 head->next = new_first;
339 new_first->prev = head;
340}
341
342/**
343 * list_cut_position - cut a list into two
344 * @list: a new list to add all removed entries
345 * @head: a list with entries
346 * @entry: an entry within head, could be the head itself
347 * and if so we won't cut the list
348 *
349 * This helper moves the initial part of @head, up to and
350 * including @entry, from @head to @list. You should
351 * pass on @entry an element you know is on @head. @list
352 * should be an empty list or a list you do not care about
353 * losing its data.
354 *
355 */
356static inline void list_cut_position(struct list_head *list,
357 struct list_head *head, struct list_head *entry)
358{
359 if (list_empty(head))
360 return;
361 if (list_is_singular(head) &&
362 (head->next != entry && head != entry))
363 return;
364 if (entry == head)
365 INIT_LIST_HEAD(list);
366 else
367 __list_cut_position(list, head, entry);
368}
369
370static inline void __list_splice(const struct list_head *list,
371 struct list_head *prev,
372 struct list_head *next)
373{
374 struct list_head *first = list->next;
375 struct list_head *last = list->prev;
376
377 first->prev = prev;
378 prev->next = first;
379
380 last->next = next;
381 next->prev = last;
382}
383
384/**
385 * list_splice - join two lists, this is designed for stacks
386 * @list: the new list to add.
387 * @head: the place to add it in the first list.
388 */
389static inline void list_splice(const struct list_head *list,
390 struct list_head *head)
391{
392 if (!list_empty(list))
393 __list_splice(list, head, head->next);
394}
395
396/**
397 * list_splice_tail - join two lists, each list being a queue
398 * @list: the new list to add.
399 * @head: the place to add it in the first list.
400 */
401static inline void list_splice_tail(struct list_head *list,
402 struct list_head *head)
403{
404 if (!list_empty(list))
405 __list_splice(list, head->prev, head);
406}
407
408/**
409 * list_splice_init - join two lists and reinitialise the emptied list.
410 * @list: the new list to add.
411 * @head: the place to add it in the first list.
412 *
413 * The list at @list is reinitialised
414 */
415static inline void list_splice_init(struct list_head *list,
416 struct list_head *head)
417{
418 if (!list_empty(list)) {
419 __list_splice(list, head, head->next);
420 INIT_LIST_HEAD(list);
421 }
422}
423
424/**
425 * list_splice_tail_init - join two lists and reinitialise the emptied list
426 * @list: the new list to add.
427 * @head: the place to add it in the first list.
428 *
429 * Each of the lists is a queue.
430 * The list at @list is reinitialised
431 */
432static inline void list_splice_tail_init(struct list_head *list,
433 struct list_head *head)
434{
435 if (!list_empty(list)) {
436 __list_splice(list, head->prev, head);
437 INIT_LIST_HEAD(list);
438 }
439}
440
441/**
442 * list_replace - replace old entry by new one
443 * @old : the element to be replaced
444 * @new : the new element to insert
445 *
446 * If @old was empty, it will be overwritten.
447 */
448static inline void list_replace(struct list_head *old,
449 struct list_head *_new)
450{
451 _new->next = old->next;
452 _new->next->prev = _new;
453 _new->prev = old->prev;
454 _new->prev->next = _new;
455}
456
457static inline void list_replace_init(struct list_head *old,
458 struct list_head *_new)
459{
460 list_replace(old, _new);
461 INIT_LIST_HEAD(old);
462}
463
464
465/**
466 * list_for_each_entry_reverse - iterate backwards over list of given type.
467 * @pos: the type * to use as a loop cursor.
468 * @head: the head for your list.
469 * @member: the name of the list_head within the struct.
470 */
471#define list_for_each_entry_reverse(pos, type, head, member) \
472 for (pos = list_last_entry(head, type, member); \
473 &pos->member != (head); \
474 pos = list_prev_entry(pos, type, member))
475
476/**
477 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
478 * @pos: the type * to use as a start point
479 * @head: the head of the list
480 * @member: the name of the list_head within the struct.
481 *
482 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
483 */
484#define list_prepare_entry(pos, type, head, member) \
485 ((pos) ? : list_entry(head, type, member))
486
487/**
488 * list_for_each_entry_continue - continue iteration over list of given type
489 * @pos: the type * to use as a loop cursor.
490 * @head: the head for your list.
491 * @member: the name of the list_head within the struct.
492 *
493 * Continue to iterate over list of given type, continuing after
494 * the current position.
495 */
496#define list_for_each_entry_continue(pos, type, head, member) \
497 for (pos = list_next_entry(pos, type, member); \
498 &pos->member != (head); \
499 pos = list_next_entry(pos, type, member))
500
501/**
502 * list_for_each_entry_continue_reverse - iterate backwards from the given point
503 * @pos: the type * to use as a loop cursor.
504 * @head: the head for your list.
505 * @member: the name of the list_head within the struct.
506 *
507 * Start to iterate over list of given type backwards, continuing after
508 * the current position.
509 */
510#define list_for_each_entry_continue_reverse(pos, type, head, member) \
511 for (pos = list_prev_entry(pos, type, member); \
512 &pos->member != (head); \
513 pos = list_prev_entry(pos, type, member))
514
515/**
516 * list_for_each_entry_from - iterate over list of given type from the current point
517 * @pos: the type * to use as a loop cursor.
518 * @head: the head for your list.
519 * @member: the name of the list_head within the struct.
520 *
521 * Iterate over list of given type, continuing from current position.
522 */
523#define list_for_each_entry_from(pos, type, head, member) \
524 for (; &pos->member != (head); \
525 pos = list_next_entry(pos, type, member))
526
527/**
528 * list_for_each_entry_safe_continue - continue list iteration safe against removal
529 * @pos: the type * to use as a loop cursor.
530 * @n: another type * to use as temporary storage
531 * @head: the head for your list.
532 * @member: the name of the list_head within the struct.
533 *
534 * Iterate over list of given type, continuing after current point,
535 * safe against removal of list entry.
536 */
537#define list_for_each_entry_safe_continue(pos, type, n, head, member) \
538 for (pos = list_next_entry(pos, type, member), \
539 n = list_next_entry(pos, type, member); \
540 &pos->member != (head); \
541 pos = n, n = list_next_entry(n, type, member))
542
543/**
544 * list_for_each_entry_safe_from - iterate over list from current point safe against removal
545 * @pos: the type * to use as a loop cursor.
546 * @n: another type * to use as temporary storage
547 * @head: the head for your list.
548 * @member: the name of the list_head within the struct.
549 *
550 * Iterate over list of given type from current point, safe against
551 * removal of list entry.
552 */
553#define list_for_each_entry_safe_from(pos, type, n, head, member) \
554 for (n = list_next_entry(pos, type, member); \
555 &pos->member != (head); \
556 pos = n, n = list_next_entry(n, type, member))
557
558/**
559 * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
560 * @pos: the type * to use as a loop cursor.
561 * @n: another type * to use as temporary storage
562 * @head: the head for your list.
563 * @member: the name of the list_head within the struct.
564 *
565 * Iterate backwards over list of given type, safe against removal
566 * of list entry.
567 */
568#define list_for_each_entry_safe_reverse(pos, type, n, head, member) \
569 for (pos = list_last_entry(head, type, member), \
570 n = list_prev_entry(pos, type, member); \
571 &pos->member != (head); \
572 pos = n, n = list_prev_entry(n, type, member))
573
574/**
575 * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
576 * @pos: the loop cursor used in the list_for_each_entry_safe loop
577 * @n: temporary storage used in list_for_each_entry_safe
578 * @member: the name of the list_head within the struct.
579 *
580 * list_safe_reset_next is not safe to use in general if the list may be
581 * modified concurrently (eg. the lock is dropped in the loop body). An
582 * exception to this is if the cursor element (pos) is pinned in the list,
583 * and list_safe_reset_next is called after re-taking the lock and before
584 * completing the current iteration of the loop body.
585 */
586#define list_safe_reset_next(pos, type, n, member) \
587 n = list_next_entry(pos, type, member)
588#endif
589
590#endif /* USBH_LIST_H_ */