From 65890dd2fb12e6751bb747221364ed9234f6b543 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Mon, 17 Feb 2020 20:27:43 +0900 Subject: [PATCH] Add a fixed-array allocator Turned out to be very convenient. --- include/QF/darray.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/include/QF/darray.h b/include/QF/darray.h index 0287f958f..7a4484575 100644 --- a/include/QF/darray.h +++ b/include/QF/darray.h @@ -67,6 +67,27 @@ ele_type *a; \ } +/** Allocate a fixed-size array using the given allocator + + The allocated array is initilized to be ungrowable, and with both size + and maxSize set to the given size. + + \param array_type Expression acceptable by typeof for determining the + type of the array. + \param array_size The size of the array. + \param alloc Allocator compatible with malloc (eg, alloca). +*/ +#define DARRAY_ALLOCFIXED(array_type, array_size, alloc) \ + ({ \ + __auto_type s = (array_size); \ + typeof (array_type) *ar = alloc (sizeof(*ar) \ + + s * sizeof (*ar->a)); \ + ar->size = ar->maxSize = s; \ + ar->grow = 0; \ + ar->a = (typeof (ar->a)) (ar + 1); \ + ar; \ + }) + /** Clear the array. If the array can grow, its backing will be freed and maxSize and a reset,