shqueue.h
上传用户:romrleung
上传日期:2022-05-23
资源大小:18897k
文件大小:11k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /*-
  2.  * See the file LICENSE for redistribution information.
  3.  *
  4.  * Copyright (c) 1996-2002
  5.  * Sleepycat Software.  All rights reserved.
  6.  *
  7.  * $Id: shqueue.h,v 11.9 2002/01/11 15:52:30 bostic Exp $
  8.  */
  9. #ifndef _SYS_SHQUEUE_H_
  10. #define _SYS_SHQUEUE_H_
  11. /*
  12.  * This file defines three types of data structures: lists, tail queues, and
  13.  * circular queues, similarly to the include file <sys/queue.h>.
  14.  *
  15.  * The difference is that this set of macros can be used for structures that
  16.  * reside in shared memory that may be mapped at different addresses in each
  17.  * process.  In most cases, the macros for shared structures exactly mirror
  18.  * the normal macros, although the macro calls require an additional type
  19.  * parameter, only used by the HEAD and ENTRY macros of the standard macros.
  20.  *
  21.  * For details on the use of these macros, see the queue(3) manual page.
  22.  */
  23. #if defined(__cplusplus)
  24. extern "C" {
  25. #endif
  26. /*
  27.  * Shared list definitions.
  28.  */
  29. #define SH_LIST_HEAD(name)
  30. struct name {
  31. ssize_t slh_first; /* first element */
  32. }
  33. #define SH_LIST_ENTRY
  34. struct {
  35. ssize_t sle_next; /* relative offset next element */
  36. ssize_t sle_prev; /* relative offset of prev element */
  37. }
  38. /*
  39.  * Shared list functions.  Since we use relative offsets for pointers,
  40.  * 0 is a valid offset.  Therefore, we use -1 to indicate end of list.
  41.  * The macros ending in "P" return pointers without checking for end
  42.  * of list, the others check for end of list and evaluate to either a
  43.  * pointer or NULL.
  44.  */
  45. #define SH_LIST_FIRSTP(head, type)
  46. ((struct type *)(((u_int8_t *)(head)) + (head)->slh_first))
  47. #define SH_LIST_FIRST(head, type)
  48. ((head)->slh_first == -1 ? NULL :
  49. ((struct type *)(((u_int8_t *)(head)) + (head)->slh_first)))
  50. #define SH_LIST_NEXTP(elm, field, type)
  51. ((struct type *)(((u_int8_t *)(elm)) + (elm)->field.sle_next))
  52. #define SH_LIST_NEXT(elm, field, type)
  53. ((elm)->field.sle_next == -1 ? NULL :
  54. ((struct type *)(((u_int8_t *)(elm)) + (elm)->field.sle_next)))
  55. #define SH_LIST_PREV(elm, field)
  56. ((ssize_t *)(((u_int8_t *)(elm)) + (elm)->field.sle_prev))
  57. #define SH_PTR_TO_OFF(src, dest)
  58. ((ssize_t)(((u_int8_t *)(dest)) - ((u_int8_t *)(src))))
  59. /*
  60.  * Take the element's next pointer and calculate what the corresponding
  61.  * Prev pointer should be -- basically it is the negation plus the offset
  62.  * of the next field in the structure.
  63.  */
  64. #define SH_LIST_NEXT_TO_PREV(elm, field)
  65. (-(elm)->field.sle_next + SH_PTR_TO_OFF(elm, &(elm)->field.sle_next))
  66. #define SH_LIST_INIT(head) (head)->slh_first = -1
  67. #define SH_LIST_INSERT_AFTER(listelm, elm, field, type) do {
  68. if ((listelm)->field.sle_next != -1) {
  69. (elm)->field.sle_next = SH_PTR_TO_OFF(elm,
  70.     SH_LIST_NEXTP(listelm, field, type));
  71. SH_LIST_NEXTP(listelm, field, type)->field.sle_prev =
  72. SH_LIST_NEXT_TO_PREV(elm, field);
  73. } else
  74. (elm)->field.sle_next = -1;
  75. (listelm)->field.sle_next = SH_PTR_TO_OFF(listelm, elm);
  76. (elm)->field.sle_prev = SH_LIST_NEXT_TO_PREV(listelm, field);
  77. } while (0)
  78. #define SH_LIST_INSERT_HEAD(head, elm, field, type) do {
  79. if ((head)->slh_first != -1) {
  80. (elm)->field.sle_next =
  81.     (head)->slh_first - SH_PTR_TO_OFF(head, elm);
  82. SH_LIST_FIRSTP(head, type)->field.sle_prev =
  83. SH_LIST_NEXT_TO_PREV(elm, field);
  84. } else
  85. (elm)->field.sle_next = -1;
  86. (head)->slh_first = SH_PTR_TO_OFF(head, elm);
  87. (elm)->field.sle_prev = SH_PTR_TO_OFF(elm, &(head)->slh_first);
  88. } while (0)
  89. #define SH_LIST_REMOVE(elm, field, type) do {
  90. if ((elm)->field.sle_next != -1) {
  91. SH_LIST_NEXTP(elm, field, type)->field.sle_prev =
  92. (elm)->field.sle_prev - (elm)->field.sle_next;
  93. *SH_LIST_PREV(elm, field) += (elm)->field.sle_next;
  94. } else
  95. *SH_LIST_PREV(elm, field) = -1;
  96. } while (0)
  97. /*
  98.  * Shared tail queue definitions.
  99.  */
  100. #define SH_TAILQ_HEAD(name)
  101. struct name {
  102. ssize_t stqh_first; /* relative offset of first element */
  103. ssize_t stqh_last; /* relative offset of last's next */
  104. }
  105. #define SH_TAILQ_ENTRY
  106. struct {
  107. ssize_t stqe_next; /* relative offset of next element */
  108. ssize_t stqe_prev; /* relative offset of prev's next */
  109. }
  110. /*
  111.  * Shared tail queue functions.
  112.  */
  113. #define SH_TAILQ_FIRSTP(head, type)
  114. ((struct type *)((u_int8_t *)(head) + (head)->stqh_first))
  115. #define SH_TAILQ_FIRST(head, type)
  116. ((head)->stqh_first == -1 ? NULL : SH_TAILQ_FIRSTP(head, type))
  117. #define SH_TAILQ_NEXTP(elm, field, type)
  118. ((struct type *)((u_int8_t *)(elm) + (elm)->field.stqe_next))
  119. #define SH_TAILQ_NEXT(elm, field, type)
  120. ((elm)->field.stqe_next == -1 ? NULL : SH_TAILQ_NEXTP(elm, field, type))
  121. #define SH_TAILQ_PREVP(elm, field)
  122. ((ssize_t *)((u_int8_t *)(elm) + (elm)->field.stqe_prev))
  123. #define SH_TAILQ_LAST(head)
  124. ((ssize_t *)(((u_int8_t *)(head)) + (head)->stqh_last))
  125. #define SH_TAILQ_NEXT_TO_PREV(elm, field)
  126. (-(elm)->field.stqe_next + SH_PTR_TO_OFF(elm, &(elm)->field.stqe_next))
  127. #define SH_TAILQ_INIT(head) {
  128. (head)->stqh_first = -1;
  129. (head)->stqh_last = SH_PTR_TO_OFF(head, &(head)->stqh_first);
  130. }
  131. #define SH_TAILQ_INSERT_HEAD(head, elm, field, type) do {
  132. if ((head)->stqh_first != -1) {
  133. (elm)->field.stqe_next =
  134.     (head)->stqh_first - SH_PTR_TO_OFF(head, elm);
  135. SH_TAILQ_FIRSTP(head, type)->field.stqe_prev =
  136. SH_TAILQ_NEXT_TO_PREV(elm, field);
  137. } else {
  138. (elm)->field.stqe_next = -1;
  139. (head)->stqh_last =
  140.     SH_PTR_TO_OFF(head, &(elm)->field.stqe_next);
  141. }
  142. (head)->stqh_first = SH_PTR_TO_OFF(head, elm);
  143. (elm)->field.stqe_prev =
  144.     SH_PTR_TO_OFF(elm, &(head)->stqh_first);
  145. } while (0)
  146. #define SH_TAILQ_INSERT_TAIL(head, elm, field) do {
  147. (elm)->field.stqe_next = -1;
  148. (elm)->field.stqe_prev =
  149.     -SH_PTR_TO_OFF(head, elm) + (head)->stqh_last;
  150. if ((head)->stqh_last ==
  151.     SH_PTR_TO_OFF((head), &(head)->stqh_first))
  152. (head)->stqh_first = SH_PTR_TO_OFF(head, elm);
  153. else
  154. *SH_TAILQ_LAST(head) = -(head)->stqh_last +
  155.     SH_PTR_TO_OFF((elm), &(elm)->field.stqe_next) +
  156.     SH_PTR_TO_OFF(head, elm);
  157. (head)->stqh_last =
  158.     SH_PTR_TO_OFF(head, &((elm)->field.stqe_next));
  159. } while (0)
  160. #define SH_TAILQ_INSERT_AFTER(head, listelm, elm, field, type) do {
  161. if ((listelm)->field.stqe_next != -1) {
  162. (elm)->field.stqe_next = (listelm)->field.stqe_next -
  163.     SH_PTR_TO_OFF(listelm, elm);
  164. SH_TAILQ_NEXTP(listelm, field, type)->field.stqe_prev =
  165.     SH_TAILQ_NEXT_TO_PREV(elm, field);
  166. } else {
  167. (elm)->field.stqe_next = -1;
  168. (head)->stqh_last =
  169.     SH_PTR_TO_OFF(head, &elm->field.stqe_next);
  170. }
  171. (listelm)->field.stqe_next = SH_PTR_TO_OFF(listelm, elm);
  172. (elm)->field.stqe_prev = SH_TAILQ_NEXT_TO_PREV(listelm, field);
  173. } while (0)
  174. #define SH_TAILQ_REMOVE(head, elm, field, type) do {
  175. if ((elm)->field.stqe_next != -1) {
  176. SH_TAILQ_NEXTP(elm, field, type)->field.stqe_prev =
  177.     (elm)->field.stqe_prev +
  178.     SH_PTR_TO_OFF(SH_TAILQ_NEXTP(elm,
  179.     field, type), elm);
  180. *SH_TAILQ_PREVP(elm, field) += elm->field.stqe_next;
  181. } else {
  182. (head)->stqh_last = (elm)->field.stqe_prev +
  183. SH_PTR_TO_OFF(head, elm);
  184. *SH_TAILQ_PREVP(elm, field) = -1;
  185. }
  186. } while (0)
  187. /*
  188.  * Shared circular queue definitions.
  189.  */
  190. #define SH_CIRCLEQ_HEAD(name)
  191. struct name {
  192. ssize_t scqh_first; /* first element */
  193. ssize_t scqh_last; /* last element */
  194. }
  195. #define SH_CIRCLEQ_ENTRY
  196. struct {
  197. ssize_t scqe_next; /* next element */
  198. ssize_t scqe_prev; /* previous element */
  199. }
  200. /*
  201.  * Shared circular queue functions.
  202.  */
  203. #define SH_CIRCLEQ_FIRSTP(head, type)
  204. ((struct type *)(((u_int8_t *)(head)) + (head)->scqh_first))
  205. #define SH_CIRCLEQ_FIRST(head, type)
  206. ((head)->scqh_first == -1 ?
  207. (void *)head : SH_CIRCLEQ_FIRSTP(head, type))
  208. #define SH_CIRCLEQ_LASTP(head, type)
  209. ((struct type *)(((u_int8_t *)(head)) + (head)->scqh_last))
  210. #define SH_CIRCLEQ_LAST(head, type)
  211. ((head)->scqh_last == -1 ? (void *)head : SH_CIRCLEQ_LASTP(head, type))
  212. #define SH_CIRCLEQ_NEXTP(elm, field, type)
  213. ((struct type *)(((u_int8_t *)(elm)) + (elm)->field.scqe_next))
  214. #define SH_CIRCLEQ_NEXT(head, elm, field, type)
  215. ((elm)->field.scqe_next == SH_PTR_TO_OFF(elm, head) ?
  216.     (void *)head : SH_CIRCLEQ_NEXTP(elm, field, type))
  217. #define SH_CIRCLEQ_PREVP(elm, field, type)
  218. ((struct type *)(((u_int8_t *)(elm)) + (elm)->field.scqe_prev))
  219. #define SH_CIRCLEQ_PREV(head, elm, field, type)
  220. ((elm)->field.scqe_prev == SH_PTR_TO_OFF(elm, head) ?
  221.     (void *)head : SH_CIRCLEQ_PREVP(elm, field, type))
  222. #define SH_CIRCLEQ_INIT(head) {
  223. (head)->scqh_first = 0;
  224. (head)->scqh_last = 0;
  225. }
  226. #define SH_CIRCLEQ_INSERT_AFTER(head, listelm, elm, field, type) do {
  227. (elm)->field.scqe_prev = SH_PTR_TO_OFF(elm, listelm);
  228. (elm)->field.scqe_next = (listelm)->field.scqe_next +
  229.     (elm)->field.scqe_prev;
  230. if (SH_CIRCLEQ_NEXTP(listelm, field, type) == (void *)head)
  231. (head)->scqh_last = SH_PTR_TO_OFF(head, elm);
  232. else
  233. SH_CIRCLEQ_NEXTP(listelm,
  234.     field, type)->field.scqe_prev =
  235.     SH_PTR_TO_OFF(SH_CIRCLEQ_NEXTP(listelm,
  236.     field, type), elm);
  237. (listelm)->field.scqe_next = -(elm)->field.scqe_prev;
  238. } while (0)
  239. #define SH_CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field, type) do {
  240. (elm)->field.scqe_next = SH_PTR_TO_OFF(elm, listelm);
  241. (elm)->field.scqe_prev = (elm)->field.scqe_next -
  242. SH_CIRCLEQ_PREVP(listelm, field, type)->field.scqe_next;
  243. if (SH_CIRCLEQ_PREVP(listelm, field, type) == (void *)(head))
  244. (head)->scqh_first = SH_PTR_TO_OFF(head, elm);
  245. else
  246. SH_CIRCLEQ_PREVP(listelm,
  247.     field, type)->field.scqe_next =
  248.     SH_PTR_TO_OFF(SH_CIRCLEQ_PREVP(listelm,
  249.     field, type), elm);
  250. (listelm)->field.scqe_prev = -(elm)->field.scqe_next;
  251. } while (0)
  252. #define SH_CIRCLEQ_INSERT_HEAD(head, elm, field, type) do {
  253. (elm)->field.scqe_prev = SH_PTR_TO_OFF(elm, head);
  254. (elm)->field.scqe_next = (head)->scqh_first +
  255. (elm)->field.scqe_prev;
  256. if ((head)->scqh_last == 0)
  257. (head)->scqh_last = -(elm)->field.scqe_prev;
  258. else
  259. SH_CIRCLEQ_FIRSTP(head, type)->field.scqe_prev =
  260.     SH_PTR_TO_OFF(SH_CIRCLEQ_FIRSTP(head, type), elm);
  261. (head)->scqh_first = -(elm)->field.scqe_prev;
  262. } while (0)
  263. #define SH_CIRCLEQ_INSERT_TAIL(head, elm, field, type) do {
  264. (elm)->field.scqe_next = SH_PTR_TO_OFF(elm, head);
  265. (elm)->field.scqe_prev = (head)->scqh_last +
  266.     (elm)->field.scqe_next;
  267. if ((head)->scqh_first == 0)
  268. (head)->scqh_first = -(elm)->field.scqe_next;
  269. else
  270. SH_CIRCLEQ_LASTP(head, type)->field.scqe_next =
  271.     SH_PTR_TO_OFF(SH_CIRCLEQ_LASTP(head, type), elm);
  272. (head)->scqh_last = -(elm)->field.scqe_next;
  273. } while (0)
  274. #define SH_CIRCLEQ_REMOVE(head, elm, field, type) do {
  275. if (SH_CIRCLEQ_NEXTP(elm, field, type) == (void *)(head))
  276. (head)->scqh_last += (elm)->field.scqe_prev;
  277. else
  278. SH_CIRCLEQ_NEXTP(elm, field, type)->field.scqe_prev +=
  279.     (elm)->field.scqe_prev;
  280. if (SH_CIRCLEQ_PREVP(elm, field, type) == (void *)(head))
  281. (head)->scqh_first += (elm)->field.scqe_next;
  282. else
  283. SH_CIRCLEQ_PREVP(elm, field, type)->field.scqe_next +=
  284.     (elm)->field.scqe_next;
  285. } while (0)
  286. #if defined(__cplusplus)
  287. }
  288. #endif
  289. #endif /* !_SYS_SHQUEUE_H_ */