ldap_queue.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /* ldap_queue.h -- queue macros */
  2. /* $OpenLDAP$ */
  3. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  4. *
  5. * Copyright 2001-2022 The OpenLDAP Foundation.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted only as authorized by the OpenLDAP
  10. * Public License.
  11. *
  12. * A copy of this license is available in file LICENSE in the
  13. * top-level directory of the distribution or, alternatively, at
  14. * <http://www.OpenLDAP.org/license.html>.
  15. */
  16. /* Copyright (c) 1991, 1993
  17. * The Regents of the University of California. All rights reserved.
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions
  21. * are met:
  22. * 1. Redistributions of source code must retain the above copyright
  23. * notice, this list of conditions and the following disclaimer.
  24. * 2. Redistributions in binary form must reproduce the above copyright
  25. * notice, this list of conditions and the following disclaimer in the
  26. * documentation and/or other materials provided with the distribution.
  27. * 3. All advertising materials mentioning features or use of this software
  28. * must display the following acknowledgement:
  29. * This product includes software developed by the University of
  30. * California, Berkeley and its contributors.
  31. * 4. Neither the name of the University nor the names of its contributors
  32. * may be used to endorse or promote products derived from this software
  33. * without specific prior written permission.
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  36. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  38. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  39. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  40. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  41. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  42. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  43. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  44. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  45. * SUCH DAMAGE.
  46. *
  47. * @(#)queue.h 8.5 (Berkeley) 8/20/94
  48. * $FreeBSD: src/sys/sys/queue.h,v 1.32.2.5 2001/09/30 21:12:54 luigi Exp $
  49. *
  50. * See also: ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
  51. */
  52. /* ACKNOWLEDGEMENTS:
  53. * This work is derived from FreeBSD queue.h work. Adapted for use in
  54. * OpenLDAP Software by Kurt D. Zeilenga.
  55. */
  56. #ifndef _LDAP_QUEUE_H_
  57. #define _LDAP_QUEUE_H_
  58. /*
  59. * This file defines five types of data structures: singly-linked lists,
  60. * singly-linked tail queues, lists, tail queues, and circular queues.
  61. *
  62. * A singly-linked list is headed by a single forward pointer. The elements
  63. * are singly linked for minimum space and pointer manipulation overhead at
  64. * the expense of O(n) removal for arbitrary elements. New elements can be
  65. * added to the list after an existing element or at the head of the list.
  66. * Elements being removed from the head of the list should use the explicit
  67. * macro for this purpose for optimum efficiency. A singly-linked list may
  68. * only be traversed in the forward direction. Singly-linked lists are ideal
  69. * for applications with large datasets and few or no removals or for
  70. * implementing a LIFO queue.
  71. *
  72. * A singly-linked tail queue is headed by a pair of pointers, one to the
  73. * head of the list and the other to the tail of the list. The elements are
  74. * singly linked for minimum space and pointer manipulation overhead at the
  75. * expense of O(n) removal for arbitrary elements. New elements can be added
  76. * to the list after an existing element, at the head of the list, or at the
  77. * end of the list. Elements being removed from the head of the tail queue
  78. * should use the explicit macro for this purpose for optimum efficiency.
  79. * A singly-linked tail queue may only be traversed in the forward direction.
  80. * Singly-linked tail queues are ideal for applications with large datasets
  81. * and few or no removals or for implementing a FIFO queue.
  82. *
  83. * A list is headed by a single forward pointer (or an array of forward
  84. * pointers for a hash table header). The elements are doubly linked
  85. * so that an arbitrary element can be removed without a need to
  86. * traverse the list. New elements can be added to the list before
  87. * or after an existing element or at the head of the list. A list
  88. * may only be traversed in the forward direction.
  89. *
  90. * A tail queue is headed by a pair of pointers, one to the head of the
  91. * list and the other to the tail of the list. The elements are doubly
  92. * linked so that an arbitrary element can be removed without a need to
  93. * traverse the list. New elements can be added to the list before or
  94. * after an existing element, at the head of the list, or at the end of
  95. * the list. A tail queue may be traversed in either direction.
  96. *
  97. * A circle queue is headed by a pair of pointers, one to the head of the
  98. * list and the other to the tail of the list. The elements are doubly
  99. * linked so that an arbitrary element can be removed without a need to
  100. * traverse the list. New elements can be added to the list before or after
  101. * an existing element, at the head of the list, or at the end of the list.
  102. * A circle queue may be traversed in either direction, but has a more
  103. * complex end of list detection. Also, it is possible to rotate the queue,
  104. * rejoining the ends and splitting it so that a given element becomes the
  105. * new head or tail.
  106. *
  107. * For details on the use of these macros, see the queue(3) manual page.
  108. * All macros are prefixed with LDAP_.
  109. *
  110. * SLIST_ LIST_ STAILQ_ TAILQ_ CIRCLEQ_
  111. * _HEAD + + + + +
  112. * _ENTRY + + + + +
  113. * _INIT + + + + +
  114. * _ENTRY_INIT + + + + +
  115. * _EMPTY + + + + +
  116. * _FIRST + + + + +
  117. * _NEXT + + + + +
  118. * _PREV - - - + +
  119. * _LAST - - + + +
  120. * _FOREACH + + + + +
  121. * _FOREACH_REVERSE - - - + +
  122. * _INSERT_HEAD + + + + +
  123. * _INSERT_BEFORE - + - + +
  124. * _INSERT_AFTER + + + + +
  125. * _INSERT_TAIL - - + + +
  126. * _REMOVE_HEAD + - + - -
  127. * _REMOVE + + + + +
  128. *
  129. */
  130. /*
  131. * Singly-linked List definitions.
  132. */
  133. #define LDAP_SLIST_HEAD(name, type) \
  134. struct name { \
  135. struct type *slh_first; /* first element */ \
  136. }
  137. #define LDAP_SLIST_HEAD_INITIALIZER(head) \
  138. { NULL }
  139. #define LDAP_SLIST_ENTRY(type) \
  140. struct { \
  141. struct type *sle_next; /* next element */ \
  142. }
  143. #define LDAP_SLIST_ENTRY_INITIALIZER(entry) \
  144. { NULL }
  145. /*
  146. * Singly-linked List functions.
  147. */
  148. #define LDAP_SLIST_EMPTY(head) ((head)->slh_first == NULL)
  149. #define LDAP_SLIST_FIRST(head) ((head)->slh_first)
  150. #define LDAP_SLIST_FOREACH(var, head, field) \
  151. for((var) = (head)->slh_first; (var); (var) = (var)->field.sle_next)
  152. #define LDAP_SLIST_INIT(head) { \
  153. (head)->slh_first = NULL; \
  154. }
  155. #define LDAP_SLIST_ENTRY_INIT(var, field) { \
  156. (var)->field.sle_next = NULL; \
  157. }
  158. #define LDAP_SLIST_INSERT_AFTER(slistelm, elm, field) do { \
  159. (elm)->field.sle_next = (slistelm)->field.sle_next; \
  160. (slistelm)->field.sle_next = (elm); \
  161. } while (0)
  162. #define LDAP_SLIST_INSERT_HEAD(head, elm, field) do { \
  163. (elm)->field.sle_next = (head)->slh_first; \
  164. (head)->slh_first = (elm); \
  165. } while (0)
  166. #define LDAP_SLIST_NEXT(elm, field) ((elm)->field.sle_next)
  167. #define LDAP_SLIST_REMOVE_HEAD(head, field) do { \
  168. (head)->slh_first = (head)->slh_first->field.sle_next; \
  169. } while (0)
  170. #define LDAP_SLIST_REMOVE(head, elm, type, field) do { \
  171. if ((head)->slh_first == (elm)) { \
  172. LDAP_SLIST_REMOVE_HEAD((head), field); \
  173. } \
  174. else { \
  175. struct type *curelm = (head)->slh_first; \
  176. while( curelm->field.sle_next != (elm) ) \
  177. curelm = curelm->field.sle_next; \
  178. curelm->field.sle_next = \
  179. curelm->field.sle_next->field.sle_next; \
  180. } \
  181. } while (0)
  182. /*
  183. * Singly-linked Tail queue definitions.
  184. */
  185. #define LDAP_STAILQ_HEAD(name, type) \
  186. struct name { \
  187. struct type *stqh_first;/* first element */ \
  188. struct type **stqh_last;/* addr of last next element */ \
  189. }
  190. #define LDAP_STAILQ_HEAD_INITIALIZER(head) \
  191. { NULL, &(head).stqh_first }
  192. #define LDAP_STAILQ_ENTRY(type) \
  193. struct { \
  194. struct type *stqe_next; /* next element */ \
  195. }
  196. #define LDAP_STAILQ_ENTRY_INITIALIZER(entry) \
  197. { NULL }
  198. /*
  199. * Singly-linked Tail queue functions.
  200. */
  201. #define LDAP_STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
  202. #define LDAP_STAILQ_INIT(head) do { \
  203. (head)->stqh_first = NULL; \
  204. (head)->stqh_last = &(head)->stqh_first; \
  205. } while (0)
  206. #define LDAP_STAILQ_ENTRY_INIT(var, field) { \
  207. (var)->field.stqe_next = NULL; \
  208. }
  209. #define LDAP_STAILQ_FIRST(head) ((head)->stqh_first)
  210. #define LDAP_STAILQ_LAST(head, type, field) \
  211. (LDAP_STAILQ_EMPTY(head) ? \
  212. NULL : \
  213. ((struct type *) \
  214. ((char *)((head)->stqh_last) - offsetof(struct type, field))))
  215. #define LDAP_STAILQ_FOREACH(var, head, field) \
  216. for((var) = (head)->stqh_first; (var); (var) = (var)->field.stqe_next)
  217. #define LDAP_STAILQ_INSERT_HEAD(head, elm, field) do { \
  218. if (((elm)->field.stqe_next = (head)->stqh_first) == NULL) \
  219. (head)->stqh_last = &(elm)->field.stqe_next; \
  220. (head)->stqh_first = (elm); \
  221. } while (0)
  222. #define LDAP_STAILQ_INSERT_TAIL(head, elm, field) do { \
  223. (elm)->field.stqe_next = NULL; \
  224. *(head)->stqh_last = (elm); \
  225. (head)->stqh_last = &(elm)->field.stqe_next; \
  226. } while (0)
  227. #define LDAP_STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
  228. if (((elm)->field.stqe_next = (tqelm)->field.stqe_next) == NULL)\
  229. (head)->stqh_last = &(elm)->field.stqe_next; \
  230. (tqelm)->field.stqe_next = (elm); \
  231. } while (0)
  232. #define LDAP_STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
  233. #define LDAP_STAILQ_REMOVE_HEAD(head, field) do { \
  234. if (((head)->stqh_first = \
  235. (head)->stqh_first->field.stqe_next) == NULL) \
  236. (head)->stqh_last = &(head)->stqh_first; \
  237. } while (0)
  238. #define LDAP_STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do { \
  239. if (((head)->stqh_first = (elm)->field.stqe_next) == NULL) \
  240. (head)->stqh_last = &(head)->stqh_first; \
  241. } while (0)
  242. #define LDAP_STAILQ_REMOVE(head, elm, type, field) do { \
  243. if ((head)->stqh_first == (elm)) { \
  244. LDAP_STAILQ_REMOVE_HEAD(head, field); \
  245. } \
  246. else { \
  247. struct type *curelm = (head)->stqh_first; \
  248. while( curelm->field.stqe_next != (elm) ) \
  249. curelm = curelm->field.stqe_next; \
  250. if((curelm->field.stqe_next = \
  251. curelm->field.stqe_next->field.stqe_next) == NULL) \
  252. (head)->stqh_last = &(curelm)->field.stqe_next; \
  253. } \
  254. } while (0)
  255. /*
  256. * List definitions.
  257. */
  258. #define LDAP_LIST_HEAD(name, type) \
  259. struct name { \
  260. struct type *lh_first; /* first element */ \
  261. }
  262. #define LDAP_LIST_HEAD_INITIALIZER(head) \
  263. { NULL }
  264. #define LDAP_LIST_ENTRY(type) \
  265. struct { \
  266. struct type *le_next; /* next element */ \
  267. struct type **le_prev; /* address of previous next element */ \
  268. }
  269. #define LDAP_LIST_ENTRY_INITIALIZER(entry) \
  270. { NULL, NULL }
  271. /*
  272. * List functions.
  273. */
  274. #define LDAP_LIST_EMPTY(head) ((head)->lh_first == NULL)
  275. #define LDAP_LIST_FIRST(head) ((head)->lh_first)
  276. #define LDAP_LIST_FOREACH(var, head, field) \
  277. for((var) = (head)->lh_first; (var); (var) = (var)->field.le_next)
  278. #define LDAP_LIST_INIT(head) do { \
  279. (head)->lh_first = NULL; \
  280. } while (0)
  281. #define LDAP_LIST_ENTRY_INIT(var, field) do { \
  282. (var)->field.le_next = NULL; \
  283. (var)->field.le_prev = NULL; \
  284. } while (0)
  285. #define LDAP_LIST_INSERT_AFTER(listelm, elm, field) do { \
  286. if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
  287. (listelm)->field.le_next->field.le_prev = \
  288. &(elm)->field.le_next; \
  289. (listelm)->field.le_next = (elm); \
  290. (elm)->field.le_prev = &(listelm)->field.le_next; \
  291. } while (0)
  292. #define LDAP_LIST_INSERT_BEFORE(listelm, elm, field) do { \
  293. (elm)->field.le_prev = (listelm)->field.le_prev; \
  294. (elm)->field.le_next = (listelm); \
  295. *(listelm)->field.le_prev = (elm); \
  296. (listelm)->field.le_prev = &(elm)->field.le_next; \
  297. } while (0)
  298. #define LDAP_LIST_INSERT_HEAD(head, elm, field) do { \
  299. if (((elm)->field.le_next = (head)->lh_first) != NULL) \
  300. (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
  301. (head)->lh_first = (elm); \
  302. (elm)->field.le_prev = &(head)->lh_first; \
  303. } while (0)
  304. #define LDAP_LIST_NEXT(elm, field) ((elm)->field.le_next)
  305. #define LDAP_LIST_REMOVE(elm, field) do { \
  306. if ((elm)->field.le_next != NULL) \
  307. (elm)->field.le_next->field.le_prev = \
  308. (elm)->field.le_prev; \
  309. *(elm)->field.le_prev = (elm)->field.le_next; \
  310. } while (0)
  311. /*
  312. * Tail queue definitions.
  313. */
  314. #define LDAP_TAILQ_HEAD(name, type) \
  315. struct name { \
  316. struct type *tqh_first; /* first element */ \
  317. struct type **tqh_last; /* addr of last next element */ \
  318. }
  319. #define LDAP_TAILQ_HEAD_INITIALIZER(head) \
  320. { NULL, &(head).tqh_first }
  321. #define LDAP_TAILQ_ENTRY(type) \
  322. struct { \
  323. struct type *tqe_next; /* next element */ \
  324. struct type **tqe_prev; /* address of previous next element */ \
  325. }
  326. #define LDAP_TAILQ_ENTRY_INITIALIZER(entry) \
  327. { NULL, NULL }
  328. /*
  329. * Tail queue functions.
  330. */
  331. #define LDAP_TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
  332. #define LDAP_TAILQ_FOREACH(var, head, field) \
  333. for (var = LDAP_TAILQ_FIRST(head); var; var = LDAP_TAILQ_NEXT(var, field))
  334. #define LDAP_TAILQ_FOREACH_REVERSE(var, head, headname, field) \
  335. for ((var) = LDAP_TAILQ_LAST((head), headname); \
  336. (var); \
  337. (var) = LDAP_TAILQ_PREV((var), headname, field))
  338. #define LDAP_TAILQ_FIRST(head) ((head)->tqh_first)
  339. #define LDAP_TAILQ_LAST(head, headname) \
  340. (*(((struct headname *)((head)->tqh_last))->tqh_last))
  341. #define LDAP_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  342. #define LDAP_TAILQ_PREV(elm, headname, field) \
  343. (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  344. #define LDAP_TAILQ_INIT(head) do { \
  345. (head)->tqh_first = NULL; \
  346. (head)->tqh_last = &(head)->tqh_first; \
  347. } while (0)
  348. #define LDAP_TAILQ_ENTRY_INIT(var, field) do { \
  349. (var)->field.tqe_next = NULL; \
  350. (var)->field.tqe_prev = NULL; \
  351. } while (0)
  352. #define LDAP_TAILQ_INSERT_HEAD(head, elm, field) do { \
  353. if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
  354. (head)->tqh_first->field.tqe_prev = \
  355. &(elm)->field.tqe_next; \
  356. else \
  357. (head)->tqh_last = &(elm)->field.tqe_next; \
  358. (head)->tqh_first = (elm); \
  359. (elm)->field.tqe_prev = &(head)->tqh_first; \
  360. } while (0)
  361. #define LDAP_TAILQ_INSERT_TAIL(head, elm, field) do { \
  362. (elm)->field.tqe_next = NULL; \
  363. (elm)->field.tqe_prev = (head)->tqh_last; \
  364. *(head)->tqh_last = (elm); \
  365. (head)->tqh_last = &(elm)->field.tqe_next; \
  366. } while (0)
  367. #define LDAP_TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
  368. if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
  369. (elm)->field.tqe_next->field.tqe_prev = \
  370. &(elm)->field.tqe_next; \
  371. else \
  372. (head)->tqh_last = &(elm)->field.tqe_next; \
  373. (listelm)->field.tqe_next = (elm); \
  374. (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
  375. } while (0)
  376. #define LDAP_TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
  377. (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
  378. (elm)->field.tqe_next = (listelm); \
  379. *(listelm)->field.tqe_prev = (elm); \
  380. (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
  381. } while (0)
  382. #define LDAP_TAILQ_REMOVE(head, elm, field) do { \
  383. if (((elm)->field.tqe_next) != NULL) \
  384. (elm)->field.tqe_next->field.tqe_prev = \
  385. (elm)->field.tqe_prev; \
  386. else \
  387. (head)->tqh_last = (elm)->field.tqe_prev; \
  388. *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
  389. } while (0)
  390. /*
  391. * Circular queue definitions.
  392. */
  393. #define LDAP_CIRCLEQ_HEAD(name, type) \
  394. struct name { \
  395. struct type *cqh_first; /* first element */ \
  396. struct type *cqh_last; /* last element */ \
  397. }
  398. #define LDAP_CIRCLEQ_HEAD_INITIALIZER(head) \
  399. { (void *)&(head), (void *)&(head) }
  400. #define LDAP_CIRCLEQ_ENTRY(type) \
  401. struct { \
  402. struct type *cqe_next; /* next element */ \
  403. struct type *cqe_prev; /* previous element */ \
  404. }
  405. /*
  406. * Circular queue functions.
  407. */
  408. #define LDAP_CIRCLEQ_EMPTY(head) ((head)->cqh_first == (void *)(head))
  409. #define LDAP_CIRCLEQ_FIRST(head) ((head)->cqh_first)
  410. #define LDAP_CIRCLEQ_FOREACH(var, head, field) \
  411. for((var) = (head)->cqh_first; \
  412. (var) != (void *)(head); \
  413. (var) = (var)->field.cqe_next)
  414. #define LDAP_CIRCLEQ_FOREACH_REVERSE(var, head, field) \
  415. for((var) = (head)->cqh_last; \
  416. (var) != (void *)(head); \
  417. (var) = (var)->field.cqe_prev)
  418. #define LDAP_CIRCLEQ_INIT(head) do { \
  419. (head)->cqh_first = (void *)(head); \
  420. (head)->cqh_last = (void *)(head); \
  421. } while (0)
  422. #define LDAP_CIRCLEQ_ENTRY_INIT(var, field) do { \
  423. (var)->field.cqe_next = NULL; \
  424. (var)->field.cqe_prev = NULL; \
  425. } while (0)
  426. #define LDAP_CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  427. (elm)->field.cqe_next = (listelm)->field.cqe_next; \
  428. (elm)->field.cqe_prev = (listelm); \
  429. if ((listelm)->field.cqe_next == (void *)(head)) \
  430. (head)->cqh_last = (elm); \
  431. else \
  432. (listelm)->field.cqe_next->field.cqe_prev = (elm); \
  433. (listelm)->field.cqe_next = (elm); \
  434. } while (0)
  435. #define LDAP_CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
  436. (elm)->field.cqe_next = (listelm); \
  437. (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
  438. if ((listelm)->field.cqe_prev == (void *)(head)) \
  439. (head)->cqh_first = (elm); \
  440. else \
  441. (listelm)->field.cqe_prev->field.cqe_next = (elm); \
  442. (listelm)->field.cqe_prev = (elm); \
  443. } while (0)
  444. #define LDAP_CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
  445. (elm)->field.cqe_next = (head)->cqh_first; \
  446. (elm)->field.cqe_prev = (void *)(head); \
  447. if ((head)->cqh_last == (void *)(head)) \
  448. (head)->cqh_last = (elm); \
  449. else \
  450. (head)->cqh_first->field.cqe_prev = (elm); \
  451. (head)->cqh_first = (elm); \
  452. } while (0)
  453. #define LDAP_CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
  454. (elm)->field.cqe_next = (void *)(head); \
  455. (elm)->field.cqe_prev = (head)->cqh_last; \
  456. if ((head)->cqh_first == (void *)(head)) \
  457. (head)->cqh_first = (elm); \
  458. else \
  459. (head)->cqh_last->field.cqe_next = (elm); \
  460. (head)->cqh_last = (elm); \
  461. } while (0)
  462. #define LDAP_CIRCLEQ_LAST(head) ((head)->cqh_last)
  463. #define LDAP_CIRCLEQ_NEXT(elm,field) ((elm)->field.cqe_next)
  464. #define LDAP_CIRCLEQ_PREV(elm,field) ((elm)->field.cqe_prev)
  465. #define LDAP_CIRCLEQ_REMOVE(head, elm, field) do { \
  466. if ((elm)->field.cqe_next == (void *)(head)) \
  467. (head)->cqh_last = (elm)->field.cqe_prev; \
  468. else \
  469. (elm)->field.cqe_next->field.cqe_prev = \
  470. (elm)->field.cqe_prev; \
  471. if ((elm)->field.cqe_prev == (void *)(head)) \
  472. (head)->cqh_first = (elm)->field.cqe_next; \
  473. else \
  474. (elm)->field.cqe_prev->field.cqe_next = \
  475. (elm)->field.cqe_next; \
  476. } while (0)
  477. #define LDAP_CIRCLEQ_LOOP_NEXT(head, elm, field) \
  478. (((elm)->field.cqe_next == (void *)(head)) \
  479. ? ((head)->cqh_first) \
  480. : ((elm)->field.cqe_next))
  481. #define LDAP_CIRCLEQ_LOOP_PREV(head, elm, field) \
  482. (((elm)->field.cqe_prev == (void *)(head)) \
  483. ? ((head)->cqh_last) \
  484. : ((elm)->field.cqe_prev))
  485. #define LDAP_CIRCLEQ_MAKE_HEAD(head, elm, field) do { \
  486. if ((elm)->field.cqe_prev != (void *)(head)) { \
  487. (head)->cqh_first->field.cqe_prev = (head)->cqh_last; \
  488. (head)->cqh_last->field.cqe_next = (head)->cqh_first; \
  489. (head)->cqh_first = elm; \
  490. (head)->cqh_last = (elm)->field.cqe_prev; \
  491. (elm)->field.cqe_prev->field.cqe_next = (void *)(head); \
  492. (elm)->field.cqe_prev = (void *)(head); \
  493. } \
  494. } while (0)
  495. #define LDAP_CIRCLEQ_MAKE_TAIL(head, elm, field) do { \
  496. if ((elm)->field.cqe_next != (void *)(head)) { \
  497. (head)->cqh_first->field.cqe_prev = (head)->cqh_last; \
  498. (head)->cqh_last->field.cqe_next = (head)->cqh_first; \
  499. (head)->cqh_first = (elm)->field.cqe_next; \
  500. (head)->cqh_last = elm; \
  501. (elm)->field.cqe_next->field.cqe_prev = (void *)(head); \
  502. (elm)->field.cqe_next = (void *)(head); \
  503. } \
  504. } while (0)
  505. #endif /* !_LDAP_QUEUE_H_ */