From 1e976ec37bf6a4cfaece2ce9270925e986bc5342 Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Fri, 14 Mar 2025 14:52:16 +0100 Subject: [PATCH] Cleanup: io: Replace 'void' MEM_[cm]allocN with templated, type-safe MEM_[cm]allocN. 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 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/135976 --- source/blender/io/alembic/intern/abc_reader_nurbs.cc | 6 +++--- source/blender/io/usd/intern/usd_capi_import.cc | 3 +-- source/blender/io/usd/intern/usd_reader_nurbs.cc | 6 +++--- .../blender/io/wavefront_obj/importer/obj_import_nurbs.cc | 4 ++-- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/source/blender/io/alembic/intern/abc_reader_nurbs.cc b/source/blender/io/alembic/intern/abc_reader_nurbs.cc index 8a5d4586313..e6a00997bc1 100644 --- a/source/blender/io/alembic/intern/abc_reader_nurbs.cc +++ b/source/blender/io/alembic/intern/abc_reader_nurbs.cc @@ -87,7 +87,7 @@ static bool set_knots(const FloatArraySamplePtr &knots, float *&nu_knots) /* Skip first and last knots, as they are used for padding. */ const size_t num_knots = knots->size() - 2; - nu_knots = static_cast(MEM_callocN(num_knots * sizeof(float), "abc_setsplineknotsu")); + nu_knots = MEM_calloc_arrayN(num_knots, "abc_setsplineknotsu"); for (size_t i = 0; i < num_knots; i++) { nu_knots[i] = (*knots)[i + 1]; @@ -104,7 +104,7 @@ void AbcNurbsReader::readObjectData(Main *bmain, const Alembic::Abc::ISampleSele std::vector>::iterator it; for (it = m_schemas.begin(); it != m_schemas.end(); ++it) { - Nurb *nu = static_cast(MEM_callocN(sizeof(Nurb), "abc_getnurb")); + Nurb *nu = MEM_callocN("abc_getnurb"); nu->flag = CU_SMOOTH; nu->type = CU_NURBS; nu->resolu = cu->resolu; @@ -136,7 +136,7 @@ void AbcNurbsReader::readObjectData(Main *bmain, const Alembic::Abc::ISampleSele const size_t num_points = positions->size(); - nu->bp = static_cast(MEM_callocN(num_points * sizeof(BPoint), "abc_setsplinetype")); + nu->bp = MEM_calloc_arrayN(num_points, "abc_setsplinetype"); BPoint *bp = nu->bp; float posw_in = 1.0f; diff --git a/source/blender/io/usd/intern/usd_capi_import.cc b/source/blender/io/usd/intern/usd_capi_import.cc index a778b0dc414..baef9d83824 100644 --- a/source/blender/io/usd/intern/usd_capi_import.cc +++ b/source/blender/io/usd/intern/usd_capi_import.cc @@ -75,8 +75,7 @@ static bool gather_objects_paths(const pxr::UsdPrim &object, ListBase *object_pa gather_objects_paths(childPrim, object_paths); } - void *usd_path_void = MEM_callocN(sizeof(CacheObjectPath), "CacheObjectPath"); - CacheObjectPath *usd_path = static_cast(usd_path_void); + CacheObjectPath *usd_path = MEM_callocN("CacheObjectPath"); STRNCPY(usd_path->path, object.GetPrimPath().GetString().c_str()); BLI_addtail(object_paths, usd_path); diff --git a/source/blender/io/usd/intern/usd_reader_nurbs.cc b/source/blender/io/usd/intern/usd_reader_nurbs.cc index 47a8a3350b7..b414c333590 100644 --- a/source/blender/io/usd/intern/usd_reader_nurbs.cc +++ b/source/blender/io/usd/intern/usd_reader_nurbs.cc @@ -31,7 +31,7 @@ static bool set_knots(const pxr::VtDoubleArray &knots, float *&nu_knots) /* Skip first and last knots, as they are used for padding. */ const size_t num_knots = knots.size(); - nu_knots = static_cast(MEM_callocN(num_knots * sizeof(float), __func__)); + nu_knots = MEM_calloc_arrayN(num_knots, __func__); for (size_t i = 0; i < num_knots; i++) { nu_knots[i] = float(knots[i]); @@ -105,7 +105,7 @@ void USDNurbsReader::read_curve_sample(Curve *cu, const double motionSampleTime) for (size_t i = 0; i < usdCounts.size(); i++) { const int num_verts = usdCounts[i]; - Nurb *nu = static_cast(MEM_callocN(sizeof(Nurb), __func__)); + Nurb *nu = MEM_callocN(__func__); nu->flag = CU_SMOOTH; nu->type = CU_NURBS; @@ -137,7 +137,7 @@ void USDNurbsReader::read_curve_sample(Curve *cu, const double motionSampleTime) float weight = 1.0f; - nu->bp = static_cast(MEM_callocN(sizeof(BPoint) * nu->pntsu, __func__)); + nu->bp = MEM_calloc_arrayN(size_t(nu->pntsu), __func__); BPoint *bp = nu->bp; for (int j = 0; j < nu->pntsu; j++, bp++, idx++) { diff --git a/source/blender/io/wavefront_obj/importer/obj_import_nurbs.cc b/source/blender/io/wavefront_obj/importer/obj_import_nurbs.cc index 986b38de3a6..7450649007c 100644 --- a/source/blender/io/wavefront_obj/importer/obj_import_nurbs.cc +++ b/source/blender/io/wavefront_obj/importer/obj_import_nurbs.cc @@ -34,7 +34,7 @@ Curve *blender::io::obj::CurveFromGeometry::create_curve(const OBJImportParams & /* Only one NURBS spline will be created in the curve object. */ curve->actnu = 0; - Nurb *nurb = static_cast(MEM_callocN(sizeof(Nurb), __func__)); + Nurb *nurb = MEM_callocN(__func__); BLI_addtail(BKE_curve_nurbs_get(curve), nurb); this->create_nurbs(curve, import_params); @@ -63,7 +63,7 @@ Object *CurveFromGeometry::create_curve_object(Main *bmain, const OBJImportParam /* Only one NURBS spline will be created in the curve object. */ curve->actnu = 0; - Nurb *nurb = static_cast(MEM_callocN(sizeof(Nurb), __func__)); + Nurb *nurb = MEM_callocN(__func__); BLI_addtail(BKE_curve_nurbs_get(curve), nurb); this->create_nurbs(curve, import_params);