|
| 1 | +/* |
| 2 | + * @Author: jiejie |
| 3 | + * @Github: https://github.com/jiejieTop |
| 4 | + * @Date: 2019-12-11 22:47:55 |
| 5 | + * @LastEditTime : 2020-01-08 20:39:26 |
| 6 | + * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. |
| 7 | + */ |
| 8 | +#ifndef _LIST_H_ |
| 9 | +#define _LIST_H_ |
| 10 | + |
| 11 | +typedef struct list_node { |
| 12 | + struct list_node *next; |
| 13 | + struct list_node *prev; |
| 14 | +} list_t; |
| 15 | + |
| 16 | +#define OFFSET_OF_FIELD(type, field) \ |
| 17 | + ((size_t)&(((type *)0)->field)) |
| 18 | + |
| 19 | +#define CONTAINER_OF_FIELD(ptr, type, field) \ |
| 20 | + ((type *)((unsigned char *)(ptr) - OFFSET_OF_FIELD(type, field))) |
| 21 | + |
| 22 | +#define LIST_NODE(node) \ |
| 23 | + { &(node), &(node) } |
| 24 | + |
| 25 | +#define LIST_DEFINE(list) \ |
| 26 | + list_t list = { &(list), &(list) } |
| 27 | + |
| 28 | +#define LIST_ENTRY(list, type, field) \ |
| 29 | + CONTAINER_OF_FIELD(list, type, field) |
| 30 | + |
| 31 | +#define LIST_FIRST_ENTRY(list, type, field) \ |
| 32 | + LIST_ENTRY((list)->next, type, field) |
| 33 | + |
| 34 | +#define LIST_FIRST_ENTRY_OR_NULL(list, type, field) \ |
| 35 | + (list_is_empty(list) ? NULL : LIST_FIRST_ENTRY(list, type, field)) |
| 36 | + |
| 37 | +#define LIST_FOR_EACH(curr, list) \ |
| 38 | + for (curr = (list)->next; curr != (list); curr = curr->next) |
| 39 | + |
| 40 | +#define LIST_FOR_EACH_PREV(curr, list) \ |
| 41 | + for (curr = (list)->prev; curr != (list); curr = curr->prev) |
| 42 | + |
| 43 | +#define LIST_FOR_EACH_SAFE(curr, next, list) \ |
| 44 | + for (curr = (list)->next, next = curr->next; curr != (list); \ |
| 45 | + curr = next, next = curr->next) |
| 46 | + |
| 47 | +#define LIST_FOR_EACH_PREV_SAFE(curr, next, list) \ |
| 48 | + for (curr = (list)->prev, next = curr->prev; \ |
| 49 | + curr != (list); \ |
| 50 | + curr = next, next = curr->prev) |
| 51 | + |
| 52 | +void list_init(list_t *list); |
| 53 | +void list_add(list_t *node, list_t *list); |
| 54 | +void list_add_tail(list_t *node, list_t *list); |
| 55 | +void list_del(list_t *entry); |
| 56 | +void list_del_init(list_t *entry); |
| 57 | +void list_move(list_t *node, list_t *list); |
| 58 | +void list_move_tail(list_t *node, list_t *list); |
| 59 | +int list_is_empty(list_t *list); |
| 60 | + |
| 61 | +#endif /* _LIST_H_ */ |
| 62 | + |
0 commit comments