All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
as_queue.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2015 Aerospike, Inc.
3  *
4  * Portions may be licensed to Aerospike, Inc. under one or more contributor
5  * license agreements.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8  * use this file except in compliance with the License. You may obtain a copy of
9  * the License at http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations under
15  * the License.
16  */
17 #pragma once
18 
19 #include <stdint.h>
20 #include <stddef.h>
21 #include <stdbool.h>
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /******************************************************************************
28  * TYPES
29  ******************************************************************************/
30 
31 /**
32  * A fast, non-thread-safe dynamic queue implementation.
33  * as_queue is not part of the generic as_val family.
34  */
35 typedef struct as_queue_s {
36  /**
37  * The block of items in the queue.
38  */
39  uint8_t* data;
40 
41  /**
42  * The total number of items allocated.
43  */
44  uint32_t capacity;
45 
46  /**
47  * Item offset of head.
48  */
49  uint32_t head;
50 
51  /**
52  * Item offset of tail.
53  */
54  uint32_t tail;
55 
56  /**
57  * The size of a single item.
58  */
59  uint32_t item_size;
60 
61  /**
62  * Internal queue flags.
63  */
64  uint32_t flags;
65 } as_queue;
66 
67 /******************************************************************************
68  * MACROS
69  ******************************************************************************/
70 
71 /**
72  * Initialize a stack allocated as_queue, with item storage on the stack.
73  * as_queue_inita() will transfer stack memory to the heap if a resize is
74  * required.
75  */
76 #define as_queue_inita(__q, __item_size, __capacity)\
77 (__q)->data = alloca((__capacity) * (__item_size));\
78 (__q)->capacity = __capacity;\
79 (__q)->head = (__q)->tail = 0;\
80 (__q)->item_size = __item_size;\
81 (__q)->flags = 0;
82 
83 /******************************************************************************
84  * FUNCTIONS
85  ******************************************************************************/
86 
87 /**
88  * Initialize a stack allocated as_queue, with item storage on the heap.
89  */
90 bool
91 as_queue_init(as_queue* queue, uint32_t item_size, uint32_t capacity);
92 
93 /**
94  * Create a heap allocated as_queue, with item storage on the heap.
95  */
96 as_queue*
97 as_queue_create(uint32_t item_size, uint32_t capacity);
98 
99 /**
100  * Release queue memory.
101  */
102 void
103 as_queue_destroy(as_queue* queue);
104 
105 /**
106  * Get the number of elements currently in the queue.
107  */
108 static inline uint32_t
110 {
111  return queue->tail - queue->head;
112 }
113 
114 /**
115  * Is queue empty?
116  */
117 static inline bool
119 {
120  return queue->tail == queue->head;
121 }
122 
123 /**
124  * Push to the tail of the queue.
125  */
126 bool
127 as_queue_push(as_queue* queue, const void* ptr);
128 
129 /**
130  * Push element on the queue only if size < capacity.
131  */
132 bool
133 as_queue_push_limit(as_queue* queue, const void* ptr);
134 
135 /**
136  * Push to the front of the queue.
137  */
138 bool
139 as_queue_push_head(as_queue* queue, const void* ptr);
140 
141 /**
142  * Pop from the head of the queue.
143  */
144 bool
145 as_queue_pop(as_queue* queue, void* ptr);
146 
147 #ifdef __cplusplus
148 } // end extern "C"
149 #endif