Cleanup: Replace 'void' MEM_[cm]allocN with templated, type-safe MEM_[cm]allocN<T>.

The main issue of 'type-less' standard C allocations is that there is no check on
allocated type possible.

This is a serious source of annoyance (and crashes) when making some
low-level structs non-trivial, as tracking down all usages of these
structs in higher-level other structs and their allocation is... really
painful.

`MEM_[cm]allocN<T>` templates on the other hand do check that the
given type is trivial, at build time (static assert), which makes such issue...
trivial to catch.

NOTE: New code should strive to use `MEM_new` (i.e. allocation and
construction) as much as possible, even for trivial PoD types.

Pull Request: https://projects.blender.org/blender/blender/pulls/134452
This commit is contained in:
Bastien Montagne
2025-03-07 10:44:49 +01:00
committed by Bastien Montagne
parent adba9bda59
commit 1e00054195
7 changed files with 18 additions and 26 deletions

View File

@@ -2249,8 +2249,7 @@ int Channelbag::channel_group_containing_index(const int fcurve_array_index)
bActionGroup &Channelbag::channel_group_create(StringRefNull name)
{
bActionGroup *new_group = static_cast<bActionGroup *>(
MEM_callocN(sizeof(bActionGroup), __func__));
bActionGroup *new_group = MEM_callocN<bActionGroup>(__func__);
/* Find the end fcurve index of the current channel groups, to be used as the
* start of the new channel group. */

View File

@@ -53,7 +53,7 @@ class ActionLegacyTest : public testing::Test {
FCurve *fcurve_add_legacy(bAction *action, const StringRefNull rna_path, const int array_index)
{
FCurve *fcurve = static_cast<FCurve *>(MEM_callocN(sizeof(FCurve), __func__));
FCurve *fcurve = MEM_callocN<FCurve>(__func__);
BKE_fcurve_rnapath_set(*fcurve, rna_path);
fcurve->array_index = array_index;
BLI_addtail(&action->curves, fcurve);
@@ -77,7 +77,7 @@ TEST_F(ActionLegacyTest, fcurves_all)
{ /* Legacy Action. */
bAction *action = create_empty_action();
FCurve *fcurve = static_cast<FCurve *>(MEM_callocN(sizeof(FCurve), __func__));
FCurve *fcurve = MEM_callocN<FCurve>(__func__);
BLI_addtail(&action->curves, fcurve);
Vector<FCurve *> fcurves_expect = {fcurve};
@@ -119,7 +119,7 @@ TEST_F(ActionLegacyTest, fcurves_for_action_slot)
{ /* Legacy Action. */
bAction *action = create_empty_action();
FCurve *fcurve = static_cast<FCurve *>(MEM_callocN(sizeof(FCurve), __func__));
FCurve *fcurve = MEM_callocN<FCurve>(__func__);
BLI_addtail(&action->curves, fcurve);
Vector<FCurve *> fcurves_expect = {fcurve};

View File

@@ -30,7 +30,7 @@ Vector<float> get_rna_values(PointerRNA *ptr, PropertyRNA *prop)
switch (RNA_property_type(prop)) {
case PROP_BOOLEAN: {
bool *tmp_bool = static_cast<bool *>(MEM_malloc_arrayN(length, sizeof(bool), __func__));
bool *tmp_bool = MEM_malloc_arrayN<bool>(length, __func__);
RNA_property_boolean_get_array(ptr, prop, tmp_bool);
for (int i = 0; i < length; i++) {
values.append(float(tmp_bool[i]));
@@ -39,7 +39,7 @@ Vector<float> get_rna_values(PointerRNA *ptr, PropertyRNA *prop)
break;
}
case PROP_INT: {
int *tmp_int = static_cast<int *>(MEM_malloc_arrayN(length, sizeof(int), __func__));
int *tmp_int = MEM_malloc_arrayN<int>(length, __func__);
RNA_property_int_get_array(ptr, prop, tmp_int);
for (int i = 0; i < length; i++) {
values.append(float(tmp_int[i]));

View File

@@ -1399,8 +1399,7 @@ blender::Map<BoneCollection *, BoneCollection *> ANIM_bonecoll_array_copy_no_mem
BLI_assert(*bcoll_array_dst == nullptr);
BLI_assert(*bcoll_array_dst_num == 0);
*bcoll_array_dst = static_cast<BoneCollection **>(
MEM_malloc_arrayN(bcoll_array_src_num, sizeof(BoneCollection *), __func__));
*bcoll_array_dst = MEM_malloc_arrayN<BoneCollection *>(bcoll_array_src_num, __func__);
*bcoll_array_dst_num = bcoll_array_src_num;
blender::Map<BoneCollection *, BoneCollection *> bcoll_map{};

View File

@@ -175,8 +175,7 @@ int insert_bezt_fcurve(FCurve *fcu, const BezTriple *bezt, eInsertKeyFlags flag)
/* Keyframing modes allow not replacing the keyframe. */
else if ((flag & INSERTKEY_REPLACE) == 0) {
/* Insert new - if we're not restricted to replacing keyframes only. */
BezTriple *newb = static_cast<BezTriple *>(
MEM_callocN((fcu->totvert + 1) * sizeof(BezTriple), "beztriple"));
BezTriple *newb = MEM_calloc_arrayN<BezTriple>(fcu->totvert + 1, "beztriple");
/* Add the beztriples that should occur before the beztriple to be pasted
* (originally in fcu). */
@@ -210,7 +209,7 @@ int insert_bezt_fcurve(FCurve *fcu, const BezTriple *bezt, eInsertKeyFlags flag)
*/
else if ((flag & INSERTKEY_REPLACE) == 0 && (fcu->fpt == nullptr)) {
/* Create new keyframes array. */
fcu->bezt = static_cast<BezTriple *>(MEM_callocN(sizeof(BezTriple), "beztriple"));
fcu->bezt = MEM_callocN<BezTriple>("beztriple");
*(fcu->bezt) = *bezt;
fcu->totvert = 1;
}
@@ -555,8 +554,7 @@ void bake_fcurve(FCurve *fcu,
{
BLI_assert(step > 0);
const int sample_count = (range[1] - range[0]) / step + 1;
float *samples = static_cast<float *>(
MEM_callocN(sample_count * sizeof(float), "Channel Bake Samples"));
float *samples = MEM_calloc_arrayN<float>(size_t(sample_count), "Channel Bake Samples");
const float sample_rate = 1.0f / step;
sample_fcurve_segment(fcu, range[0], sample_rate, samples, sample_count);
@@ -564,8 +562,7 @@ void bake_fcurve(FCurve *fcu,
remove_fcurve_key_range(fcu, range, remove_existing);
}
BezTriple *baked_keys = static_cast<BezTriple *>(
MEM_callocN(sample_count * sizeof(BezTriple), "beztriple"));
BezTriple *baked_keys = MEM_calloc_arrayN<BezTriple>(size_t(sample_count), "beztriple");
const KeyframeSettings settings = get_keyframe_settings(true);
@@ -636,8 +633,7 @@ void bake_fcurve_segments(FCurve *fcu)
sfra = int(floor(start->vec[1][0]));
if (range) {
value_cache = static_cast<TempFrameValCache *>(
MEM_callocN(sizeof(TempFrameValCache) * range, "IcuFrameValCache"));
value_cache = MEM_calloc_arrayN<TempFrameValCache>(size_t(range), "IcuFrameValCache");
/* Sample values. */
for (n = 1, fp = value_cache; n < range && fp; n++, fp++) {

View File

@@ -79,8 +79,7 @@ class KeyframingTest : public testing::Test {
object = BKE_object_add_only_object(bmain, OB_EMPTY, "Empty");
object_rna_pointer = RNA_id_pointer_create(&object->id);
Bone *bone = static_cast<Bone *>(MEM_mallocN(sizeof(Bone), "BONE"));
memset(bone, 0, sizeof(Bone));
Bone *bone = MEM_callocN<Bone>("BONE");
STRNCPY(bone->name, "Bone");
armature = BKE_armature_add(bmain, "Armature");
@@ -160,8 +159,7 @@ class KeyframingTest : public testing::Test {
*/
void ensure_action_is_legacy(bAction &action)
{
bActionGroup *new_group = static_cast<bActionGroup *>(
MEM_callocN(sizeof(bActionGroup), __func__));
bActionGroup *new_group = MEM_callocN<bActionGroup>(__func__);
STRNCPY(new_group->name, "Legacy Forcer");
BLI_addtail(&action.groups, new_group);
}

View File

@@ -67,11 +67,11 @@ class PoseTest : public testing::Test {
bArmature *armature = BKE_armature_add(bmain, "ArmatureA");
obj_armature_a->data = armature;
Bone *bone = static_cast<Bone *>(MEM_callocN(sizeof(Bone), "BONE"));
Bone *bone = MEM_callocN<Bone>("BONE");
STRNCPY(bone->name, "BoneA");
BLI_addtail(&armature->bonebase, bone);
bone = static_cast<Bone *>(MEM_callocN(sizeof(Bone), "BONE"));
bone = MEM_callocN<Bone>("BONE");
STRNCPY(bone->name, "BoneB");
BLI_addtail(&armature->bonebase, bone);
@@ -80,11 +80,11 @@ class PoseTest : public testing::Test {
armature = BKE_armature_add(bmain, "ArmatureB");
obj_armature_b->data = armature;
bone = static_cast<Bone *>(MEM_callocN(sizeof(Bone), "BONE"));
bone = MEM_callocN<Bone>("BONE");
STRNCPY(bone->name, "BoneA");
BLI_addtail(&armature->bonebase, bone);
bone = static_cast<Bone *>(MEM_callocN(sizeof(Bone), "BONE"));
bone = MEM_callocN<Bone>("BONE");
STRNCPY(bone->name, "BoneB");
BLI_addtail(&armature->bonebase, bone);