epoc32/include/stdapis/sys/queue.h
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:27:01 +0100
branchSymbian2
changeset 3 e1b950c65cb4
parent 0 061f57f2323e
child 4 837f303aceeb
permissions -rw-r--r--
Attempt to represent the S^2->S^3 header reorganisation as a series of "hg rename" operations
williamr@2
     1
/*-
williamr@2
     2
 * Copyright (c) 1991, 1993
williamr@2
     3
 *	The Regents of the University of California.  All rights reserved.
williamr@2
     4
 *
williamr@2
     5
 * Redistribution and use in source and binary forms, with or without
williamr@2
     6
 * modification, are permitted provided that the following conditions
williamr@2
     7
 * are met:
williamr@2
     8
 * 1. Redistributions of source code must retain the above copyright
williamr@2
     9
 *    notice, this list of conditions and the following disclaimer.
williamr@2
    10
 * 2. Redistributions in binary form must reproduce the above copyright
williamr@2
    11
 *    notice, this list of conditions and the following disclaimer in the
williamr@2
    12
 *    documentation and/or other materials provided with the distribution.
williamr@2
    13
 * 4. Neither the name of the University nor the names of its contributors
williamr@2
    14
 *    may be used to endorse or promote products derived from this software
williamr@2
    15
 *    without specific prior written permission.
williamr@2
    16
 *
williamr@2
    17
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
williamr@2
    18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
williamr@2
    19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
williamr@2
    20
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
williamr@2
    21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
williamr@2
    22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
williamr@2
    23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
williamr@2
    24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
williamr@2
    25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
williamr@2
    26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
williamr@2
    27
 * SUCH DAMAGE.
williamr@2
    28
 * © Portions copyright (c) 2007 Symbian Software Ltd. All rights reserved.
williamr@2
    29
 *	@(#)queue.h	8.5 (Berkeley) 8/20/94
williamr@2
    30
 * $FreeBSD: src/sys/sys/queue.h,v 1.60.2.1 2005/08/16 22:41:39 phk Exp $
williamr@2
    31
 */
williamr@2
    32
williamr@2
    33
#ifndef _SYS_QUEUE_H_
williamr@2
    34
#define	_SYS_QUEUE_H_
williamr@2
    35
williamr@2
    36
#include <sys/cdefs.h>
williamr@2
    37
williamr@2
    38
