xref: /OK3568_Linux_fs/kernel/tools/usb/usbip/libsrc/list.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _LIST_H
3*4882a593Smuzhiyun #define _LIST_H
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun /* Stripped down implementation of linked list taken
6*4882a593Smuzhiyun  * from the Linux Kernel.
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun /*
10*4882a593Smuzhiyun  * Simple doubly linked list implementation.
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * Some of the internal functions ("__xxx") are useful when
13*4882a593Smuzhiyun  * manipulating whole lists rather than single entries, as
14*4882a593Smuzhiyun  * sometimes we already know the next/prev entries and we can
15*4882a593Smuzhiyun  * generate better code by using them directly rather than
16*4882a593Smuzhiyun  * using the generic single-entry routines.
17*4882a593Smuzhiyun  */
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun struct list_head {
20*4882a593Smuzhiyun 	struct list_head *next, *prev;
21*4882a593Smuzhiyun };
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #define LIST_HEAD_INIT(name) { &(name), &(name) }
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #define LIST_HEAD(name) \
26*4882a593Smuzhiyun 	struct list_head name = LIST_HEAD_INIT(name)
27*4882a593Smuzhiyun 
INIT_LIST_HEAD(struct list_head * list)28*4882a593Smuzhiyun static inline void INIT_LIST_HEAD(struct list_head *list)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun 	list->next = list;
31*4882a593Smuzhiyun 	list->prev = list;
32*4882a593Smuzhiyun }
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun /*
35*4882a593Smuzhiyun  * Insert a new entry between two known consecutive entries.
36*4882a593Smuzhiyun  *
37*4882a593Smuzhiyun  * This is only for internal list manipulation where we know
38*4882a593Smuzhiyun  * the prev/next entries already!
39*4882a593Smuzhiyun  */
__list_add(struct list_head * new,struct list_head * prev,struct list_head * next)40*4882a593Smuzhiyun static inline void __list_add(struct list_head *new,
41*4882a593Smuzhiyun 			      struct list_head *prev,
42*4882a593Smuzhiyun 			      struct list_head *next)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun 	next->prev = new;
45*4882a593Smuzhiyun 	new->next = next;
46*4882a593Smuzhiyun 	new->prev = prev;
47*4882a593Smuzhiyun 	prev->next = new;
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun /**
51*4882a593Smuzhiyun  * list_add - add a new entry
52*4882a593Smuzhiyun  * @new: new entry to be added
53*4882a593Smuzhiyun  * @head: list head to add it after
54*4882a593Smuzhiyun  *
55*4882a593Smuzhiyun  * Insert a new entry after the specified head.
56*4882a593Smuzhiyun  * This is good for implementing stacks.
57*4882a593Smuzhiyun  */
list_add(struct list_head * new,struct list_head * head)58*4882a593Smuzhiyun static inline void list_add(struct list_head *new, struct list_head *head)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun 	__list_add(new, head, head->next);
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun /*
64*4882a593Smuzhiyun  * Delete a list entry by making the prev/next entries
65*4882a593Smuzhiyun  * point to each other.
66*4882a593Smuzhiyun  *
67*4882a593Smuzhiyun  * This is only for internal list manipulation where we know
68*4882a593Smuzhiyun  * the prev/next entries already!
69*4882a593Smuzhiyun  */
__list_del(struct list_head * prev,struct list_head * next)70*4882a593Smuzhiyun static inline void __list_del(struct list_head * prev, struct list_head * next)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun 	next->prev = prev;
73*4882a593Smuzhiyun 	prev->next = next;
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun #define POISON_POINTER_DELTA 0
77*4882a593Smuzhiyun #define LIST_POISON1  ((void *) 0x00100100 + POISON_POINTER_DELTA)
78*4882a593Smuzhiyun #define LIST_POISON2  ((void *) 0x00200200 + POISON_POINTER_DELTA)
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun /**
81*4882a593Smuzhiyun  * list_del - deletes entry from list.
82*4882a593Smuzhiyun  * @entry: the element to delete from the list.
83*4882a593Smuzhiyun  * Note: list_empty() on entry does not return true after this, the entry is
84*4882a593Smuzhiyun  * in an undefined state.
85*4882a593Smuzhiyun  */
__list_del_entry(struct list_head * entry)86*4882a593Smuzhiyun static inline void __list_del_entry(struct list_head *entry)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun 	__list_del(entry->prev, entry->next);
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun 
list_del(struct list_head * entry)91*4882a593Smuzhiyun static inline void list_del(struct list_head *entry)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun 	__list_del(entry->prev, entry->next);
94*4882a593Smuzhiyun 	entry->next = LIST_POISON1;
95*4882a593Smuzhiyun 	entry->prev = LIST_POISON2;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun /**
99*4882a593Smuzhiyun  * list_entry - get the struct for this entry
100*4882a593Smuzhiyun  * @ptr:	the &struct list_head pointer.
101*4882a593Smuzhiyun  * @type:	the type of the struct this is embedded in.
102*4882a593Smuzhiyun  * @member:	the name of the list_head within the struct.
103*4882a593Smuzhiyun  */
104*4882a593Smuzhiyun #define list_entry(ptr, type, member) \
105*4882a593Smuzhiyun 	container_of(ptr, type, member)
106*4882a593Smuzhiyun /**
107*4882a593Smuzhiyun  * list_for_each	-	iterate over a list
108*4882a593Smuzhiyun  * @pos:	the &struct list_head to use as a loop cursor.
109*4882a593Smuzhiyun  * @head:	the head for your list.
110*4882a593Smuzhiyun  */
111*4882a593Smuzhiyun #define list_for_each(pos, head) \
112*4882a593Smuzhiyun 	for (pos = (head)->next; pos != (head); pos = pos->next)
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun /**
115*4882a593Smuzhiyun  * list_for_each_safe - iterate over a list safe against removal of list entry
116*4882a593Smuzhiyun  * @pos:	the &struct list_head to use as a loop cursor.
117*4882a593Smuzhiyun  * @n:		another &struct list_head to use as temporary storage
118*4882a593Smuzhiyun  * @head:	the head for your list.
119*4882a593Smuzhiyun  */
120*4882a593Smuzhiyun #define list_for_each_safe(pos, n, head) \
121*4882a593Smuzhiyun 	for (pos = (head)->next, n = pos->next; pos != (head); \
122*4882a593Smuzhiyun 		pos = n, n = pos->next)
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun /**
127*4882a593Smuzhiyun  * container_of - cast a member of a structure out to the containing structure
128*4882a593Smuzhiyun  * @ptr:	the pointer to the member.
129*4882a593Smuzhiyun  * @type:	the type of the container struct this is embedded in.
130*4882a593Smuzhiyun  * @member:	the name of the member within the struct.
131*4882a593Smuzhiyun  *
132*4882a593Smuzhiyun  */
133*4882a593Smuzhiyun #define container_of(ptr, type, member) ({			\
134*4882a593Smuzhiyun 	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
135*4882a593Smuzhiyun 	(type *)( (char *)__mptr - offsetof(type,member) );})
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun #endif
138