diff --git a/core/templates/cowdata.h b/core/templates/cowdata.h index f22ae1f1d37b..67fe2db17092 100644 --- a/core/templates/cowdata.h +++ b/core/templates/cowdata.h @@ -133,14 +133,14 @@ class CowData { } _FORCE_INLINE_ USize _get_alloc_size(USize p_elements) const { - return next_po2(p_elements * sizeof(T)); + if constexpr ((sizeof(T) & (sizeof(T) - 1)) == 0) { // Is T power of 2. + return next_po2(p_elements * sizeof(T) + DATA_OFFSET); + } else { + return next_po2(p_elements) * sizeof(T) + DATA_OFFSET; + } } _FORCE_INLINE_ bool _get_alloc_size_checked(USize p_elements, USize *out) const { - if (unlikely(p_elements == 0)) { - *out = 0; - return true; - } #if defined(__GNUC__) && defined(IS_32_BIT) USize o; USize p; @@ -157,7 +157,7 @@ class CowData { // and hope for the best. *out = _get_alloc_size(p_elements); #endif - return *out; + return true; } void _unref(void *p_data); @@ -281,7 +281,7 @@ typename CowData::USize CowData::_copy_on_write() { /* in use by more than me */ USize current_size = *_get_size(); - uint8_t *mem_new = (uint8_t *)Memory::alloc_static(_get_alloc_size(current_size) + DATA_OFFSET, false); + uint8_t *mem_new = (uint8_t *)Memory::alloc_static(_get_alloc_size(current_size), false); ERR_FAIL_NULL_V(mem_new, 0); SafeNumeric *_refc_ptr = _get_refcount_ptr(mem_new); @@ -337,7 +337,7 @@ Error CowData::resize(Size p_size) { if (alloc_size != current_alloc_size) { if (current_size == 0) { // alloc from scratch - uint8_t *mem_new = (uint8_t *)Memory::alloc_static(alloc_size + DATA_OFFSET, false); + uint8_t *mem_new = (uint8_t *)Memory::alloc_static(alloc_size, false); ERR_FAIL_NULL_V(mem_new, ERR_OUT_OF_MEMORY); SafeNumeric *_refc_ptr = _get_refcount_ptr(mem_new); @@ -350,7 +350,7 @@ Error CowData::resize(Size p_size) { _ptr = _data_ptr; } else { - uint8_t *mem_new = (uint8_t *)Memory::realloc_static(((uint8_t *)_ptr) - DATA_OFFSET, alloc_size + DATA_OFFSET, false); + uint8_t *mem_new = (uint8_t *)Memory::realloc_static(((uint8_t *)_ptr) - DATA_OFFSET, alloc_size, false); ERR_FAIL_NULL_V(mem_new, ERR_OUT_OF_MEMORY); SafeNumeric *_refc_ptr = _get_refcount_ptr(mem_new); @@ -384,7 +384,7 @@ Error CowData::resize(Size p_size) { } if (alloc_size != current_alloc_size) { - uint8_t *mem_new = (uint8_t *)Memory::realloc_static(((uint8_t *)_ptr) - DATA_OFFSET, alloc_size + DATA_OFFSET, false); + uint8_t *mem_new = (uint8_t *)Memory::realloc_static(((uint8_t *)_ptr) - DATA_OFFSET, alloc_size, false); ERR_FAIL_NULL_V(mem_new, ERR_OUT_OF_MEMORY); SafeNumeric *_refc_ptr = _get_refcount_ptr(mem_new);