| 1 |
|
| 2 |
#ifndef LIST_H |
| 3 |
#define LIST_H |
| 4 |
|
| 5 |
#include <stddef.h> |
| 6 |
|
| 7 |
#include "list_types.h" |
| 8 |
|
| 9 |
|
| 10 |
#define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b)) |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#define container_of(ptr, type, member) ({ \ |
| 20 |
void *__mptr = (void *)(ptr); \ |
| 21 |
_Static_assert(__same_type(*(ptr), ((type *)0)->member) || \ |
| 22 |
__same_type(*(ptr), void), \ |
| 23 |
"pointer type mismatch in container_of()"); \ |
| 24 |
((type *)(__mptr - offsetof(type, member))); }) |
| 25 |
|
| 26 |
#define LIST_POISON1 ((void *) 0x100) |
| 27 |
#define LIST_POISON2 ((void *) 0x122) |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
#define LIST_HEAD_INIT(name) { &(name), &(name) } |
| 40 |
|
| 41 |
#define LIST_HEAD(name) \ |
| 42 |
struct list_head name = LIST_HEAD_INIT(name) |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
static inline void INIT_LIST_HEAD(struct list_head *list) |
| 52 |
{ |
| 53 |
list->next = list; |
| 54 |
list->prev = list; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 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 |
prev->next = new; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 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 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
static inline void list_add_tail(struct list_head *new, struct list_head *head) |
| 95 |
{ |
| 96 |
__list_add(new, head->prev, head); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
static inline void __list_del(struct list_head *prev, struct list_head *next) |
| 107 |
{ |
| 108 |
next->prev = prev; |
| 109 |
prev->next = next; |
| 110 |
} |
| 111 |
|
| 112 |
static inline void __list_del_entry(struct list_head *entry) |
| 113 |
{ |
| 114 |
__list_del(entry->prev, entry->next); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
static inline void list_del(struct list_head *entry) |
| 124 |
{ |
| 125 |
__list_del_entry(entry); |
| 126 |
entry->next = LIST_POISON1; |
| 127 |
entry->prev = LIST_POISON2; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
static inline void list_replace(struct list_head *old, |
| 138 |
struct list_head *new) |
| 139 |
{ |
| 140 |
new->next = old->next; |
| 141 |
new->next->prev = new; |
| 142 |
new->prev = old->prev; |
| 143 |
new->prev->next = new; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
static inline void list_replace_init(struct list_head *old, |
| 154 |
struct list_head *new) |
| 155 |
{ |
| 156 |
list_replace(old, new); |
| 157 |
INIT_LIST_HEAD(old); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
static inline void list_move(struct list_head *list, struct list_head *head) |
| 166 |
{ |
| 167 |
__list_del_entry(list); |
| 168 |
list_add(list, head); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
static inline void list_move_tail(struct list_head *list, |
| 177 |
struct list_head *head) |
| 178 |
{ |
| 179 |
__list_del_entry(list); |
| 180 |
list_add_tail(list, head); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
static inline int list_is_first(const struct list_head *list, const struct list_head *head) |
| 189 |
{ |
| 190 |
return list->prev == head; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
static inline int list_is_last(const struct list_head *list, const struct list_head *head) |
| 199 |
{ |
| 200 |
return list->next == head; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
static inline int list_is_head(const struct list_head *list, const struct list_head *head) |
| 209 |
{ |
| 210 |
return list == head; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
static inline int list_empty(const struct list_head *head) |
| 218 |
{ |
| 219 |
return head->next == head; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
#define list_entry(ptr, type, member) \ |
| 229 |
container_of(ptr, type, member) |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
#define list_first_entry(ptr, type, member) \ |
| 240 |
list_entry((ptr)->next, type, member) |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
#define list_last_entry(ptr, type, member) \ |
| 251 |
list_entry((ptr)->prev, type, member) |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
#define list_next_entry(pos, member) \ |
| 259 |
list_entry((pos)->member.next, typeof(*(pos)), member) |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
#define list_prev_entry(pos, member) \ |
| 267 |
list_entry((pos)->member.prev, typeof(*(pos)), member) |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
#define list_entry_is_head(pos, head, member) \ |
| 276 |
(&pos->member == (head)) |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
#define list_for_each_entry(pos, head, member) \ |
| 285 |
for (pos = list_first_entry(head, typeof(*pos), member); \ |
| 286 |
!list_entry_is_head(pos, head, member); \ |
| 287 |
pos = list_next_entry(pos, member)) |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
#define list_for_each_entry_reverse(pos, head, member) \ |
| 296 |
for (pos = list_last_entry(head, typeof(*pos), member); \ |
| 297 |
!list_entry_is_head(pos, head, member); \ |
| 298 |
pos = list_prev_entry(pos, member)) |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
#define list_for_each_entry_safe(pos, n, head, member) \ |
| 308 |
for (pos = list_first_entry(head, typeof(*pos), member), \ |
| 309 |
n = list_next_entry(pos, member); \ |
| 310 |
!list_entry_is_head(pos, head, member); \ |
| 311 |
pos = n, n = list_next_entry(n, member)) |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
#define HLIST_HEAD_INIT { .first = NULL } |
| 321 |
#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL) |
| 322 |
static inline void INIT_HLIST_NODE(struct hlist_node *h) |
| 323 |
{ |
| 324 |
h->next = NULL; |
| 325 |
h->pprev = NULL; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
static inline int hlist_unhashed(const struct hlist_node *h) |
| 337 |
{ |
| 338 |
return !h->pprev; |
| 339 |
} |
| 340 |
|
| 341 |
static inline void __hlist_del(struct hlist_node *n) |
| 342 |
{ |
| 343 |
struct hlist_node *next = n->next; |
| 344 |
struct hlist_node **pprev = n->pprev; |
| 345 |
|
| 346 |
*pprev = next; |
| 347 |
if (next) |
| 348 |
next->pprev = pprev; |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
static inline void hlist_del(struct hlist_node *n) |
| 359 |
{ |
| 360 |
__hlist_del(n); |
| 361 |
n->next = LIST_POISON1; |
| 362 |
n->pprev = LIST_POISON2; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
static inline void hlist_del_init(struct hlist_node *n) |
| 372 |
{ |
| 373 |
if (!hlist_unhashed(n)) { |
| 374 |
__hlist_del(n); |
| 375 |
INIT_HLIST_NODE(n); |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) |
| 388 |
{ |
| 389 |
struct hlist_node *first = h->first; |
| 390 |
|
| 391 |
n->next = first; |
| 392 |
if (first) |
| 393 |
first->pprev = &n->next; |
| 394 |
h->first = n; |
| 395 |
n->pprev = &h->first; |
| 396 |
} |
| 397 |
|
| 398 |
#define hlist_entry(ptr, type, member) container_of(ptr, type, member) |
| 399 |
|
| 400 |
#define hlist_entry_safe(ptr, type, member) \ |
| 401 |
({ typeof(ptr) ____ptr = (ptr); \ |
| 402 |
____ptr ? hlist_entry(____ptr, type, member) : NULL; \ |
| 403 |
}) |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
#define hlist_for_each_entry(pos, head, member) \ |
| 412 |
for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\ |
| 413 |
pos; \ |
| 414 |
pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
#define hlist_for_each_entry_safe(pos, n, head, member) \ |
| 424 |
for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\ |
| 425 |
pos && ({ n = pos->member.next; 1; }); \ |
| 426 |
pos = hlist_entry_safe(n, typeof(*pos), member)) |
| 427 |
|
| 428 |
#endif |