2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1996-2002
5
* Sleepycat Software. All rights reserved.
10
#ifndef _SYS_SHQUEUE_H_
11
#define _SYS_SHQUEUE_H_
14
* This file defines three types of data structures: lists, tail queues, and
15
* circular queues, similarly to the include file <sys/queue.h>.
17
* The difference is that this set of macros can be used for structures that
18
* reside in shared memory that may be mapped at different addresses in each
19
* process. In most cases, the macros for shared structures exactly mirror
20
* the normal macros, although the macro calls require an additional type
21
* parameter, only used by the HEAD and ENTRY macros of the standard macros.
23
* For details on the use of these macros, see the queue(3) manual page.
26
#if defined(__cplusplus)
31
* Shared list definitions.
33
#define SH_LIST_HEAD(name) \
35
ssize_t slh_first; /* first element */ \
38
#define SH_LIST_ENTRY \
40
ssize_t sle_next; /* relative offset next element */ \
41
ssize_t sle_prev; /* relative offset of prev element */ \
45
* Shared list functions. Since we use relative offsets for pointers,
46
* 0 is a valid offset. Therefore, we use -1 to indicate end of list.
47
* The macros ending in "P" return pointers without checking for end
48
* of list, the others check for end of list and evaluate to either a
52
#define SH_LIST_FIRSTP(head, type) \
53
((struct type *)(((u_int8_t *)(head)) + (head)->slh_first))
55
#define SH_LIST_FIRST(head, type) \
56
((head)->slh_first == -1 ? NULL : \
57
((struct type *)(((u_int8_t *)(head)) + (head)->slh_first)))
59
#define SH_LIST_NEXTP(elm, field, type) \
60
((struct type *)(((u_int8_t *)(elm)) + (elm)->field.sle_next))
62
#define SH_LIST_NEXT(elm, field, type) \
63
((elm)->field.sle_next == -1 ? NULL : \
64
((struct type *)(((u_int8_t *)(elm)) + (elm)->field.sle_next)))
66
#define SH_LIST_PREV(elm, field) \
67
((ssize_t *)(((u_int8_t *)(elm)) + (elm)->field.sle_prev))
69
#define SH_PTR_TO_OFF(src, dest) \
70
((ssize_t)(((u_int8_t *)(dest)) - ((u_int8_t *)(src))))
73
* Take the element's next pointer and calculate what the corresponding
74
* Prev pointer should be -- basically it is the negation plus the offset
75
* of the next field in the structure.
77
#define SH_LIST_NEXT_TO_PREV(elm, field) \
78
(-(elm)->field.sle_next + SH_PTR_TO_OFF(elm, &(elm)->field.sle_next))
80
#define SH_LIST_INIT(head) (head)->slh_first = -1
82
#define SH_LIST_INSERT_AFTER(listelm, elm, field, type) do { \
83
if ((listelm)->field.sle_next != -1) { \
84
(elm)->field.sle_next = SH_PTR_TO_OFF(elm, \
85
SH_LIST_NEXTP(listelm, field, type)); \
86
SH_LIST_NEXTP(listelm, field, type)->field.sle_prev = \
87
SH_LIST_NEXT_TO_PREV(elm, field); \
89
(elm)->field.sle_next = -1; \
90
(listelm)->field.sle_next = SH_PTR_TO_OFF(listelm, elm); \
91
(elm)->field.sle_prev = SH_LIST_NEXT_TO_PREV(listelm, field); \
94
#define SH_LIST_INSERT_HEAD(head, elm, field, type) do { \
95
if ((head)->slh_first != -1) { \
96
(elm)->field.sle_next = \
97
(head)->slh_first - SH_PTR_TO_OFF(head, elm); \
98
SH_LIST_FIRSTP(head, type)->field.sle_prev = \
99
SH_LIST_NEXT_TO_PREV(elm, field); \
101
(elm)->field.sle_next = -1; \
102
(head)->slh_first = SH_PTR_TO_OFF(head, elm); \
103
(elm)->field.sle_prev = SH_PTR_TO_OFF(elm, &(head)->slh_first); \
106
#define SH_LIST_REMOVE(elm, field, type) do { \
107
if ((elm)->field.sle_next != -1) { \
108
SH_LIST_NEXTP(elm, field, type)->field.sle_prev = \
109
(elm)->field.sle_prev - (elm)->field.sle_next; \
110
*SH_LIST_PREV(elm, field) += (elm)->field.sle_next; \
112
*SH_LIST_PREV(elm, field) = -1; \
116
* Shared tail queue definitions.
118
#define SH_TAILQ_HEAD(name) \
120
ssize_t stqh_first; /* relative offset of first element */ \
121
ssize_t stqh_last; /* relative offset of last's next */ \
124
#define SH_TAILQ_ENTRY \
126
ssize_t stqe_next; /* relative offset of next element */ \
127
ssize_t stqe_prev; /* relative offset of prev's next */ \
131
* Shared tail queue functions.
133
#define SH_TAILQ_FIRSTP(head, type) \
134
((struct type *)((u_int8_t *)(head) + (head)->stqh_first))
136
#define SH_TAILQ_FIRST(head, type) \
137
((head)->stqh_first == -1 ? NULL : SH_TAILQ_FIRSTP(head, type))
139
#define SH_TAILQ_NEXTP(elm, field, type) \
140
((struct type *)((u_int8_t *)(elm) + (elm)->field.stqe_next))
142
#define SH_TAILQ_NEXT(elm, field, type) \
143
((elm)->field.stqe_next == -1 ? NULL : SH_TAILQ_NEXTP(elm, field, type))
145
#define SH_TAILQ_PREVP(elm, field) \
146
((ssize_t *)((u_int8_t *)(elm) + (elm)->field.stqe_prev))
148
#define SH_TAILQ_LAST(head) \
149
((ssize_t *)(((u_int8_t *)(head)) + (head)->stqh_last))
151
#define SH_TAILQ_NEXT_TO_PREV(elm, field) \
152
(-(elm)->field.stqe_next + SH_PTR_TO_OFF(elm, &(elm)->field.stqe_next))
154
#define SH_TAILQ_INIT(head) { \
155
(head)->stqh_first = -1; \
156
(head)->stqh_last = SH_PTR_TO_OFF(head, &(head)->stqh_first); \
159
#define SH_TAILQ_INSERT_HEAD(head, elm, field, type) do { \
160
if ((head)->stqh_first != -1) { \
161
(elm)->field.stqe_next = \
162
(head)->stqh_first - SH_PTR_TO_OFF(head, elm); \
163
SH_TAILQ_FIRSTP(head, type)->field.stqe_prev = \
164
SH_TAILQ_NEXT_TO_PREV(elm, field); \
166
(elm)->field.stqe_next = -1; \
167
(head)->stqh_last = \
168
SH_PTR_TO_OFF(head, &(elm)->field.stqe_next); \
170
(head)->stqh_first = SH_PTR_TO_OFF(head, elm); \
171
(elm)->field.stqe_prev = \
172
SH_PTR_TO_OFF(elm, &(head)->stqh_first); \
175
#define SH_TAILQ_INSERT_TAIL(head, elm, field) do { \
176
(elm)->field.stqe_next = -1; \
177
(elm)->field.stqe_prev = \
178
-SH_PTR_TO_OFF(head, elm) + (head)->stqh_last; \
179
if ((head)->stqh_last == \
180
SH_PTR_TO_OFF((head), &(head)->stqh_first)) \
181
(head)->stqh_first = SH_PTR_TO_OFF(head, elm); \
183
*SH_TAILQ_LAST(head) = -(head)->stqh_last + \
184
SH_PTR_TO_OFF((elm), &(elm)->field.stqe_next) + \
185
SH_PTR_TO_OFF(head, elm); \
186
(head)->stqh_last = \
187
SH_PTR_TO_OFF(head, &((elm)->field.stqe_next)); \
190
#define SH_TAILQ_INSERT_AFTER(head, listelm, elm, field, type) do { \
191
if ((listelm)->field.stqe_next != -1) { \
192
(elm)->field.stqe_next = (listelm)->field.stqe_next - \
193
SH_PTR_TO_OFF(listelm, elm); \
194
SH_TAILQ_NEXTP(listelm, field, type)->field.stqe_prev = \
195
SH_TAILQ_NEXT_TO_PREV(elm, field); \
197
(elm)->field.stqe_next = -1; \
198
(head)->stqh_last = \
199
SH_PTR_TO_OFF(head, &elm->field.stqe_next); \
201
(listelm)->field.stqe_next = SH_PTR_TO_OFF(listelm, elm); \
202
(elm)->field.stqe_prev = SH_TAILQ_NEXT_TO_PREV(listelm, field); \
205
#define SH_TAILQ_REMOVE(head, elm, field, type) do { \
206
if ((elm)->field.stqe_next != -1) { \
207
SH_TAILQ_NEXTP(elm, field, type)->field.stqe_prev = \
208
(elm)->field.stqe_prev + \
209
SH_PTR_TO_OFF(SH_TAILQ_NEXTP(elm, \
210
field, type), elm); \
211
*SH_TAILQ_PREVP(elm, field) += elm->field.stqe_next; \
213
(head)->stqh_last = (elm)->field.stqe_prev + \
214
SH_PTR_TO_OFF(head, elm); \
215
*SH_TAILQ_PREVP(elm, field) = -1; \
220
* Shared circular queue definitions.
222
#define SH_CIRCLEQ_HEAD(name) \
224
ssize_t scqh_first; /* first element */ \
225
ssize_t scqh_last; /* last element */ \
228
#define SH_CIRCLEQ_ENTRY \
230
ssize_t scqe_next; /* next element */ \
231
ssize_t scqe_prev; /* previous element */ \
235
* Shared circular queue functions.
237
#define SH_CIRCLEQ_FIRSTP(head, type) \
238
((struct type *)(((u_int8_t *)(head)) + (head)->scqh_first))
240
#define SH_CIRCLEQ_FIRST(head, type) \
241
((head)->scqh_first == -1 ? \
242
(void *)head : SH_CIRCLEQ_FIRSTP(head, type))
244
#define SH_CIRCLEQ_LASTP(head, type) \
245
((struct type *)(((u_int8_t *)(head)) + (head)->scqh_last))
247
#define SH_CIRCLEQ_LAST(head, type) \
248
((head)->scqh_last == -1 ? (void *)head : SH_CIRCLEQ_LASTP(head, type))
250
#define SH_CIRCLEQ_NEXTP(elm, field, type) \
251
((struct type *)(((u_int8_t *)(elm)) + (elm)->field.scqe_next))
253
#define SH_CIRCLEQ_NEXT(head, elm, field, type) \
254
((elm)->field.scqe_next == SH_PTR_TO_OFF(elm, head) ? \
255
(void *)head : SH_CIRCLEQ_NEXTP(elm, field, type))
257
#define SH_CIRCLEQ_PREVP(elm, field, type) \
258
((struct type *)(((u_int8_t *)(elm)) + (elm)->field.scqe_prev))
260
#define SH_CIRCLEQ_PREV(head, elm, field, type) \
261
((elm)->field.scqe_prev == SH_PTR_TO_OFF(elm, head) ? \
262
(void *)head : SH_CIRCLEQ_PREVP(elm, field, type))
264
#define SH_CIRCLEQ_INIT(head) { \
265
(head)->scqh_first = 0; \
266
(head)->scqh_last = 0; \
269
#define SH_CIRCLEQ_INSERT_AFTER(head, listelm, elm, field, type) do { \
270
(elm)->field.scqe_prev = SH_PTR_TO_OFF(elm, listelm); \
271
(elm)->field.scqe_next = (listelm)->field.scqe_next + \
272
(elm)->field.scqe_prev; \
273
if (SH_CIRCLEQ_NEXTP(listelm, field, type) == (void *)head) \
274
(head)->scqh_last = SH_PTR_TO_OFF(head, elm); \
276
SH_CIRCLEQ_NEXTP(listelm, \
277
field, type)->field.scqe_prev = \
278
SH_PTR_TO_OFF(SH_CIRCLEQ_NEXTP(listelm, \
279
field, type), elm); \
280
(listelm)->field.scqe_next = -(elm)->field.scqe_prev; \
283
#define SH_CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field, type) do { \
284
(elm)->field.scqe_next = SH_PTR_TO_OFF(elm, listelm); \
285
(elm)->field.scqe_prev = (elm)->field.scqe_next - \
286
SH_CIRCLEQ_PREVP(listelm, field, type)->field.scqe_next;\
287
if (SH_CIRCLEQ_PREVP(listelm, field, type) == (void *)(head)) \
288
(head)->scqh_first = SH_PTR_TO_OFF(head, elm); \
290
SH_CIRCLEQ_PREVP(listelm, \
291
field, type)->field.scqe_next = \
292
SH_PTR_TO_OFF(SH_CIRCLEQ_PREVP(listelm, \
293
field, type), elm); \
294
(listelm)->field.scqe_prev = -(elm)->field.scqe_next; \
297
#define SH_CIRCLEQ_INSERT_HEAD(head, elm, field, type) do { \
298
(elm)->field.scqe_prev = SH_PTR_TO_OFF(elm, head); \
299
(elm)->field.scqe_next = (head)->scqh_first + \
300
(elm)->field.scqe_prev; \
301
if ((head)->scqh_last == 0) \
302
(head)->scqh_last = -(elm)->field.scqe_prev; \
304
SH_CIRCLEQ_FIRSTP(head, type)->field.scqe_prev = \
305
SH_PTR_TO_OFF(SH_CIRCLEQ_FIRSTP(head, type), elm); \
306
(head)->scqh_first = -(elm)->field.scqe_prev; \
309
#define SH_CIRCLEQ_INSERT_TAIL(head, elm, field, type) do { \
310
(elm)->field.scqe_next = SH_PTR_TO_OFF(elm, head); \
311
(elm)->field.scqe_prev = (head)->scqh_last + \
312
(elm)->field.scqe_next; \
313
if ((head)->scqh_first == 0) \
314
(head)->scqh_first = -(elm)->field.scqe_next; \
316
SH_CIRCLEQ_LASTP(head, type)->field.scqe_next = \
317
SH_PTR_TO_OFF(SH_CIRCLEQ_LASTP(head, type), elm); \
318
(head)->scqh_last = -(elm)->field.scqe_next; \
321
#define SH_CIRCLEQ_REMOVE(head, elm, field, type) do { \
322
if (SH_CIRCLEQ_NEXTP(elm, field, type) == (void *)(head)) \
323
(head)->scqh_last += (elm)->field.scqe_prev; \
325
SH_CIRCLEQ_NEXTP(elm, field, type)->field.scqe_prev += \
326
(elm)->field.scqe_prev; \
327
if (SH_CIRCLEQ_PREVP(elm, field, type) == (void *)(head)) \
328
(head)->scqh_first += (elm)->field.scqe_next; \
330
SH_CIRCLEQ_PREVP(elm, field, type)->field.scqe_next += \
331
(elm)->field.scqe_next; \
334
#if defined(__cplusplus)
337
#endif /* !_SYS_SHQUEUE_H_ */