1 /* $OpenBSD: list.h,v 1.2 2021/07/27 13:36:59 mglocker Exp $ */
2 /* linux list functions for the BSDs, cut down for dwctwo(4).
3 * Created: Mon Apr 7 14:30:16 1999 by anholt@FreeBSD.org
4 */
5 /*-
6 * Copyright 2003 Eric Anholt
7 * All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the next
17 * paragraph) shall be included in all copies or substantial portions of the
18 * Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 * OTHER DEALINGS IN THE SOFTWARE.
27 *
28 * Authors:
29 * Eric Anholt <anholt@FreeBSD.org>
30 *
31 */
32
33 #ifndef _DWC2_LINUX_LIST_H_
34 #define _DWC2_LINUX_LIST_H_
35
36 /*
37 * From <dev/pci/drm/include/linux/kernel.h>
38 */
39 #define container_of(ptr, type, member) ({ \
40 const __typeof( ((type *)0)->member ) *__mptr = (ptr); \
41 (type *)( (char *)__mptr - offsetof(type,member) );})
42
43 /*
44 * From <dev/pci/drm/include/linux/types.h>
45 */
46 typedef unsigned int gfp_t;
47
48 struct list_head {
49 struct list_head *next, *prev;
50 };
51
52 /*
53 * From <dev/pci/drm/include/linux/list.h>
54 */
55 #define list_entry(ptr, type, member) container_of(ptr, type, member)
56
57 static inline void
INIT_LIST_HEAD(struct list_head * head)58 INIT_LIST_HEAD(struct list_head *head) {
59 (head)->next = head;
60 (head)->prev = head;
61 }
62
63 static inline int
list_empty(const struct list_head * head)64 list_empty(const struct list_head *head) {
65 return (head)->next == head;
66 }
67
68 static inline void
list_add(struct list_head * new,struct list_head * head)69 list_add(struct list_head *new, struct list_head *head) {
70 (head)->next->prev = new;
71 (new)->next = (head)->next;
72 (new)->prev = head;
73 (head)->next = new;
74 }
75
76 static inline void
list_add_tail(struct list_head * entry,struct list_head * head)77 list_add_tail(struct list_head *entry, struct list_head *head) {
78 (entry)->prev = (head)->prev;
79 (entry)->next = head;
80 (head)->prev->next = entry;
81 (head)->prev = entry;
82 }
83
84 static inline void
list_del(struct list_head * entry)85 list_del(struct list_head *entry) {
86 (entry)->next->prev = (entry)->prev;
87 (entry)->prev->next = (entry)->next;
88 }
89
list_move(struct list_head * list,struct list_head * head)90 static inline void list_move(struct list_head *list, struct list_head *head)
91 {
92 list_del(list);
93 list_add(list, head);
94 }
95
list_move_tail(struct list_head * list,struct list_head * head)96 static inline void list_move_tail(struct list_head *list,
97 struct list_head *head)
98 {
99 list_del(list);
100 list_add_tail(list, head);
101 }
102
103 static inline void
list_del_init(struct list_head * entry)104 list_del_init(struct list_head *entry) {
105 (entry)->next->prev = (entry)->prev;
106 (entry)->prev->next = (entry)->next;
107 INIT_LIST_HEAD(entry);
108 }
109
110 #define list_for_each(entry, head) \
111 for (entry = (head)->next; entry != head; entry = (entry)->next)
112
113 #define list_for_each_safe(entry, temp, head) \
114 for (entry = (head)->next, temp = (entry)->next; \
115 entry != head; \
116 entry = temp, temp = entry->next)
117
118 #define list_for_each_entry(pos, head, member) \
119 for (pos = list_entry((head)->next, __typeof(*pos), member); \
120 &pos->member != (head); \
121 pos = list_entry(pos->member.next, __typeof(*pos), member))
122
123 #define list_for_each_entry_safe(pos, n, head, member) \
124 for (pos = list_entry((head)->next, __typeof(*pos), member), \
125 n = list_entry(pos->member.next, __typeof(*pos), member); \
126 &pos->member != (head); \
127 pos = n, n = list_entry(n->member.next, __typeof(*n), member))
128
129 #define list_first_entry(ptr, type, member) \
130 list_entry((ptr)->next, type, member)
131
132 #endif /* _DWC2_LINUX_LIST_H_ */
133