1*f7c14bbaSAndroid Build Coastguard Worker /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2*f7c14bbaSAndroid Build Coastguard Worker
3*f7c14bbaSAndroid Build Coastguard Worker #ifndef __LINUX_LIST_H
4*f7c14bbaSAndroid Build Coastguard Worker #define __LINUX_LIST_H
5*f7c14bbaSAndroid Build Coastguard Worker
6*f7c14bbaSAndroid Build Coastguard Worker #define LIST_HEAD_INIT(name) { &(name), &(name) }
7*f7c14bbaSAndroid Build Coastguard Worker #define LIST_HEAD(name) \
8*f7c14bbaSAndroid Build Coastguard Worker struct list_head name = LIST_HEAD_INIT(name)
9*f7c14bbaSAndroid Build Coastguard Worker
10*f7c14bbaSAndroid Build Coastguard Worker #define POISON_POINTER_DELTA 0
11*f7c14bbaSAndroid Build Coastguard Worker #define LIST_POISON1 ((void *) 0x100 + POISON_POINTER_DELTA)
12*f7c14bbaSAndroid Build Coastguard Worker #define LIST_POISON2 ((void *) 0x200 + POISON_POINTER_DELTA)
13*f7c14bbaSAndroid Build Coastguard Worker
14*f7c14bbaSAndroid Build Coastguard Worker
INIT_LIST_HEAD(struct list_head * list)15*f7c14bbaSAndroid Build Coastguard Worker static inline void INIT_LIST_HEAD(struct list_head *list)
16*f7c14bbaSAndroid Build Coastguard Worker {
17*f7c14bbaSAndroid Build Coastguard Worker list->next = list;
18*f7c14bbaSAndroid Build Coastguard Worker list->prev = list;
19*f7c14bbaSAndroid Build Coastguard Worker }
20*f7c14bbaSAndroid Build Coastguard Worker
__list_add(struct list_head * new,struct list_head * prev,struct list_head * next)21*f7c14bbaSAndroid Build Coastguard Worker static inline void __list_add(struct list_head *new,
22*f7c14bbaSAndroid Build Coastguard Worker struct list_head *prev,
23*f7c14bbaSAndroid Build Coastguard Worker struct list_head *next)
24*f7c14bbaSAndroid Build Coastguard Worker {
25*f7c14bbaSAndroid Build Coastguard Worker next->prev = new;
26*f7c14bbaSAndroid Build Coastguard Worker new->next = next;
27*f7c14bbaSAndroid Build Coastguard Worker new->prev = prev;
28*f7c14bbaSAndroid Build Coastguard Worker prev->next = new;
29*f7c14bbaSAndroid Build Coastguard Worker }
30*f7c14bbaSAndroid Build Coastguard Worker
31*f7c14bbaSAndroid Build Coastguard Worker /**
32*f7c14bbaSAndroid Build Coastguard Worker * list_add - add a new entry
33*f7c14bbaSAndroid Build Coastguard Worker * @new: new entry to be added
34*f7c14bbaSAndroid Build Coastguard Worker * @head: list head to add it after
35*f7c14bbaSAndroid Build Coastguard Worker *
36*f7c14bbaSAndroid Build Coastguard Worker * Insert a new entry after the specified head.
37*f7c14bbaSAndroid Build Coastguard Worker * This is good for implementing stacks.
38*f7c14bbaSAndroid Build Coastguard Worker */
list_add(struct list_head * new,struct list_head * head)39*f7c14bbaSAndroid Build Coastguard Worker static inline void list_add(struct list_head *new, struct list_head *head)
40*f7c14bbaSAndroid Build Coastguard Worker {
41*f7c14bbaSAndroid Build Coastguard Worker __list_add(new, head, head->next);
42*f7c14bbaSAndroid Build Coastguard Worker }
43*f7c14bbaSAndroid Build Coastguard Worker
44*f7c14bbaSAndroid Build Coastguard Worker /*
45*f7c14bbaSAndroid Build Coastguard Worker * Delete a list entry by making the prev/next entries
46*f7c14bbaSAndroid Build Coastguard Worker * point to each other.
47*f7c14bbaSAndroid Build Coastguard Worker *
48*f7c14bbaSAndroid Build Coastguard Worker * This is only for internal list manipulation where we know
49*f7c14bbaSAndroid Build Coastguard Worker * the prev/next entries already!
50*f7c14bbaSAndroid Build Coastguard Worker */
__list_del(struct list_head * prev,struct list_head * next)51*f7c14bbaSAndroid Build Coastguard Worker static inline void __list_del(struct list_head * prev, struct list_head * next)
52*f7c14bbaSAndroid Build Coastguard Worker {
53*f7c14bbaSAndroid Build Coastguard Worker next->prev = prev;
54*f7c14bbaSAndroid Build Coastguard Worker prev->next = next;
55*f7c14bbaSAndroid Build Coastguard Worker }
56*f7c14bbaSAndroid Build Coastguard Worker
57*f7c14bbaSAndroid Build Coastguard Worker /**
58*f7c14bbaSAndroid Build Coastguard Worker * list_del - deletes entry from list.
59*f7c14bbaSAndroid Build Coastguard Worker * @entry: the element to delete from the list.
60*f7c14bbaSAndroid Build Coastguard Worker * Note: list_empty() on entry does not return true after this, the entry is
61*f7c14bbaSAndroid Build Coastguard Worker * in an undefined state.
62*f7c14bbaSAndroid Build Coastguard Worker */
__list_del_entry(struct list_head * entry)63*f7c14bbaSAndroid Build Coastguard Worker static inline void __list_del_entry(struct list_head *entry)
64*f7c14bbaSAndroid Build Coastguard Worker {
65*f7c14bbaSAndroid Build Coastguard Worker __list_del(entry->prev, entry->next);
66*f7c14bbaSAndroid Build Coastguard Worker }
67*f7c14bbaSAndroid Build Coastguard Worker
list_del(struct list_head * entry)68*f7c14bbaSAndroid Build Coastguard Worker static inline void list_del(struct list_head *entry)
69*f7c14bbaSAndroid Build Coastguard Worker {
70*f7c14bbaSAndroid Build Coastguard Worker __list_del(entry->prev, entry->next);
71*f7c14bbaSAndroid Build Coastguard Worker entry->next = LIST_POISON1;
72*f7c14bbaSAndroid Build Coastguard Worker entry->prev = LIST_POISON2;
73*f7c14bbaSAndroid Build Coastguard Worker }
74*f7c14bbaSAndroid Build Coastguard Worker
list_empty(const struct list_head * head)75*f7c14bbaSAndroid Build Coastguard Worker static inline int list_empty(const struct list_head *head)
76*f7c14bbaSAndroid Build Coastguard Worker {
77*f7c14bbaSAndroid Build Coastguard Worker return head->next == head;
78*f7c14bbaSAndroid Build Coastguard Worker }
79*f7c14bbaSAndroid Build Coastguard Worker
80*f7c14bbaSAndroid Build Coastguard Worker #define list_entry(ptr, type, member) \
81*f7c14bbaSAndroid Build Coastguard Worker container_of(ptr, type, member)
82*f7c14bbaSAndroid Build Coastguard Worker #define list_first_entry(ptr, type, member) \
83*f7c14bbaSAndroid Build Coastguard Worker list_entry((ptr)->next, type, member)
84*f7c14bbaSAndroid Build Coastguard Worker #define list_next_entry(pos, member) \
85*f7c14bbaSAndroid Build Coastguard Worker list_entry((pos)->member.next, typeof(*(pos)), member)
86*f7c14bbaSAndroid Build Coastguard Worker #define list_for_each_entry(pos, head, member) \
87*f7c14bbaSAndroid Build Coastguard Worker for (pos = list_first_entry(head, typeof(*pos), member); \
88*f7c14bbaSAndroid Build Coastguard Worker &pos->member != (head); \
89*f7c14bbaSAndroid Build Coastguard Worker pos = list_next_entry(pos, member))
90*f7c14bbaSAndroid Build Coastguard Worker
91*f7c14bbaSAndroid Build Coastguard Worker #endif
92