-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrtailq.h
158 lines (134 loc) · 5.46 KB
/
rtailq.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* $Id: rtailq.h,v 1.7 2011/08/21 11:50:33 ryo Exp $ */
/*-
* Copyright (c) 2011 SHIMIZU Ryo <ryo@nerv.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _RTAILQ_H_
#define _RTAILQ_H_
/*
* relocatable TAILQ
*/
#include <stdint.h>
#define RTAILQ_HEAD(name) \
struct name { \
uintptr_t rtqh_first; \
uintptr_t rtqh_last; \
}
#define RTAILQ_HEAD_INITIALIZER(head) \
{ 0, 0 }
#define RTAILQ_ENTRY(type) \
struct { \
uintptr_t rtqe_next; \
uintptr_t rtqe_prev; \
}
/*
* RTAILQ Functions
*/
#define RTAILQ_PTR2OFF(ptr, head) \
((uintptr_t)(ptr) - (uintptr_t)(head))
#define _RTAILQ_OFF2PTR(off, head, type) \
((type *)(((uintptr_t)(off)) + ((uintptr_t)(head))))
#define RTAILQ_OFF2PTR(off, head, type) \
_RTAILQ_OFF2PTR(off, head, struct type) \
#define RTAILQ_INIT(head) do { \
(head)->rtqh_first = 0; \
(head)->rtqh_last = 0; \
} while (/*CONSTCOND*/0)
#define RTAILQ_INSERT_HEAD(head, elm, type, field) do { \
if (((elm)->field.rtqe_next = (head)->rtqh_first) != 0) \
RTAILQ_OFF2PTR((head)->rtqh_first, head, \
type)->field.rtqe_prev = \
RTAILQ_PTR2OFF(&(elm)->field.rtqe_next, head); \
else \
(head)->rtqh_last = \
RTAILQ_PTR2OFF(&(elm)->field.rtqe_next, head); \
(head)->rtqh_first = RTAILQ_PTR2OFF(elm, head); \
(elm)->field.rtqe_prev = \
RTAILQ_PTR2OFF(&(head)->rtqh_first, head); \
} while (/*CONSTCOND*/0)
#define RTAILQ_INSERT_TAIL(head, elm, type, field) do { \
(elm)->field.rtqe_next = 0; \
(elm)->field.rtqe_prev = (head)->rtqh_last; \
*_RTAILQ_OFF2PTR((head)->rtqh_last, head, uintptr_t) = \
RTAILQ_PTR2OFF(elm, head); \
(head)->rtqh_last = RTAILQ_PTR2OFF(&(elm)->field.rtqe_next, \
head); \
} while (/*CONSTCOND*/0)
#define RTAILQ_REMOVE(head, elm, type, field) do { \
if ((elm)->field.rtqe_next != 0) \
RTAILQ_OFF2PTR((elm)->field.rtqe_next, head, \
type)->field.rtqe_prev = (elm)->field.rtqe_prev; \
else \
(head)->rtqh_last = (elm)->field.rtqe_prev; \
*_RTAILQ_OFF2PTR((elm)->field.rtqe_prev, head, uintptr_t) = \
(elm)->field.rtqe_next; \
} while (/*CONSTCOND*/0)
#define RTAILQ_FOREACH(var, head, type, field) \
for ((var) = RTAILQ_OFF2PTR((head)->rtqh_first, head, type); \
RTAILQ_PTR2OFF(var, head) != 0; \
(var) = RTAILQ_OFF2PTR((var)->field.rtqe_next, head, type))
#define RTAILQ_EMPTY(head) \
((head)->rtqh_first == 0)
#define RTAILQ_FIRST(head, type) \
(RTAILQ_EMPTY(head) ? NULL : \
RTAILQ_OFF2PTR((head)->rtqh_first, head, type))
#define RTAILQ_NEXT(elm, head, type, field) \
(((elm)->field.rtqe_next == 0) ? NULL : \
RTAILQ_OFF2PTR((elm)->field.rtqe_next, head, type))
#define RTAILQ_LAST(head, type) \
_RTAILQ_OFF2PTR(_RTAILQ_OFF2PTR( \
(head)->rtqh_last, head, uintptr_t *)[1], head, \
uintptr_t)[0] == 0 ? NULL : \
RTAILQ_OFF2PTR(_RTAILQ_OFF2PTR(_RTAILQ_OFF2PTR( \
(head)->rtqh_last, head, uintptr_t *)[1], head, \
uintptr_t)[0], head, type)
#define RTAILQ_PREV(elm, head, type, field) \
(((*_RTAILQ_OFF2PTR(_RTAILQ_OFF2PTR( \
(elm)->field.rtqe_prev, head, uintptr_t *)[1], head, \
uintptr_t)) == 0) ? NULL : \
RTAILQ_OFF2PTR(*_RTAILQ_OFF2PTR(_RTAILQ_OFF2PTR( \
(elm)->field.rtqe_prev, head, uintptr_t *)[1], head, \
uintptr_t), head, type))
/*
* for debug
*/
#define RTAILQ_DUMP_HEAD(head, type, prefix) do { \
printf(prefix "head = %p\n", head); \
printf(prefix "head->rtqh_first = VAL:%p (PTR:%p)\n", \
(void*)((head)->rtqh_first), \
RTAILQ_OFF2PTR((head)->rtqh_first, head, type)); \
printf(prefix "head->rtqh_last = VAL:%p (PTR:%p)\n", \
(void*)((head)->rtqh_last), \
RTAILQ_OFF2PTR((head)->rtqh_last, head, type)); \
} while (/*CONSTCOND*/0)
#define RTAILQ_DUMP(elm, head, type, field, prefix) do { \
printf(prefix "elm = %p\n", elm); \
printf(prefix "elm->rtqe_next = VAL:%p (PTR:%p)\n", \
(void*)((elm)->field.rtqe_next), \
RTAILQ_OFF2PTR((elm)->field.rtqe_next, head, type)); \
printf(prefix "elm->rtqe_prev = VAL:%p (PTR:%p)\n", \
(void*)((elm)->field.rtqe_prev), \
RTAILQ_OFF2PTR((elm)->field.rtqe_prev, head, type)); \
} while (/*CONSTCOND*/0)
#endif /* _RTAILQ_H_ */