1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef LIST_H
3*4882a593Smuzhiyun #define LIST_H
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun /*
6*4882a593Smuzhiyun * Copied from include/linux/...
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #undef offsetof
10*4882a593Smuzhiyun #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun /**
13*4882a593Smuzhiyun * container_of - cast a member of a structure out to the containing structure
14*4882a593Smuzhiyun * @ptr: the pointer to the member.
15*4882a593Smuzhiyun * @type: the type of the container struct this is embedded in.
16*4882a593Smuzhiyun * @member: the name of the member within the struct.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun */
19*4882a593Smuzhiyun #define container_of(ptr, type, member) ({ \
20*4882a593Smuzhiyun const typeof( ((type *)0)->member ) *__mptr = (ptr); \
21*4882a593Smuzhiyun (type *)( (char *)__mptr - offsetof(type,member) );})
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun struct list_head {
25*4882a593Smuzhiyun struct list_head *next, *prev;
26*4882a593Smuzhiyun };
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #define LIST_HEAD_INIT(name) { &(name), &(name) }
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #define LIST_HEAD(name) \
32*4882a593Smuzhiyun struct list_head name = LIST_HEAD_INIT(name)
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun /**
35*4882a593Smuzhiyun * list_entry - get the struct for this entry
36*4882a593Smuzhiyun * @ptr: the &struct list_head pointer.
37*4882a593Smuzhiyun * @type: the type of the struct this is embedded in.
38*4882a593Smuzhiyun * @member: the name of the list_head within the struct.
39*4882a593Smuzhiyun */
40*4882a593Smuzhiyun #define list_entry(ptr, type, member) \
41*4882a593Smuzhiyun container_of(ptr, type, member)
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun /**
44*4882a593Smuzhiyun * list_for_each_entry - iterate over list of given type
45*4882a593Smuzhiyun * @pos: the type * to use as a loop cursor.
46*4882a593Smuzhiyun * @head: the head for your list.
47*4882a593Smuzhiyun * @member: the name of the list_head within the struct.
48*4882a593Smuzhiyun */
49*4882a593Smuzhiyun #define list_for_each_entry(pos, head, member) \
50*4882a593Smuzhiyun for (pos = list_entry((head)->next, typeof(*pos), member); \
51*4882a593Smuzhiyun &pos->member != (head); \
52*4882a593Smuzhiyun pos = list_entry(pos->member.next, typeof(*pos), member))
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun /**
55*4882a593Smuzhiyun * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
56*4882a593Smuzhiyun * @pos: the type * to use as a loop cursor.
57*4882a593Smuzhiyun * @n: another type * to use as temporary storage
58*4882a593Smuzhiyun * @head: the head for your list.
59*4882a593Smuzhiyun * @member: the name of the list_head within the struct.
60*4882a593Smuzhiyun */
61*4882a593Smuzhiyun #define list_for_each_entry_safe(pos, n, head, member) \
62*4882a593Smuzhiyun for (pos = list_entry((head)->next, typeof(*pos), member), \
63*4882a593Smuzhiyun n = list_entry(pos->member.next, typeof(*pos), member); \
64*4882a593Smuzhiyun &pos->member != (head); \
65*4882a593Smuzhiyun pos = n, n = list_entry(n->member.next, typeof(*n), member))
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun /**
68*4882a593Smuzhiyun * list_empty - tests whether a list is empty
69*4882a593Smuzhiyun * @head: the list to test.
70*4882a593Smuzhiyun */
list_empty(const struct list_head * head)71*4882a593Smuzhiyun static inline int list_empty(const struct list_head *head)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun return head->next == head;
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun /*
77*4882a593Smuzhiyun * Insert a new entry between two known consecutive entries.
78*4882a593Smuzhiyun *
79*4882a593Smuzhiyun * This is only for internal list manipulation where we know
80*4882a593Smuzhiyun * the prev/next entries already!
81*4882a593Smuzhiyun */
__list_add(struct list_head * _new,struct list_head * prev,struct list_head * next)82*4882a593Smuzhiyun static inline void __list_add(struct list_head *_new,
83*4882a593Smuzhiyun struct list_head *prev,
84*4882a593Smuzhiyun struct list_head *next)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun next->prev = _new;
87*4882a593Smuzhiyun _new->next = next;
88*4882a593Smuzhiyun _new->prev = prev;
89*4882a593Smuzhiyun prev->next = _new;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun /**
93*4882a593Smuzhiyun * list_add_tail - add a new entry
94*4882a593Smuzhiyun * @new: new entry to be added
95*4882a593Smuzhiyun * @head: list head to add it before
96*4882a593Smuzhiyun *
97*4882a593Smuzhiyun * Insert a new entry before the specified head.
98*4882a593Smuzhiyun * This is useful for implementing queues.
99*4882a593Smuzhiyun */
list_add_tail(struct list_head * _new,struct list_head * head)100*4882a593Smuzhiyun static inline void list_add_tail(struct list_head *_new, struct list_head *head)
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun __list_add(_new, head->prev, head);
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun /*
106*4882a593Smuzhiyun * Delete a list entry by making the prev/next entries
107*4882a593Smuzhiyun * point to each other.
108*4882a593Smuzhiyun *
109*4882a593Smuzhiyun * This is only for internal list manipulation where we know
110*4882a593Smuzhiyun * the prev/next entries already!
111*4882a593Smuzhiyun */
__list_del(struct list_head * prev,struct list_head * next)112*4882a593Smuzhiyun static inline void __list_del(struct list_head *prev, struct list_head *next)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun next->prev = prev;
115*4882a593Smuzhiyun prev->next = next;
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun #define LIST_POISON1 ((void *) 0x00100100)
119*4882a593Smuzhiyun #define LIST_POISON2 ((void *) 0x00200200)
120*4882a593Smuzhiyun /**
121*4882a593Smuzhiyun * list_del - deletes entry from list.
122*4882a593Smuzhiyun * @entry: the element to delete from the list.
123*4882a593Smuzhiyun * Note: list_empty() on entry does not return true after this, the entry is
124*4882a593Smuzhiyun * in an undefined state.
125*4882a593Smuzhiyun */
list_del(struct list_head * entry)126*4882a593Smuzhiyun static inline void list_del(struct list_head *entry)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun __list_del(entry->prev, entry->next);
129*4882a593Smuzhiyun entry->next = (struct list_head*)LIST_POISON1;
130*4882a593Smuzhiyun entry->prev = (struct list_head*)LIST_POISON2;
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun #endif
133