| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
#include <stdbool.h> |
| 6 |
|
| 7 |
struct list_node { |
| 8 |
struct list_node *next, *prev; |
| 9 |
}; |
| 10 |
|
| 11 |
#define list_entry(ptr, type, node) container_of(ptr, type, node) |
| 12 |
#define list_next_entry(ptr, node) list_entry((ptr)->node.next, __typeof__(*(ptr)), node) |
| 13 |
#define list_prev_entry(ptr, node) list_entry((ptr)->node.prev, __typeof__(*(ptr)), node) |
| 14 |
|
| 15 |
|
| 16 |
static inline void __list_insert_between(struct list_node *prev, struct list_node *next, |
| 17 |
struct list_node *new_) { |
| 18 |
new_->prev = prev; |
| 19 |
new_->next = next; |
| 20 |
next->prev = new_; |
| 21 |
prev->next = new_; |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
static inline void list_insert_after(struct list_node *curr, struct list_node *new_) { |
| 26 |
__list_insert_between(curr, curr->next, new_); |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
static inline void list_insert_before(struct list_node *curr, struct list_node *new_) { |
| 31 |
__list_insert_between(curr->prev, curr, new_); |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
static inline void __list_link(struct list_node *prev, struct list_node *next) { |
| 36 |
next->prev = prev; |
| 37 |
prev->next = next; |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
static inline void list_remove(struct list_node *to_remove) { |
| 42 |
__list_link(to_remove->prev, to_remove->next); |
| 43 |
to_remove->prev = (void *)-1; |
| 44 |
to_remove->next = (void *)-2; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
static inline void list_move_before(struct list_node *to_move, struct list_node *new_next) { |
| 49 |
list_remove(to_move); |
| 50 |
list_insert_before(new_next, to_move); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
static inline void list_move_after(struct list_node *to_move, struct list_node *new_prev) { |
| 55 |
list_remove(to_move); |
| 56 |
list_insert_after(new_prev, to_move); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
static inline void list_init_head(struct list_node *head) { |
| 61 |
head->next = head->prev = head; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
static inline void list_replace(struct list_node *old, struct list_node *n) { |
| 66 |
__list_insert_between(old->prev, old->next, n); |
| 67 |
old->prev = (void *)-1; |
| 68 |
old->next = (void *)-2; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
static inline bool list_is_empty(const struct list_node *head) { |
| 74 |
return head->prev == head; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
static inline void list_splice(struct list_node *from, struct list_node *to) { |
| 79 |
if (list_is_empty(from)) { |
| 80 |
return; |
| 81 |
} |
| 82 |
__list_link(from->prev, to->next); |
| 83 |
__list_link(to, from->next); |
| 84 |
list_init_head(from); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
static inline bool |
| 89 |
list_node_is_first(const struct list_node *head, const struct list_node *to_check) { |
| 90 |
return head->next == to_check; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
static inline bool |
| 95 |
list_node_is_last(const struct list_node *head, const struct list_node *to_check) { |
| 96 |
return head->prev == to_check; |
| 97 |
} |
| 98 |
|
| 99 |
#define list_foreach(type, i, head, member) \ |
| 100 |
for (type *i = list_entry((head)->next, type, member); &i->member != (head); \ |
| 101 |
i = list_next_entry(i, member)) |
| 102 |
|
| 103 |
|
| 104 |
#define list_foreach_safe(type, i, head, member) \ |
| 105 |
for (type *i = list_entry((head)->next, type, member), \ |
| 106 |
*__tmp = list_next_entry(i, member); \ |
| 107 |
&i->member != (head); i = __tmp, __tmp = list_next_entry(i, member)) |