/*
williamr@2
    39
 * This file defines four types of data structures: singly-linked lists,
williamr@2
    40
 * singly-linked tail queues, lists and tail queues.
williamr@2
    41
 *
williamr@2
    42
 * A singly-linked list is headed by a single forward pointer. The elements
williamr@2
    43
 * are singly linked for minimum space and pointer manipulation overhead at
williamr@2
    44
 * the expense of O(n) removal for arbitrary elements. New elements can be
williamr@2
    45
 * added to the list after an existing element or at the head of the list.
williamr@2
    46
 * Elements being removed from the head of the list should use the explicit
williamr@2
    47
 * macro for this purpose for optimum efficiency. A singly-linked list may
williamr@2
    48
 * only be traversed in the forward direction.  Singly-linked lists are ideal
williamr@2
    49
 * for applications with large datasets and few or no removals or for
williamr@2
    50
 * implementing a LIFO queue.
williamr@2
    51
 *
williamr@2
    52
 * A singly-linked tail queue is headed by a pair of pointers, one to the
williamr@2
    53
 * head of the list and the other to the tail of the list. The elements are
williamr@2
    54
 * singly linked for minimum space and pointer manipulation overhead at the
williamr@2
    55
 * expense of O(n) removal for arbitrary elements. New elements can be added
williamr@2
    56
 * to the list after an existing element, at the head of the list, or at the
williamr@2
    57
 * end of the list. Elements being removed from the head of the tail queue
williamr@2
    58
 * should use the explicit macro for this purpose for optimum efficiency.
williamr@2
    59
 * A singly-linked tail queue may only be traversed in the forward direction.
williamr@2
    60
 * Singly-linked tail queues are ideal for applications with large datasets
williamr@2
    61
 * and few or no removals or for implementing a FIFO queue.
williamr@2
    62
 *
williamr@2
    63
 * A list is headed by a single forward pointer (or an array of forward
williamr@2
    64
 * pointers for a hash table header). The elements are doubly linked
williamr@2
    65
 * so that an arbitrary element can be removed without a need to
williamr@2
    66
 * traverse the list. New elements can be added to the list before
williamr@2
    67
 * or after an existing element or at the head of the list. A list
williamr@2
    68
 * may only be traversed in the forward direction.
williamr@2
    69
 *
williamr@2
    70
 * A tail queue is headed by a pair of pointers, one to the head of the
williamr@2
    71
 * list and the other to the tail of the list. The elements are doubly
williamr@2
    72
 * linked so that an arbitrary element can be removed without a need to
williamr@2
    73
 * traverse the list. New elements can be added to the list before or
williamr@2
    74
 * after an existing element, at the head of the list, or at the end of
williamr@2
    75
 * the list. A tail queue may be traversed in either direction.
williamr@2
    76
 *
williamr@2
    77
 * For details on the use of these macros, see the queue(3) manual page.
williamr@2
    78
 *
williamr@2
    79
 *
williamr@2
    80
 *				SLIST	LIST	STAILQ	TAILQ
williamr@2
    81
 * _HEAD			+	+	+	+
williamr@2
    82
 * _HEAD_INITIALIZER		+	+	+	+
williamr@2
    83
 * _ENTRY			+	+	+	+
williamr@2
    84
 * _INIT			+	+	+	+
williamr@2
    85
 * _EMPTY			+	+	+	+
williamr@2
    86
 * _FIRST			+	+	+	+
williamr@2
    87
 * _NEXT			+	+	+	+
williamr@2
    88
 * _PREV			-	-	-	+
williamr@2
    89
 * _LAST			-	-	+	+
williamr@2
    90
 * _FOREACH			+	+	+	+
williamr@2
    91
 * _FOREACH_SAFE		+	+	+	+
williamr@2
    92
 * _FOREACH_REVERSE		-	-	-	+
williamr@2
    93
 * _FOREACH_REVERSE_SAFE	-	-	-	+
williamr@2
    94
 * _INSERT_HEAD			+	+	+	+
williamr@2
    95
 * _INSERT_BEFORE		-	+	-	+
williamr@2
    96
 * _INSERT_AFTER		+	+	+	+
williamr@2
    97
 * _INSERT_TAIL			-	-	+	+
williamr@2
    98
 * _CONCAT			-	-	+	+
williamr@2
    99
 * _REMOVE_HEAD			+	-	+	-
williamr@2
   100
 * _REMOVE			+	+	+	+
williamr@2
   101
 *
williamr@2
   102
 */
williamr@2
   103
#define	QUEUE_MACRO_DEBUG 0
williamr@2
   104
#if QUEUE_MACRO_DEBUG
williamr@2
   105
/* Store the last 2 places the queue element or head was altered */
williamr@2
   106
struct qm_trace {
williamr@2
   107
	char * lastfile;
williamr@2
   108
	int lastline;
williamr@2
   109
	char * prevfile;
williamr@2
   110
	int prevline;
williamr@2
   111
};
williamr@2
   112
williamr@2
   113
#define	TRACEBUF	struct qm_trace trace;
williamr@2
   114
#define	TRASHIT(x)	do {(x) = (void *)-1;} while (0)
williamr@2
   115
williamr@2
   116
#define	QMD_TRACE_HEAD(head) do {					\
williamr@2
   117
	(head)->trace.prevline = (head)->trace.lastline;		\
williamr@2
   118
	(head)->trace.prevfile = (head)->trace.lastfile;		\
williamr@2
   119
	(head)->trace.lastline = __LINE__;				\
williamr@2
   120
	(head)->trace.lastfile = __FILE__;				\
williamr@2
   121
} while (0)
williamr@2
   122
williamr@2
   123
#define	QMD_TRACE_ELEM(elem) do {					\
williamr@2
   124
	(elem)->trace.prevline = (elem)->trace.lastline;		\
williamr@2
   125
	(elem)->trace.prevfile = (elem)->trace.lastfile;		\
williamr@2
   126
	(elem)->trace.lastline = __LINE__;				\
