18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci#ifndef LIST_H 38c2ecf20Sopenharmony_ci#define LIST_H 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci/* 68c2ecf20Sopenharmony_ci * Copied from include/linux/... 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#undef offsetof 108c2ecf20Sopenharmony_ci#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci/** 138c2ecf20Sopenharmony_ci * container_of - cast a member of a structure out to the containing structure 148c2ecf20Sopenharmony_ci * @ptr: the pointer to the member. 158c2ecf20Sopenharmony_ci * @type: the type of the container struct this is embedded in. 168c2ecf20Sopenharmony_ci * @member: the name of the member within the struct. 178c2ecf20Sopenharmony_ci * 188c2ecf20Sopenharmony_ci */ 198c2ecf20Sopenharmony_ci#define container_of(ptr, type, member) ({ \ 208c2ecf20Sopenharmony_ci const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 218c2ecf20Sopenharmony_ci (type *)( (char *)__mptr - offsetof(type,member) );}) 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_cistruct list_head { 258c2ecf20Sopenharmony_ci struct list_head *next, *prev; 268c2ecf20Sopenharmony_ci}; 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci#define LIST_HEAD_INIT(name) { &(name), &(name) } 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci#define LIST_HEAD(name) \ 328c2ecf20Sopenharmony_ci struct list_head name = LIST_HEAD_INIT(name) 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci/** 358c2ecf20Sopenharmony_ci * list_entry - get the struct for this entry 368c2ecf20Sopenharmony_ci * @ptr: the &struct list_head pointer. 378c2ecf20Sopenharmony_ci * @type: the type of the struct this is embedded in. 388c2ecf20Sopenharmony_ci * @member: the name of the list_head within the struct. 398c2ecf20Sopenharmony_ci */ 408c2ecf20Sopenharmony_ci#define list_entry(ptr, type, member) \ 418c2ecf20Sopenharmony_ci container_of(ptr, type, member) 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci/** 448c2ecf20Sopenharmony_ci * list_for_each_entry - iterate over list of given type 458c2ecf20Sopenharmony_ci * @pos: the type * to use as a loop cursor. 468c2ecf20Sopenharmony_ci * @head: the head for your list. 478c2ecf20Sopenharmony_ci * @member: the name of the list_head within the struct. 488c2ecf20Sopenharmony_ci */ 498c2ecf20Sopenharmony_ci#define list_for_each_entry(pos, head, member) \ 508c2ecf20Sopenharmony_ci for (pos = list_entry((head)->next, typeof(*pos), member); \ 518c2ecf20Sopenharmony_ci &pos->member != (head); \ 528c2ecf20Sopenharmony_ci pos = list_entry(pos->member.next, typeof(*pos), member)) 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci/** 558c2ecf20Sopenharmony_ci * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry 568c2ecf20Sopenharmony_ci * @pos: the type * to use as a loop cursor. 578c2ecf20Sopenharmony_ci * @n: another type * to use as temporary storage 588c2ecf20Sopenharmony_ci * @head: the head for your list. 598c2ecf20Sopenharmony_ci * @member: the name of the list_head within the struct. 608c2ecf20Sopenharmony_ci */ 618c2ecf20Sopenharmony_ci#define list_for_each_entry_safe(pos, n, head, member) \ 628c2ecf20Sopenharmony_ci for (pos = list_entry((head)->next, typeof(*pos), member), \ 638c2ecf20Sopenharmony_ci n = list_entry(pos->member.next, typeof(*pos), member); \ 648c2ecf20Sopenharmony_ci &pos->member != (head); \ 658c2ecf20Sopenharmony_ci pos = n, n = list_entry(n->member.next, typeof(*n), member)) 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci/** 688c2ecf20Sopenharmony_ci * list_empty - tests whether a list is empty 698c2ecf20Sopenharmony_ci * @head: the list to test. 708c2ecf20Sopenharmony_ci */ 718c2ecf20Sopenharmony_cistatic inline int list_empty(const struct list_head *head) 728c2ecf20Sopenharmony_ci{ 738c2ecf20Sopenharmony_ci return head->next == head; 748c2ecf20Sopenharmony_ci} 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci/* 778c2ecf20Sopenharmony_ci * Insert a new entry between two known consecutive entries. 788c2ecf20Sopenharmony_ci * 798c2ecf20Sopenharmony_ci * This is only for internal list manipulation where we know 808c2ecf20Sopenharmony_ci * the prev/next entries already! 818c2ecf20Sopenharmony_ci */ 828c2ecf20Sopenharmony_cistatic inline void __list_add(struct list_head *_new, 838c2ecf20Sopenharmony_ci struct list_head *prev, 848c2ecf20Sopenharmony_ci struct list_head *next) 858c2ecf20Sopenharmony_ci{ 868c2ecf20Sopenharmony_ci next->prev = _new; 878c2ecf20Sopenharmony_ci _new->next = next; 888c2ecf20Sopenharmony_ci _new->prev = prev; 898c2ecf20Sopenharmony_ci prev->next = _new; 908c2ecf20Sopenharmony_ci} 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci/** 938c2ecf20Sopenharmony_ci * list_add_tail - add a new entry 948c2ecf20Sopenharmony_ci * @new: new entry to be added 958c2ecf20Sopenharmony_ci * @head: list head to add it before 968c2ecf20Sopenharmony_ci * 978c2ecf20Sopenharmony_ci * Insert a new entry before the specified head. 988c2ecf20Sopenharmony_ci * This is useful for implementing queues. 998c2ecf20Sopenharmony_ci */ 1008c2ecf20Sopenharmony_cistatic inline void list_add_tail(struct list_head *_new, struct list_head *head) 1018c2ecf20Sopenharmony_ci{ 1028c2ecf20Sopenharmony_ci __list_add(_new, head->prev, head); 1038c2ecf20Sopenharmony_ci} 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci/* 1068c2ecf20Sopenharmony_ci * Delete a list entry by making the prev/next entries 1078c2ecf20Sopenharmony_ci * point to each other. 1088c2ecf20Sopenharmony_ci * 1098c2ecf20Sopenharmony_ci * This is only for internal list manipulation where we know 1108c2ecf20Sopenharmony_ci * the prev/next entries already! 1118c2ecf20Sopenharmony_ci */ 1128c2ecf20Sopenharmony_cistatic inline void __list_del(struct list_head *prev, struct list_head *next) 1138c2ecf20Sopenharmony_ci{ 1148c2ecf20Sopenharmony_ci next->prev = prev; 1158c2ecf20Sopenharmony_ci prev->next = next; 1168c2ecf20Sopenharmony_ci} 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci#define LIST_POISON1 ((void *) 0x00100100) 1198c2ecf20Sopenharmony_ci#define LIST_POISON2 ((void *) 0x00200200) 1208c2ecf20Sopenharmony_ci/** 1218c2ecf20Sopenharmony_ci * list_del - deletes entry from list. 1228c2ecf20Sopenharmony_ci * @entry: the element to delete from the list. 1238c2ecf20Sopenharmony_ci * Note: list_empty() on entry does not return true after this, the entry is 1248c2ecf20Sopenharmony_ci * in an undefined state. 1258c2ecf20Sopenharmony_ci */ 1268c2ecf20Sopenharmony_cistatic inline void list_del(struct list_head *entry) 1278c2ecf20Sopenharmony_ci{ 1288c2ecf20Sopenharmony_ci __list_del(entry->prev, entry->next); 1298c2ecf20Sopenharmony_ci entry->next = (struct list_head*)LIST_POISON1; 1308c2ecf20Sopenharmony_ci entry->prev = (struct list_head*)LIST_POISON2; 1318c2ecf20Sopenharmony_ci} 1328c2ecf20Sopenharmony_ci#endif 133