forked from arkime/arkime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdll.h
65 lines (52 loc) · 2.33 KB
/
dll.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
/******************************************************************************/
/* dll.h -- Double Linked List
*
* Simple macro based DLL with counts that supports having a single item in
* multiple lists by using a prefix. Every element in the list needs to
* have a [prefix]next and [prefix]prev elements. The list head needs to have
* a [prefix]count element.
*
* WARNING: If using a seperate head structure AND a single item is in multiple
* lists the byte position in the structure of the next and prev elements MUST be
* the same between the head structure and item structures.
*/
#ifndef _DLL_HEADER
#define _DLL_HEADER
#define DLL_INIT(name,head) \
((head)->name##count = 0, \
(head)->name##next = (head)->name##prev = (void *)head \
)
#define DLL_PUSH_TAIL(name,head,element) \
((element)->name##next = (void *)(head), \
(element)->name##prev = (head)->name##prev, \
(head)->name##prev->name##next = (element), \
(head)->name##prev = (element), \
(head)->name##count++ \
)
#define DLL_PUSH_HEAD(name,head,element) \
((element)->name##next = (head)->name##next, \
(element)->name##prev = (void *)(head), \
(head)->name##next->name##prev = (element), \
(head)->name##next = (element), \
(head)->name##count++ \
)
#define DLL_REMOVE(name,head,element) \
((element)->name##prev->name##next = (element)->name##next, \
(element)->name##next->name##prev = (element)->name##prev, \
(element)->name##prev = 0, \
(element)->name##next = 0, \
(head)->name##count-- \
)
#define DLL_POP_HEAD(name, head, element) \
((head)->name##count == 0 ? ((element) = NULL, 0) : ((element) = (head)->name##next, DLL_REMOVE(name, (head), (element)), 1))
#define DLL_POP_TAIL(name, head, element) \
((head)->name##count == 0 ? ((element) = NULL, 0) : ((element) = (head)->name##prev, DLL_REMOVE(name, (head), (element)), 1))
#define DLL_PEEK_HEAD(name,head) \
((head)->name##count == 0?NULL:(head)->name##next)
#define DLL_PEEK_TAIL(name,head) \
((head)->name##count == 0?NULL:(head)->name##prev)
#define DLL_FOREACH(name,head,element) \
for ((element) = (head)->name##next; \
(element) != (void *)(head); \
(element)=(element)->name##next)
#endif