xref: /openbmc/u-boot/scripts/kconfig/list.h (revision e91610da7c8a9fe42f3e5a75f06c3d1a0cb5f815)
1*e91610daSEugeniu Rosca /* SPDX-License-Identifier: GPL-2.0 */
20a9064fbSMasahiro Yamada #ifndef LIST_H
30a9064fbSMasahiro Yamada #define LIST_H
40a9064fbSMasahiro Yamada 
50a9064fbSMasahiro Yamada /*
60a9064fbSMasahiro Yamada  * Copied from include/linux/...
70a9064fbSMasahiro Yamada  */
80a9064fbSMasahiro Yamada 
90a9064fbSMasahiro Yamada #undef offsetof
100a9064fbSMasahiro Yamada #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
110a9064fbSMasahiro Yamada 
120a9064fbSMasahiro Yamada /**
130a9064fbSMasahiro Yamada  * container_of - cast a member of a structure out to the containing structure
140a9064fbSMasahiro Yamada  * @ptr:        the pointer to the member.
150a9064fbSMasahiro Yamada  * @type:       the type of the container struct this is embedded in.
160a9064fbSMasahiro Yamada  * @member:     the name of the member within the struct.
170a9064fbSMasahiro Yamada  *
180a9064fbSMasahiro Yamada  */
190a9064fbSMasahiro Yamada #define container_of(ptr, type, member) ({                      \
200a9064fbSMasahiro Yamada 	const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
210a9064fbSMasahiro Yamada 	(type *)( (char *)__mptr - offsetof(type,member) );})
220a9064fbSMasahiro Yamada 
230a9064fbSMasahiro Yamada 
240a9064fbSMasahiro Yamada struct list_head {
250a9064fbSMasahiro Yamada 	struct list_head *next, *prev;
260a9064fbSMasahiro Yamada };
270a9064fbSMasahiro Yamada 
280a9064fbSMasahiro Yamada 
290a9064fbSMasahiro Yamada #define LIST_HEAD_INIT(name) { &(name), &(name) }
300a9064fbSMasahiro Yamada 
310a9064fbSMasahiro Yamada #define LIST_HEAD(name) \
320a9064fbSMasahiro Yamada 	struct list_head name = LIST_HEAD_INIT(name)
330a9064fbSMasahiro Yamada 
340a9064fbSMasahiro Yamada /**
350a9064fbSMasahiro Yamada  * list_entry - get the struct for this entry
360a9064fbSMasahiro Yamada  * @ptr:	the &struct list_head pointer.
370a9064fbSMasahiro Yamada  * @type:	the type of the struct this is embedded in.
389b5f0b1dSMasahiro Yamada  * @member:	the name of the list_head within the struct.
390a9064fbSMasahiro Yamada  */
400a9064fbSMasahiro Yamada #define list_entry(ptr, type, member) \
410a9064fbSMasahiro Yamada 	container_of(ptr, type, member)
420a9064fbSMasahiro Yamada 
430a9064fbSMasahiro Yamada /**
440a9064fbSMasahiro Yamada  * list_for_each_entry	-	iterate over list of given type
450a9064fbSMasahiro Yamada  * @pos:	the type * to use as a loop cursor.
460a9064fbSMasahiro Yamada  * @head:	the head for your list.
479b5f0b1dSMasahiro Yamada  * @member:	the name of the list_head within the struct.
480a9064fbSMasahiro Yamada  */
490a9064fbSMasahiro Yamada #define list_for_each_entry(pos, head, member)				\
500a9064fbSMasahiro Yamada 	for (pos = list_entry((head)->next, typeof(*pos), member);	\
510a9064fbSMasahiro Yamada 	     &pos->member != (head); 	\
520a9064fbSMasahiro Yamada 	     pos = list_entry(pos->member.next, typeof(*pos), member))
530a9064fbSMasahiro Yamada 
540a9064fbSMasahiro Yamada /**
550a9064fbSMasahiro Yamada  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
560a9064fbSMasahiro Yamada  * @pos:	the type * to use as a loop cursor.
570a9064fbSMasahiro Yamada  * @n:		another type * to use as temporary storage
580a9064fbSMasahiro Yamada  * @head:	the head for your list.
599b5f0b1dSMasahiro Yamada  * @member:	the name of the list_head within the struct.
600a9064fbSMasahiro Yamada  */
610a9064fbSMasahiro Yamada #define list_for_each_entry_safe(pos, n, head, member)			\
620a9064fbSMasahiro Yamada 	for (pos = list_entry((head)->next, typeof(*pos), member),	\
630a9064fbSMasahiro Yamada 		n = list_entry(pos->member.next, typeof(*pos), member);	\
640a9064fbSMasahiro Yamada 	     &pos->member != (head);					\
650a9064fbSMasahiro Yamada 	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
660a9064fbSMasahiro Yamada 
670a9064fbSMasahiro Yamada /**
680a9064fbSMasahiro Yamada  * list_empty - tests whether a list is empty
690a9064fbSMasahiro Yamada  * @head: the list to test.
700a9064fbSMasahiro Yamada  */
list_empty(const struct list_head * head)710a9064fbSMasahiro Yamada static inline int list_empty(const struct list_head *head)
720a9064fbSMasahiro Yamada {
730a9064fbSMasahiro Yamada 	return head->next == head;
740a9064fbSMasahiro Yamada }
750a9064fbSMasahiro Yamada 
760a9064fbSMasahiro Yamada /*
770a9064fbSMasahiro Yamada  * Insert a new entry between two known consecutive entries.
780a9064fbSMasahiro Yamada  *
790a9064fbSMasahiro Yamada  * This is only for internal list manipulation where we know
800a9064fbSMasahiro Yamada  * the prev/next entries already!
810a9064fbSMasahiro Yamada  */
__list_add(struct list_head * _new,struct list_head * prev,struct list_head * next)820a9064fbSMasahiro Yamada static inline void __list_add(struct list_head *_new,
830a9064fbSMasahiro Yamada 			      struct list_head *prev,
840a9064fbSMasahiro Yamada 			      struct list_head *next)
850a9064fbSMasahiro Yamada {
860a9064fbSMasahiro Yamada 	next->prev = _new;
870a9064fbSMasahiro Yamada 	_new->next = next;
880a9064fbSMasahiro Yamada 	_new->prev = prev;
890a9064fbSMasahiro Yamada 	prev->next = _new;
900a9064fbSMasahiro Yamada }
910a9064fbSMasahiro Yamada 
920a9064fbSMasahiro Yamada /**
930a9064fbSMasahiro Yamada  * list_add_tail - add a new entry
940a9064fbSMasahiro Yamada  * @new: new entry to be added
950a9064fbSMasahiro Yamada  * @head: list head to add it before
960a9064fbSMasahiro Yamada  *
970a9064fbSMasahiro Yamada  * Insert a new entry before the specified head.
980a9064fbSMasahiro Yamada  * This is useful for implementing queues.
990a9064fbSMasahiro Yamada  */
list_add_tail(struct list_head * _new,struct list_head * head)1000a9064fbSMasahiro Yamada static inline void list_add_tail(struct list_head *_new, struct list_head *head)
1010a9064fbSMasahiro Yamada {
1020a9064fbSMasahiro Yamada 	__list_add(_new, head->prev, head);
1030a9064fbSMasahiro Yamada }
1040a9064fbSMasahiro Yamada 
1050a9064fbSMasahiro Yamada /*
1060a9064fbSMasahiro Yamada  * Delete a list entry by making the prev/next entries
1070a9064fbSMasahiro Yamada  * point to each other.
1080a9064fbSMasahiro Yamada  *
1090a9064fbSMasahiro Yamada  * This is only for internal list manipulation where we know
1100a9064fbSMasahiro Yamada  * the prev/next entries already!
1110a9064fbSMasahiro Yamada  */
__list_del(struct list_head * prev,struct list_head * next)1120a9064fbSMasahiro Yamada static inline void __list_del(struct list_head *prev, struct list_head *next)
1130a9064fbSMasahiro Yamada {
1140a9064fbSMasahiro Yamada 	next->prev = prev;
1150a9064fbSMasahiro Yamada 	prev->next = next;
1160a9064fbSMasahiro Yamada }
1170a9064fbSMasahiro Yamada 
1180a9064fbSMasahiro Yamada #define LIST_POISON1  ((void *) 0x00100100)
1190a9064fbSMasahiro Yamada #define LIST_POISON2  ((void *) 0x00200200)
1200a9064fbSMasahiro Yamada /**
1210a9064fbSMasahiro Yamada  * list_del - deletes entry from list.
1220a9064fbSMasahiro Yamada  * @entry: the element to delete from the list.
1230a9064fbSMasahiro Yamada  * Note: list_empty() on entry does not return true after this, the entry is
1240a9064fbSMasahiro Yamada  * in an undefined state.
1250a9064fbSMasahiro Yamada  */
list_del(struct list_head * entry)1260a9064fbSMasahiro Yamada static inline void list_del(struct list_head *entry)
1270a9064fbSMasahiro Yamada {
1280a9064fbSMasahiro Yamada 	__list_del(entry->prev, entry->next);
1290a9064fbSMasahiro Yamada 	entry->next = (struct list_head*)LIST_POISON1;
1300a9064fbSMasahiro Yamada 	entry->prev = (struct list_head*)LIST_POISON2;
1310a9064fbSMasahiro Yamada }
1320a9064fbSMasahiro Yamada #endif
133