williamr@2
   127
	(elem)->trace.lastfile = __FILE__;				\
williamr@2
   128
} while (0)
williamr@2
   129
williamr@2
   130
#else
williamr@2
   131
#define	QMD_TRACE_ELEM(elem)
williamr@2
   132
#define	QMD_TRACE_HEAD(head)
williamr@2
   133
#define	TRACEBUF
williamr@2
   134
#define	TRASHIT(x)
williamr@2
   135
#endif	/* QUEUE_MACRO_DEBUG */
williamr@2
   136
williamr@2
   137
/*
williamr@2
   138
 * Singly-linked List declarations.
williamr@2
   139
 */
williamr@2
   140
#define	SLIST_HEAD(name, type)						\
williamr@2
   141
struct name {								\
williamr@2
   142
	struct type *slh_first;	/* first element */			\
williamr@2
   143
}
williamr@2
   144
williamr@2
   145
#define	SLIST_HEAD_INITIALIZER(head)					\
williamr@2
   146
	{ NULL }
williamr@2
   147
williamr@2
   148
#define	SLIST_ENTRY(type)						\
williamr@2
   149
struct {								\
williamr@2
   150
	struct type *sle_next;	/* next element */			\
williamr@2
   151
}
williamr@2
   152
williamr@2
   153
/*
williamr@2
   154
 * Singly-linked List functions.
williamr@2
   155
 */
williamr@2
   156
#define	SLIST_EMPTY(head)	((head)->slh_first == NULL)
williamr@2
   157
williamr@2
   158
#define	SLIST_FIRST(head)	((head)->slh_first)
williamr@2
   159
williamr@2
   160
#define	SLIST_FOREACH(var, head, field)					\
williamr@2
   161
	for ((var) = SLIST_FIRST((head));				\
williamr@2
   162
	    (var);							\
williamr@2
   163
	    (var) = SLIST_NEXT((var), field))
williamr@2
   164
williamr@2
   165
#define	SLIST_FOREACH_SAFE(var, head, field, tvar)			\
williamr@2
   166
	for ((var) = SLIST_FIRST((head));				\
williamr@2
   167
	    (var) && ((tvar) = SLIST_NEXT((var), field), 1);		\
williamr@2
   168
	    (var) = (tvar))
williamr@2
   169
williamr@2
   170
#define	SLIST_FOREACH_PREVPTR(var, varp, head, field)			\
williamr@2
   171
	for ((varp) = &SLIST_FIRST((head));				\
williamr@2
   172
	    ((var) = *(varp)) != NULL;					\
williamr@2
   173
	    (varp) = &SLIST_NEXT((var), field))
williamr@2
   174
williamr@2
   175
#define	SLIST_INIT(head) do {						\
williamr@2
   176
	SLIST_FIRST((head)) = NULL;					\
williamr@2
   177
} while (0)
williamr@2
   178
williamr@2
   179
#define	SLIST_INSERT_AFTER(slistelm, elm, field) do {			\
williamr@2
   180
	SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field);	\
williamr@2
   181
	SLIST_NEXT((slistelm), field) = (elm);				\
williamr@2
   182
} while (0)
williamr@2
   183
williamr@2
   184
#define	SLIST_INSERT_HEAD(head, elm, field) do {			\
williamr@2
   185
	SLIST_NEXT((elm), field) = SLIST_FIRST((head));			\
williamr@2
   186
	SLIST_FIRST((head)) = (elm);					\
williamr@2
   187
} while (0)
williamr@2
   188
williamr@2
   189
#define	SLIST_NEXT(elm, field)	((elm)->field.sle_next)
williamr@2
   190
williamr@2
   191
#define	SLIST_REMOVE(head, elm, type, field) do {			\
williamr@2
   192
	if (SLIST_FIRST((head)) == (elm)) {				\
williamr@2
   193
		SLIST_REMOVE_HEAD((head), field);			\
williamr@2
   194
	}								\
williamr@2
   195
	else {								\
williamr@2
   196
		struct type *curelm = SLIST_FIRST((head));		\
williamr@2
   197
		while (SLIST_NEXT(curelm, field) != (elm))		\
williamr@2
   198
			curelm = SLIST_NEXT(curelm, field);		\
williamr@2
   199
		SLIST_NEXT(curelm, field) =				\
williamr@2
   200
		    SLIST_NEXT(SLIST_NEXT(curelm, field), field);	\
williamr@2
   201
	}								\
