-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.h
68 lines (56 loc) · 1.95 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 __LIST_H__
#define __LIST_H__
#include <stddef.h>
// list node to link the *real* node into list
struct list_head {
struct list_head *next, *prev;
};
// check whether the list is empty (contains only one pseudo list node)
#define list_empty(list) ((list)->next == (list))
// get the *real* node from the list node
#define list_entry(ptr, type, member) \
(type *)((char *)ptr - offsetof(type, member))
// iterate the list
#define list_for_each_entry(pos, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
// iterate the list safely, during which node could be added or removed in the list
#define list_for_each_entry_safe(pos, q, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member), \
q = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = q, q = list_entry(pos->member.next, typeof(*q), member))
// initialize the list head
static inline void init_list_head(struct list_head *list)
{
list->next = list->prev = list;
}
// insert a new node between prev and next
static inline void list_insert(struct list_head *new,
struct list_head *prev,
struct list_head *next)
{
next->prev = new;
prev->next = new;
new->next = next;
new->prev = prev;
}
// add a list node at the head of the list
static inline void list_add_head(struct list_head *new, struct list_head *head)
{
list_insert(new, head, head->next);
}
// add a list node at the tail of the list
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
list_insert(new, head->prev, head);
}
// delete the node from the list (note that it only remove the entry from
// list, but not free allocated memory)
static inline void list_delete_entry(struct list_head *entry)
{
entry->next->prev = entry->prev;
entry->prev->next = entry->next;
}
#endif