-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.h
68 lines (53 loc) · 1.73 KB
/
list.h
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#ifndef TK_LIST_H
#define TK_LIST_H
#ifndef NULL
#define NULL 0
#endif
typedef struct list_head {
struct list_head *prev, *next;
}list_head;
// 初始化链表
#define INIT_LIST_HEAD(ptr) do {\
struct list_head *_ptr = (struct list_head *)ptr; \
(_ptr)->next = (_ptr); (_ptr->prev) = (_ptr); \
}while(0)
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) ); \
})
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
#define list_for_each_prev(pos, head) \
for (pos = (head)->prev; pos != (head); pos = pos->prev)
// 插入新节点
static inline void __list_add(struct list_head *_new, struct list_head *prev, struct list_head *next) {
_new->next = next;
next->prev = _new;
prev->next = _new;
_new->prev = prev;
}
// 头部新增
static inline void list_add(struct list_head *_new, struct list_head *head) {
__list_add(_new, head, head->next);
}
// 尾部新增
static inline void list_add_tail(struct list_head *_new, struct list_head *head) {
__list_add(_new, head->prev, head);
}
// 删除节点
static inline void __list_del(struct list_head *prev, struct list_head *next) {
prev->next = next;
next->prev = prev;
}
// 删除entry节点
static inline void list_del(struct list_head *entry) {
__list_del(entry->prev, entry->next);
}
// 链表判空
static inline int list_empty(struct list_head *head) {
return (head->next == head) && (head->prev == head);
}
#endif