Inproved.
This commit is contained in:
parent
5821f30362
commit
6eafdf7755
@ -2,7 +2,7 @@
|
|||||||
`name` is an identifier prefix that satisfies `C` naming conventions when
|
`name` is an identifier prefix that satisfies `C` naming conventions when
|
||||||
mangled and `type` is defined tag-type associated therewith. When expanding
|
mangled and `type` is defined tag-type associated therewith. When expanding
|
||||||
the array, resizing may be necessary and incurs amortised cost; any pointers
|
the array, resizing may be necessary and incurs amortised cost; any pointers
|
||||||
to this memory may become stale. */
|
to this memory may become stale. @param{MIN_ARRAY_CAPACITY} Defaults to 3. */
|
||||||
|
|
||||||
#include <stdlib.h> /* size_t realloc free */
|
#include <stdlib.h> /* size_t realloc free */
|
||||||
#include <string.h> /* memmove strcmp memcpy */
|
#include <string.h> /* memmove strcmp memcpy */
|
||||||
@ -10,7 +10,9 @@
|
|||||||
#include <assert.h> /* assert */
|
#include <assert.h> /* assert */
|
||||||
|
|
||||||
#define MIN_ARRAY_IDLE { 0, 0, 0 }
|
#define MIN_ARRAY_IDLE { 0, 0, 0 }
|
||||||
|
#ifndef MIN_ARRAY_CAPACITY
|
||||||
#define MIN_ARRAY_CAPACITY 3
|
#define MIN_ARRAY_CAPACITY 3
|
||||||
|
#endif
|
||||||
#define MIN_ARRAY(name, type) \
|
#define MIN_ARRAY(name, type) \
|
||||||
struct name##_array { type *data; size_t size, capacity; }; \
|
struct name##_array { type *data; size_t size, capacity; }; \
|
||||||
/** Initialises `a` to idle. */ \
|
/** Initialises `a` to idle. */ \
|
||||||
@ -20,9 +22,9 @@ static void name##_array(struct name##_array *const a) \
|
|||||||
static void name##_array_(struct name##_array *const a) \
|
static void name##_array_(struct name##_array *const a) \
|
||||||
{ assert(a); free(a->data); name##_array(a); } \
|
{ assert(a); free(a->data); name##_array(a); } \
|
||||||
/** Ensures `min_capacity` of `a`. @param[min_capacity] If zero, does nothing.
|
/** Ensures `min_capacity` of `a`. @param[min_capacity] If zero, does nothing.
|
||||||
@return Success; otherwise, `errno` will be set.
|
@return Success; otherwise, `errno` will be set.
|
||||||
@throws[ERANGE] Tried allocating more then can fit in `size_t` or `realloc`
|
@throws[ERANGE] Tried allocating more then can fit in an object or doesn't
|
||||||
doesn't follow POSIX. @throws[realloc, ERANGE] */ \
|
follow POSIX. @throws[realloc] */ \
|
||||||
static int name##_array_reserve(struct name##_array *const a, \
|
static int name##_array_reserve(struct name##_array *const a, \
|
||||||
const size_t min) { \
|
const size_t min) { \
|
||||||
size_t c0; \
|
size_t c0; \
|
||||||
@ -71,7 +73,6 @@ static type *name##_array_buffer(struct name##_array *const a, \
|
|||||||
static type *name##_array_append(struct name##_array *const a, \
|
static type *name##_array_append(struct name##_array *const a, \
|
||||||
const size_t n) { \
|
const size_t n) { \
|
||||||
type *buffer; \
|
type *buffer; \
|
||||||
assert(a); \
|
|
||||||
if(!(buffer = name##_array_buffer(a, n))) return 0; \
|
if(!(buffer = name##_array_buffer(a, n))) return 0; \
|
||||||
assert(n <= a->capacity && a->size <= a->capacity - n); \
|
assert(n <= a->capacity && a->size <= a->capacity - n); \
|
||||||
return a->size += n, buffer; \
|
return a->size += n, buffer; \
|
||||||
@ -82,6 +83,5 @@ static type *name##_array_new(struct name##_array *const a) \
|
|||||||
{ return name##_array_append(a, 1); } \
|
{ return name##_array_append(a, 1); } \
|
||||||
/* It's perfectly valid that these functions are not used. */ \
|
/* It's perfectly valid that these functions are not used. */ \
|
||||||
static void name##_unused_coda(void); static void name##_unused(void) { \
|
static void name##_unused_coda(void); static void name##_unused(void) { \
|
||||||
name##_array(0); name##_array_buffer(0, 0); name##_array_append(0, 0); \
|
name##_array(0); name##_array_new(0); name##_unused_coda(); } \
|
||||||
name##_array_new(0); name##_unused_coda(); } \
|
|
||||||
static void name##_unused_coda(void) { name##_unused(); }
|
static void name##_unused_coda(void) { name##_unused(); }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user