Add utility to calculate allocation size for array
This commit is contained in:
@@ -200,12 +200,16 @@ void _array_clear(GenericArray array, u64 item_size) {
|
|||||||
header->count = 0;
|
header->count = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
u64 _array_alloc_size(u64 capacity, u64 item_size) {
|
||||||
|
return sizeof(ArrayHeader) + item_size * capacity;
|
||||||
|
}
|
||||||
|
|
||||||
GenericArray _array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size, b8 fill) {
|
GenericArray _array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size, b8 fill) {
|
||||||
wapp_runtime_assert(allocator != NULL, "`allocator` should not be NULL");
|
wapp_runtime_assert(allocator != NULL, "`allocator` should not be NULL");
|
||||||
|
|
||||||
GenericArray output = NULL;
|
GenericArray output = NULL;
|
||||||
|
|
||||||
u64 allocation_size = sizeof(ArrayHeader) + item_size * capacity;
|
u64 allocation_size = _array_alloc_size(capacity, item_size);
|
||||||
ArrayHeader *header = wapp_mem_allocator_alloc(allocator, allocation_size);
|
ArrayHeader *header = wapp_mem_allocator_alloc(allocator, allocation_size);
|
||||||
if (!header) {
|
if (!header) {
|
||||||
goto RETURN_ARRAY_ALLOC;
|
goto RETURN_ARRAY_ALLOC;
|
||||||
|
|||||||
@@ -166,6 +166,7 @@ typedef Str8 *Str8Array;
|
|||||||
#define wapp_array_clear(TYPE, ARRAY) \
|
#define wapp_array_clear(TYPE, ARRAY) \
|
||||||
(_array_clear((GenericArray)ARRAY, \
|
(_array_clear((GenericArray)ARRAY, \
|
||||||
sizeof(TYPE)))
|
sizeof(TYPE)))
|
||||||
|
#define wapp_array_alloc_size(TYPE, CAPACITY) _array_alloc_size(CAPACITY, sizeof(TYPE))
|
||||||
|
|
||||||
typedef struct header ArrayHeader;
|
typedef struct header ArrayHeader;
|
||||||
struct header {
|
struct header {
|
||||||
@@ -189,6 +190,7 @@ GenericArray _array_extend_alloc(const Allocator *allocator, GenericArray dst, c
|
|||||||
GenericArray _array_copy_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src, u64 item_size);
|
GenericArray _array_copy_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src, u64 item_size);
|
||||||
void *_array_pop(GenericArray array, u64 item_size);
|
void *_array_pop(GenericArray array, u64 item_size);
|
||||||
void _array_clear(GenericArray array, u64 item_size);
|
void _array_clear(GenericArray array, u64 item_size);
|
||||||
|
u64 _array_alloc_size(u64 capacity, u64 item_size);
|
||||||
GenericArray _array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size, b8 fill);
|
GenericArray _array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size, b8 fill);
|
||||||
|
|
||||||
#ifdef WAPP_PLATFORM_CPP
|
#ifdef WAPP_PLATFORM_CPP
|
||||||
|
|||||||
Reference in New Issue
Block a user