xref: /openbmc/linux/scripts/kconfig/list.h (revision b2441318)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2bad9955dSBenjamin Poirier #ifndef LIST_H
3bad9955dSBenjamin Poirier #define LIST_H
4bad9955dSBenjamin Poirier 
5bad9955dSBenjamin Poirier /*
6bad9955dSBenjamin Poirier  * Copied from include/linux/...
7bad9955dSBenjamin Poirier  */
8bad9955dSBenjamin Poirier 
9bad9955dSBenjamin Poirier #undef offsetof
10bad9955dSBenjamin Poirier #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
11bad9955dSBenjamin Poirier 
12bad9955dSBenjamin Poirier /**
13bad9955dSBenjamin Poirier  * container_of - cast a member of a structure out to the containing structure
14bad9955dSBenjamin Poirier  * @ptr:        the pointer to the member.
15bad9955dSBenjamin Poirier  * @type:       the type of the container struct this is embedded in.
16bad9955dSBenjamin Poirier  * @member:     the name of the member within the struct.
17bad9955dSBenjamin Poirier  *
18bad9955dSBenjamin Poirier  */
19bad9955dSBenjamin Poirier #define container_of(ptr, type, member) ({                      \
20bad9955dSBenjamin Poirier 	const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
21bad9955dSBenjamin Poirier 	(type *)( (char *)__mptr - offsetof(type,member) );})
22bad9955dSBenjamin Poirier 
23bad9955dSBenjamin Poirier 
24bad9955dSBenjamin Poirier struct list_head {
25bad9955dSBenjamin Poirier 	struct list_head *next, *prev;
26bad9955dSBenjamin Poirier };
27bad9955dSBenjamin Poirier 
28bad9955dSBenjamin Poirier 
29bad9955dSBenjamin Poirier #define LIST_HEAD_INIT(name) { &(name), &(name) }
30bad9955dSBenjamin Poirier 
31bad9955dSBenjamin Poirier #define LIST_HEAD(name) \
32bad9955dSBenjamin Poirier 	struct list_head name = LIST_HEAD_INIT(name)
33bad9955dSBenjamin Poirier 
34bad9955dSBenjamin Poirier /**
35bad9955dSBenjamin Poirier  * list_entry - get the struct for this entry
36bad9955dSBenjamin Poirier  * @ptr:	the &struct list_head pointer.
37bad9955dSBenjamin Poirier  * @type:	the type of the struct this is embedded in.
383943f42cSAndrey Utkin  * @member:	the name of the list_head within the struct.
39bad9955dSBenjamin Poirier  */
40bad9955dSBenjamin Poirier #define list_entry(ptr, type, member) \
41bad9955dSBenjamin Poirier 	container_of(ptr, type, member)
42bad9955dSBenjamin Poirier 
43bad9955dSBenjamin Poirier /**
44bad9955dSBenjamin Poirier  * list_for_each_entry	-	iterate over list of given type
45bad9955dSBenjamin Poirier  * @pos:	the type * to use as a loop cursor.
46bad9955dSBenjamin Poirier  * @head:	the head for your list.
473943f42cSAndrey Utkin  * @member:	the name of the list_head within the struct.
48bad9955dSBenjamin Poirier  */
49bad9955dSBenjamin Poirier #define list_for_each_entry(pos, head, member)				\
50bad9955dSBenjamin Poirier 	for (pos = list_entry((head)->next, typeof(*pos), member);	\
51bad9955dSBenjamin Poirier 	     &pos->member != (head); 	\
52bad9955dSBenjamin Poirier 	     pos = list_entry(pos->member.next, typeof(*pos), member))
53bad9955dSBenjamin Poirier 
54bad9955dSBenjamin Poirier /**
55edb749f4SBenjamin Poirier  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
56edb749f4SBenjamin Poirier  * @pos:	the type * to use as a loop cursor.
57edb749f4SBenjamin Poirier  * @n:		another type * to use as temporary storage
58edb749f4SBenjamin Poirier  * @head:	the head for your list.
593943f42cSAndrey Utkin  * @member:	the name of the list_head within the struct.
60edb749f4SBenjamin Poirier  */
61edb749f4SBenjamin Poirier #define list_for_each_entry_safe(pos, n, head, member)			\
62edb749f4SBenjamin Poirier 	for (pos = list_entry((head)->next, typeof(*pos), member),	\
63edb749f4SBenjamin Poirier 		n = list_entry(pos->member.next, typeof(*pos), member);	\
64edb749f4SBenjamin Poirier 	     &pos->member != (head);					\
65edb749f4SBenjamin Poirier 	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
66edb749f4SBenjamin Poirier 
67edb749f4SBenjamin Poirier /**
68bad9955dSBenjamin Poirier  * list_empty - tests whether a list is empty
69bad9955dSBenjamin Poirier  * @head: the list to test.
70bad9955dSBenjamin Poirier  */
list_empty(const struct list_head * head)71bad9955dSBenjamin Poirier static inline int list_empty(const struct list_head *head)
72bad9955dSBenjamin Poirier {
73bad9955dSBenjamin Poirier 	return head->next == head;
74bad9955dSBenjamin Poirier }
75bad9955dSBenjamin Poirier 
76bad9955dSBenjamin Poirier /*
77bad9955dSBenjamin Poirier  * Insert a new entry between two known consecutive entries.
78bad9955dSBenjamin Poirier  *
79bad9955dSBenjamin Poirier  * This is only for internal list manipulation where we know
80bad9955dSBenjamin Poirier  * the prev/next entries already!
81bad9955dSBenjamin Poirier  */
__list_add(struct list_head * _new,struct list_head * prev,struct list_head * next)82bad9955dSBenjamin Poirier static inline void __list_add(struct list_head *_new,
83bad9955dSBenjamin Poirier 			      struct list_head *prev,
84bad9955dSBenjamin Poirier 			      struct list_head *next)
85bad9955dSBenjamin Poirier {
86bad9955dSBenjamin Poirier 	next->prev = _new;
87bad9955dSBenjamin Poirier 	_new->next = next;
88bad9955dSBenjamin Poirier 	_new->prev = prev;
89bad9955dSBenjamin Poirier 	prev->next = _new;
90bad9955dSBenjamin Poirier }
91bad9955dSBenjamin Poirier 
92bad9955dSBenjamin Poirier /**
93bad9955dSBenjamin Poirier  * list_add_tail - add a new entry
94bad9955dSBenjamin Poirier  * @new: new entry to be added
95bad9955dSBenjamin Poirier  * @head: list head to add it before
96bad9955dSBenjamin Poirier  *
97bad9955dSBenjamin Poirier  * Insert a new entry before the specified head.
98bad9955dSBenjamin Poirier  * This is useful for implementing queues.
99bad9955dSBenjamin Poirier  */
list_add_tail(struct list_head * _new,struct list_head * head)100bad9955dSBenjamin Poirier static inline void list_add_tail(struct list_head *_new, struct list_head *head)
101bad9955dSBenjamin Poirier {
102bad9955dSBenjamin Poirier 	__list_add(_new, head->prev, head);
103bad9955dSBenjamin Poirier }
104bad9955dSBenjamin Poirier 
1059a69abf8SBenjamin Poirier /*
1069a69abf8SBenjamin Poirier  * Delete a list entry by making the prev/next entries
1079a69abf8SBenjamin Poirier  * point to each other.
1089a69abf8SBenjamin Poirier  *
1099a69abf8SBenjamin Poirier  * This is only for internal list manipulation where we know
1109a69abf8SBenjamin Poirier  * the prev/next entries already!
1119a69abf8SBenjamin Poirier  */
__list_del(struct list_head * prev,struct list_head * next)1129a69abf8SBenjamin Poirier static inline void __list_del(struct list_head *prev, struct list_head *next)
1139a69abf8SBenjamin Poirier {
1149a69abf8SBenjamin Poirier 	next->prev = prev;
1159a69abf8SBenjamin Poirier 	prev->next = next;
1169a69abf8SBenjamin Poirier }
1179a69abf8SBenjamin Poirier 
1189a69abf8SBenjamin Poirier #define LIST_POISON1  ((void *) 0x00100100)
1199a69abf8SBenjamin Poirier #define LIST_POISON2  ((void *) 0x00200200)
1209a69abf8SBenjamin Poirier /**
1219a69abf8SBenjamin Poirier  * list_del - deletes entry from list.
1229a69abf8SBenjamin Poirier  * @entry: the element to delete from the list.
1239a69abf8SBenjamin Poirier  * Note: list_empty() on entry does not return true after this, the entry is
1249a69abf8SBenjamin Poirier  * in an undefined state.
1259a69abf8SBenjamin Poirier  */
list_del(struct list_head * entry)1269a69abf8SBenjamin Poirier static inline void list_del(struct list_head *entry)
1279a69abf8SBenjamin Poirier {
1289a69abf8SBenjamin Poirier 	__list_del(entry->prev, entry->next);
12921ca352bSYann E. MORIN 	entry->next = (struct list_head*)LIST_POISON1;
13021ca352bSYann E. MORIN 	entry->prev = (struct list_head*)LIST_POISON2;
1319a69abf8SBenjamin Poirier }
132bad9955dSBenjamin Poirier #endif
133