summaryrefslogtreecommitdiff
path: root/src/dbinc/shqueue.h
blob: 224644627733f7b01fd084487ab78174073fe973 (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1996, 2012 Oracle and/or its affiliates.  All rights reserved.
 *
 * $Id$
 */

#ifndef	_DB_SHQUEUE_H_
#define	_DB_SHQUEUE_H_

/*
 * This file defines three types of data structures: chains, lists and
 * tail queues similarly to the include file <sys/queue.h>.
 *
 * The difference is that this set of macros can be used for structures that
 * reside in shared memory that may be mapped at different addresses in each
 * process.  In most cases, the macros for shared structures exactly mirror
 * the normal macros, although the macro calls require an additional type
 * parameter, only used by the HEAD and ENTRY macros of the standard macros.
 *
 * Since we use relative offsets of type ssize_t rather than pointers, 0
 * (aka NULL) is a valid offset and cannot be used to indicate the end
 * of a list.  Therefore, we use -1 to indicate end of list.
 *
 * The macros ending in "P" return pointers without checking for end or
 * beginning of lists, the others check for end of list and evaluate to
 * either a pointer or NULL.
 *
 * For details on the use of these macros, see the queue(3) manual page.
 */

#if defined(__cplusplus)
extern "C" {
#endif

#define	SH_PTR_TO_OFF(src, dest)					\
	((db_ssize_t)(((u_int8_t *)(dest)) - ((u_int8_t *)(src))))

#define SH_OFF_TO_PTR(base, off, type)           \
       ((type *) (((u_int8_t *)(base)) + (db_ssize_t) (off)))


/*
 * Shared memory chain definitions.
 */
#define	SH_CHAIN_ENTRY							\
struct {								\
	db_ssize_t sce_next;	/* relative offset to next element */	\
	db_ssize_t sce_prev;	/* relative offset of prev element */	\
}

#define	SH_CHAIN_INIT(elm, field)					\
	(elm)->field.sce_next = (elm)->field.sce_prev =	-1

#define	SH_CHAIN_HASNEXT(elm, field)	((elm)->field.sce_next != -1)
#define	SH_CHAIN_NEXTP(elm, field, type)				\
    ((struct type *)((u_int8_t *)(elm) + (elm)->field.sce_next))
#define	SH_CHAIN_NEXT(elm, field, type)	(SH_CHAIN_HASNEXT(elm, field) ?	\
    SH_CHAIN_NEXTP(elm, field, type) : (struct type *)NULL)

#define	SH_CHAIN_HASPREV(elm, field)	((elm)->field.sce_prev != -1)
#define	SH_CHAIN_PREVP(elm, field, type)				\
    ((struct type *)((u_int8_t *)(elm) + (elm)->field.sce_prev))
#define	SH_CHAIN_PREV(elm, field, type)	(SH_CHAIN_HASPREV(elm, field) ?	\
     SH_CHAIN_PREVP(elm, field, type) : (struct type *)NULL)

#define	SH_CHAIN_SINGLETON(elm, field)					\
    (!(SH_CHAIN_HASNEXT(elm, field) || SH_CHAIN_HASPREV(elm, field)))

#define	SH_CHAIN_INSERT_AFTER(listelm, elm, field, type) do {		\
	struct type *__next = SH_CHAIN_NEXT(listelm, field, type);	\
	if (__next != NULL) {						\
		(elm)->field.sce_next =	SH_PTR_TO_OFF(elm, __next);	\
		__next->field.sce_prev = SH_PTR_TO_OFF(__next, elm);	\
	} else								\
		(elm)->field.sce_next = -1;				\
	(elm)->field.sce_prev = SH_PTR_TO_OFF(elm, listelm);		\
	(listelm)->field.sce_next = SH_PTR_TO_OFF(listelm, elm);	\
} while (0)

