1b2441318SGreg Kroah-Hartman/* SPDX-License-Identifier: GPL-2.0 */
252d04485SValentina Manea#ifndef _LIST_H
352d04485SValentina Manea#define _LIST_H
452d04485SValentina Manea
552d04485SValentina Manea/* Stripped down implementation of linked list taken
652d04485SValentina Manea * from the Linux Kernel.
752d04485SValentina Manea */
852d04485SValentina Manea
952d04485SValentina Manea/*
1052d04485SValentina Manea * Simple doubly linked list implementation.
1152d04485SValentina Manea *
1252d04485SValentina Manea * Some of the internal functions ("__xxx") are useful when
1352d04485SValentina Manea * manipulating whole lists rather than single entries, as
1452d04485SValentina Manea * sometimes we already know the next/prev entries and we can
1552d04485SValentina Manea * generate better code by using them directly rather than
1652d04485SValentina Manea * using the generic single-entry routines.
1752d04485SValentina Manea */
1852d04485SValentina Manea
1952d04485SValentina Maneastruct list_head {
2052d04485SValentina Manea	struct list_head *next, *prev;
2152d04485SValentina Manea};
2252d04485SValentina Manea
2352d04485SValentina Manea#define LIST_HEAD_INIT(name) { &(name), &(name) }
2452d04485SValentina Manea
2552d04485SValentina Manea#define LIST_HEAD(name) \
2652d04485SValentina Manea	struct list_head name = LIST_HEAD_INIT(name)
2752d04485SValentina Manea
2852d04485SValentina Maneastatic inline void INIT_LIST_HEAD(struct list_head *list)
2952d04485SValentina Manea{
3052d04485SValentina Manea	list->next = list;
3152d04485SValentina Manea	list->prev = list;
3252d04485SValentina Manea}
3352d04485SValentina Manea
3452d04485SValentina Manea/*
3552d04485SValentina Manea * Insert a new entry between two known consecutive entries.
3652d04485SValentina Manea *
3752d04485SValentina Manea * This is only for internal list manipulation where we know
3852d04485SValentina Manea * the prev/next entries already!
3952d04485SValentina Manea */
4052d04485SValentina Maneastatic inline void __list_add(struct list_head *new,
4152d04485SValentina Manea			      struct list_head *prev,
4252d04485SValentina Manea			      struct list_head *next)
4352d04485SValentina Manea{
4452d04485SValentina Manea	next->prev = new;
4552d04485SValentina Manea	new->next = next;
4652d04485SValentina Manea	new->prev = prev;
4752d04485SValentina Manea	prev->next = new;
4852d04485SValentina Manea}
4952d04485SValentina Manea
5052d04485SValentina Manea/**
5152d04485SValentina Manea * list_add - add a new entry
5252d04485SValentina Manea * @new: new entry to be added
5352d04485SValentina Manea * @head: list head to add it after
5452d04485SValentina Manea *
5552d04485SValentina Manea * Insert a new entry after the specified head.
5652d04485SValentina Manea * This is good for implementing stacks.
5752d04485SValentina Manea */
5852d04485SValentina Maneastatic inline void list_add(struct list_head *new, struct list_head *head)
5952d04485SValentina Manea{
6052d04485SValentina Manea	__list_add(new, head, head->next);
6152d04485SValentina Manea}
6252d04485SValentina Manea
6352d04485SValentina Manea/*
6452d04485SValentina Manea * Delete a list entry by making the prev/next entries
6552d04485SValentina Manea * point to each other.
6652d04485SValentina Manea *
6752d04485SValentina Manea * This is only for internal list manipulation where we know
6852d04485SValentina Manea * the prev/next entries already!
6952d04485SValentina Manea */
7052d04485SValentina Maneastatic inline void __list_del(struct list_head * prev, struct list_head * next)
7152d04485SValentina Manea{
7252d04485SValentina Manea	next->prev = prev;
7352d04485SValentina Manea	prev->next = next;
7452d04485SValentina Manea}
7552d04485SValentina Manea
7652d04485SValentina Manea#define POISON_POINTER_DELTA 0
7752d04485SValentina Manea#define LIST_POISON1  ((void *) 0x00100100 + POISON_POINTER_DELTA)
7852d04485SValentina Manea#define LIST_POISON2  ((void *) 0x00200200 + POISON_POINTER_DELTA)
7952d04485SValentina Manea
80dfbe56bfSRandy Dunlapstatic inline void __list_del_entry(struct list_head *entry)
81dfbe56bfSRandy Dunlap{
82dfbe56bfSRandy Dunlap	__list_del(entry->prev, entry->next);
83dfbe56bfSRandy Dunlap}
84dfbe56bfSRandy Dunlap
8552d04485SValentina Manea/**
8652d04485SValentina Manea * list_del - deletes entry from list.
8752d04485SValentina Manea * @entry: the element to delete from the list.
8852d04485SValentina Manea * Note: list_empty() on entry does not return true after this, the entry is
8952d04485SValentina Manea * in an undefined state.
9052d04485SValentina Manea */
9152d04485SValentina Maneastatic inline void list_del(struct list_head *entry)
9252d04485SValentina Manea{
9352d04485SValentina Manea	__list_del(entry->prev, entry->next);
9452d04485SValentina Manea	entry->next = LIST_POISON1;
9552d04485SValentina Manea	entry->prev = LIST_POISON2;
9652d04485SValentina Manea}
9752d04485SValentina Manea
9852d04485SValentina Manea/**
9952d04485SValentina Manea * list_entry - get the struct for this entry
10052d04485SValentina Manea * @ptr:	the &struct list_head pointer.
10152d04485SValentina Manea * @type:	the type of the struct this is embedded in.
1023943f42cSAndrey Utkin * @member:	the name of the list_head within the struct.
10352d04485SValentina Manea */
10452d04485SValentina Manea#define list_entry(ptr, type, member) \
10552d04485SValentina Manea	container_of(ptr, type, member)
10652d04485SValentina Manea/**
10752d04485SValentina Manea * list_for_each	-	iterate over a list
10852d04485SValentina Manea * @pos:	the &struct list_head to use as a loop cursor.
10952d04485SValentina Manea * @head:	the head for your list.
11052d04485SValentina Manea */
11152d04485SValentina Manea#define list_for_each(pos, head) \
11252d04485SValentina Manea	for (pos = (head)->next; pos != (head); pos = pos->next)
11352d04485SValentina Manea
11452d04485SValentina Manea/**
11552d04485SValentina Manea * list_for_each_safe - iterate over a list safe against removal of list entry
11652d04485SValentina Manea * @pos:	the &struct list_head to use as a loop cursor.
11752d04485SValentina Manea * @n:		another &struct list_head to use as temporary storage
11852d04485SValentina Manea * @head:	the head for your list.
11952d04485SValentina Manea */
12052d04485SValentina Manea#define list_for_each_safe(pos, n, head) \
12152d04485SValentina Manea	for (pos = (head)->next, n = pos->next; pos != (head); \
12252d04485SValentina Manea		pos = n, n = pos->next)
12352d04485SValentina Manea
12452d04485SValentina Manea#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
12552d04485SValentina Manea
12652d04485SValentina Manea/**
12752d04485SValentina Manea * container_of - cast a member of a structure out to the containing structure
12852d04485SValentina Manea * @ptr:	the pointer to the member.
12952d04485SValentina Manea * @type:	the type of the container struct this is embedded in.
13052d04485SValentina Manea * @member:	the name of the member within the struct.
13152d04485SValentina Manea *
13252d04485SValentina Manea */
13352d04485SValentina Manea#define container_of(ptr, type, member) ({			\
13452d04485SValentina Manea	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
13552d04485SValentina Manea	(type *)( (char *)__mptr - offsetof(type,member) );})
13652d04485SValentina Manea
13752d04485SValentina Manea#endif
138