This commit is contained in:
Neil 2021-08-29 13:27:11 -07:00
parent 29fe592108
commit d2bc0e161d
1 changed files with 12 additions and 13 deletions

View File

@ -2,7 +2,8 @@
`name` is an identifier prefix that satisfies `C` naming conventions when
mangled and `type` is defined tag-type associated therewith. When expanding
the array, resizing may be necessary and incurs amortised cost; any pointers
to this memory may become stale. @param{MIN_ARRAY_CAPACITY} Defaults to 3. */
to this memory may become stale.
@param[MIN_ARRAY_CAPACITY] Must be greater than 1; defaults to 3. */
#include <stdlib.h> /* size_t realloc free */
#include <errno.h> /* errno */
@ -10,7 +11,7 @@
#define MIN_ARRAY_IDLE { 0, 0, 0 }
#ifndef MIN_ARRAY_CAPACITY
#define MIN_ARRAY_CAPACITY 3
#define MIN_ARRAY_CAPACITY 3 /* > 1 */
#endif
#define MIN_ARRAY(name, type) \
struct name##_array { type *data; size_t size, capacity; }; \
@ -20,10 +21,9 @@ static void name##_array(struct name##_array *const a) \
/** Destroys `a` and returns it to idle. */ \
static void name##_array_(struct name##_array *const a) \
{ assert(a); free(a->data); name##_array(a); } \
/** Ensures `min_capacity` of `a`. @param[min_capacity] If zero, does nothing.
@return Success; otherwise, `errno` will be set.
@throws[ERANGE] Tried allocating more then can fit in an object or doesn't
follow POSIX. @throws[realloc] */ \
/** Ensures `min_capacity` of `a`. @return Success; otherwise, `errno` will be
set. @throws[ERANGE] Tried allocating more then can fit in an object or
doesn't follow POSIX. @throws[realloc] */ \
static int name##_array_reserve(struct name##_array *const a, \
const size_t min) { \
size_t c0; \
@ -53,9 +53,9 @@ static int name##_array_reserve(struct name##_array *const a, \
return 1; \
} \
/** The capacity of `a` will be increased to at least `n` elements beyond the
size. Invalidates any pointers in `a`.
@return The start of the buffered space at the back of the array. If `a` is
idle and `buffer` is zero, a null pointer is returned, otherwise null
size. It will invalidate pointers in `a` if `n` is greater than the buffer
space. @return The start of the buffered space at the back of the array. If
`a` is idle and `buffer` is zero, a null pointer is returned, otherwise null
indicates an error. @throws[realloc, ERANGE] */ \
static type *name##_array_buffer(struct name##_array *const a, \
const size_t n) { \
@ -65,10 +65,9 @@ static type *name##_array_buffer(struct name##_array *const a, \
? a->data + a->size : 0; \
} \
/** Adds `n` elements to the back of `a`. It will invalidate pointers in `a` if
`n` is greater than the buffer space.
@return A pointer to the elements. If `a` is idle and `n` is zero, a null
pointer will be returned, otherwise null indicates an error.
@throws[realloc, ERANGE] */ \
`n` is greater than the buffer space. @return A pointer to the elements. If
`a` is idle and `n` is zero, a null pointer will be returned, otherwise null
indicates an error. @throws[realloc, ERANGE] */ \
static type *name##_array_append(struct name##_array *const a, \
const size_t n) { \
type *buffer; \