-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlist.h
More file actions
70 lines (57 loc) · 1.88 KB
/
list.h
File metadata and controls
70 lines (57 loc) · 1.88 KB
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
69
70
/* just a simple generic double list for talloc_test.c usage example */
#ifndef _LIST_H_
#define _LIST_H_
struct list_head
{
struct list_head *next, *prev;
};
#define LIST_INIT(list) \
struct list_head list = { .next = &(list), .prev = &(list) }
#define list_init(list) do { (list)->next = (list), (list)->prev = (list); } while(0)
#define LIST_ADD(element, before, after) do { \
(element)->prev = (before); \
(element)->next = (after); \
(before)->next = (element); \
(after)->prev = (element); \
}while(0)
#define LIST_DEL(before, after) do { \
(before)->next = (after); \
(after)->prev = (before); \
}while(0)
#define LIST_EMPTY(list) ( (list)->next == (list) )
#define list_entry(element, cast, field) \
(cast*) ( (unsigned char *)element - (unsigned long)&((cast*)0)->field )
#define list_for_each(iter, list) \
for(iter = (list)->next ; iter != (list); iter = (iter)->next)
static __inline__ void list_add(struct list_head *element, struct list_head *head)
{
if(!element->next && !element->prev)
{
struct list_head *after = head->next;
LIST_ADD(element, head, after);
}
}
static __inline__ void list_add_tail(struct list_head *element, struct list_head *head)
{
if(!element->next && !element->prev)
{
struct list_head *before = head->prev;
LIST_ADD(element, before, head);
}
}
static __inline__ void list_del(struct list_head *element)
{
if(element->next && element->prev)
{
struct list_head *before = element->prev;
struct list_head *after = element->next;
LIST_DEL(before, after);
element->next = element->prev = NULL;
}
}
static __inline__ void list_del_init(struct list_head *element)
{
list_del(element);
list_init(element);
}
#endif