]> git.ozlabs.org Git - ccan/blob - ccan/ccan_tokenizer/queue.h
failtest: don't insist parents and children write the same thing to files.
[ccan] / ccan / ccan_tokenizer / queue.h
1 /*
2         Copyright (c) 2009  Joseph A. Adams
3         All rights reserved.
4         
5         Redistribution and use in source and binary forms, with or without
6         modification, are permitted provided that the following conditions
7         are met:
8         1. Redistributions of source code must retain the above copyright
9            notice, this list of conditions and the following disclaimer.
10         2. Redistributions in binary form must reproduce the above copyright
11            notice, this list of conditions and the following disclaimer in the
12            documentation and/or other materials provided with the distribution.
13         3. The name of the author may not be used to endorse or promote products
14            derived from this software without specific prior written permission.
15         
16         THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17         IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18         OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19         IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20         INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21         NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22         DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23         THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24         (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25         THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #ifndef CCAN_QUEUE_H
29 #define CCAN_QUEUE_H
30
31 #include <stdint.h>
32 #include <ccan/talloc/talloc.h>
33
34 #ifndef HAVE_ATTRIBUTE_MAY_ALIAS
35 #define HAVE_ATTRIBUTE_MAY_ALIAS 1
36 #endif
37
38 #if HAVE_ATTRIBUTE_MAY_ALIAS==1
39 #define queue_alias(ptr) /* nothing */
40 #define queue(type) struct {size_t head, tail, flag; type *item;} __attribute__((__may_alias__))
41 #else
42 #define queue_alias(ptr) qsort(ptr, 0, 1, queue_alias_helper) //hack
43 #define queue(type) struct {size_t head, tail, flag; type *item;}
44 #endif
45
46 int queue_alias_helper(const void *a, const void *b);
47
48 #define queue_init(queue, ctx) do {(queue).head = (queue).tail = 0; (queue).flag = 3; (queue).item = talloc_size(ctx, sizeof(*(queue).item)*4);} while(0)
49 #define queue_free(queue) do {talloc_free((queue).item);} while(0)
50
51 #define queue_count(queue) (((queue).tail-(queue).head) & (queue).flag)
52 #define enqueue(queue, ...) \
53         do { \
54                 (queue).item[(queue).tail++] = (__VA_ARGS__); \
55                 (queue).tail &= (queue).flag; \
56                 if ((queue).tail == (queue).head) { \
57                         queue_enqueue_helper(&(queue), sizeof(*(queue).item)); \
58                         queue_alias(&(queue)); \
59                 } \
60         } while(0)
61 #define dequeue_check(queue) ((queue).head != (queue).tail ? dequeue(queue) : NULL)
62 #define dequeue(queue) ((queue).item[queue_dequeue_helper(&(queue).head, (queue).flag)])
63
64 //TODO:  Test us
65 #define queue_next(queue) ((queue).item[(queue).head])
66 #define queue_item(queue, pos) ((queue).item[((queue).head+(pos)) & (queue).flag])
67 #define queue_skip(queue) do {(queue).head++; (queue).head &= (queue).flag;} while(0)
68
69 void queue_enqueue_helper(void *qp, size_t itemSize);
70
71 static inline size_t queue_dequeue_helper(size_t *head, size_t flag) {
72         size_t ret = (*head)++;
73         *head &= flag;
74         return ret;
75 }
76
77 #endif