williamr@2
   202
} while (0)
williamr@2
   203
williamr@2
   204
#define	SLIST_REMOVE_HEAD(head, field) do {				\
williamr@2
   205
	SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field);	\
williamr@2
   206
} while (0)
williamr@2
   207
williamr@2
   208
/*
williamr@2
   209
 * Singly-linked Tail queue declarations.
williamr@2
   210
 */
williamr@2
   211
#define	STAILQ_HEAD(name, type)						\
williamr@2
   212
struct name {								\
williamr@2
   213
	struct type *stqh_first;/* first element */			\
williamr@2
   214
	struct type **stqh_last;/* addr of last next element */		\
williamr@2
   215
}
williamr@2
   216
williamr@2
   217
#define	STAILQ_HEAD_INITIALIZER(head)					\
williamr@2
   218
	{ NULL, &(head).stqh_first }
williamr@2
   219
williamr@2
   220
#define	STAILQ_ENTRY(type)						\
williamr@2
   221
struct {								\
williamr@2
   222
	struct type *stqe_next;	/* next element */			\
williamr@2
   223
}
williamr@2
   224
williamr@2
   225
/*
williamr@2
   226
 * Singly-linked Tail queue functions.
williamr@2
   227
 */
williamr@2
   228
#define	STAILQ_CONCAT(head1, head2) do {				\
williamr@2
   229
	if (!STAILQ_EMPTY((head2))) {					\
williamr@2
   230
		*(head1)->stqh_last = (head2)->stqh_first;		\
williamr@2
   231
		(head1)->stqh_last = (head2)->stqh_last;		\
williamr@2
   232
		STAILQ_INIT((head2));					\
williamr@2
   233
	}								\
williamr@2
   234
} while (0)
williamr@2
   235
williamr@2
   236
#define	STAILQ_EMPTY(head)	((head)->stqh_first == NULL)
williamr@2
   237
williamr@2
   238
#define	STAILQ_FIRST(head)	((head)->stqh_first)
williamr@2
   239
williamr@2
   240
#define	STAILQ_FOREACH(var, head, field)				\
williamr@2
   241
	for((var) = STAILQ_FIRST((head));				\
williamr@2
   242
	   (var);							\
williamr@2
   243
	   (var) = STAILQ_NEXT((var), field))
williamr@2
   244
williamr@2
   245
williamr@2
   246
#define	STAILQ_FOREACH_SAFE(var, head, field, tvar)			\
williamr@2
   247
	for ((var) = STAILQ_FIRST((head));				\
williamr@2
   248
	    (var) && ((tvar) = STAILQ_NEXT((var), field), 1);		\
williamr@2
   249
	    (var) = (tvar))
williamr@2
   250
williamr@2
   251
#define	STAILQ_INIT(head) do {						\
williamr@2
   252
	STAILQ_FIRST((head)) = NULL;					\
williamr@2
   253
	(head)->stqh_last = &STAILQ_FIRST((head));			\
williamr@2
   254
} while (0)
williamr@2
   255
williamr@2
   256
#define	STAILQ_INSERT_AFTER(head, tqelm, elm, field) do {		\
williamr@2
   257
	if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
williamr@2
   258
		(head)->stqh_last = &STAILQ_NEXT((elm), field);		\
williamr@2
   259
	STAILQ_NEXT((tqelm), field) = (elm);				\
williamr@2
   260
} while (0)
williamr@2
   261
williamr@2
   262
#define	STAILQ_INSERT_HEAD(head, elm, field) do {			\
williamr@2
   263
	if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL)	\
williamr@2
   264
		(head)->stqh_last = &STAILQ_NEXT((elm), field);		\
williamr@2
   265
	STAILQ_FIRST((head)) = (elm);					\
williamr@2
   266
} while (0)
williamr@2
   267
williamr@2
   268
#define	STAILQ_INSERT_TAIL(head, elm, field) do {			\
williamr@2
   269
	STAILQ_NEXT((elm), field) = NULL;				\
