/* SPDX-License-Identifier: GPL-2.0 */1#ifndef _LIST_H2#define _LIST_H34/* Stripped down implementation of linked list taken5* from the Linux Kernel.6*/78/*9* Simple doubly linked list implementation.10*11* Some of the internal functions ("__xxx") are useful when12* manipulating whole lists rather than single entries, as13* sometimes we already know the next/prev entries and we can14* generate better code by using them directly rather than15* using the generic single-entry routines.16*/1718struct list_head {19struct list_head *next, *prev;20};2122#define LIST_HEAD_INIT(name) { &(name), &(name) }2324#define LIST_HEAD(name) \25struct list_head name = LIST_HEAD_INIT(name)2627static inline void INIT_LIST_HEAD(struct list_head *list)28{29list->next = list;30list->prev = list;31}3233/*34* Insert a new entry between two known consecutive entries.35*36* This is only for internal list manipulation where we know37* the prev/next entries already!38*/39static inline void __list_add(struct list_head *new,40struct list_head *prev,41struct list_head *next)42{43next->prev = new;44new->next = next;45new->prev = prev;46prev->next = new;47}4849/**50* list_add - add a new entry51* @new: new entry to be added52* @head: list head to add it after53*54* Insert a new entry after the specified head.55* This is good for implementing stacks.56*/57static inline void list_add(struct list_head *new, struct list_head *head)58{59__list_add(new, head, head->next);60}6162/*63* Delete a list entry by making the prev/next entries64* point to each other.65*66* This is only for internal list manipulation where we know67* the prev/next entries already!68*/69static inline void __list_del(struct list_head * prev, struct list_head * next)70{71next->prev = prev;72prev->next = next;73}7475#define POISON_POINTER_DELTA 076#define LIST_POISON1 ((void *) 0x00100100 + POISON_POINTER_DELTA)77#define LIST_POISON2 ((void *) 0x00200200 + POISON_POINTER_DELTA)7879static inline void __list_del_entry(struct list_head *entry)80{81__list_del(entry->prev, entry->next);82}8384/**85* list_del - deletes entry from list.86* @entry: the element to delete from the list.87* Note: list_empty() on entry does not return true after this, the entry is88* in an undefined state.89*/90static inline void list_del(struct list_head *entry)91{92__list_del(entry->prev, entry->next);93entry->next = LIST_POISON1;94entry->prev = LIST_POISON2;95}9697/**98* list_entry - get the struct for this entry99* @ptr: the &struct list_head pointer.100* @type: the type of the struct this is embedded in.101* @member: the name of the list_head within the struct.102*/103#define list_entry(ptr, type, member) \104container_of(ptr, type, member)105/**106* list_for_each - iterate over a list107* @pos: the &struct list_head to use as a loop cursor.108* @head: the head for your list.109*/110#define list_for_each(pos, head) \111for (pos = (head)->next; pos != (head); pos = pos->next)112113/**114* list_for_each_safe - iterate over a list safe against removal of list entry115* @pos: the &struct list_head to use as a loop cursor.116* @n: another &struct list_head to use as temporary storage117* @head: the head for your list.118*/119#define list_for_each_safe(pos, n, head) \120for (pos = (head)->next, n = pos->next; pos != (head); \121pos = n, n = pos->next)122123#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)124125/**126* container_of - cast a member of a structure out to the containing structure127* @ptr: the pointer to the member.128* @type: the type of the container struct this is embedded in.129* @member: the name of the member within the struct.130*131*/132#define container_of(ptr, type, member) ({ \133const typeof( ((type *)0)->member ) *__mptr = (ptr); \134(type *)( (char *)__mptr - offsetof(type,member) );})135136#endif137138139