From 605c1ddfae31cdb939ac883893c01b74e0e6d9e8 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Wed, 16 Sep 2026 01:28:11 +0100 Subject: [PATCH] Add wpArrayZero --- CHANGELOG.md | 1 + src/base/array/array.c | 8 ++++++++ src/base/array/array.h | 6 +++--- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d620b55..67f6dbf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `WpSplitmix64State` - `wpPrngSplitmix64Init`, `wpPrngSplitmix64InitWithSeed`, `wpPrngSplitmix64`, `wpPrngSplitmix64InRange`, `wpPrngSplitmix64Choice` - `wpStr8Clear` +- `wpArrayZero` to zero all elements in an array without setting the count to 0 ### Changed diff --git a/src/base/array/array.c b/src/base/array/array.c index d056697..8e4fb65 100644 --- a/src/base/array/array.c +++ b/src/base/array/array.c @@ -208,6 +208,14 @@ void *arrayPop(WpArray array, u64 item_size) { return out; } +void arrayZero(WpArray array, u64 item_size) { + wpRuntimeAssert(array != NULL, "`array` should not be NULL"); + arrayValidate(array, item_size); + + WpArrayHeader *header = arrayHeader(array); + memset(array, 0, header->capacity * header->item_size); +} + void arrayClear(WpArray array, u64 item_size) { wpRuntimeAssert(array != NULL, "`array` should not be NULL"); arrayValidate(array, item_size); diff --git a/src/base/array/array.h b/src/base/array/array.h index 5c952d9..14d949d 100644 --- a/src/base/array/array.h +++ b/src/base/array/array.h @@ -168,9 +168,8 @@ typedef enum WpArrayInitFlags { (WpArray)SRC_ARRAY, \ FLAGS, \ sizeof(TYPE))) -#define wpArrayClear(TYPE, ARRAY) \ - (arrayClear((WpArray)ARRAY, \ - sizeof(TYPE))) +#define wpArrayZero(TYPE, ARRAY) (arrayZero((WpArray)ARRAY, sizeof(TYPE))) +#define wpArrayClear(TYPE, ARRAY) (arrayClear((WpArray)ARRAY, sizeof(TYPE))) #define wpArrayCalcAllocSize(TYPE, CAPACITY) arrayCalcAllocSize(CAPACITY, sizeof(TYPE)) #define wpArrayAllocCapacity(TYPE, ALLOCATOR_PTR, CAPACITY, FLAGS) \ ((TYPE *)arrayAllocCapacity(ALLOCATOR_PTR, CAPACITY, FLAGS, sizeof(TYPE))) @@ -206,6 +205,7 @@ WpArray arrayExtendAlloc(const WpAllocator *allocator, WpArray dst, const WpArra WpArray arrayCopyAlloc(const WpAllocator *allocator, WpArray dst, const WpArray src, WpArrayInitFlags flags, u64 item_size); void *arrayPop(WpArray array, u64 item_size); +void arrayZero(WpArray array, u64 item_size); void arrayClear(WpArray array, u64 item_size); u64 arrayCalcAllocSize(u64 capacity, u64 item_size); WpArray arrayAllocCapacity(const WpAllocator *allocator, u64 capacity, WpArrayInitFlags flags,