data-structures-c
Loading...
Searching...
No Matches
fqueue_template.h
Go to the documentation of this file.
1// Copyright (c) 2026 abxh
2// SPDX-License-Identifier: MIT
3
8
13
14#ifdef __cplusplus
15#ifdef __GNUC__
16#define restrict __restrict__
17#else
18#define restrict
19#endif
20extern "C" {
21#endif
22
23#include <stdbool.h>
24#include <stddef.h>
25#include <stdint.h>
26
27// macro definitions: {{{
28
33#ifndef PASTE
34#define PASTE(a, b) a##b
35#endif
36
41#ifndef XPASTE
42#define XPASTE(a, b) PASTE(a, b)
43#endif
44
49#ifndef JOIN
50#define JOIN(a, b) XPASTE(a, XPASTE(_, b))
51#endif
52
60#ifndef IS_POW2
61#define IS_POW2(X) ((X) != 0 && ((X) & ((X) - 1)) == 0)
62#endif
63
74#ifndef FQUEUE_FOR_EACH
75#define FQUEUE_FOR_EACH(self, index, value) \
76 for ((index) = 0; (index) < (self)->count \
77 && ((value) = (self)->values[((self)->begin_index + (index)) & ((self)->capacity - 1)], true); \
78 (index)++)
79#endif
80
91#ifndef FQUEUE_FOR_EACH_REVERSE
92#define FQUEUE_FOR_EACH_REVERSE(self, index, value) \
93 for ((index) = 0; (index) < (self)->count \
94 && ((value) = (self)->values[((self)->end_index - 1 - (index)) & ((self)->capacity - 1)], true); \
95 (index)++)
96#endif
97
108#ifndef FQUEUE_CALC_SIZEOF
109#define FQUEUE_CALC_SIZEOF(fqueue_name, capacity) \
110 (uint32_t)(offsetof(struct fqueue_name, values) + capacity * sizeof(((struct fqueue_name *)0)->values[0]))
111#endif
112
123#ifndef FQUEUE_CALC_SIZEOF_OVERFLOWS
124#define FQUEUE_CALC_SIZEOF_OVERFLOWS(fqueue_name, capacity) \
125 (capacity > (UINT32_MAX - offsetof(struct fqueue_name, values)) / sizeof(((struct fqueue_name *)0)->values[0]))
126#endif
127
135#ifndef NAME
136#define NAME fqueue
137#error "Must define NAME."
138#else
139#define FQUEUE_NAME NAME
140#endif
141
149#ifndef VALUE_TYPE
150#define VALUE_TYPE int
151#define FUNCTION_DEFINITIONS
152#define TYPE_DEFINITIONS
153#error "Must define VALUE_TYPE."
154#endif
155
160#ifndef FUNCTION_LINKAGE
161#define FUNCTION_LINKAGE
162#endif
163
165#define FQUEUE_TYPE struct FQUEUE_NAME
166#define FQUEUE_INIT JOIN(FQUEUE_NAME, init)
167#define FQUEUE_IS_EMPTY JOIN(FQUEUE_NAME, is_empty)
168#define FQUEUE_IS_FULL JOIN(FQUEUE_NAME, is_full)
170
171// }}}
172
173// type definitions: {{{
174
175struct FQUEUE_NAME;
176
181#ifdef TYPE_DEFINITIONS
182
186struct FQUEUE_NAME {
187 uint32_t begin_index;
188 uint32_t end_index;
189 uint32_t count;
190 uint32_t capacity;
192};
193
194#endif
195
196// }}}
197
198// function declarations: {{{
199
206FUNCTION_LINKAGE FQUEUE_TYPE *JOIN(FQUEUE_NAME, init)(FQUEUE_TYPE *self, const uint32_t pow2_capacity);
207
220FUNCTION_LINKAGE FQUEUE_TYPE *JOIN(FQUEUE_NAME,
221 create_custom)(const uint32_t min_capacity, void *context_ptr,
222 void *(*allocate)(void *context_ptr, size_t alignment, size_t size));
223
234FUNCTION_LINKAGE FQUEUE_TYPE *JOIN(FQUEUE_NAME, create)(const uint32_t min_capacity);
235
245FUNCTION_LINKAGE void JOIN(FQUEUE_NAME, destroy_custom)(FQUEUE_TYPE *self, void *context_ptr,
246 void (*deallocate)(void *context_ptr, void *mem));
247
255FUNCTION_LINKAGE void JOIN(FQUEUE_NAME, destroy)(FQUEUE_TYPE *self);
256
264FUNCTION_LINKAGE bool JOIN(FQUEUE_NAME, is_empty)(const FQUEUE_TYPE *self);
265
273FUNCTION_LINKAGE bool JOIN(FQUEUE_NAME, is_full)(const FQUEUE_TYPE *self);
274
286FUNCTION_LINKAGE VALUE_TYPE JOIN(FQUEUE_NAME, at)(const FQUEUE_TYPE *self, const uint32_t index);
287
295FUNCTION_LINKAGE VALUE_TYPE JOIN(FQUEUE_NAME, get_front)(const FQUEUE_TYPE *self);
296
304FUNCTION_LINKAGE VALUE_TYPE JOIN(FQUEUE_NAME, get_back)(const FQUEUE_TYPE *self);
305
313FUNCTION_LINKAGE VALUE_TYPE JOIN(FQUEUE_NAME, peek)(const FQUEUE_TYPE *self);
314
321FUNCTION_LINKAGE bool JOIN(FQUEUE_NAME, enqueue)(FQUEUE_TYPE *self, const VALUE_TYPE value);
322
330FUNCTION_LINKAGE VALUE_TYPE JOIN(FQUEUE_NAME, dequeue)(FQUEUE_TYPE *self);
331
337FUNCTION_LINKAGE void JOIN(FQUEUE_NAME, clear)(FQUEUE_TYPE *self);
338
345FUNCTION_LINKAGE void JOIN(FQUEUE_NAME, copy)(FQUEUE_TYPE *restrict dest_ptr, const FQUEUE_TYPE *restrict src_ptr);
346
347// }}}
348
349// function definitions: {{{
350
355#ifdef FUNCTION_DEFINITIONS
356
357#include <assert.h>
358#include <stdalign.h>
359#include <stdlib.h>
360#include <string.h>
361
362#include "round_up_pow2_32.h" // round_up_pow2_32
363
364FUNCTION_LINKAGE FQUEUE_TYPE *JOIN(FQUEUE_NAME, init)(FQUEUE_TYPE *self, const uint32_t pow2_capacity)
365{
366 assert(self);
367 assert(IS_POW2(pow2_capacity));
368
369 self->begin_index = self->end_index = 0;
370 self->count = 0;
371 self->capacity = pow2_capacity;
372
373 return self;
374}
375
376FUNCTION_LINKAGE FQUEUE_TYPE *JOIN(FQUEUE_NAME,
377 create_custom)(const uint32_t min_capacity, void *context_ptr,
378 void *(*allocate)(void *context_ptr, size_t alignment, size_t size))
379{
380 if (min_capacity == 0 || min_capacity > UINT32_MAX / 2 + 1) {
381 return NULL;
382 }
383
384 const uint32_t capacity = round_up_pow2_32(min_capacity);
385
386 if (FQUEUE_CALC_SIZEOF_OVERFLOWS(FQUEUE_NAME, capacity)) {
387 return NULL;
388 }
389
390 const uint32_t size = FQUEUE_CALC_SIZEOF(FQUEUE_NAME, capacity);
391
392 FQUEUE_TYPE *self = (FQUEUE_TYPE *)allocate(context_ptr, alignof(FQUEUE_TYPE), size);
393
394 if (!self) {
395 return NULL;
396 }
397
398 memset(self, 0, size);
399 FQUEUE_INIT(self, capacity);
400
401 return self;
402}
403
405static inline void *JOIN(internal, JOIN(FQUEUE_NAME, allocate))(void *context_ptr, size_t alignment, size_t size)
406{
407 (void)context_ptr;
408 (void)alignment;
409 return malloc(size);
410}
412
413FUNCTION_LINKAGE FQUEUE_TYPE *JOIN(FQUEUE_NAME, create)(const uint32_t capacity)
414{
415 return JOIN(FQUEUE_NAME, create_custom)(capacity, NULL, JOIN(internal, JOIN(FQUEUE_NAME, allocate)));
416}
417
418FUNCTION_LINKAGE void JOIN(FQUEUE_NAME, destroy_custom)(FQUEUE_TYPE *self, void *context_ptr,
419 void (*deallocate)(void *context_ptr, void *mem))
420{
421 assert(self != NULL);
422
423 deallocate(context_ptr, self);
424}
425
427static inline void JOIN(internal, JOIN(FQUEUE_NAME, deallocate))(void *context_ptr, void *mem)
428{
429 (void)context_ptr;
430 free(mem);
431}
433
434FUNCTION_LINKAGE void JOIN(FQUEUE_NAME, destroy)(FQUEUE_TYPE *self)
435{
436 assert(self != NULL);
437
438 JOIN(FQUEUE_NAME, destroy_custom)(self, NULL, JOIN(internal, JOIN(FQUEUE_NAME, deallocate)));
439}
440
441FUNCTION_LINKAGE bool JOIN(FQUEUE_NAME, is_empty)(const FQUEUE_TYPE *self)
442{
443 assert(self != NULL);
444
445 return self->count == 0;
446}
447
448FUNCTION_LINKAGE bool JOIN(FQUEUE_NAME, is_full)(const FQUEUE_TYPE *self)
449{
450 assert(self != NULL);
451
452 return self->count == self->capacity;
453}
454
455FUNCTION_LINKAGE VALUE_TYPE JOIN(FQUEUE_NAME, at)(const FQUEUE_TYPE *self, const uint32_t index)
456{
457 assert(self != NULL);
458 assert(index < self->count);
459
460 const uint32_t index_mask = (self->capacity - 1);
461
462 return self->values[(self->begin_index + index) & index_mask];
463}
464
465FUNCTION_LINKAGE VALUE_TYPE JOIN(FQUEUE_NAME, get_front)(const FQUEUE_TYPE *self)
466{
467 assert(self != NULL);
468 assert(!FQUEUE_IS_EMPTY(self));
469
470 return self->values[self->begin_index];
471}
472
473FUNCTION_LINKAGE VALUE_TYPE JOIN(FQUEUE_NAME, get_back)(const FQUEUE_TYPE *self)
474{
475 assert(self != NULL);
476 assert(!FQUEUE_IS_EMPTY(self));
477
478 const uint32_t index_mask = (self->capacity - 1);
479
480 return self->values[(self->end_index - 1) & index_mask];
481}
482
483FUNCTION_LINKAGE VALUE_TYPE JOIN(FQUEUE_NAME, peek)(const FQUEUE_TYPE *self)
484{
485 return JOIN(FQUEUE_NAME, get_front)(self);
486}
487
488FUNCTION_LINKAGE bool JOIN(FQUEUE_NAME, enqueue)(FQUEUE_TYPE *self, const VALUE_TYPE value)
489{
490 assert(self != NULL);
491 assert(!FQUEUE_IS_FULL(self));
492
493 const uint32_t index_mask = (self->capacity - 1);
494
495 self->values[self->end_index] = value;
496 self->end_index++;
497 self->end_index &= index_mask;
498 self->count++;
499
500 return true;
501}
502
503FUNCTION_LINKAGE VALUE_TYPE JOIN(FQUEUE_NAME, dequeue)(FQUEUE_TYPE *self)
504{
505 assert(self != NULL);
506 assert(!FQUEUE_IS_EMPTY(self));
507
508 const uint32_t index_mask = (self->capacity - 1);
509
510 const VALUE_TYPE value = self->values[self->begin_index];
511 self->begin_index++;
512 self->begin_index &= index_mask;
513 self->count--;
514
515 return value;
516}
517
518FUNCTION_LINKAGE void JOIN(FQUEUE_NAME, clear)(FQUEUE_TYPE *self)
519{
520 assert(self != NULL);
521
522 self->count = 0;
523 self->begin_index = self->end_index = 0;
524}
525
526FUNCTION_LINKAGE void JOIN(FQUEUE_NAME, copy)(FQUEUE_TYPE *restrict dest_ptr, const FQUEUE_TYPE *restrict src_ptr)
527{
528 assert(src_ptr != NULL);
529 assert(dest_ptr != NULL);
530 assert(src_ptr->count <= dest_ptr->capacity);
531 assert(FQUEUE_IS_EMPTY(dest_ptr));
532
533 const uint32_t src_begin_index = src_ptr->begin_index;
534 const uint32_t src_index_mask = src_ptr->capacity - 1;
535
536 for (uint32_t i = 0; i < src_ptr->count; i++) {
537 dest_ptr->values[i] = src_ptr->values[(src_begin_index + i) & src_index_mask];
538 }
539
540 dest_ptr->count = src_ptr->count;
541 dest_ptr->begin_index = 0;
542 dest_ptr->end_index = src_ptr->count;
543}
544
545#endif
546
547// }}}
548
549// macro undefs: {{{
550
551#undef NAME
552#undef VALUE_TYPE
553#undef FUNCTION_LINKAGE
554#undef FUNCTION_DEFINITIONS
555#undef TYPE_DEFINITIONS
556
557#undef FQUEUE_NAME
558#undef FQUEUE_TYPE
559#undef FQUEUE_CALC_SIZEOF
560#undef FQUEUE_INIT
561#undef FQUEUE_IS_EMPTY
562#undef FQUEUE_IS_FULL
563
564// }}}
565
566#ifdef __cplusplus
567}
568#endif
569
570// vim: ft=c fdm=marker
#define JOIN(a, b)
First expand tokens, then paste them together with a _ in between.
Definition fqueue_template.h:50
#define IS_POW2(X)
Macro to check if a number is a power of two.
Definition fqueue_template.h:61
#define FQUEUE_CALC_SIZEOF(fqueue_name, capacity)
Calculate the size of the queue struct. No overflow checks.
Definition fqueue_template.h:109
#define FQUEUE_CALC_SIZEOF_OVERFLOWS(fqueue_name, capacity)
Check for a given capacity, if the equivalent size of the queue struct overflows.
Definition fqueue_template.h:124
#define VALUE_TYPE
Queue value type. This must be manually defined before including this header file.
Definition fqueue_template.h:150
#define FUNCTION_LINKAGE
Specify function linkage e.g. static inline.
Definition fstack_template.h:146
#define VALUE_TYPE
Stack value type. This must be manually defined before including this header file.
Definition fstack_template.h:135
uint32_t count
Number of values.
Definition fqueue_template.h:189
uint32_t end_index
Index used to track the back of the queue.
Definition fqueue_template.h:188
VALUE_TYPE values[]
Array of values.
Definition fqueue_template.h:191
uint32_t begin_index
Index used to track the front of the queue.
Definition fqueue_template.h:187
uint32_t capacity
Maximum number of values allocated for.
Definition fqueue_template.h:190