-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathsc_queue.h
288 lines (260 loc) · 10.8 KB
/
sc_queue.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* BSD-3-Clause
*
* Copyright 2021 Ozan Tezcan
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SC_QUEUE_H
#define SC_QUEUE_H
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#define SC_QUEUE_VERSION "2.0.0"
#ifdef SC_HAVE_CONFIG_H
#include "config.h"
#else
#define sc_queue_calloc calloc
#define sc_queue_free free
#endif
#ifndef SC_QUEUE_MAX
#define SC_QUEUE_MAX (SIZE_MAX)
#endif
#define sc_queue_def(T, name) \
struct sc_queue_##name { \
bool oom; \
size_t cap; \
size_t first; \
size_t last; \
/* NOLINTNEXTLINE */ \
T *elems; \
}
#define sc_queue_expand(q) \
do { \
size_t _cap, _len, _off; \
size_t _pos = ((q)->last + 1) & ((q)->cap - 1); \
void *_dst, *_src; \
\
if (_pos == (q)->first) { \
if ((q)->cap > SC_QUEUE_MAX / 2ul) { \
(q)->oom = true; \
break; \
} \
_cap = (q)->cap * 2; \
_dst = sc_queue_calloc(_cap, sizeof(*((q)->elems))); \
if (_dst == NULL) { \
(q)->oom = true; \
break; \
} \
_len = ((q)->cap - (q)->first) * sizeof(*(q)->elems); \
_off = ((q)->first * sizeof(*((q)->elems))); \
_src = ((char *) (q)->elems) + _off; \
\
memcpy(_dst, _src, _len); \
memcpy(((char *) _dst) + _len, (q)->elems, _off); \
(q)->oom = false; \
(q)->last = (q)->cap - 1; \
(q)->first = 0; \
(q)->cap = _cap; \
sc_queue_free((q)->elems); \
(q)->elems = _dst; \
} \
} while (0)
/**
* Init queue. Call sc_queue_oom(q) to see if memory allocation succeeded.
* @param q queue
*/
#define sc_queue_init(q) \
do { \
(q)->oom = false; \
(q)->cap = 8; \
(q)->first = 0; \
(q)->last = 0; \
(q)->elems = sc_queue_calloc(1, sizeof(*(q)->elems) * 8); \
if ((q)->elems == NULL) { \
(q)->oom = true; \
} \
} while (0)
/**
* Destroy queue
* @param q queue
*/
#define sc_queue_term(q) \
do { \
sc_queue_free((q)->elems); \
(q)->elems = NULL; \
(q)->cap = 0; \
(q)->first = 0; \
(q)->last = 0; \
(q)->oom = false; \
} while (0)
/**
* @param q queue
* @return true if last add operation failed, false otherwise.
*/
#define sc_queue_oom(q) ((q)->oom)
/**
* @param q queue
* @return element count
*/
#define sc_queue_size(q) (((q)->last - (q)->first) & ((q)->cap - 1))
/**
* Clear the queue without deallocating underlying memory.
* @param q queue
*/
#define sc_queue_clear(q) \
do { \
(q)->first = 0; \
(q)->last = 0; \
(q)->oom = false; \
} while (0)
/**
* @param q queue
* @return true if queue is empty
*/
#define sc_queue_empty(q) (((q)->last == (q)->first))
/**
* @param q queue
* @return index of the first element. If queue is empty, result is undefined.
*/
#define sc_queue_first(q) ((q)->first)
/**
* @param q queue
* @return index of the last element. If queue is empty, result is undefined.
*/
#define sc_queue_last(q) ((q)->last)
/**
* @param q queue
* @param i index
* @return index of the next element after i, if i is the last element,
* result is undefined.
*/
#define sc_queue_next(q, i) (((i) + 1) & ((q)->cap - 1))
/**
* Returns element at index 'i', so regular loops are possible :
*
* for (size_t i = 0; i < sc_queue_size(q); i++) {
* printf("%d" \n, sc_queue_at(q, i));
* }
*
* @param q queue
* @return element at index i
*/
#define sc_queue_at(q, i) (q)->elems[(((q)->first) + (i)) & ((q)->cap - 1)]
/**
* @param q queue
* @return peek first element, if queue is empty, result is undefined
*/
#define sc_queue_peek_first(q) ((q)->elems[(q)->first])
/**
* @param q queue
* @return peek last element, if queue is empty, result is undefined
*/
#define sc_queue_peek_last(q) (q)->elems[((q)->last - 1) & ((q)->cap - 1)]
/**
* Call sc_queue_oom(q) after this function to check out of memory condition.
*
* @param q queue
* @param elem elem to be added at the end of the list
*/
#define sc_queue_add_last(q, elem) \
do { \
sc_queue_expand(q); \
if ((q)->oom) { \
break; \
} \
(q)->oom = false; \
(q)->elems[(q)->last] = elem; \
(q)->last = ((q)->last + 1) & ((q)->cap - 1); \
} while (0)
/**
* @param q queue
* @return delete the last element from the queue and return its value.
* If queue is empty, result is undefined.
*/
#define sc_queue_del_last(q) \
((q)->elems[((q)->last = ((q)->last - 1) & ((q)->cap - 1))])
/**
* Call sc_queue_oom(q) after this function to check out of memory condition.
*
* @param q queue.
* @param elem elem to be added at the head of the list.
*/
#define sc_queue_add_first(q, elem) \
do { \
sc_queue_expand(q); \
if ((q)->oom) { \
break; \
} \
(q)->oom = false; \
(q)->first = ((q)->first - 1) & ((q)->cap - 1); \
(q)->elems[(q)->first] = elem; \
} while (0)
static inline size_t sc_queue_inc_first(size_t *first, size_t cap)
{
size_t tmp = *first;
*first = (*first + 1) & (cap - 1);
return tmp;
}
/**
* @param q queue
* @return delete the first element from the queue and return its value.
* If queue is empty, result is undefined.
*/
#define sc_queue_del_first(q) \
(q)->elems[sc_queue_inc_first(&(q)->first, (q)->cap)]
/**
* For each loop,
*
* struct sc_queue_int queue;
* sc_queue_init(&queue, 4);"
*
* int elem;
* sc_queue_foreach(&queue, elem) {
* printf("Elem : %d \n, elem);
* }
*/
#define sc_queue_foreach(q, elem) \
for (size_t _k = 1, _i = sc_queue_first(q); \
_k && _i != sc_queue_last(q); \
_k = !_k, _i = sc_queue_next(q, _i)) \
for ((elem) = (q)->elems[_i]; _k; _k = !_k)
// (type, name)
sc_queue_def(int, int);
sc_queue_def(unsigned int, uint);
sc_queue_def(long, long);
sc_queue_def(long long, ll);
sc_queue_def(unsigned long, ulong);
sc_queue_def(unsigned long long, ull);
sc_queue_def(uint32_t, 32);
sc_queue_def(uint64_t, 64);
sc_queue_def(double, double);
sc_queue_def(const char *, str);
sc_queue_def(void *, ptr);
#endif