Add Memory::alloc_static_zeroed to allocate memory that's filled with zeroes.

This is generally faster than `malloc` followed by `memset` / loop-set to 0.
This commit is contained in:
Lukas Tenbrink
2025-03-14 15:15:09 +01:00
parent db66343528
commit 3207066e19
8 changed files with 32 additions and 47 deletions

View File

@ -93,6 +93,7 @@ void Memory::free_aligned_static(void *p_memory) {
free(p);
}
template <bool p_ensure_zero>
void *Memory::alloc_static(size_t p_bytes, bool p_pad_align) {
#ifdef DEBUG_ENABLED
bool prepad = true;
@ -100,7 +101,12 @@ void *Memory::alloc_static(size_t p_bytes, bool p_pad_align) {
bool prepad = p_pad_align;
#endif
void *mem = malloc(p_bytes + (prepad ? DATA_OFFSET : 0));
void *mem;
if constexpr (p_ensure_zero) {
mem = calloc(1, p_bytes + (prepad ? DATA_OFFSET : 0));
} else {
mem = malloc(p_bytes + (prepad ? DATA_OFFSET : 0));
}
ERR_FAIL_NULL_V(mem, nullptr);
@ -120,6 +126,9 @@ void *Memory::alloc_static(size_t p_bytes, bool p_pad_align) {
}
}
template void *Memory::alloc_static<true>(size_t p_bytes, bool p_pad_align);
template void *Memory::alloc_static<false>(size_t p_bytes, bool p_pad_align);
void *Memory::realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align) {
if (p_memory == nullptr) {
return alloc_static(p_bytes, p_pad_align);