list.h 7.5 KB
Newer Older
R
Roberto Sassu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _LINUX_LIST_H
#define _LINUX_LIST_H

# define POISON_POINTER_DELTA 0

/*
 * These are non-NULL pointers that will result in page faults
 * under normal circumstances, used to verify that nobody uses
 * non-initialized list entries.
 */
#define LIST_POISON1  ((void *) 0x100 + POISON_POINTER_DELTA)
#define LIST_POISON2  ((void *) 0x200 + POISON_POINTER_DELTA)

/*
 * Copied from include/linux/...
 */

#undef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

/**
 * container_of - cast a member of a structure out to the containing structure
 * @ptr:        the pointer to the member.
 * @type:       the type of the container struct this is embedded in.
 * @member:     the name of the member within the struct.
 *
 */
#define container_of(ptr, type, member) ({                      \
A
Anakin Zhang 已提交
30 31
    const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
    (type *)( (char *)__mptr - offsetof(type,member) );})
R
Roberto Sassu 已提交
32 33 34 35

#define LIST_HEAD_INIT(name) { &(name), &(name) }

#define LIST_HEAD(name) \
A
Anakin Zhang 已提交
36
    struct list_head name = LIST_HEAD_INIT(name)
R
Roberto Sassu 已提交
37

R
Roberto Sassu 已提交
38 39 40 41
#define HLIST_HEAD_INIT { .first = NULL }
#define HLIST_HEAD(name) struct hlist_head name = {  .first = NULL }
#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)

R
Roberto Sassu 已提交
42
struct list_head {
A
Anakin Zhang 已提交
43
    struct list_head *next, *prev;
R
Roberto Sassu 已提交
44 45
};

R
Roberto Sassu 已提交
46
struct hlist_head {
A
Anakin Zhang 已提交
47
    struct hlist_node *first;
R
Roberto Sassu 已提交
48 49 50
};

struct hlist_node {
A
Anakin Zhang 已提交
51
    struct hlist_node *next, **pprev;
R
Roberto Sassu 已提交
52 53
};

R
Roberto Sassu 已提交
54 55
static inline void INIT_LIST_HEAD(struct list_head *list)
{
A
Anakin Zhang 已提交
56 57
    list->next = list;
    list->prev = list;
R
Roberto Sassu 已提交
58 59 60 61 62 63 64 65 66
}

/*
 * Insert a new entry between two known consecutive entries.
 *
 * This is only for internal list manipulation where we know
 * the prev/next entries already!
 */
static inline void __list_add(struct list_head *new,
A
Anakin Zhang 已提交
67 68
                  struct list_head *prev,
                  struct list_head *next)
R
Roberto Sassu 已提交
69
{
A
Anakin Zhang 已提交
70 71 72 73
    next->prev = new;
    new->next = next;
    new->prev = prev;
    prev->next = new;
R
Roberto Sassu 已提交
74 75 76 77 78 79 80 81 82 83 84 85
}

/**
 * list_add - add a new entry
 * @new: new entry to be added
 * @head: list head to add it after
 *
 * Insert a new entry after the specified head.
 * This is good for implementing stacks.
 */
static inline void list_add(struct list_head *new, struct list_head *head)
{
A
Anakin Zhang 已提交
86
    __list_add(new, head, head->next);
R
Roberto Sassu 已提交
87 88 89 90 91 92 93 94 95 96 97 98
}

/**
 * list_add_tail - add a new entry
 * @new: new entry to be added
 * @head: list head to add it before
 *
 * Insert a new entry before the specified head.
 * This is useful for implementing queues.
 */
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
A
Anakin Zhang 已提交
99
    __list_add(new, head->prev, head);
R
Roberto Sassu 已提交
100 101 102 103 104 105 106 107 108 109 110
}

/*
 * Delete a list entry by making the prev/next entries
 * point to each other.
 *
 * This is only for internal list manipulation where we know
 * the prev/next entries already!
 */
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
A
Anakin Zhang 已提交
111 112
    next->prev = prev;
    prev->next = next;
R
Roberto Sassu 已提交
113 114 115 116 117 118 119 120 121 122
}

/**
 * list_del - deletes entry from list.
 * @entry: the element to delete from the list.
 * Note: list_empty() on entry does not return true after this, the entry is
 * in an undefined state.
 */
static inline void __list_del_entry(struct list_head *entry)
{
A
Anakin Zhang 已提交
123
    __list_del(entry->prev, entry->next);
R
Roberto Sassu 已提交
124 125 126 127
}

static inline void list_del(struct list_head *entry)
{
A
Anakin Zhang 已提交
128 129 130
    __list_del_entry(entry);
    entry->next = LIST_POISON1;
    entry->prev = LIST_POISON2;
R
Roberto Sassu 已提交
131 132 133 134 135 136 137 138
}

/**
 * list_empty - tests whether a list is empty
 * @head: the list to test.
 */
static inline int list_empty(const struct list_head *head)
{
A
Anakin Zhang 已提交
139
    return head->next == head;
R
Roberto Sassu 已提交
140 141 142 143 144 145 146 147 148
}

/**
 * list_entry - get the struct for this entry
 * @ptr:	the &struct list_head pointer.
 * @type:	the type of the struct this is embedded in.
 * @member:	the name of the list_head within the struct.
 */
