-
Notifications
You must be signed in to change notification settings - Fork 0
/
ring.h
88 lines (68 loc) · 2.21 KB
/
ring.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
#ifndef _JIMCORE_RING_H_
#define _JIMCORE_RING_H_
#include <stddef.h>
typedef struct ring {
struct ring *prev;
struct ring *next;
} ring_t;
static inline void
ring_init(ring_t *e)
{
e->next = e;
e->prev = e;
}
#define ring_static_init(r) r = { &r, &r }
static inline bool
ring_is_empty(ring_t *e)
{
return e->next == e;
}
static inline void
_ring_append(ring_t *head, ring_t *e)
{
e->next = head;
e->prev = head->prev;
head->prev->next = e;
head->prev = e;
}
#define ring_append(l, n, e) \
_ring_append(l, &e->n)
static inline void
_ring_prepend(ring_t *head, ring_t *e)
{
e->next = head->next;
e->prev = head;
head->next->prev = e;
head->next = e;
}
#define ring_prepend(l, n, e) \
_ring_prepend(l, &e->n)
#define ring_entry(n, t, e) \
((t *)((char *)(e) - offsetof(t, n)))
#define ring_get_head(l, n, t) \
(ring_is_empty(l) ? NULL : ring_entry(n, t, (l)->next))
#define ring_get_tail(l, n, t) \
(ring_is_empty(l) ? NULL : ring_entry(n, t, (l)->prev))
#define ring_next(l, n, t, e) \
((e)->n.next == (l) ? NULL : ring_entry(n, t, (e)->n.next))
#define ring_prev(l, n, t, e) \
((e)->n.prev == (l) ? NULL : ring_entry(n, t, e->n.prev))
static inline void
_ring_delete(ring_t *e)
{
e->prev->next = e->next;
e->next->prev = e->prev;
ring_init(e);
}
#define ring_delete(n, e) \
_ring_delete(&e->n)
#define ring_foreach_decl(l, n, t, e) \
for (t *e = ring_get_head((l), n, t); \
e; \
e = ring_next((l), n, t, e))
#define ring_foreach_mutable_decl(l, n, t, e) \
for (t *e = ring_get_head((l), n, t), \
*__nxt = e ? ring_next((l), n, t, e) : NULL; \
e; \
e = __nxt, __nxt = ring_next((l), n, t, __nxt))
#endif