16#define restrict __restrict__
34#define PASTE(a, b) a##b
42#define XPASTE(a, b) PASTE(a, b)
50#define JOIN(a, b) XPASTE(a, XPASTE(_, b))
61#define IS_POW2(X) ((X) != 0 && ((X) & ((X) - 1)) == 0)
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); \
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); \
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]))
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]))
137#error "Must define NAME."
139#define FQUEUE_NAME NAME
150#define VALUE_TYPE int
151#define FUNCTION_DEFINITIONS
152#define TYPE_DEFINITIONS
153#error "Must define VALUE_TYPE."
160#ifndef FUNCTION_LINKAGE
161#define FUNCTION_LINKAGE
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)
181#ifdef TYPE_DEFINITIONS
206FUNCTION_LINKAGE FQUEUE_TYPE *
JOIN(FQUEUE_NAME, init)(FQUEUE_TYPE *self,
const uint32_t pow2_capacity);
221 create_custom)(
const uint32_t min_capacity,
void *context_ptr,
222 void *(*allocate)(
void *context_ptr,
size_t alignment,
size_t size));
246 void (*deallocate)(
void *context_ptr,
void *mem));
345FUNCTION_LINKAGE void JOIN(FQUEUE_NAME, copy)(FQUEUE_TYPE *restrict dest_ptr,
const FQUEUE_TYPE *restrict src_ptr);
355#ifdef FUNCTION_DEFINITIONS
362#include "round_up_pow2_32.h"
367 assert(
IS_POW2(pow2_capacity));
369 self->begin_index = self->end_index = 0;
371 self->capacity = pow2_capacity;
377 create_custom)(
const uint32_t min_capacity,
void *context_ptr,
378 void *(*allocate)(
void *context_ptr,
size_t alignment,
size_t size))
380 if (min_capacity == 0 || min_capacity > UINT32_MAX / 2 + 1) {
384 const uint32_t capacity = round_up_pow2_32(min_capacity);
392 FQUEUE_TYPE *self = (FQUEUE_TYPE *)allocate(context_ptr,
alignof(FQUEUE_TYPE), size);
398 memset(self, 0, size);
399 FQUEUE_INIT(self, capacity);
405static inline void *
JOIN(internal,
JOIN(FQUEUE_NAME, allocate))(
void *context_ptr,
size_t alignment,
size_t size)
415 return JOIN(FQUEUE_NAME, create_custom)(capacity, NULL,
JOIN(internal,
JOIN(FQUEUE_NAME, allocate)));
419 void (*deallocate)(
void *context_ptr,
void *mem))
421 assert(self != NULL);
423 deallocate(context_ptr, self);
427static inline void JOIN(internal,
JOIN(FQUEUE_NAME, deallocate))(
void *context_ptr,
void *mem)
436 assert(self != NULL);
438 JOIN(FQUEUE_NAME, destroy_custom)(self, NULL,
JOIN(internal,
JOIN(FQUEUE_NAME, deallocate)));
443 assert(self != NULL);
445 return self->count == 0;
450 assert(self != NULL);
452 return self->count == self->capacity;
457 assert(self != NULL);
458 assert(index < self->count);
460 const uint32_t index_mask = (self->capacity - 1);
462 return self->values[(self->begin_index + index) & index_mask];
467 assert(self != NULL);
468 assert(!FQUEUE_IS_EMPTY(self));
470 return self->values[self->begin_index];
475 assert(self != NULL);
476 assert(!FQUEUE_IS_EMPTY(self));
478 const uint32_t index_mask = (self->capacity - 1);
480 return self->values[(self->end_index - 1) & index_mask];
485 return JOIN(FQUEUE_NAME, get_front)(self);
490 assert(self != NULL);
491 assert(!FQUEUE_IS_FULL(self));
493 const uint32_t index_mask = (self->capacity - 1);
495 self->values[self->end_index] = value;
497 self->end_index &= index_mask;
505 assert(self != NULL);
506 assert(!FQUEUE_IS_EMPTY(self));
508 const uint32_t index_mask = (self->capacity - 1);
510 const VALUE_TYPE value = self->values[self->begin_index];
512 self->begin_index &= index_mask;
520 assert(self != NULL);
523 self->begin_index = self->end_index = 0;
526FUNCTION_LINKAGE void JOIN(FQUEUE_NAME, copy)(FQUEUE_TYPE *restrict dest_ptr,
const FQUEUE_TYPE *restrict src_ptr)
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));
533 const uint32_t src_begin_index = src_ptr->begin_index;
534 const uint32_t src_index_mask = src_ptr->capacity - 1;
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];
540 dest_ptr->count = src_ptr->count;
541 dest_ptr->begin_index = 0;
542 dest_ptr->end_index = src_ptr->count;
553#undef FUNCTION_LINKAGE
554#undef FUNCTION_DEFINITIONS
555#undef TYPE_DEFINITIONS
559#undef FQUEUE_CALC_SIZEOF
561#undef FQUEUE_IS_EMPTY
#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