#define list_entry(ptr, type, member) \
A
Anakin Zhang 已提交
149
    container_of(ptr, type, member)
R
Roberto Sassu 已提交
150 151 152 153 154 155 156 157 158 159

/**
 * list_first_entry - get the first element from a list
 * @ptr:	the list head to take the element from.
 * @type:	the type of the struct this is embedded in.
 * @member:	the name of the list_head within the struct.
 *
 * Note, that list is expected to be not empty.
 */
#define list_first_entry(ptr, type, member) \
A
Anakin Zhang 已提交
160
    list_entry((ptr)->next, type, member)
R
Roberto Sassu 已提交
161 162 163 164 165 166 167 168 169 170

/**
 * list_last_entry - get the last element from a list
 * @ptr:	the list head to take the element from.
 * @type:	the type of the struct this is embedded in.
 * @member:	the name of the list_head within the struct.
 *
 * Note, that list is expected to be not empty.
 */
#define list_last_entry(ptr, type, member) \
A
Anakin Zhang 已提交
171
    list_entry((ptr)->prev, type, member)
R
Roberto Sassu 已提交
172 173 174 175 176 177 178 179 180 181

/**
 * list_first_entry_or_null - get the first element from a list
 * @ptr:	the list head to take the element from.
 * @type:	the type of the struct this is embedded in.
 * @member:	the name of the list_head within the struct.
 *
 * Note that if the list is empty, it returns NULL.
 */
#define list_first_entry_or_null(ptr, type, member) ({ \
A
Anakin Zhang 已提交
182 183 184
    struct list_head *head__ = (ptr); \
    struct list_head *pos__ = READ_ONCE(head__->next); \
    pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
R
Roberto Sassu 已提交
185 186 187 188 189 190 191 192
})

/**
 * list_next_entry - get the next element in list
 * @pos:	the type * to cursor
 * @member:	the name of the list_head within the struct.
 */
#define list_next_entry(pos, member) \
A
Anakin Zhang 已提交
193
    list_entry((pos)->member.next, typeof(*(pos)), member)
R
Roberto Sassu 已提交
194 195 196 197 198 199 200

/**
 * list_prev_entry - get the prev element in list
 * @pos:	the type * to cursor
 * @member:	the name of the list_head within the struct.
 */
#define list_prev_entry(pos, member) \
A
Anakin Zhang 已提交
201
    list_entry((pos)->member.prev, typeof(*(pos)), member)
R
Roberto Sassu 已提交
202 203 204 205 206 207 208 209

/**
 * list_for_each_entry	-	iterate over list of given type
 * @pos:	the type * to use as a loop cursor.
 * @head:	the head for your list.
 * @member:	the name of the list_head within the struct.
 */
#define list_for_each_entry(pos, head, member)				\
A
Anakin Zhang 已提交
210 211 212
    for (pos = list_first_entry(head, typeof(*pos), member);	\
         &pos->member != (head);					\
         pos = list_next_entry(pos, member))
R
Roberto Sassu 已提交
213 214 215 216 217 218 219 220 221

/**
 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
 * @pos:	the type * to use as a loop cursor.
 * @n:		another type * to use as temporary storage
 * @head:	the head for your list.
 * @member:	the name of the list_head within the struct.
 */
#define list_for_each_entry_safe(pos, n, head, member)			\
A
Anakin Zhang 已提交
222 223 224 225
    for (pos = list_first_entry(head, typeof(*pos), member),	\
        n = list_next_entry(pos, member);			\
         &pos->member != (head); 					\
         pos = n, n = list_next_entry(n, member))
R
Roberto Sassu 已提交
226

R
Roberto Sassu 已提交
227 228
static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
{
A
Anakin Zhang 已提交
229 230 231 232 233 234
    struct hlist_node *first = h->first;
    n->next = first;
    if (first)
        first->pprev = &n->next;
    h->first = n;
    n->pprev = &h->first;
R
Roberto Sassu 已提交
235 236 237 238 239
}

#define hlist_entry(ptr, type, member) container_of(ptr,type,member)

#define hlist_for_each(pos, head) \
A
Anakin Zhang 已提交
240
    for (pos = (head)->first; pos ; pos = pos->next)
R
Roberto Sassu 已提交
241 242

#define hlist_for_each_safe(pos, n, head) \
A
Anakin Zhang 已提交
243 244
    for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
         pos = n)
R
Roberto Sassu 已提交
245 246

#define hlist_entry_safe(ptr, type, member) \
A
Anakin Zhang 已提交
247 248 249
    ({ typeof(ptr) ____ptr = (ptr); \
       ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
    })
R
Roberto Sassu 已提交
250 251 252 253 254 255 256 257

/**
 * hlist_for_each_entry	- iterate over list of given type
 * @pos:	the type * to use as a loop cursor.
 * @head:	the head for your list.
 * @member:	the name of the hlist_node within the struct.
 */
#define hlist_for_each_entry(pos, head, member)				\
A
Anakin Zhang 已提交
258 259 260
    for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
         pos;							\
         pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
R
Roberto Sassu 已提交
261 262 263 264

#define hlist_for_each_entry_rcu hlist_for_each_entry
#define hlist_add_head_rcu hlist_add_head

R
Roberto Sassu 已提交
265
#endif /*_LINUX_LIST_H*/