#define	SH_CHAIN_INSERT_BEFORE(listelm, elm, field, type) do {		\
	struct type *__prev = SH_CHAIN_PREV(listelm, field, type);	\
	if (__prev != NULL) {						\
		(elm)->field.sce_prev = SH_PTR_TO_OFF(elm, __prev);	\
		__prev->field.sce_next = SH_PTR_TO_OFF(__prev, elm);	\
	} else								\
		(elm)->field.sce_prev = -1;				\
	(elm)->field.sce_next = SH_PTR_TO_OFF(elm, listelm);		\
	(listelm)->field.sce_prev = SH_PTR_TO_OFF(listelm, elm);	\
} while (0)

#define	SH_CHAIN_REMOVE(elm, field, type) do {				\
	struct type *__prev = SH_CHAIN_PREV(elm, field, type);		\
	struct type *__next = SH_CHAIN_NEXT(elm, field, type);		\
	if (__next != NULL)						\
		__next->field.sce_prev = (__prev == NULL) ? -1 :	\
		    SH_PTR_TO_OFF(__next, __prev);			\
	if (__prev != NULL)						\
		__prev->field.sce_next = (__next == NULL) ? -1 :	\
		    SH_PTR_TO_OFF(__prev, __next);			\
	SH_CHAIN_INIT(elm, field);					\
} while (0)

/*
 * Shared memory list definitions.
 */
#define	SH_LIST_HEAD(name)						\
struct name {								\
	db_ssize_t slh_first;	/* first element */			\
}

#define	SH_LIST_HEAD_INITIALIZER(head)					\
	{ -1 }

#define	SH_LIST_ENTRY							\
struct {								\
	db_ssize_t sle_next;	/* relative offset to next element */	\
	db_ssize_t sle_prev;	/* relative offset of prev element */	\
}

/*
 * Shared memory list functions.
 */
#define	SH_LIST_EMPTY(head)						\
	((head)->slh_first == -1)

#define	SH_LIST_FIRSTP(head, type)					\
	((struct type *)(((u_int8_t *)(head)) + (head)->slh_first))

#define	SH_LIST_FIRST(head, type)					\
	(SH_LIST_EMPTY(head) ? NULL :					\
	((struct type *)(((u_int8_t *)(head)) + (head)->slh_first)))

#define	SH_LIST_NEXTP(elm, field, type)					\
	((struct type *)(((u_int8_t *)(elm)) + (elm)->field.sle_next))

#define	SH_LIST_NEXT(elm, field, type)					\
	((elm)->field.sle_next == -1 ? NULL :				\
	((struct type *)(((u_int8_t *)(elm)) + (elm)->field.sle_next)))

  /*
   *__SH_LIST_PREV_OFF is private API.  It calculates the address of
   * the elm->field.sle_next member of a SH_LIST structure.  All offsets
   * between elements are relative to that point in SH_LIST structures.
   */
#define	__SH_LIST_PREV_OFF(elm, field)					\
	((db_ssize_t *)(((u_int8_t *)(elm)) + (elm)->field.sle_prev))

#define	SH_LIST_PREV(elm, field, type)					\
	(struct type *)((db_ssize_t)(elm) - (*__SH_LIST_PREV_OFF(elm, field)))

#define	SH_LIST_FOREACH(var, head, field, type)				\
	for ((var) = SH_LIST_FIRST((head), type);			\
	    (var) != NULL;						\
	    (var) = SH_LIST_NEXT((var), field, type))

/*
 * Given correct A.next: B.prev = SH_LIST_NEXT_TO_PREV(A)
 * in a list [A, B]
 * The prev value is always the offset from an element to its preceding
 * element's next location, not the beginning of the structure.  To get
 * to the beginning of an element structure in memory given an element
 * do the following:
 * A = B - (B.prev + (&B.next - B))
 * Take the element's next pointer and calculate what the corresponding
 * Prev pointer should be -- basically it is the negation plus the offset
 * of the next field in the structure.
 */
#define	SH_LIST_NEXT_TO_PREV(elm, field)				\
	(((elm)->field.sle_next == -1 ? 0 : -(elm)->field.sle_next) +	\
	   SH_PTR_TO_OFF(elm, &(elm)->field.sle_next))

#define	SH_LIST_INIT(head) (head)->slh_first = -1

