forked from xwax/xwax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.h
119 lines (97 loc) · 2.81 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Copyright (C) 2021 Mark Hills <mark@xwax.org>
*
* This file is part of "xwax".
*
* "xwax" is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License, version 3 as
* published by the Free Software Foundation.
*
* "xwax" is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>.
*
*/
/*
* Double-linked lists
*/
#ifndef LIST_H
#define LIST_H
#include <stdbool.h>
#include <stddef.h> /* offsetof() */
#define container_of(ptr, type, member) \
((type*)((char*)ptr - offsetof(type, member)))
struct list {
struct list *prev, *next;
};
#define LIST_INIT(list) { \
.next = &list, \
.prev = &list \
}
static inline void list_init(struct list *list)
{
list->next = list;
list->prev = list;
}
static inline void __list_add(struct list *new, struct list *prev,
struct list *next)
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
}
/*
* Insert a new list entry after the given head
*/
static inline void list_add(struct list *new, struct list *head)
{
__list_add(new, head, head->next);
}
/*
* Insert a new list entry before the given head (ie. end of list)
*/
static inline void list_add_tail(struct list *new, struct list *head)
{
__list_add(new, head->prev, head);
}
static inline void list_del(struct list *entry)
{
struct list *next, *prev;
next = entry->next;
prev = entry->prev;
next->prev = prev;
prev->next = next;
}
static inline bool list_empty(const struct list *head)
{
return head->next == head;
}
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
/*
* Iterate through each item in the list
*/
#define list_for_each(item, head, member) \
for (item = list_entry((head)->next, typeof(*item), member); \
&item->member != (head); \
item = list_entry(item->member.next, typeof(*item), member))
/*
* Iterate through each item in the list, with a buffer to support
* the safe removal of a list entry.
*
* pos: current entry (struct type*)
* tmp: temporary storage (struct type*)
* head: top of list (struct list*)
* member: the name of the 'struct list' within 'struct type'
*/
#define list_for_each_safe(pos, tmp, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member), \
tmp = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = tmp, tmp = list_entry(tmp->member.next, typeof(*tmp), member))
#endif