williamr@2
   270
	*(head)->stqh_last = (elm);					\
williamr@2
   271
	(head)->stqh_last = &STAILQ_NEXT((elm), field);			\
williamr@2
   272
} while (0)
williamr@2
   273
williamr@2
   274
#define	STAILQ_LAST(head, type, field)					\
williamr@2
   275
	(STAILQ_EMPTY((head)) ?						\
williamr@2
   276
		NULL :							\
williamr@2
   277
	        ((struct type *)					\
williamr@2
   278
		((char *)((head)->stqh_last) - __offsetof(struct type, field))))
williamr@2
   279
williamr@2
   280
#define	STAILQ_NEXT(elm, field)	((elm)->field.stqe_next)
williamr@2
   281
williamr@2
   282
#define	STAILQ_REMOVE(head, elm, type, field) do {			\
williamr@2
   283
	if (STAILQ_FIRST((head)) == (elm)) {				\
williamr@2
   284
		STAILQ_REMOVE_HEAD((head), field);			\
williamr@2
   285
	}								\
williamr@2
   286
	else {								\
williamr@2
   287
		struct type *curelm = STAILQ_FIRST((head));		\
williamr@2
   288
		while (STAILQ_NEXT(curelm, field) != (elm))		\
williamr@2
   289
			curelm = STAILQ_NEXT(curelm, field);		\
williamr@2
   290
		if ((STAILQ_NEXT(curelm, field) =			\
williamr@2
   291
		     STAILQ_NEXT(STAILQ_NEXT(curelm, field), field)) == NULL)\
williamr@2
   292
			(head)->stqh_last = &STAILQ_NEXT((curelm), field);\
williamr@2
   293
	}								\
williamr@2
   294
} while (0)
williamr@2
   295
williamr@2
   296
#define	STAILQ_REMOVE_HEAD(head, field) do {				\
williamr@2
   297
	if ((STAILQ_FIRST((head)) =					\
williamr@2
   298
	     STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL)		\
williamr@2
   299
		(head)->stqh_last = &STAILQ_FIRST((head));		\
williamr@2
   300
} while (0)
williamr@2
   301
williamr@2
   302
#define	STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do {			\
williamr@2
   303
	if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL)	\
williamr@2
   304
		(head)->stqh_last = &STAILQ_FIRST((head));		\
williamr@2
   305
} while (0)
williamr@2
   306
williamr@2
   307
/*
williamr@2
   308
 * List declarations.
williamr@2
   309
 */
williamr@2
   310
#define	LIST_HEAD(name, type)						\
williamr@2
   311
struct name {								\
williamr@2
   312
	struct type *lh_first;	/* first element */			\
williamr@2
   313
}
williamr@2
   314
williamr@2
   315
#define	LIST_HEAD_INITIALIZER(head)					\
williamr@2
   316
	{ NULL }
williamr@2
   317
williamr@2
   318
#define	LIST_ENTRY(type)						\
williamr@2
   319
struct {								\
williamr@2
   320
	struct type *le_next;	/* next element */			\
williamr@2
   321
	struct type **le_prev;	/* address of previous next element */	\
williamr@2
   322
}
williamr@2
   323
williamr@2
   324
/*
williamr@2
   325
 * List functions.
williamr@2
   326
 */
williamr@2
   327
williamr@2
   328
#define	LIST_EMPTY(head)	((head)->lh_first == NULL)
williamr@2
   329
williamr@2
   330
#define	LIST_FIRST(head)	((head)->lh_first)
williamr@2
   331
williamr@2
   332
#define	LIST_FOREACH(var, head, field)					\
williamr@2
   333
	for ((var) = LIST_FIRST((head));				\
williamr@2
   334
	    (var);							\
williamr@2
   335
	    (var) = LIST_NEXT((var), field))
williamr@2
   336
williamr@2
   337
#define	LIST_FOREACH_SAFE(var, head, field, tvar)			\
williamr@2
   338
	for ((var) = LIST_FIRST((head));				\
williamr@2
   339
	    (var) && ((tvar) = LIST_NEXT((var), field), 1);		\
williamr@2
   340
	    (var) = (tvar))