#define	SH_LIST_INSERT_BEFORE(head, listelm, elm, field, type) do {	\
	if (listelm == SH_LIST_FIRST(head, type)) {			\
	SH_LIST_INSERT_HEAD(head, elm, field, type);			\
	} else {							\
		(elm)->field.sle_next = SH_PTR_TO_OFF(elm, listelm);	\
		(elm)->field.sle_prev = SH_LIST_NEXT_TO_PREV(		\
			SH_LIST_PREV((listelm), field, type), field) +	\
		(elm)->field.sle_next;					\
		(SH_LIST_PREV(listelm, field, type))->field.sle_next =	\
			(SH_PTR_TO_OFF((SH_LIST_PREV(listelm, field,	\
						     type)), elm));	\
	(listelm)->field.sle_prev = SH_LIST_NEXT_TO_PREV(elm, field);	\
	}								\
} while (0)

#define	SH_LIST_INSERT_AFTER(listelm, elm, field, type) do {		\
	if ((listelm)->field.sle_next != -1) {				\
		(elm)->field.sle_next = SH_PTR_TO_OFF(elm,		\
		    SH_LIST_NEXTP(listelm, field, type));		\
		SH_LIST_NEXTP(listelm, field, type)->field.sle_prev =	\
			SH_LIST_NEXT_TO_PREV(elm, field);		\
	} else								\
		(elm)->field.sle_next = -1;				\
	(listelm)->field.sle_next = SH_PTR_TO_OFF(listelm, elm);	\
	(elm)->field.sle_prev = SH_LIST_NEXT_TO_PREV(listelm, field);	\
} while (0)

#define	SH_LIST_INSERT_HEAD(head, elm, field, type) do {		\
	if ((head)->slh_first != -1) {					\
		(elm)->field.sle_next =					\
		    (head)->slh_first - SH_PTR_TO_OFF(head, elm);	\
		SH_LIST_FIRSTP(head, type)->field.sle_prev =		\
			SH_LIST_NEXT_TO_PREV(elm, field);		\
	} else								\
		(elm)->field.sle_next = -1;				\
	(head)->slh_first = SH_PTR_TO_OFF(head, elm);			\
	(elm)->field.sle_prev = SH_PTR_TO_OFF(elm, &(head)->slh_first);	\
} while (0)

#define	SH_LIST_REMOVE(elm, field, type) do {				\
	if ((elm)->field.sle_next != -1) {				\
		SH_LIST_NEXTP(elm, field, type)->field.sle_prev =	\
			(elm)->field.sle_prev - (elm)->field.sle_next;	\
		*__SH_LIST_PREV_OFF(elm, field) += (elm)->field.sle_next;\
	} else								\
		*__SH_LIST_PREV_OFF(elm, field) = -1;			\
} while (0)

#define	SH_LIST_REMOVE_HEAD(head, field, type) do {			\
	if (!SH_LIST_EMPTY(head)) {					\
		SH_LIST_REMOVE(SH_LIST_FIRSTP(head, type), field, type);\
	}								\
} while (0)

/*
 * Shared memory tail queue definitions.
 */
#define	SH_TAILQ_HEAD(name)						\
struct name {								\
	db_ssize_t stqh_first;	/* relative offset of first element */	\
	db_ssize_t stqh_last;	/* relative offset of last's next */	\
}

#define	SH_TAILQ_HEAD_INITIALIZER(head)					\
	{ -1, 0 }

#define	SH_TAILQ_ENTRY							\
struct {								\
	db_ssize_t stqe_next;	/* relative offset of next element */	\
	db_ssize_t stqe_prev;	/* relative offset of prev's next */	\
}

/*
 * Shared memory tail queue functions.
 */

#define	SH_TAILQ_EMPTY(head)						\
	((head)->stqh_first == -1)

#define	SH_TAILQ_FIRSTP(head, type)					\
	((struct type *)((u_int8_t *)(head) + (head)->stqh_first))

#define	SH_TAILQ_FIRST(head, type)					\
	(SH_TAILQ_EMPTY(head) ? NULL : SH_TAILQ_FIRSTP(head, type))

