Skip to content
Snippets Groups Projects
Commit ecc3e78d authored by Jacques Lucke's avatar Jacques Lucke
Browse files

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
parent d2f8af9b
No related branches found
No related tags found
No related merge requests found
...@@ -341,6 +341,10 @@ template<typename T> inline T *MEM_cnew(const char *allocation_name, const T &ot ...@@ -341,6 +341,10 @@ template<typename T> inline T *MEM_cnew(const char *allocation_name, const T &ot
{ \ { \
return MEM_mallocN(num_bytes, _id); \ 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) \ void operator delete(void *mem) \
{ \ { \
if (mem) { \ if (mem) { \
...@@ -351,6 +355,10 @@ template<typename T> inline T *MEM_cnew(const char *allocation_name, const T &ot ...@@ -351,6 +355,10 @@ template<typename T> inline T *MEM_cnew(const char *allocation_name, const T &ot
{ \ { \
return MEM_mallocN(num_bytes, _id "[]"); \ 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) \ void operator delete[](void *mem) \
{ \ { \
if (mem) { \ if (mem) { \
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment