Fix incorrect memory allocation in release builds.

Co-authored-by: lightyears <lightyears1998@hotmail.com>
This commit is contained in:
bruvzg
2023-02-21 12:47:40 +02:00
parent 2f07eb07ee
commit ba4b50118d
4 changed files with 72 additions and 21 deletions

View File

@@ -40,6 +40,10 @@
#include <type_traits>
#ifndef PAD_ALIGN
#define PAD_ALIGN 16 //must always be greater than this at much
#endif
void *operator new(size_t p_size, const char *p_description); ///< operator new that takes a description and uses MemoryStaticPool
void *operator new(size_t p_size, void *(*p_allocfunc)(size_t p_size)); ///< operator new that takes a description and uses MemoryStaticPool
void *operator new(size_t p_size, void *p_pointer, size_t check, const char *p_description); ///< operator new that takes a description and uses a pointer to the preallocated memory
@@ -64,9 +68,9 @@ class Memory {
Memory();
public:
static void *alloc_static(size_t p_bytes);
static void *realloc_static(void *p_memory, size_t p_bytes);
static void free_static(void *p_ptr);
static void *alloc_static(size_t p_bytes, bool p_pad_align = false);
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);
};
_ALWAYS_INLINE_ void postinitialize_handler(void *) {}
@@ -140,7 +144,7 @@ T *memnew_arr_template(size_t p_elements, const char *p_descr = "") {
same strategy used by std::vector, and the Vector class, so it should be safe.*/
size_t len = sizeof(T) * p_elements;
uint64_t *mem = (uint64_t *)Memory::alloc_static(len);
uint64_t *mem = (uint64_t *)Memory::alloc_static(len, true);
T *failptr = nullptr; // Get rid of a warning.
ERR_FAIL_COND_V(!mem, failptr);
*(mem - 1) = p_elements;
@@ -169,7 +173,7 @@ void memdelete_arr(T *p_class) {
}
}
Memory::free_static(ptr);
Memory::free_static(ptr, true);
}
struct _GlobalNil {

View File

@@ -217,7 +217,7 @@ void CowData<T>::_unref(void *p_data) {
}
// free mem
Memory::free_static((uint8_t *)p_data);
Memory::free_static((uint8_t *)p_data, true);
}
template <class T>
@@ -233,7 +233,7 @@ uint32_t CowData<T>::_copy_on_write() {
/* in use by more than me */
uint32_t current_size = *_get_size();
uint32_t *mem_new = (uint32_t *)Memory::alloc_static(_get_alloc_size(current_size));
uint32_t *mem_new = (uint32_t *)Memory::alloc_static(_get_alloc_size(current_size), true);
new (mem_new - 2) SafeNumeric<uint32_t>(1); // refcount
*(mem_new - 1) = current_size; // size
@@ -286,7 +286,7 @@ Error CowData<T>::resize(int p_size) {
if (alloc_size != current_alloc_size) {
if (current_size == 0) {
// alloc from scratch
uint32_t *ptr = (uint32_t *)Memory::alloc_static(alloc_size);
uint32_t *ptr = (uint32_t *)Memory::alloc_static(alloc_size, true);
ERR_FAIL_COND_V(!ptr, ERR_OUT_OF_MEMORY);
*(ptr - 1) = 0; // size, currently none
new (ptr - 2) SafeNumeric<uint32_t>(1); // refcount
@@ -294,7 +294,7 @@ Error CowData<T>::resize(int p_size) {
_ptr = (T *)ptr;
} else {
uint32_t *_ptrnew = (uint32_t *)Memory::realloc_static(_ptr, alloc_size);
uint32_t *_ptrnew = (uint32_t *)Memory::realloc_static(_ptr, alloc_size, true);
ERR_FAIL_COND_V(!_ptrnew, ERR_OUT_OF_MEMORY);
new (_ptrnew - 2) SafeNumeric<uint32_t>(rc); // refcount
@@ -324,7 +324,7 @@ Error CowData<T>::resize(int p_size) {
}
if (alloc_size != current_alloc_size) {
uint32_t *_ptrnew = (uint32_t *)Memory::realloc_static(_ptr, alloc_size);
uint32_t *_ptrnew = (uint32_t *)Memory::realloc_static(_ptr, alloc_size, true);
ERR_FAIL_COND_V(!_ptrnew, ERR_OUT_OF_MEMORY);
new (_ptrnew - 2) SafeNumeric<uint32_t>(rc); // refcount