From ecc3e78d787cce8a3f202e7de26575e2d47baea2 Mon Sep 17 00:00:00 2001
From: Jacques Lucke <jacques@blender.org>
Date: Tue, 20 Feb 2024 18:44:26 +0100
Subject: [PATCH] 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
---
 intern/guardedalloc/MEM_guardedalloc.h | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/intern/guardedalloc/MEM_guardedalloc.h b/intern/guardedalloc/MEM_guardedalloc.h
index e137dada335..ee4158af923 100644
--- a/intern/guardedalloc/MEM_guardedalloc.h
+++ b/intern/guardedalloc/MEM_guardedalloc.h
@@ -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) { \
-- 
GitLab