#define	SH_TAILQ_NEXTP(elm, field, type)				\
	((struct type *)((u_int8_t *)(elm) + (elm)->field.stqe_next))

#define	SH_TAILQ_NEXT(elm, field, type)					\
	((elm)->field.stqe_next == -1 ? NULL :				\
	((struct type *)((u_int8_t *)(elm) + (elm)->field.stqe_next)))

  /*
   * __SH_TAILQ_PREV_OFF is private API.  It calculates the address of
   * the elm->field.stqe_next member of a SH_TAILQ structure.  All
   * offsets between elements are relative to that point in SH_TAILQ
   * structures.
   */
#define	__SH_TAILQ_PREV_OFF(elm, field)					\
	((db_ssize_t *)(((u_int8_t *)(elm)) + (elm)->field.stqe_prev))

#define	SH_TAILQ_PREVP(elm, field, type)				\
	(struct type *)((db_ssize_t)elm - (*__SH_TAILQ_PREV_OFF(elm, field)))

#define	SH_TAILQ_PREV(head, elm, field, type)				\
	(((elm) == SH_TAILQ_FIRST(head, type)) ? NULL :		\
	  (struct type *)((db_ssize_t)elm - (*__SH_TAILQ_PREV_OFF(elm, field))))

  /*
   * __SH_TAILQ_LAST_OFF is private API.  It calculates the address of
   * the stqe_next member of a SH_TAILQ structure in the last element
   * of this list.  All offsets between elements are relative to that
   * point in SH_TAILQ structures.
   */
#define	__SH_TAILQ_LAST_OFF(head)					\
	((db_ssize_t *)(((u_int8_t *)(head)) + (head)->stqh_last))

#define	SH_TAILQ_LASTP(head, field, type)				\
	((struct type *)((db_ssize_t)(head) +				\
	 ((db_ssize_t)((head)->stqh_last) -				\
	 ((db_ssize_t)SH_PTR_TO_OFF(SH_TAILQ_FIRST(head, type),		\
		&(SH_TAILQ_FIRSTP(head, type)->field.stqe_next))))))

#define	SH_TAILQ_LAST(head, field, type)				\
	(SH_TAILQ_EMPTY(head) ? NULL : SH_TAILQ_LASTP(head, field, type))

/*
 * Given correct A.next: B.prev = SH_TAILQ_NEXT_TO_PREV(A)
 * in a list [A, B]
 * The prev value is always the offset from an element to its preceding
 * element's next location, not the beginning of the structure.  To get
 * to the beginning of an element structure in memory given an element
 * do the following:
 * A = B - (B.prev + (&B.next - B))
 */
#define	SH_TAILQ_NEXT_TO_PREV(elm, field)				\
	(((elm)->field.stqe_next == -1 ? 0 :				\
		(-(elm)->field.stqe_next) +				\
		SH_PTR_TO_OFF(elm, &(elm)->field.stqe_next)))

#define	SH_TAILQ_FOREACH(var, head, field, type)			\
	for ((var) = SH_TAILQ_FIRST((head), type);			\
	    (var) != NULL;						\
	    (var) = SH_TAILQ_NEXT((var), field, type))

#define	SH_TAILQ_FOREACH_REVERSE(var, head, field, type)		\
	for ((var) = SH_TAILQ_LAST((head), field, type);		\
	    (var) != NULL;						\
	    (var) = SH_TAILQ_PREV((head), (var), field, type))

#define	SH_TAILQ_INIT(head) {						\
	(head)->stqh_first = -1;					\
	(head)->stqh_last = SH_PTR_TO_OFF(head, &(head)->stqh_first);	\
}