williamr@2
   341
williamr@2
   342
#define	LIST_INIT(head) do {						\
williamr@2
   343
	LIST_FIRST((head)) = NULL;					\
williamr@2
   344
} while (0)
williamr@2
   345
williamr@2
   346
#define	LIST_INSERT_AFTER(listelm, elm, field) do {			\
williamr@2
   347
	if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
williamr@2
   348
		LIST_NEXT((listelm), field)->field.le_prev =		\
williamr@2
   349
		    &LIST_NEXT((elm), field);				\
williamr@2
   350
	LIST_NEXT((listelm), field) = (elm);				\
williamr@2
   351
	(elm)->field.le_prev = &LIST_NEXT((listelm), field);		\
williamr@2
   352
} while (0)
williamr@2
   353
williamr@2
   354
#define	LIST_INSERT_BEFORE(listelm, elm, field) do {			\
williamr@2
   355
	(elm)->field.le_prev = (listelm)->field.le_prev;		\
williamr@2
   356
	LIST_NEXT((elm), field) = (listelm);				\
williamr@2
   357
	*(listelm)->field.le_prev = (elm);				\
williamr@2
   358
	(listelm)->field.le_prev = &LIST_NEXT((elm), field);		\
williamr@2
   359
} while (0)
williamr@2
   360
williamr@2
   361
#define	LIST_INSERT_HEAD(head, elm, field) do {				\
williamr@2
   362
	if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL)	\
williamr@2
   363
		LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
williamr@2
   364
	LIST_FIRST((head)) = (elm);					\
williamr@2
   365
	(elm)->field.le_prev = &LIST_FIRST((head));			\
williamr@2
   366
} while (0)
williamr@2
   367
williamr@2
   368
#define	LIST_NEXT(elm, field)	((elm)->field.le_next)
williamr@2
   369
williamr@2
   370
#define	LIST_REMOVE(elm, field) do {					\
williamr@2
   371
	if (LIST_NEXT((elm), field) != NULL)				\
williamr@2
   372
		LIST_NEXT((elm), field)->field.le_prev = 		\
williamr@2
   373
		    (elm)->field.le_prev;				\
williamr@2
   374
	*(elm)->field.le_prev = LIST_NEXT((elm), field);		\
williamr@2
   375
} while (0)
williamr@2
   376
williamr@2
   377
/*
williamr@2
   378
 * Tail queue declarations.
williamr@2
   379
 */
williamr@2
   380
#define	TAILQ_HEAD(name, type)						\
williamr@2
   381
struct name {								\
williamr@2
   382
	struct type *tqh_first;	/* first element */			\
williamr@2
   383
	struct type **tqh_last;	/* addr of last next element */		\
williamr@2
   384
	TRACEBUF							\
williamr@2
   385
}
williamr@2
   386
williamr@2
   387
#define	TAILQ_HEAD_INITIALIZER(head)					\
williamr@2
   388
	{ NULL, &(head).tqh_first }
williamr@2
   389
williamr@2
   390
#define	TAILQ_ENTRY(type)						\
williamr@2
   391
struct {								\
williamr@2
   392
	struct type *tqe_next;	/* next element */			\
williamr@2
   393
	struct type **tqe_prev;	/* address of previous next element */	\
williamr@2
   394
	TRACEBUF							\
williamr@2
   395
}
williamr@2
   396
williamr@2
   397
/*
williamr@2
   398
 * Tail queue functions.
williamr@2
   399
 */
williamr@2
   400
#define	TAILQ_CONCAT(head1, head2, field) do {				\
williamr@2
   401
	if (!TAILQ_EMPTY(head2)) {					\
williamr@2
   402
		*(head1)->tqh_last = (head2)->tqh_first;		\
williamr@2
   403
		(head2)->tqh_first->field.tqe_prev = (head1)->tqh_last;	\
williamr@2
   404
		(head1)->tqh_last = (head2)->tqh_last;			\
williamr@2
   405
		TAILQ_INIT((head2));					\
williamr@2
   406
		QMD_TRACE_HEAD(head1);					\
williamr@2
   407
		QMD_TRACE_HEAD(head2);					\
williamr@2
   408
	}								\
