Add padding to stack array
This commit is contained in:
@@ -45,123 +45,126 @@ typedef iptr *IptrArray;
|
|||||||
typedef Str8 *Str8Array;
|
typedef Str8 *Str8Array;
|
||||||
|
|
||||||
#ifdef WAPP_PLATFORM_CPP
|
#ifdef WAPP_PLATFORM_CPP
|
||||||
#define wapp_array(TYPE, ...) ([&]() { \
|
#define wapp_array(TYPE, ...) ([&]() { \
|
||||||
u64 capacity = _calc_array_capacity(TYPE, __VA_ARGS__); \
|
u64 capacity = _calc_array_capacity(TYPE, __VA_ARGS__); \
|
||||||
\
|
\
|
||||||
TYPE items[_calc_array_capacity(TYPE, __VA_ARGS__)] = {__VA_ARGS__}; \
|
TYPE items[_calc_array_capacity(TYPE, __VA_ARGS__)] = {__VA_ARGS__}; \
|
||||||
\
|
\
|
||||||
wapp_persist u8 array[ \
|
wapp_persist u8 array[ \
|
||||||
sizeof(ArrayHeader) + _calc_array_capacity(TYPE, __VA_ARGS__) * sizeof(TYPE) \
|
sizeof(ArrayHeader) + _calc_array_capacity(TYPE, __VA_ARGS__) * sizeof(TYPE) \
|
||||||
] = {0}; \
|
] = {0}; \
|
||||||
ArrayHeader *header = (ArrayHeader *)array; \
|
ArrayHeader *header = (ArrayHeader *)array; \
|
||||||
header->magic = WAPP_ARRAY_MAGIC; \
|
header->magic = WAPP_ARRAY_MAGIC; \
|
||||||
header->count = _calc_array_count(TYPE, __VA_ARGS__); \
|
header->count = _calc_array_count(TYPE, __VA_ARGS__); \
|
||||||
header->capacity = _calc_array_capacity(TYPE, __VA_ARGS__); \
|
header->capacity = _calc_array_capacity(TYPE, __VA_ARGS__); \
|
||||||
header->item_size = sizeof(TYPE); \
|
header->item_size = sizeof(TYPE); \
|
||||||
\
|
\
|
||||||
u8 *buf = (u8 *)(header + 1); \
|
u8 *buf = (u8 *)(header + 1); \
|
||||||
memcpy(buf, items, capacity * sizeof(TYPE)); \
|
memcpy(buf, items, capacity * sizeof(TYPE)); \
|
||||||
return (TYPE *)buf; \
|
return (TYPE *)buf; \
|
||||||
}())
|
}())
|
||||||
#define wapp_array_with_capacity(TYPE, CAPACITY, FILL) ([&]() { \
|
#define wapp_array_with_capacity(TYPE, CAPACITY, FILL) ([&]() { \
|
||||||
wapp_persist u8 array[ \
|
wapp_persist u8 array[ \
|
||||||
sizeof(ArrayHeader) + CAPACITY * sizeof(TYPE) \
|
sizeof(ArrayHeader) + CAPACITY * sizeof(TYPE) \
|
||||||
] = {0}; \
|
] = {0}; \
|
||||||
ArrayHeader *header = (ArrayHeader *)array; \
|
ArrayHeader *header = (ArrayHeader *)array; \
|
||||||
header->magic = WAPP_ARRAY_MAGIC; \
|
header->magic = WAPP_ARRAY_MAGIC; \
|
||||||
header->count = FILL ? CAPACITY : 0; \
|
header->count = FILL ? CAPACITY : 0; \
|
||||||
header->capacity = CAPACITY; \
|
header->capacity = CAPACITY; \
|
||||||
header->item_size = sizeof(TYPE); \
|
header->item_size = sizeof(TYPE); \
|
||||||
\
|
\
|
||||||
return (TYPE *)(header + 1); \
|
return (TYPE *)(header + 1); \
|
||||||
}())
|
}())
|
||||||
#define wapp_array_pop(TYPE, ARRAY) ([&]() { \
|
#define wapp_array_pop(TYPE, ARRAY) ([&]() { \
|
||||||
if (ARRAY == NULL || _array_count((GenericArray)ARRAY) == 0) { \
|
if (ARRAY == NULL || _array_count((GenericArray)ARRAY) == 0) { \
|
||||||
TYPE result{}; \
|
TYPE result{}; \
|
||||||
return result; \
|
return result; \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
return *((TYPE *)_array_pop((GenericArray)ARRAY, sizeof(TYPE))); \
|
return *((TYPE *)_array_pop((GenericArray)ARRAY, sizeof(TYPE))); \
|
||||||
}())
|
}())
|
||||||
#else
|
#else
|
||||||
#define _stack_array(TYPE, SIZE) struct { ArrayHeader header; TYPE items[SIZE]; }
|
#define _stack_array(TYPE, SIZE) struct {ArrayHeader header; \
|
||||||
#define wapp_array(TYPE, ...) \
|
TYPE items[SIZE]; \
|
||||||
(TYPE *)( \
|
wapp_misc_utils_padding_size(sizeof(ArrayHeader) + \
|
||||||
(_stack_array(TYPE, _calc_array_capacity(TYPE, __VA_ARGS__))){ \
|
sizeof(TYPE) * SIZE);}
|
||||||
.header = { \
|
#define wapp_array(TYPE, ...) \
|
||||||
.magic = WAPP_ARRAY_MAGIC, \
|
(TYPE *)( \
|
||||||
.count = _calc_array_count(TYPE, __VA_ARGS__), \
|
(_stack_array(TYPE, _calc_array_capacity(TYPE, __VA_ARGS__))){ \
|
||||||
.capacity = _calc_array_capacity(TYPE, __VA_ARGS__), \
|
.header = { \
|
||||||
.item_size = sizeof(TYPE), \
|
.magic = WAPP_ARRAY_MAGIC, \
|
||||||
}, \
|
.count = _calc_array_count(TYPE, __VA_ARGS__), \
|
||||||
.items = {__VA_ARGS__}, \
|
.capacity = _calc_array_capacity(TYPE, __VA_ARGS__), \
|
||||||
}.items \
|
.item_size = sizeof(TYPE), \
|
||||||
|
}, \
|
||||||
|
.items = {__VA_ARGS__}, \
|
||||||
|
}.items \
|
||||||
)
|
)
|
||||||
#define wapp_array_with_capacity(TYPE, CAPACITY, FILL) \
|
#define wapp_array_with_capacity(TYPE, CAPACITY, FILL) \
|
||||||
(TYPE *)( \
|
(TYPE *)( \
|
||||||
(_stack_array(TYPE, CAPACITY)){ \
|
(_stack_array(TYPE, CAPACITY)){ \
|
||||||
.header = { \
|
.header = { \
|
||||||
.magic = WAPP_ARRAY_MAGIC, \
|
.magic = WAPP_ARRAY_MAGIC, \
|
||||||
.count = FILL ? CAPACITY : 0, \
|
.count = FILL ? CAPACITY : 0, \
|
||||||
.capacity = CAPACITY, \
|
.capacity = CAPACITY, \
|
||||||
.item_size = sizeof(TYPE), \
|
.item_size = sizeof(TYPE), \
|
||||||
}, \
|
}, \
|
||||||
.items = {0}, \
|
.items = {0}, \
|
||||||
}.items \
|
}.items \
|
||||||
)
|
)
|
||||||
#define wapp_array_pop(TYPE, ARRAY) \
|
#define wapp_array_pop(TYPE, ARRAY) \
|
||||||
(ARRAY == NULL || _array_count((GenericArray)ARRAY) == 0 ? \
|
(ARRAY == NULL || _array_count((GenericArray)ARRAY) == 0 ? \
|
||||||
(TYPE){0} : \
|
(TYPE){0} : \
|
||||||
*((TYPE *)_array_pop((GenericArray)ARRAY, sizeof(TYPE))) \
|
*((TYPE *)_array_pop((GenericArray)ARRAY, sizeof(TYPE))) \
|
||||||
)
|
)
|
||||||
#endif // !WAPP_PLATFORM_CPP
|
#endif // !WAPP_PLATFORM_CPP
|
||||||
|
|
||||||
#define wapp_array_count(ARRAY) \
|
#define wapp_array_count(ARRAY) \
|
||||||
_array_count((GenericArray)ARRAY)
|
_array_count((GenericArray)ARRAY)
|
||||||
#define wapp_array_capacity(ARRAY) \
|
#define wapp_array_capacity(ARRAY) \
|
||||||
_array_capacity((GenericArray)ARRAY)
|
_array_capacity((GenericArray)ARRAY)
|
||||||
#define wapp_array_item_size(ARRAY) \
|
#define wapp_array_item_size(ARRAY) \
|
||||||
_array_item_size((GenericArray)ARRAY)
|
_array_item_size((GenericArray)ARRAY)
|
||||||
#define wapp_array_set_count(ARRAY, COUNT) \
|
#define wapp_array_set_count(ARRAY, COUNT) \
|
||||||
_array_set_count((GenericArray)ARRAY, COUNT)
|
_array_set_count((GenericArray)ARRAY, COUNT)
|
||||||
#define wapp_array_get(TYPE, ARRAY, INDEX) \
|
#define wapp_array_get(TYPE, ARRAY, INDEX) \
|
||||||
((TYPE *)_array_get((GenericArray)ARRAY, \
|
((TYPE *)_array_get((GenericArray)ARRAY, \
|
||||||
INDEX, \
|
INDEX, \
|
||||||
sizeof(TYPE)))
|
sizeof(TYPE)))
|
||||||
#define wapp_array_set(TYPE, ARRAY, INDEX, VALUE_PTR) \
|
#define wapp_array_set(TYPE, ARRAY, INDEX, VALUE_PTR) \
|
||||||
_array_set((GenericArray)ARRAY, \
|
_array_set((GenericArray)ARRAY, \
|
||||||
INDEX, \
|
INDEX, \
|
||||||
(u8 *)VALUE_PTR, \
|
(u8 *)VALUE_PTR, \
|
||||||
sizeof(TYPE))
|
sizeof(TYPE))
|
||||||
#define wapp_array_append_capped(TYPE, ARRAY, VALUE_PTR) \
|
#define wapp_array_append_capped(TYPE, ARRAY, VALUE_PTR) \
|
||||||
_array_append_capped((GenericArray)ARRAY, \
|
_array_append_capped((GenericArray)ARRAY, \
|
||||||
(u8 *)VALUE_PTR, \
|
(u8 *)VALUE_PTR, \
|
||||||
sizeof(TYPE))
|
sizeof(TYPE))
|
||||||
#define wapp_array_extend_capped(TYPE, DST_ARRAY, SRC_ARRAY) \
|
#define wapp_array_extend_capped(TYPE, DST_ARRAY, SRC_ARRAY) \
|
||||||
_array_extend_capped((GenericArray)DST_ARRAY, \
|
_array_extend_capped((GenericArray)DST_ARRAY, \
|
||||||
(GenericArray)SRC_ARRAY, \
|
(GenericArray)SRC_ARRAY, \
|
||||||
sizeof(TYPE))
|
sizeof(TYPE))
|
||||||
#define wapp_array_copy_capped(TYPE, DST_ARRAY, SRC_ARRAY) \
|
#define wapp_array_copy_capped(TYPE, DST_ARRAY, SRC_ARRAY) \
|
||||||
_array_copy_capped((GenericArray)DST_ARRAY, \
|
_array_copy_capped((GenericArray)DST_ARRAY, \
|
||||||
(GenericArray)SRC_ARRAY, \
|
(GenericArray)SRC_ARRAY, \
|
||||||
sizeof(TYPE))
|
sizeof(TYPE))
|
||||||
#define wapp_array_append_alloc(TYPE, ALLOCATOR_PTR, ARRAY, VALUE_PTR) \
|
#define wapp_array_append_alloc(TYPE, ALLOCATOR_PTR, ARRAY, VALUE_PTR) \
|
||||||
(TYPE *)_array_append_alloc(ALLOCATOR_PTR, \
|
(TYPE *)_array_append_alloc(ALLOCATOR_PTR, \
|
||||||
(GenericArray)ARRAY, \
|
(GenericArray)ARRAY, \
|
||||||
(u8 *)VALUE_PTR, \
|
(u8 *)VALUE_PTR, \
|
||||||
sizeof(TYPE))
|
sizeof(TYPE))
|
||||||
#define wapp_array_extend_alloc(TYPE, ALLOCATOR_PTR, DST_ARRAY, SRC_ARRAY) \
|
#define wapp_array_extend_alloc(TYPE, ALLOCATOR_PTR, DST_ARRAY, SRC_ARRAY) \
|
||||||
(TYPE *)_array_extend_alloc(ALLOCATOR_PTR, \
|
(TYPE *)_array_extend_alloc(ALLOCATOR_PTR, \
|
||||||
(GenericArray)DST_ARRAY, \
|
(GenericArray)DST_ARRAY, \
|
||||||
(GenericArray)SRC_ARRAY, \
|
(GenericArray)SRC_ARRAY, \
|
||||||
sizeof(TYPE))
|
sizeof(TYPE))
|
||||||
#define wapp_array_copy_alloc(TYPE, ALLOCATOR_PTR, DST_ARRAY, SRC_ARRAY) \
|
#define wapp_array_copy_alloc(TYPE, ALLOCATOR_PTR, DST_ARRAY, SRC_ARRAY) \
|
||||||
(TYPE *)_array_copy_alloc(ALLOCATOR_PTR, \
|
(TYPE *)_array_copy_alloc(ALLOCATOR_PTR, \
|
||||||
(GenericArray)DST_ARRAY, \
|
(GenericArray)DST_ARRAY, \
|
||||||
(GenericArray)SRC_ARRAY, \
|
(GenericArray)SRC_ARRAY, \
|
||||||
sizeof(TYPE))
|
sizeof(TYPE))
|
||||||
#define wapp_array_clear(TYPE, ARRAY) \
|
#define wapp_array_clear(TYPE, ARRAY) \
|
||||||
_array_clear((GenericArray)ARRAY, \
|
_array_clear((GenericArray)ARRAY, \
|
||||||
sizeof(TYPE))
|
sizeof(TYPE))
|
||||||
|
|
||||||
typedef struct header ArrayHeader;
|
typedef struct header ArrayHeader;
|
||||||
|
|||||||
Reference in New Issue
Block a user