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-2017 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  * Total items used which includes items in queue and items popped from queue.
63  */
64  uint32_t total;
65 
66  /**
67  * Internal queue flags.
68  */
69  uint32_t flags;
70 } as_queue;
71 
72 /******************************************************************************
73  * MACROS
74  ******************************************************************************/
75 
76 /**
77  * Initialize a stack allocated as_queue, with item storage on the stack.
78  * as_queue_inita() will transfer stack memory to the heap if a resize is
79  * required.
80  */
81 #define as_queue_inita(__q, __item_size, __capacity)\
82 (__q)->data = alloca((__capacity) * (__item_size));\
83 (__q)->capacity = __capacity;\
84 (__q)->head = (__q)->tail = 0;\
85 (__q)->item_size = __item_size;\
86 (__q)->total = 0;\
87 (__q)->flags = 0;
88 
89 /******************************************************************************
90  * FUNCTIONS
91  ******************************************************************************/
92 
93 /**
94  * Initialize a stack allocated as_queue, with item storage on the heap.
95  */
96 bool
97 as_queue_init(as_queue* queue, uint32_t item_size, uint32_t capacity);
98 
99 /**
100  * Create a heap allocated as_queue, with item storage on the heap.
101  */
102 as_queue*
103 as_queue_create(uint32_t item_size, uint32_t capacity);
104 
105 /**
106  * Release queue memory.
107  */
108 void
109 as_queue_destroy(as_queue* queue);
110 
111 /**
112  * Get the number of elements currently in the queue.
113  */
114 static inline uint32_t
116 {
117  return queue->tail - queue->head;
118 }
119 
120 /**
121  * Is queue empty?
122  */
123 static inline bool
125 {
126  return queue->tail == queue->head;
127 }
128 
129 /**
130  * Push to the tail of the queue.
131  */
132 bool
133 as_queue_push(as_queue* queue, const void* ptr);
134 
135 /**
136  * Push element on the queue only if size < capacity.
137  */
138 bool
139 as_queue_push_limit(as_queue* queue, const void* ptr);
140 
141 /**
142  * Push to the front of the queue.
143  */
144 bool
145 as_queue_push_head(as_queue* queue, const void* ptr);
146 
147 /**
148  * Pop from the head of the queue.
149  */
150 bool
151 as_queue_pop(as_queue* queue, void* ptr);
152 
153 /**
154  * Increment total counter if within capacity.
155  */
156 static inline bool
158 {
159  if (queue->total < queue->capacity) {
160  queue->total++;
161  return true;
162  }
163  return false;
164 }
165 
166 /**
167  * Decrement total counter.
168  */
169 static inline void
171 {
172  queue->total--;
173 }
174 
175 #ifdef __cplusplus
176 } // end extern "C"
177 #endif
bool as_queue_push_limit(as_queue *queue, const void *ptr)
void as_queue_destroy(as_queue *queue)
uint32_t tail
Definition: as_queue.h:54
bool as_queue_pop(as_queue *queue, void *ptr)
as_queue * as_queue_create(uint32_t item_size, uint32_t capacity)
uint32_t head
Definition: as_queue.h:49
uint8_t * data
Definition: as_queue.h:39
uint32_t item_size
Definition: as_queue.h:59
uint32_t capacity
Definition: as_queue.h:44
static uint32_t as_queue_size(as_queue *queue)
Definition: as_queue.h:115
bool as_queue_push(as_queue *queue, const void *ptr)
uint32_t total
Definition: as_queue.h:64
uint32_t flags
Definition: as_queue.h:69
bool as_queue_push_head(as_queue *queue, const void *ptr)
bool as_queue_init(as_queue *queue, uint32_t item_size, uint32_t capacity)
static bool as_queue_empty(as_queue *queue)
Definition: as_queue.h:124
static bool as_queue_incr_total(as_queue *queue)
Definition: as_queue.h:157
static void as_queue_decr_total(as_queue *queue)
Definition: as_queue.h:170