162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 262306a36Sopenharmony_ci#ifndef LIST_H 362306a36Sopenharmony_ci#define LIST_H 462306a36Sopenharmony_ci 562306a36Sopenharmony_ci#include <stdbool.h> 662306a36Sopenharmony_ci#include <stddef.h> 762306a36Sopenharmony_ci 862306a36Sopenharmony_ci/* Are two types/vars the same type (ignoring qualifiers)? */ 962306a36Sopenharmony_ci#define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b)) 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ci/** 1262306a36Sopenharmony_ci * container_of - cast a member of a structure out to the containing structure 1362306a36Sopenharmony_ci * @ptr: the pointer to the member. 1462306a36Sopenharmony_ci * @type: the type of the container struct this is embedded in. 1562306a36Sopenharmony_ci * @member: the name of the member within the struct. 1662306a36Sopenharmony_ci * 1762306a36Sopenharmony_ci */ 1862306a36Sopenharmony_ci#define container_of(ptr, type, member) ({ \ 1962306a36Sopenharmony_ci void *__mptr = (void *)(ptr); \ 2062306a36Sopenharmony_ci _Static_assert(__same_type(*(ptr), ((type *)0)->member) || \ 2162306a36Sopenharmony_ci __same_type(*(ptr), void), \ 2262306a36Sopenharmony_ci "pointer type mismatch in container_of()"); \ 2362306a36Sopenharmony_ci ((type *)(__mptr - offsetof(type, member))); }) 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci#define LIST_POISON1 ((void *) 0x100) 2662306a36Sopenharmony_ci#define LIST_POISON2 ((void *) 0x122) 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ci/* 2962306a36Sopenharmony_ci * Circular doubly linked list implementation. 3062306a36Sopenharmony_ci * 3162306a36Sopenharmony_ci * Some of the internal functions ("__xxx") are useful when 3262306a36Sopenharmony_ci * manipulating whole lists rather than single entries, as 3362306a36Sopenharmony_ci * sometimes we already know the next/prev entries and we can 3462306a36Sopenharmony_ci * generate better code by using them directly rather than 3562306a36Sopenharmony_ci * using the generic single-entry routines. 3662306a36Sopenharmony_ci */ 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_cistruct list_head { 3962306a36Sopenharmony_ci struct list_head *next, *prev; 4062306a36Sopenharmony_ci}; 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ci#define LIST_HEAD_INIT(name) { &(name), &(name) } 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ci#define LIST_HEAD(name) \ 4562306a36Sopenharmony_ci struct list_head name = LIST_HEAD_INIT(name) 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ci/** 4862306a36Sopenharmony_ci * INIT_LIST_HEAD - Initialize a list_head structure 4962306a36Sopenharmony_ci * @list: list_head structure to be initialized. 5062306a36Sopenharmony_ci * 5162306a36Sopenharmony_ci * Initializes the list_head to point to itself. If it is a list header, 5262306a36Sopenharmony_ci * the result is an empty list. 5362306a36Sopenharmony_ci */ 5462306a36Sopenharmony_cistatic inline void INIT_LIST_HEAD(struct list_head *list) 5562306a36Sopenharmony_ci{ 5662306a36Sopenharmony_ci list->next = list; 5762306a36Sopenharmony_ci list->prev = list; 5862306a36Sopenharmony_ci} 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_ci/* 6162306a36Sopenharmony_ci * Insert a new entry between two known consecutive entries. 6262306a36Sopenharmony_ci * 6362306a36Sopenharmony_ci * This is only for internal list manipulation where we know 6462306a36Sopenharmony_ci * the prev/next entries already! 6562306a36Sopenharmony_ci */ 6662306a36Sopenharmony_cistatic inline void __list_add(struct list_head *new, 6762306a36Sopenharmony_ci struct list_head *prev, 6862306a36Sopenharmony_ci struct list_head *next) 6962306a36Sopenharmony_ci{ 7062306a36Sopenharmony_ci next->prev = new; 7162306a36Sopenharmony_ci new->next = next; 7262306a36Sopenharmony_ci new->prev = prev; 7362306a36Sopenharmony_ci prev->next = new; 7462306a36Sopenharmony_ci} 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_ci/** 7762306a36Sopenharmony_ci * list_add - add a new entry 7862306a36Sopenharmony_ci * @new: new entry to be added 7962306a36Sopenharmony_ci * @head: list head to add it after 8062306a36Sopenharmony_ci * 8162306a36Sopenharmony_ci * Insert a new entry after the specified head. 8262306a36Sopenharmony_ci * This is good for implementing stacks. 8362306a36Sopenharmony_ci */ 8462306a36Sopenharmony_cistatic inline void list_add(struct list_head *new, struct list_head *head) 8562306a36Sopenharmony_ci{ 8662306a36Sopenharmony_ci __list_add(new, head, head->next); 8762306a36Sopenharmony_ci} 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ci/** 9062306a36Sopenharmony_ci * list_add_tail - add a new entry 9162306a36Sopenharmony_ci * @new: new entry to be added 9262306a36Sopenharmony_ci * @head: list head to add it before 9362306a36Sopenharmony_ci * 9462306a36Sopenharmony_ci * Insert a new entry before the specified head. 9562306a36Sopenharmony_ci * This is useful for implementing queues. 9662306a36Sopenharmony_ci */ 9762306a36Sopenharmony_cistatic inline void list_add_tail(struct list_head *new, struct list_head *head) 9862306a36Sopenharmony_ci{ 9962306a36Sopenharmony_ci __list_add(new, head->prev, head); 10062306a36Sopenharmony_ci} 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_ci/* 10362306a36Sopenharmony_ci * Delete a list entry by making the prev/next entries 10462306a36Sopenharmony_ci * point to each other. 10562306a36Sopenharmony_ci * 10662306a36Sopenharmony_ci * This is only for internal list manipulation where we know 10762306a36Sopenharmony_ci * the prev/next entries already! 10862306a36Sopenharmony_ci */ 10962306a36Sopenharmony_cistatic inline void __list_del(struct list_head *prev, struct list_head *next) 11062306a36Sopenharmony_ci{ 11162306a36Sopenharmony_ci next->prev = prev; 11262306a36Sopenharmony_ci prev->next = next; 11362306a36Sopenharmony_ci} 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_cistatic inline void __list_del_entry(struct list_head *entry) 11662306a36Sopenharmony_ci{ 11762306a36Sopenharmony_ci __list_del(entry->prev, entry->next); 11862306a36Sopenharmony_ci} 11962306a36Sopenharmony_ci 12062306a36Sopenharmony_ci/** 12162306a36Sopenharmony_ci * list_del - deletes entry from list. 12262306a36Sopenharmony_ci * @entry: the element to delete from the list. 12362306a36Sopenharmony_ci * Note: list_empty() on entry does not return true after this, the entry is 12462306a36Sopenharmony_ci * in an undefined state. 12562306a36Sopenharmony_ci */ 12662306a36Sopenharmony_cistatic inline void list_del(struct list_head *entry) 12762306a36Sopenharmony_ci{ 12862306a36Sopenharmony_ci __list_del_entry(entry); 12962306a36Sopenharmony_ci entry->next = LIST_POISON1; 13062306a36Sopenharmony_ci entry->prev = LIST_POISON2; 13162306a36Sopenharmony_ci} 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_ci/** 13462306a36Sopenharmony_ci * list_is_head - tests whether @list is the list @head 13562306a36Sopenharmony_ci * @list: the entry to test 13662306a36Sopenharmony_ci * @head: the head of the list 13762306a36Sopenharmony_ci */ 13862306a36Sopenharmony_cistatic inline int list_is_head(const struct list_head *list, const struct list_head *head) 13962306a36Sopenharmony_ci{ 14062306a36Sopenharmony_ci return list == head; 14162306a36Sopenharmony_ci} 14262306a36Sopenharmony_ci 14362306a36Sopenharmony_ci/** 14462306a36Sopenharmony_ci * list_empty - tests whether a list is empty 14562306a36Sopenharmony_ci * @head: the list to test. 14662306a36Sopenharmony_ci */ 14762306a36Sopenharmony_cistatic inline int list_empty(const struct list_head *head) 14862306a36Sopenharmony_ci{ 14962306a36Sopenharmony_ci return head->next == head; 15062306a36Sopenharmony_ci} 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_ci/** 15362306a36Sopenharmony_ci * list_entry - get the struct for this entry 15462306a36Sopenharmony_ci * @ptr: the &struct list_head pointer. 15562306a36Sopenharmony_ci * @type: the type of the struct this is embedded in. 15662306a36Sopenharmony_ci * @member: the name of the list_head within the struct. 15762306a36Sopenharmony_ci */ 15862306a36Sopenharmony_ci#define list_entry(ptr, type, member) \ 15962306a36Sopenharmony_ci container_of(ptr, type, member) 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_ci/** 16262306a36Sopenharmony_ci * list_first_entry - get the first element from a list 16362306a36Sopenharmony_ci * @ptr: the list head to take the element from. 16462306a36Sopenharmony_ci * @type: the type of the struct this is embedded in. 16562306a36Sopenharmony_ci * @member: the name of the list_head within the struct. 16662306a36Sopenharmony_ci * 16762306a36Sopenharmony_ci * Note, that list is expected to be not empty. 16862306a36Sopenharmony_ci */ 16962306a36Sopenharmony_ci#define list_first_entry(ptr, type, member) \ 17062306a36Sopenharmony_ci list_entry((ptr)->next, type, member) 17162306a36Sopenharmony_ci 17262306a36Sopenharmony_ci/** 17362306a36Sopenharmony_ci * list_next_entry - get the next element in list 17462306a36Sopenharmony_ci * @pos: the type * to cursor 17562306a36Sopenharmony_ci * @member: the name of the list_head within the struct. 17662306a36Sopenharmony_ci */ 17762306a36Sopenharmony_ci#define list_next_entry(pos, member) \ 17862306a36Sopenharmony_ci list_entry((pos)->member.next, typeof(*(pos)), member) 17962306a36Sopenharmony_ci 18062306a36Sopenharmony_ci/** 18162306a36Sopenharmony_ci * list_entry_is_head - test if the entry points to the head of the list 18262306a36Sopenharmony_ci * @pos: the type * to cursor 18362306a36Sopenharmony_ci * @head: the head for your list. 18462306a36Sopenharmony_ci * @member: the name of the list_head within the struct. 18562306a36Sopenharmony_ci */ 18662306a36Sopenharmony_ci#define list_entry_is_head(pos, head, member) \ 18762306a36Sopenharmony_ci (&pos->member == (head)) 18862306a36Sopenharmony_ci 18962306a36Sopenharmony_ci/** 19062306a36Sopenharmony_ci * list_for_each_entry - iterate over list of given type 19162306a36Sopenharmony_ci * @pos: the type * to use as a loop cursor. 19262306a36Sopenharmony_ci * @head: the head for your list. 19362306a36Sopenharmony_ci * @member: the name of the list_head within the struct. 19462306a36Sopenharmony_ci */ 19562306a36Sopenharmony_ci#define list_for_each_entry(pos, head, member) \ 19662306a36Sopenharmony_ci for (pos = list_first_entry(head, typeof(*pos), member); \ 19762306a36Sopenharmony_ci !list_entry_is_head(pos, head, member); \ 19862306a36Sopenharmony_ci pos = list_next_entry(pos, member)) 19962306a36Sopenharmony_ci 20062306a36Sopenharmony_ci/** 20162306a36Sopenharmony_ci * list_for_each_entry_safe - iterate over list of given type. Safe against removal of list entry 20262306a36Sopenharmony_ci * @pos: the type * to use as a loop cursor. 20362306a36Sopenharmony_ci * @n: another type * to use as temporary storage 20462306a36Sopenharmony_ci * @head: the head for your list. 20562306a36Sopenharmony_ci * @member: the name of the list_head within the struct. 20662306a36Sopenharmony_ci */ 20762306a36Sopenharmony_ci#define list_for_each_entry_safe(pos, n, head, member) \ 20862306a36Sopenharmony_ci for (pos = list_first_entry(head, typeof(*pos), member), \ 20962306a36Sopenharmony_ci n = list_next_entry(pos, member); \ 21062306a36Sopenharmony_ci !list_entry_is_head(pos, head, member); \ 21162306a36Sopenharmony_ci pos = n, n = list_next_entry(n, member)) 21262306a36Sopenharmony_ci 21362306a36Sopenharmony_ci#endif /* LIST_H */ 214