Refactor: Replace MEM_cnew with a type-aware template version of MEM_callocN.

The general idea is to keep the 'old', C-style MEM_callocN signature, and slowly
replace most of its usages with the new, C++-style type-safer template version.

* `MEM_cnew<T>` allocation version is renamed to `MEM_callocN<T>`.
* `MEM_cnew_array<T>` allocation version is renamed to `MEM_calloc_arrayN<T>`.
* `MEM_cnew<T>` duplicate version is renamed to `MEM_dupallocN<T>`.

Similar templates type-safe version of `MEM_mallocN` will be added soon
as well.

Following discussions in !134452.

NOTE: For now static type checking in `MEM_callocN` and related are slightly
different for Windows MSVC. This compiler seems to consider structs using the
`DNA_DEFINE_CXX_METHODS` macro as non-trivial (likely because their default
copy constructors are deleted). So using checks on trivially
constructible/destructible instead on this compiler/system.

Pull Request: https://projects.blender.org/blender/blender/pulls/134771
This commit is contained in:
Bastien Montagne
2025-03-05 16:35:09 +01:00
committed by Bastien Montagne
parent 65889672e2
commit dd168a35c5
423 changed files with 1122 additions and 1024 deletions

View File

@@ -20,6 +20,22 @@ enum class AllocationType {
/** Internal implementation of #MEM_freeN, exposed because #MEM_delete needs access to it. */
extern void (*mem_freeN_ex)(void *vmemh, AllocationType allocation_type);
/**
* Internal implementation of #MEM_callocN, exposed because public #MEM_callocN cannot be a
* function pointer, to allow its overload by C++ template version.
*/
extern void *(*mem_callocN)(size_t len, const char *str) /* ATTR_MALLOC */ ATTR_WARN_UNUSED_RESULT
ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2);
/**
* Internal implementation of #MEM_calloc_arrayN, exposed because public #MEM_calloc_arrayN cannot
* be a function pointer, to allow its overload by C++ template version.
*/
extern void *(*mem_calloc_arrayN)(size_t len,
size_t size,
const char *str) /* ATTR_MALLOC */ ATTR_WARN_UNUSED_RESULT
ATTR_ALLOC_SIZE(1, 2) ATTR_NONNULL(3);
/** Internal implementation of #MEM_mallocN_aligned, exposed because #MEM_new needs access to it.
*/
extern void *(*mem_mallocN_aligned_ex)(size_t len,
@@ -27,6 +43,12 @@ extern void *(*mem_mallocN_aligned_ex)(size_t len,
const char *str,
AllocationType allocation_type);
/**
* Internal implementation of #MEM_dupallocN, exposed because public #MEM_dupallocN cannot be a
* function pointer, to allow its overload by C++ template version.
*/
extern void *(*mem_dupallocN)(const void *vmemh) /* ATTR_MALLOC */ ATTR_WARN_UNUSED_RESULT;
/**
* Store a std::any into a static opaque storage vector. The only purpose of this call is to
* control the lifetime of the given data, there is no way to access it from here afterwards. User