Fix #118402: support overaligned types in MEM_CXX_CLASS_ALLOC_FUNCS

Previously, the alignment of structs that use `MEM_CXX_CLASS_ALLOC_FUNCS`
were not taken into account when doing the allocation. This can cause some data
to be mis-aligned and leads to crashes when cpu instructions or code expect the
data to be aligned.

The fix is to provide an overload of `operator new` that accepts the alignment as parameter.

More info: https://en.cppreference.com/w/cpp/language/new (search for `align_val_t`).

Pull Request: https://projects.blender.org/blender/blender/pulls/118526
This commit is contained in:
Jacques Lucke 2024-02-20 18:44:26 +01:00
parent d2f8af9b08
commit ecc3e78d78
1 changed files with 8 additions and 0 deletions

View File

@ -341,6 +341,10 @@ template<typename T> inline T *MEM_cnew(const char *allocation_name, const T &ot
{ \
return MEM_mallocN(num_bytes, _id); \
} \
void *operator new(size_t num_bytes, std::align_val_t alignment) \
{ \
return MEM_mallocN_aligned(num_bytes, size_t(alignment), _id); \
} \
void operator delete(void *mem) \
{ \
if (mem) { \
@ -351,6 +355,10 @@ template<typename T> inline T *MEM_cnew(const char *allocation_name, const T &ot
{ \
return MEM_mallocN(num_bytes, _id "[]"); \
} \
void *operator new[](size_t num_bytes, std::align_val_t alignment) \
{ \
return MEM_mallocN_aligned(num_bytes, size_t(alignment), _id "[]"); \
} \
void operator delete[](void *mem) \
{ \
if (mem) { \