MEM: Make MEM_cnew
return aligned memory
Now made it so that `MEM_cnew()` can be used for types with any alignment. Fixes #120407. Pull Request: https://projects.blender.org/blender/blender/pulls/120569
This commit is contained in:
parent
77451ea83f
commit
60a3d85b88
@ -38,6 +38,8 @@
|
||||
#include "../../source/blender/blenlib/BLI_compiler_attrs.h"
|
||||
#include "../../source/blender/blenlib/BLI_sys_types.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
@ -309,7 +311,16 @@ template<typename T> inline void MEM_delete(const T *ptr)
|
||||
template<typename T> inline T *MEM_cnew(const char *allocation_name)
|
||||
{
|
||||
static_assert(std::is_trivial_v<T>, "For non-trivial types, MEM_new should be used.");
|
||||
return static_cast<T *>(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<T *>(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<T *>(ptr);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user