From cf7e075e8a8a2ea9a878ac73824b4e364b88b9af Mon Sep 17 00:00:00 2001 From: Marc Gilleron Date: Sun, 24 Apr 2022 17:27:06 +0100 Subject: [PATCH] Fix macros to compile release --- storage/voxel_memory_pool.cpp | 29 +++++++++++++++-------------- util/memory.h | 6 ++++++ 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/storage/voxel_memory_pool.cpp b/storage/voxel_memory_pool.cpp index a8db5fda..b7cc7c5e 100644 --- a/storage/voxel_memory_pool.cpp +++ b/storage/voxel_memory_pool.cpp @@ -1,5 +1,6 @@ #include "voxel_memory_pool.h" #include "../util/macros.h" +#include "../util/memory.h" #include "../util/profiling.h" #include "../util/string_funcs.h" @@ -10,19 +11,19 @@ VoxelMemoryPool *g_memory_pool = nullptr; } // namespace void VoxelMemoryPool::create_singleton() { - CRASH_COND(g_memory_pool != nullptr); - g_memory_pool = memnew(VoxelMemoryPool); + ZN_ASSERT(g_memory_pool == nullptr); + g_memory_pool = ZN_NEW(VoxelMemoryPool); } void VoxelMemoryPool::destroy_singleton() { - CRASH_COND(g_memory_pool == nullptr); + ZN_ASSERT(g_memory_pool != nullptr); VoxelMemoryPool *pool = g_memory_pool; g_memory_pool = nullptr; memdelete(pool); } VoxelMemoryPool &VoxelMemoryPool::get_singleton() { - CRASH_COND(g_memory_pool == nullptr); + ZN_ASSERT(g_memory_pool != nullptr); return *g_memory_pool; } @@ -39,13 +40,13 @@ VoxelMemoryPool::~VoxelMemoryPool() { uint8_t *VoxelMemoryPool::allocate(size_t size) { ZN_PROFILE_SCOPE(); - CRASH_COND(size == 0); + ZN_ASSERT(size != 0); uint8_t *block = nullptr; // Not calculating `pot` immediately because the function we use to calculate it uses 32 bits, // while `size_t` can be larger than that. if (size > get_highest_supported_size()) { // Sorry, memory is not pooled past this size - block = (uint8_t *)memalloc(size * sizeof(uint8_t)); + block = (uint8_t *)ZN_ALLOC(size * sizeof(uint8_t)); #ifdef DEBUG_ENABLED if (block != nullptr) { _debug_nonpooled_used_blocks.add(block); @@ -66,9 +67,9 @@ uint8_t *VoxelMemoryPool::allocate(size_t size) { // which must be greater or equal to `size` const size_t capacity = get_size_from_pool_index(pot); #ifdef DEBUG_ENABLED - CRASH_COND(capacity < size); + ZN_ASSERT(capacity >= size); #endif - block = (uint8_t *)memalloc(capacity * sizeof(uint8_t)); + block = (uint8_t *)ZN_ALLOC(capacity * sizeof(uint8_t)); } #ifdef DEBUG_ENABLED if (block != nullptr) { @@ -77,7 +78,7 @@ uint8_t *VoxelMemoryPool::allocate(size_t size) { #endif } if (block == nullptr) { - ERR_PRINT("Out of memory"); + ZN_PRINT_ERROR("Out of memory"); } else { ++_used_blocks; _used_memory += size; @@ -86,8 +87,8 @@ uint8_t *VoxelMemoryPool::allocate(size_t size) { } void VoxelMemoryPool::recycle(uint8_t *block, size_t size) { - CRASH_COND(size == 0); - CRASH_COND(block == nullptr); + ZN_ASSERT(size != 0); + ZN_ASSERT(block != nullptr); // Not calculating `pot` immediately because the function we use to calculate it uses 32 bits, // while `size_t` can be larger than that. if (size > get_highest_supported_size()) { @@ -95,7 +96,7 @@ void VoxelMemoryPool::recycle(uint8_t *block, size_t size) { // Make sure this allocation was done by this pool in this scenario _debug_nonpooled_used_blocks.remove(block); #endif - memfree(block); + ZN_FREE(block); } else { const unsigned int pot = get_pool_index_from_size(size); Pool &pool = _pot_pools[pot]; @@ -116,7 +117,7 @@ void VoxelMemoryPool::clear_unused_blocks() { MutexLock lock(pool.mutex); for (unsigned int i = 0; i < pool.blocks.size(); ++i) { void *block = pool.blocks[i]; - memfree(block); + ZN_FREE(block); } _total_memory -= get_size_from_pool_index(pot) * pool.blocks.size(); pool.blocks.clear(); @@ -129,7 +130,7 @@ void VoxelMemoryPool::clear() { MutexLock lock(pool.mutex); for (unsigned int i = 0; i < pool.blocks.size(); ++i) { void *block = pool.blocks[i]; - memfree(block); + ZN_FREE(block); } pool.blocks.clear(); } diff --git a/util/memory.h b/util/memory.h index 666928ec..5d1627b8 100644 --- a/util/memory.h +++ b/util/memory.h @@ -12,11 +12,17 @@ // In modules, memnew and memdelete work for anything. However in GDExtension it might not be the case... #define ZN_NEW(t) memnew(t) #define ZN_DELETE(t) memdelete(t) +#define ZN_ALLOC(size) memalloc(size) +#define ZN_REALLOC(p, size) memrealloc(p, size) +#define ZN_FREE(p) memfree(p) #else #define ZN_NEW(t) new t #define ZN_DELETE(t) delete t +#define ZN_ALLOC(size) malloc(size) +#define ZN_REALLOC(p, size) realloc(p, size) +#define ZN_FREE(p) free(p) #endif