| 1 |
/* SPDX-License-Identifier: GPL-2.0 */ |
| 2 |
#ifndef _LINUX_LIST_H |
| 3 |
#define _LINUX_LIST_H |
| 4 |
|
| 5 |
/* simplified for our use case */ |
| 6 |
#define READ_ONCE(x) (x) |
| 7 |
#define WRITE_ONCE(dst, src) (dst = (src)) |
| 8 |
#define typeof(x) __typeof__(x) |
| 9 |
#define POISON_POINTER_DELTA 0 |
| 10 |
|
| 11 |
/* following code is taken from <linux/list.h> of v4.20-rc7-202-g1d51b4b1d3f2 */ |
| 12 |
|
| 13 |
/** |
| 14 |
* container_of - cast a member of a structure out to the containing structure |
| 15 |
* @ptr: the pointer to the member. |
| 16 |
* @type: the type of the container struct this is embedded in. |
| 17 |
* @member: the name of the member within the struct. |
| 18 |
* |
| 19 |
*/ |
| 20 |
#define container_of(ptr, type, member) ({ \ |
| 21 |
void *__mptr = (void *)(ptr); \ |
| 22 |
((type *)(__mptr - offsetof(type, member))); }) |
| 23 |
|
| 24 |
/* |
| 25 |
* These are non-NULL pointers that will result in page faults |
| 26 |
* under normal circumstances, used to verify that nobody uses |
| 27 |
* non-initialized list entries. |
| 28 |
*/ |
| 29 |
#define LIST_POISON1 ((void *) 0x100 + POISON_POINTER_DELTA) |
| 30 |
#define LIST_POISON2 ((void *) 0x200 + POISON_POINTER_DELTA) |
| 31 |
|
| 32 |
struct list_head { |
| 33 |
struct list_head *next, *prev; |
| 34 |
}; |
| 35 |
|
| 36 |
/* |
| 37 |
* Simple doubly linked list implementation. |
| 38 |
* |
| 39 |
* Some of the internal functions ("__xxx") are useful when |
| 40 |
* manipulating whole lists rather than single entries, as |
| 41 |
* sometimes we already know the next/prev entries and we can |
| 42 |
* generate better code by using them directly rather than |
| 43 |
* using the generic single-entry routines. |
| 44 |
*/ |
| 45 |
|
| 46 |
#define LIST_HEAD_INIT(name) { &(name), &(name) } |
| 47 |
|
| 48 |
#define LIST_HEAD(name) \ |
| 49 |
struct list_head name = LIST_HEAD_INIT(name) |
| 50 |
|
| 51 |
static inline void INIT_LIST_HEAD(struct list_head *list) |
| 52 |
{ |
| 53 |
WRITE_ONCE(list->next, list); |
| 54 |
list->prev = list; |
| 55 |
} |
| 56 |
|
| 57 |
/* |
| 58 |
* Insert a new entry between two known consecutive entries. |
| 59 |
* |
| 60 |
* This is only for internal list manipulation where we know |
| 61 |
* the prev/next entries already! |
| 62 |
*/ |
| 63 |
static inline void __list_add(struct list_head *new, |
| 64 |
struct list_head *prev, |
| 65 |
struct list_head *next) |
| 66 |
{ |
| 67 |
next->prev = new; |
| 68 |
new->next = next; |
| 69 |
new->prev = prev; |
| 70 |
WRITE_ONCE(prev->next, new); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* list_add - add a new entry |
| 75 |
* @new: new entry to be added |
| 76 |
* @head: list head to add it after |
| 77 |
* |
| 78 |
* Insert a new entry after the specified head. |
| 79 |
* This is good for implementing stacks. |
| 80 |
*/ |
| 81 |
static inline void list_add(struct list_head *new, struct list_head *head) |
| 82 |
{ |
| 83 |
__list_add(new, head, head->next); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
/** |
| 88 |
* list_add_tail - add a new entry |
| 89 |
* @new: new entry to be added |
| 90 |
* @head: list head to add it before |
| 91 |
* |
| 92 |
* Insert a new entry before the specified head. |
| 93 |
* This is useful for implementing queues. |
| 94 |
*/ |
| 95 |
static inline void list_add_tail(struct list_head *new, struct list_head *head) |
| 96 |
{ |
| 97 |
__list_add(new, head->prev, head); |
| 98 |
} |
| 99 |
|
| 100 |
/* |
| 101 |
* Delete a list entry by making the prev/next entries |
| 102 |
* point to each other. |
| 103 |
* |
| 104 |
* This is only for internal list manipulation where we know |
| 105 |
* the prev/next entries already! |
| 106 |
*/ |
| 107 |
static inline void __list_del(struct list_head * prev, struct list_head * next) |
| 108 |
{ |
| 109 |
next->prev = prev; |
| 110 |
WRITE_ONCE(prev->next, next); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* list_del - deletes entry from list. |
| 115 |
* @entry: the element to delete from the list. |
| 116 |
* Note: list_empty() on entry does not return true after this, the entry is |
| 117 |
* in an undefined state. |
| 118 |
*/ |
| 119 |
static inline void __list_del_entry(struct list_head *entry) |
| 120 |
{ |
| 121 |
__list_del(entry->prev, entry->next); |
| 122 |
} |
| 123 |
|
| 124 |
static inline void list_del(struct list_head *entry) |
| 125 |
{ |
| 126 |
__list_del_entry(entry); |
| 127 |
entry->next = LIST_POISON1; |
| 128 |
entry->prev = LIST_POISON2; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* list_entry - get the struct for this entry |
| 133 |
* @ptr: the &struct list_head pointer. |
| 134 |
* @type: the type of the struct this is embedded in. |
| 135 |
* @member: the name of the list_head within the struct. |
| 136 |
*/ |
| 137 |
#define list_entry(ptr, type, member) \ |
| 138 |
container_of(ptr, type, member) |
| 139 |
|
| 140 |
/** |
| 141 |
* list_first_entry - get the first element from a list |
| 142 |
* @ptr: the list head to take the element from. |
| 143 |
* @type: the type of the struct this is embedded in. |
| 144 |
* @member: the name of the list_head within the struct. |
| 145 |
* |
| 146 |
* Note, that list is expected to be not empty. |
| 147 |
*/ |
| 148 |
#define list_first_entry(ptr, type, member) \ |
| 149 |
list_entry((ptr)->next, type, member) |
| 150 |
|
| 151 |
/** |
| 152 |
* list_last_entry - get the last element from a list |
| 153 |
* @ptr: the list head to take the element from. |
| 154 |
* @type: the type of the struct this is embedded in. |
| 155 |
* @member: the name of the list_head within the struct. |
| 156 |
* |
| 157 |
* Note, that list is expected to be not empty. |
| 158 |
*/ |
| 159 |
#define list_last_entry(ptr, type, member) \ |
| 160 |
list_entry((ptr)->prev, type, member) |
| 161 |
|
| 162 |
/** |
| 163 |
* list_next_entry - get the next element in list |
| 164 |
* @pos: the type * to cursor |
| 165 |
* @member: the name of the list_head within the struct. |
| 166 |
*/ |
| 167 |
#define list_next_entry(pos, member) \ |
| 168 |
list_entry((pos)->member.next, typeof(*(pos)), member) |
| 169 |
|
| 170 |
/** |
| 171 |
* list_prev_entry - get the prev element in list |
| 172 |
* @pos: the type * to cursor |
| 173 |
* @member: the name of the list_head within the struct. |
| 174 |
*/ |
| 175 |
#define list_prev_entry(pos, member) \ |
| 176 |
list_entry((pos)->member.prev, typeof(*(pos)), member) |
| 177 |
|
| 178 |
/** |
| 179 |
* list_for_each_entry - iterate over list of given type |
| 180 |
* @pos: the type * to use as a loop cursor. |
| 181 |
* @head: the head for your list. |
| 182 |
* @member: the name of the list_head within the struct. |
| 183 |
*/ |
| 184 |
#define list_for_each_entry(pos, head, member) \ |
| 185 |
for (pos = list_first_entry(head, typeof(*pos), member); \ |
| 186 |
&pos->member != (head); \ |
| 187 |
pos = list_next_entry(pos, member)) |
| 188 |
|
| 189 |
/** |
| 190 |
* list_for_each_entry_reverse - iterate backwards over list of given type. |
| 191 |
* @pos: the type * to use as a loop cursor. |
| 192 |
* @head: the head for your list. |
| 193 |
* @member: the name of the list_head within the struct. |
| 194 |
*/ |
| 195 |
#define list_for_each_entry_reverse(pos, head, member) \ |
| 196 |
for (pos = list_last_entry(head, typeof(*pos), member); \ |
| 197 |
&pos->member != (head); \ |
| 198 |
pos = list_prev_entry(pos, member)) |
| 199 |
|
| 200 |
#endif |