williamr@2
   409
} while (0)
williamr@2
   410
williamr@2
   411
#define	TAILQ_EMPTY(head)	((head)->tqh_first == NULL)
williamr@2
   412
williamr@2
   413
#define	TAILQ_FIRST(head)	((head)->tqh_first)
williamr@2
   414
williamr@2
   415
#define	TAILQ_FOREACH(var, head, field)					\
williamr@2
   416
	for ((var) = TAILQ_FIRST((head));				\
williamr@2
   417
	    (var);							\
williamr@2
   418
	    (var) = TAILQ_NEXT((var), field))
williamr@2
   419
williamr@2
   420
#define	TAILQ_FOREACH_SAFE(var, head, field, tvar)			\
williamr@2
   421
	for ((var) = TAILQ_FIRST((head));				\
williamr@2
   422
	    (var) && ((tvar) = TAILQ_NEXT((var), field), 1);		\
williamr@2
   423
	    (var) = (tvar))
williamr@2
   424
williamr@2
   425
#define	TAILQ_FOREACH_REVERSE(var, head, headname, field)		\
williamr@2
   426
	for ((var) = TAILQ_LAST((head), headname);			\
williamr@2
   427
	    (var);							\
williamr@2
   428
	    (var) = TAILQ_PREV((var), headname, field))
williamr@2
   429
williamr@2
   430
#define	TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar)	\
williamr@2
   431
	for ((var) = TAILQ_LAST((head), headname);			\
williamr@2
   432
	    (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1);	\
williamr@2
   433
	    (var) = (tvar))
williamr@2
   434
williamr@2
   435
#define	TAILQ_INIT(head) do {						\
williamr@2
   436
	TAILQ_FIRST((head)) = NULL;					\
williamr@2
   437
	(head)->tqh_last = &TAILQ_FIRST((head));			\
williamr@2
   438
	QMD_TRACE_HEAD(head);						\
williamr@2
   439
} while (0)
williamr@2
   440
williamr@2
   441
#define	TAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\
williamr@2
   442
	if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
williamr@2
   443
		TAILQ_NEXT((elm), field)->field.tqe_prev = 		\
williamr@2
   444
		    &TAILQ_NEXT((elm), field);				\
williamr@2
   445
	else {								\
williamr@2
   446
		(head)->tqh_last = &TAILQ_NEXT((elm), field);		\
williamr@2
   447
		QMD_TRACE_HEAD(head);					\
williamr@2
   448
	}								\
williamr@2
   449
	TAILQ_NEXT((listelm), field) = (elm);				\
williamr@2
   450
	(elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field);		\
williamr@2
   451
	QMD_TRACE_ELEM(&(elm)->field);					\
williamr@2
   452
	QMD_TRACE_ELEM(&listelm->field);				\
williamr@2
   453
} while (0)
williamr@2
   454
williamr@2
   455
#define	TAILQ_INSERT_BEFORE(listelm, elm, field) do {			\
williamr@2
   456
	(elm)->field.tqe_prev = (listelm)->field.tqe_prev;		\
williamr@2
   457
	TAILQ_NEXT((elm), field) = (listelm);				\
williamr@2
   458
	*(listelm)->field.tqe_prev = (elm);				\
williamr@2
   459
	(listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field);		\
williamr@2
   460
	QMD_TRACE_ELEM(&(elm)->field);					\
williamr@2
   461
	QMD_TRACE_ELEM(&listelm->field);				\
williamr@2
   462
} while (0)
williamr@2
   463
williamr@2
   464
#define	TAILQ_INSERT_HEAD(head, elm, field) do {			\
williamr@2
   465
	if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL)	\
williamr@2
   466
		TAILQ_FIRST((head))->field.tqe_prev =			\
williamr@2
   467
		    &TAILQ_NEXT((elm), field);				\
williamr@2
   468
	else								\
williamr@2
   469
		(head)->tqh_last = &TAILQ_NEXT((elm), field);		\
williamr@2
   470
	TAILQ_FIRST((head)) = (elm);					\
williamr@2
   471
	(elm)->field.tqe_prev = &TAILQ_FIRST((head));			\
williamr@2
   472
	QMD_TRACE_HEAD(head);						\
