diff --git a/intern/guardedalloc/MEM_guardedalloc.h b/intern/guardedalloc/MEM_guardedalloc.h index 02011da11cb..d5efc16b4ea 100644 --- a/intern/guardedalloc/MEM_guardedalloc.h +++ b/intern/guardedalloc/MEM_guardedalloc.h @@ -38,6 +38,8 @@ #include "../../source/blender/blenlib/BLI_compiler_attrs.h" #include "../../source/blender/blenlib/BLI_sys_types.h" +#include + #ifdef __cplusplus extern "C" { #endif @@ -309,7 +311,16 @@ template inline void MEM_delete(const T *ptr) template inline T *MEM_cnew(const char *allocation_name) { static_assert(std::is_trivial_v, "For non-trivial types, MEM_new should be used."); - return static_cast(MEM_callocN(sizeof(T), allocation_name)); + if (alignof(T) <= MEM_MIN_CPP_ALIGNMENT) { + /* TODO: Could possibly cover more cases, like alignment of 8 or 16. Need to be careful as the + * alignment of MEM_callocN is not really guaranteed. */ + return static_cast(MEM_callocN(sizeof(T), allocation_name)); + } + void *ptr = MEM_mallocN_aligned(sizeof(T), alignof(T), allocation_name); + if (ptr) { + memset(ptr, 0, sizeof(T)); + } + return static_cast(ptr); } /**