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:
@ -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);
|
||||
|
||||
@ -54,7 +54,9 @@ public:
|
||||
static constexpr size_t ELEMENT_OFFSET = ((SIZE_OFFSET + sizeof(uint64_t)) % alignof(uint64_t) == 0) ? (SIZE_OFFSET + sizeof(uint64_t)) : ((SIZE_OFFSET + sizeof(uint64_t)) + alignof(uint64_t) - ((SIZE_OFFSET + sizeof(uint64_t)) % alignof(uint64_t)));
|
||||
static constexpr size_t DATA_OFFSET = ((ELEMENT_OFFSET + sizeof(uint64_t)) % alignof(max_align_t) == 0) ? (ELEMENT_OFFSET + sizeof(uint64_t)) : ((ELEMENT_OFFSET + sizeof(uint64_t)) + alignof(max_align_t) - ((ELEMENT_OFFSET + sizeof(uint64_t)) % alignof(max_align_t)));
|
||||
|
||||
template <bool p_ensure_zero = false>
|
||||
static void *alloc_static(size_t p_bytes, bool p_pad_align = false);
|
||||
_FORCE_INLINE_ static void *alloc_static_zeroed(size_t p_bytes, bool p_pad_align = false) { return alloc_static<true>(p_bytes, p_pad_align); }
|
||||
static void *realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align = false);
|
||||
static void free_static(void *p_ptr, bool p_pad_align = false);
|
||||
|
||||
@ -107,6 +109,7 @@ void operator delete(void *p_mem, void *p_pointer, size_t check, const char *p_d
|
||||
#endif
|
||||
|
||||
#define memalloc(m_size) Memory::alloc_static(m_size)
|
||||
#define memalloc_zeroed(m_size) Memory::alloc_static_zeroed(m_size)
|
||||
#define memrealloc(m_mem, m_size) Memory::realloc_static(m_mem, m_size)
|
||||
#define memfree(m_mem) Memory::free_static(m_mem)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user