williamr@2
   473
	QMD_TRACE_ELEM(&(elm)->field);					\
williamr@2
   474
} while (0)
williamr@2
   475
williamr@2
   476
#define	TAILQ_INSERT_TAIL(head, elm, field) do {			\
williamr@2
   477
	TAILQ_NEXT((elm), field) = NULL;				\
williamr@2
   478
	(elm)->field.tqe_prev = (head)->tqh_last;			\
williamr@2
   479
	*(head)->tqh_last = (elm);					\
williamr@2
   480
	(head)->tqh_last = &TAILQ_NEXT((elm), field);			\
williamr@2
   481
	QMD_TRACE_HEAD(head);						\
williamr@2
   482
	QMD_TRACE_ELEM(&(elm)->field);					\
williamr@2
   483
} while (0)
williamr@2
   484
williamr@2
   485
#define	TAILQ_LAST(head, headname)					\
williamr@2
   486
	(*(((struct headname *)((head)->tqh_last))->tqh_last))
williamr@2
   487
williamr@2
   488
#define	TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
williamr@2
   489
williamr@2
   490
#define	TAILQ_PREV(elm, headname, field)				\
williamr@2
   491
	(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
williamr@2
   492
williamr@2
   493
#define	TAILQ_REMOVE(head, elm, field) do {				\
williamr@2
   494
	if ((TAILQ_NEXT((elm), field)) != NULL)				\
williamr@2
   495
		TAILQ_NEXT((elm), field)->field.tqe_prev = 		\
williamr@2
   496
		    (elm)->field.tqe_prev;				\
williamr@2
   497
	else {								\
williamr@2
   498
		(head)->tqh_last = (elm)->field.tqe_prev;		\
williamr@2
   499
		QMD_TRACE_HEAD(head);					\
williamr@2
   500
	}								\
williamr@2
   501
	*(elm)->field.tqe_prev = TAILQ_NEXT((elm), field);		\
williamr@2
   502
	TRASHIT((elm)->field.tqe_next);					\
williamr@2
   503
	TRASHIT((elm)->field.tqe_prev);					\
williamr@2
   504
	QMD_TRACE_ELEM(&(elm)->field);					\
williamr@2
   505
} while (0)
williamr@2
   506
williamr@2
   507
williamr@2
   508
#ifdef _KERNEL
williamr@2
   509
williamr@2
   510
/*
williamr@2
   511
 * XXX insque() and remque() are an old way of handling certain queues.
williamr@2
   512
 * They bogusly assumes that all queue heads look alike.
williamr@2
   513
 */
williamr@2
   514
williamr@2
   515
struct quehead {
williamr@2
   516
	struct quehead *qh_link;
williamr@2
   517
	struct quehead *qh_rlink;
williamr@2
   518
};
williamr@2
   519
williamr@2
   520
#ifdef __CC_SUPPORTS___INLINE
williamr@2
   521
williamr@2
   522
static __inline void
williamr@2
   523
insque(void *a, void *b)
williamr@2
   524
{
williamr@2
   525
	struct quehead *element = (struct quehead *)a,
williamr@2
   526
		 *head = (struct quehead *)b;
williamr@2
   527
williamr@2
   528
	element->qh_link = head->qh_link;
williamr@2
   529
	element->qh_rlink = head;
williamr@2
   530
	head->qh_link = element;
williamr@2
   531
	element->qh_link->qh_rlink = element;
williamr@2
   532
}
williamr@2
   533
williamr@2
   534
static __inline void
williamr@2
   535
remque(void *a)
williamr@2
   536
{
williamr@2
   537
	struct quehead *element = (struct quehead *)a;
williamr@2
   538
williamr@2
   539
	element->qh_link->qh_rlink = element->qh_rlink;
williamr@2
   540
	element->qh_rlink->qh_link = element->qh_link;
williamr@2
   541
	element->qh_rlink = 0;
williamr@2
   542
}
williamr@2
   543
williamr@2
   544
#endif /* __CC_SUPPORTS___INLINE */
williamr@2
   545
williamr@2
   546
#endif /* _KERNEL */
williamr@2
   547
williamr@2
   548
#endif /* !_SYS_QUEUE_H_ */