#define	SH_TAILQ_INSERT_HEAD(head, elm, field, type) do {		\
	if ((head)->stqh_first != -1) {					\
		(elm)->field.stqe_next =				\
		    (head)->stqh_first - SH_PTR_TO_OFF(head, elm);	\
		SH_TAILQ_FIRSTP(head, type)->field.stqe_prev =		\
			SH_TAILQ_NEXT_TO_PREV(elm, field);		\
	} else {							\
		(head)->stqh_last =					\
		    SH_PTR_TO_OFF(head, &(elm)->field.stqe_next);	\
		(elm)->field.stqe_next = -1;				\
	}								\
	(head)->stqh_first = SH_PTR_TO_OFF(head, elm);			\
	(elm)->field.stqe_prev =					\
	    SH_PTR_TO_OFF(elm, &(head)->stqh_first);			\
} while (0)

#define	SH_TAILQ_INSERT_TAIL(head, elm, field) do {			\
	(elm)->field.stqe_next = -1;					\
	(elm)->field.stqe_prev =					\
	    -SH_PTR_TO_OFF(head, elm) + (head)->stqh_last;		\
	if ((head)->stqh_last ==					\
	    SH_PTR_TO_OFF((head), &(head)->stqh_first))			\
		(head)->stqh_first = SH_PTR_TO_OFF(head, elm);		\
	else								\
		*__SH_TAILQ_LAST_OFF(head) = -(head)->stqh_last +	\
		    SH_PTR_TO_OFF((elm), &(elm)->field.stqe_next) +	\
		    SH_PTR_TO_OFF(head, elm);				\
	(head)->stqh_last =						\
	    SH_PTR_TO_OFF(head, &((elm)->field.stqe_next));		\
} while (0)

#define	SH_TAILQ_INSERT_BEFORE(head, listelm, elm, field, type) do {	\
	if (listelm == SH_TAILQ_FIRST(head, type)) {			\
		SH_TAILQ_INSERT_HEAD(head, elm, field, type);		\
	} else {							\
		(elm)->field.stqe_next = SH_PTR_TO_OFF(elm, listelm);	\
		(elm)->field.stqe_prev = SH_TAILQ_NEXT_TO_PREV(		\
			SH_TAILQ_PREVP((listelm), field, type), field) + \
			(elm)->field.stqe_next;				\
		(SH_TAILQ_PREVP(listelm, field, type))->field.stqe_next =\
		(SH_PTR_TO_OFF((SH_TAILQ_PREVP(listelm, field, type)),	\
			elm));						\
		(listelm)->field.stqe_prev =				\
			SH_TAILQ_NEXT_TO_PREV(elm, field);		\
	}								\
} while (0)

#define	SH_TAILQ_INSERT_AFTER(head, listelm, elm, field, type) do {	\
	if ((listelm)->field.stqe_next != -1) {				\
		(elm)->field.stqe_next = (listelm)->field.stqe_next -	\
		    SH_PTR_TO_OFF(listelm, elm);			\
		SH_TAILQ_NEXTP(listelm, field, type)->field.stqe_prev =	\
		    SH_TAILQ_NEXT_TO_PREV(elm, field);			\
	} else {							\
		(elm)->field.stqe_next = -1;				\
		(head)->stqh_last =					\
		    SH_PTR_TO_OFF(head, &(elm)->field.stqe_next);	\
	}								\
	(listelm)->field.stqe_next = SH_PTR_TO_OFF(listelm, elm);	\
	(elm)->field.stqe_prev = SH_TAILQ_NEXT_TO_PREV(listelm, field);	\
} while (0)

#define	SH_TAILQ_REMOVE(head, elm, field, type) do {			\
	if ((elm)->field.stqe_next != -1) {				\
		SH_TAILQ_NEXTP(elm, field, type)->field.stqe_prev =	\
		    (elm)->field.stqe_prev +				\
		    SH_PTR_TO_OFF(SH_TAILQ_NEXTP(elm,			\
		    field, type), elm);					\
		*__SH_TAILQ_PREV_OFF(elm, field) += (elm)->field.stqe_next;\
	} else {							\
		(head)->stqh_last = (elm)->field.stqe_prev +		\
			SH_PTR_TO_OFF(head, elm);			\
		*__SH_TAILQ_PREV_OFF(elm, field) = -1;			\
	}								\
} while (0)

#if defined(__cplusplus)
}
#endif
#endif	/* !_DB_SHQUEUE_H_ */