Apache2
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
apr_ring.h
Go to the documentation of this file.
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements. See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * This code draws heavily from the 4.4BSD <sys/queue.h> macros
19  * and Dean Gaudet's "splim/ring.h".
20  * <http://www.freebsd.org/cgi/cvsweb.cgi/src/sys/sys/queue.h>
21  * <http://www.arctic.org/~dean/splim/>
22  *
23  * We'd use Dean's code directly if we could guarantee the
24  * availability of inline functions.
25  */
26 
27 #ifndef APR_RING_H
28 #define APR_RING_H
29 
35 /*
36  * for offsetof()
37  */
38 #include "apr_general.h"
39 
70 #define APR_RING_ENTRY(elem) \
71  struct { \
72  struct elem * volatile next; \
73  struct elem * volatile prev; \
74  }
75 
91 #define APR_RING_HEAD(head, elem) \
92  struct head { \
93  struct elem * volatile next; \
94  struct elem * volatile prev; \
95  }
96 
159 #define APR_RING_SENTINEL(hp, elem, link) \
160  (struct elem *)((char *)(&(hp)->next) - APR_OFFSETOF(struct elem, link))
161 
166 #define APR_RING_FIRST(hp) (hp)->next
167 
171 #define APR_RING_LAST(hp) (hp)->prev
172 
177 #define APR_RING_NEXT(ep, link) (ep)->link.next
178 
183 #define APR_RING_PREV(ep, link) (ep)->link.prev
184 
185 
192 #define APR_RING_INIT(hp, elem, link) do { \
193  APR_RING_FIRST((hp)) = APR_RING_SENTINEL((hp), elem, link); \
194  APR_RING_LAST((hp)) = APR_RING_SENTINEL((hp), elem, link); \
195  } while (0)
196 
204 #define APR_RING_EMPTY(hp, elem, link) \
205  (APR_RING_FIRST((hp)) == APR_RING_SENTINEL((hp), elem, link))
206 
212 #define APR_RING_ELEM_INIT(ep, link) do { \
213  APR_RING_NEXT((ep), link) = (ep); \
214  APR_RING_PREV((ep), link) = (ep); \
215  } while (0)
216 
217 
228 #define APR_RING_SPLICE_BEFORE(lep, ep1, epN, link) do { \
229  APR_RING_NEXT((epN), link) = (lep); \
230  APR_RING_PREV((ep1), link) = APR_RING_PREV((lep), link); \
231  APR_RING_NEXT(APR_RING_PREV((lep), link), link) = (ep1); \
232  APR_RING_PREV((lep), link) = (epN); \
233  } while (0)
234 
245 #define APR_RING_SPLICE_AFTER(lep, ep1, epN, link) do { \
246  APR_RING_PREV((ep1), link) = (lep); \
247  APR_RING_NEXT((epN), link) = APR_RING_NEXT((lep), link); \
248  APR_RING_PREV(APR_RING_NEXT((lep), link), link) = (epN); \
249  APR_RING_NEXT((lep), link) = (ep1); \
250  } while (0)
251 
261 #define APR_RING_INSERT_BEFORE(lep, nep, link) \
262  APR_RING_SPLICE_BEFORE((lep), (nep), (nep), link)
263 
273 #define APR_RING_INSERT_AFTER(lep, nep, link) \
274  APR_RING_SPLICE_AFTER((lep), (nep), (nep), link)
275 
276 
286 #define APR_RING_SPLICE_HEAD(hp, ep1, epN, elem, link) \
287  APR_RING_SPLICE_AFTER(APR_RING_SENTINEL((hp), elem, link), \
288  (ep1), (epN), link)
289 
299 #define APR_RING_SPLICE_TAIL(hp, ep1, epN, elem, link) \
300  APR_RING_SPLICE_BEFORE(APR_RING_SENTINEL((hp), elem, link), \
301  (ep1), (epN), link)
302 
311 #define APR_RING_INSERT_HEAD(hp, nep, elem, link) \
312  APR_RING_SPLICE_HEAD((hp), (nep), (nep), elem, link)
313 
322 #define APR_RING_INSERT_TAIL(hp, nep, elem, link) \
323  APR_RING_SPLICE_TAIL((hp), (nep), (nep), elem, link)
324 
332 #define APR_RING_CONCAT(h1, h2, elem, link) do { \
333  if (!APR_RING_EMPTY((h2), elem, link)) { \
334  APR_RING_SPLICE_BEFORE(APR_RING_SENTINEL((h1), elem, link), \
335  APR_RING_FIRST((h2)), \
336  APR_RING_LAST((h2)), link); \
337  APR_RING_INIT((h2), elem, link); \
338  } \
339  } while (0)
340 
348 #define APR_RING_PREPEND(h1, h2, elem, link) do { \
349  if (!APR_RING_EMPTY((h2), elem, link)) { \
350  APR_RING_SPLICE_AFTER(APR_RING_SENTINEL((h1), elem, link), \
351  APR_RING_FIRST((h2)), \
352  APR_RING_LAST((h2)), link); \
353  APR_RING_INIT((h2), elem, link); \
354  } \
355  } while (0)
356 
364 #define APR_RING_UNSPLICE(ep1, epN, link) do { \
365  APR_RING_NEXT(APR_RING_PREV((ep1), link), link) = \
366  APR_RING_NEXT((epN), link); \
367  APR_RING_PREV(APR_RING_NEXT((epN), link), link) = \
368  APR_RING_PREV((ep1), link); \
369  } while (0)
370 
377 #define APR_RING_REMOVE(ep, link) \
378  APR_RING_UNSPLICE((ep), (ep), link)
379 
387 #define APR_RING_FOREACH(ep, head, elem, link) \
388  for (ep = APR_RING_FIRST(head); \
389  ep != APR_RING_SENTINEL(head, elem, link); \
390  ep = APR_RING_NEXT(ep, link))
391 
400 #define APR_RING_FOREACH_SAFE(ep1, ep2, head, elem, link) \
401  for (ep1 = APR_RING_FIRST(head), ep2 = APR_RING_NEXT(ep1, link); \
402  ep1 != APR_RING_SENTINEL(head, elem, link); \
403  ep1 = ep2, ep2 = APR_RING_NEXT(ep1, link))
404 
405 /* Debugging tools: */
406 
407 #ifdef APR_RING_DEBUG
408 #include <stdio.h>
409 #include <assert.h>
410 
411 #define APR_RING_CHECK_ONE(msg, ptr) \
412  fprintf(stderr, "*** %s %p\n", msg, ptr)
413 
414 #define APR_RING_CHECK(hp, elem, link, msg) \
415  APR_RING_CHECK_ELEM(APR_RING_SENTINEL(hp, elem, link), elem, link, msg)
416 
417 #define APR_RING_CHECK_ELEM(ep, elem, link, msg) do { \
418  struct elem *start = (ep); \
419  struct elem *here = start; \
420  fprintf(stderr, "*** ring check start -- %s\n", msg); \
421  do { \
422  fprintf(stderr, "\telem %p\n", here); \
423  fprintf(stderr, "\telem->next %p\n", \
424  APR_RING_NEXT(here, link)); \
425  fprintf(stderr, "\telem->prev %p\n", \
426  APR_RING_PREV(here, link)); \
427  fprintf(stderr, "\telem->next->prev %p\n", \
428  APR_RING_PREV(APR_RING_NEXT(here, link), link)); \
429  fprintf(stderr, "\telem->prev->next %p\n", \
430  APR_RING_NEXT(APR_RING_PREV(here, link), link)); \
431  if (APR_RING_PREV(APR_RING_NEXT(here, link), link) != here) { \
432  fprintf(stderr, "\t*** elem->next->prev != elem\n"); \
433  break; \
434  } \
435  if (APR_RING_NEXT(APR_RING_PREV(here, link), link) != here) { \
436  fprintf(stderr, "\t*** elem->prev->next != elem\n"); \
437  break; \
438  } \
439  here = APR_RING_NEXT(here, link); \
440  } while (here != start); \
441  fprintf(stderr, "*** ring check end\n"); \
442  } while (0)
443 
444 #define APR_RING_CHECK_CONSISTENCY(hp, elem, link) \
445  APR_RING_CHECK_ELEM_CONSISTENCY(APR_RING_SENTINEL(hp, elem, link),\
446  elem, link)
447 
448 #define APR_RING_CHECK_ELEM_CONSISTENCY(ep, elem, link) do { \
449  struct elem *start = (ep); \
450  struct elem *here = start; \
451  do { \
452  assert(APR_RING_PREV(APR_RING_NEXT(here, link), link) == here); \
453  assert(APR_RING_NEXT(APR_RING_PREV(here, link), link) == here); \
454  here = APR_RING_NEXT(here, link); \
455  } while (here != start); \
456  } while (0)
457 
458 #else
459 
465 #define APR_RING_CHECK_ONE(msg, ptr)
466 
476 #define APR_RING_CHECK(hp, elem, link, msg)
477 
486 #define APR_RING_CHECK_CONSISTENCY(hp, elem, link)
487 
497 #define APR_RING_CHECK_ELEM(ep, elem, link, msg)
498 
508 #define APR_RING_CHECK_ELEM_CONSISTENCY(ep, elem, link)
509 #endif
510 
513 #endif /* !APR_RING_H */
APR Miscellaneous library routines.