Cleanup: io: 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/135976
This commit is contained in:
Bastien Montagne
2025-03-14 14:52:16 +01:00
committed by Bastien Montagne
parent b8bd8ba36d
commit 1e976ec37b
4 changed files with 9 additions and 10 deletions

View File

@@ -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<float *>(MEM_callocN(num_knots * sizeof(float), "abc_setsplineknotsu"));
nu_knots = MEM_calloc_arrayN<float>(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<std::pair<INuPatchSchema, IObject>>::iterator it;
for (it = m_schemas.begin(); it != m_schemas.end(); ++it) {
Nurb *nu = static_cast<Nurb *>(MEM_callocN(sizeof(Nurb), "abc_getnurb"));
Nurb *nu = MEM_callocN<Nurb>("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<BPoint *>(MEM_callocN(num_points * sizeof(BPoint), "abc_setsplinetype"));
nu->bp = MEM_calloc_arrayN<BPoint>(num_points, "abc_setsplinetype");
BPoint *bp = nu->bp;
float posw_in = 1.0f;

View File

@@ -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<CacheObjectPath *>(usd_path_void);
CacheObjectPath *usd_path = MEM_callocN<CacheObjectPath>("CacheObjectPath");
STRNCPY(usd_path->path, object.GetPrimPath().GetString().c_str());
BLI_addtail(object_paths, usd_path);

View File

@@ -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<float *>(MEM_callocN(num_knots * sizeof(float), __func__));
nu_knots = MEM_calloc_arrayN<float>(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<Nurb *>(MEM_callocN(sizeof(Nurb), __func__));
Nurb *nu = MEM_callocN<Nurb>(__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<BPoint *>(MEM_callocN(sizeof(BPoint) * nu->pntsu, __func__));
nu->bp = MEM_calloc_arrayN<BPoint>(size_t(nu->pntsu), __func__);
BPoint *bp = nu->bp;
for (int j = 0; j < nu->pntsu; j++, bp++, idx++) {

View File

@@ -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<Nurb *>(MEM_callocN(sizeof(Nurb), __func__));
Nurb *nurb = MEM_callocN<Nurb>(__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<Nurb *>(MEM_callocN(sizeof(Nurb), __func__));
Nurb *nurb = MEM_callocN<Nurb>(__func__);
BLI_addtail(BKE_curve_nurbs_get(curve), nurb);
this->create_nurbs(curve, import_params);