diff --git a/source/blender/python/generic/bgl.cc b/source/blender/python/generic/bgl.cc index bbaec530bbf..c9dfbe43542 100644 --- a/source/blender/python/generic/bgl.cc +++ b/source/blender/python/generic/bgl.cc @@ -692,8 +692,7 @@ static Buffer *BGL_MakeBuffer_FromData( Py_XINCREF(parent); buffer->parent = parent; buffer->ndimensions = ndimensions; - buffer->dimensions = static_cast( - MEM_mallocN(ndimensions * sizeof(int), "Buffer dimensions")); + buffer->dimensions = MEM_malloc_arrayN(size_t(ndimensions), "Buffer dimensions"); memcpy(buffer->dimensions, dimensions, ndimensions * sizeof(int)); buffer->type = type; buffer->buf.asvoid = buf; diff --git a/source/blender/python/generic/idprop_py_api.cc b/source/blender/python/generic/idprop_py_api.cc index 3d847acbc0f..cdbbaab7323 100644 --- a/source/blender/python/generic/idprop_py_api.cc +++ b/source/blender/python/generic/idprop_py_api.cc @@ -2442,7 +2442,7 @@ static int BPy_IDArray_getbuffer(BPy_IDArray *self, Py_buffer *view, int flags) view->itemsize = itemsize; view->format = (char *)idp_format_from_array_type(prop->subtype); - Py_ssize_t *shape = static_cast(MEM_mallocN(sizeof(Py_ssize_t), __func__)); + Py_ssize_t *shape = MEM_mallocN(__func__); shape[0] = prop->len; view->shape = shape; diff --git a/source/blender/python/generic/idprop_py_ui_api.cc b/source/blender/python/generic/idprop_py_ui_api.cc index 525123f1cc9..e4ebdf0f5d7 100644 --- a/source/blender/python/generic/idprop_py_ui_api.cc +++ b/source/blender/python/generic/idprop_py_ui_api.cc @@ -178,7 +178,7 @@ static bool idprop_ui_data_update_int_default(IDProperty *idprop, } Py_ssize_t len = PySequence_Size(default_value); - int *new_default_array = (int *)MEM_malloc_arrayN(len, sizeof(int), __func__); + int *new_default_array = MEM_malloc_arrayN(size_t(len), __func__); if (PyC_AsArray( new_default_array, sizeof(int), default_value, len, &PyLong_Type, "ui_data_update") == -1) @@ -338,7 +338,7 @@ static bool idprop_ui_data_update_bool_default(IDProperty *idprop, } Py_ssize_t len = PySequence_Size(default_value); - int8_t *new_default_array = (int8_t *)MEM_malloc_arrayN(len, sizeof(int8_t), __func__); + int8_t *new_default_array = MEM_malloc_arrayN(size_t(len), __func__); if (PyC_AsArray(new_default_array, sizeof(int8_t), default_value, @@ -428,7 +428,7 @@ static bool idprop_ui_data_update_float_default(IDProperty *idprop, } Py_ssize_t len = PySequence_Size(default_value); - double *new_default_array = (double *)MEM_malloc_arrayN(len, sizeof(double), __func__); + double *new_default_array = MEM_malloc_arrayN(size_t(len), __func__); if (PyC_AsArray(new_default_array, sizeof(double), default_value, diff --git a/source/blender/python/generic/py_capi_utils.cc b/source/blender/python/generic/py_capi_utils.cc index 9619663b2e8..920bcd3afaa 100644 --- a/source/blender/python/generic/py_capi_utils.cc +++ b/source/blender/python/generic/py_capi_utils.cc @@ -1614,8 +1614,8 @@ bool PyC_RunString_AsStringAndSize(const char *imports[], ok = false; } else { - char *val_alloc = static_cast(MEM_mallocN(val_len + 1, __func__)); - memcpy(val_alloc, val, val_len + 1); + char *val_alloc = MEM_malloc_arrayN(size_t(val_len) + 1, __func__); + memcpy(val_alloc, val, (size_t(val_len) + 1) * sizeof(*val_alloc)); *r_value = val_alloc; *r_value_size = val_len; ok = true; @@ -1658,8 +1658,8 @@ bool PyC_RunString_AsStringAndSizeOrNone(const char *imports[], ok = false; } else { - char *val_alloc = static_cast(MEM_mallocN(val_len + 1, __func__)); - memcpy(val_alloc, val, val_len + 1); + char *val_alloc = MEM_malloc_arrayN(size_t(val_len) + 1, __func__); + memcpy(val_alloc, val, (size_t(val_len) + 1) * sizeof(val_alloc)); *r_value = val_alloc; *r_value_size = val_len; ok = true; diff --git a/source/blender/python/gpu/gpu_py_buffer.cc b/source/blender/python/gpu/gpu_py_buffer.cc index c0964a8c715..aa2c45453a0 100644 --- a/source/blender/python/gpu/gpu_py_buffer.cc +++ b/source/blender/python/gpu/gpu_py_buffer.cc @@ -151,9 +151,8 @@ static BPyGPUBuffer *pygpu_buffer_make_from_data(PyObject *parent, buffer->parent = nullptr; buffer->format = format; buffer->shape_len = shape_len; - buffer->shape = static_cast( - MEM_mallocN(shape_len * sizeof(*buffer->shape), "BPyGPUBuffer shape")); - memcpy(buffer->shape, shape, shape_len * sizeof(*buffer->shape)); + buffer->shape = MEM_malloc_arrayN(size_t(shape_len), "BPyGPUBuffer shape"); + memcpy(buffer->shape, shape, sizeof(*buffer->shape) * size_t(shape_len)); buffer->buf.as_void = buf; if (parent) { @@ -265,14 +264,13 @@ static int pygpu_buffer_dimensions_set(BPyGPUBuffer *self, PyObject *value, void return -1; } - size_t size = shape_len * sizeof(*self->shape); if (shape_len != self->shape_len) { MEM_freeN(self->shape); - self->shape = static_cast(MEM_mallocN(size, __func__)); + self->shape = MEM_malloc_arrayN(size_t(shape_len), __func__); } self->shape_len = shape_len; - memcpy(self->shape, shape, size); + memcpy(self->shape, shape, sizeof(*self->shape) * size_t(shape_len)); return 0; } @@ -638,8 +636,7 @@ static int pygpu_buffer__bf_getbuffer(BPyGPUBuffer *self, Py_buffer *view, int f view->shape = self->shape; } if (flags & PyBUF_STRIDES) { - view->strides = static_cast( - MEM_mallocN(view->ndim * sizeof(*view->strides), "BPyGPUBuffer strides")); + view->strides = MEM_malloc_arrayN(size_t(view->ndim), "BPyGPUBuffer strides"); pygpu_buffer_strides_calc( eGPUDataFormat(self->format), view->ndim, view->shape, view->strides); } diff --git a/source/blender/python/intern/bpy_app_icons.cc b/source/blender/python/intern/bpy_app_icons.cc index 003535686c4..12982df6833 100644 --- a/source/blender/python/intern/bpy_app_icons.cc +++ b/source/blender/python/intern/bpy_app_icons.cc @@ -68,15 +68,14 @@ static PyObject *bpy_app_icons_new_triangles(PyObject * /*self*/, PyObject *args return nullptr; } - const int coords_size = sizeof(uchar[2]) * tris_len * 3; - const int colors_size = sizeof(uchar[4]) * tris_len * 3; - uchar(*coords)[2] = static_cast(MEM_mallocN(coords_size, __func__)); - uchar(*colors)[4] = static_cast(MEM_mallocN(colors_size, __func__)); + const size_t items_num = size_t(tris_len) * 3; + uchar(*coords)[2] = MEM_malloc_arrayN(items_num, __func__); + uchar(*colors)[4] = MEM_malloc_arrayN(items_num, __func__); - memcpy(coords, PyBytes_AS_STRING(py_coords), coords_size); - memcpy(colors, PyBytes_AS_STRING(py_colors), colors_size); + memcpy(coords, PyBytes_AS_STRING(py_coords), sizeof(*coords) * items_num); + memcpy(colors, PyBytes_AS_STRING(py_colors), sizeof(*colors) * items_num); - Icon_Geom *geom = static_cast(MEM_mallocN(sizeof(*geom), __func__)); + Icon_Geom *geom = MEM_mallocN(__func__); geom->coords_len = tris_len; geom->coords_range[0] = coords_range[0]; geom->coords_range[1] = coords_range[1]; diff --git a/source/blender/python/intern/bpy_operator.cc b/source/blender/python/intern/bpy_operator.cc index 4fd80344093..0ac94accab4 100644 --- a/source/blender/python/intern/bpy_operator.cc +++ b/source/blender/python/intern/bpy_operator.cc @@ -229,7 +229,7 @@ static PyObject *pyop_call(PyObject * /*self*/, PyObject *args) if (error_val == 0) { ReportList *reports; - reports = static_cast(MEM_mallocN(sizeof(ReportList), "wmOperatorReportList")); + reports = MEM_mallocN("wmOperatorReportList"); /* Own so these don't move into global reports. */ BKE_reports_init(reports, RPT_STORE | RPT_OP_HOLD | RPT_PRINT_HANDLED_BY_OWNER); diff --git a/source/blender/python/intern/bpy_props.cc b/source/blender/python/intern/bpy_props.cc index 2358d6a8ebe..2ab3ce382ad 100644 --- a/source/blender/python/intern/bpy_props.cc +++ b/source/blender/python/intern/bpy_props.cc @@ -174,7 +174,7 @@ static BPyPropStore *bpy_prop_py_data_ensure(PropertyRNA *prop) { BPyPropStore *prop_store = static_cast(RNA_property_py_data_get(prop)); if (prop_store == nullptr) { - prop_store = static_cast(MEM_callocN(sizeof(*prop_store), __func__)); + prop_store = MEM_callocN(__func__); RNA_def_py_data(prop, prop_store); BLI_addtail(&g_bpy_prop_store_list, prop_store); } @@ -1763,8 +1763,7 @@ static const EnumPropertyItem *enum_items_from_py(PyObject *seq_fast, /* blank value */ *r_default_value = 0; - items = static_cast( - MEM_callocN(sizeof(EnumPropertyItem) * (seq_len + 1), "enum_items_from_py1")); + items = MEM_calloc_arrayN(size_t(seq_len) + 1, "enum_items_from_py1"); for (i = 0; i < seq_len; i++) { EnumPropertyItem tmp = {0, "", 0, "", ""}; diff --git a/source/blender/python/intern/bpy_rna_gizmo.cc b/source/blender/python/intern/bpy_rna_gizmo.cc index 2b16345177f..185b5cea979 100644 --- a/source/blender/python/intern/bpy_rna_gizmo.cc +++ b/source/blender/python/intern/bpy_rna_gizmo.cc @@ -399,7 +399,7 @@ static PyObject *bpy_gizmo_target_set_handler(PyObject * /*self*/, PyObject *arg } } - data = static_cast(MEM_callocN(sizeof(*data), __func__)); + data = MEM_callocN(__func__); for (int i = 0; i < BPY_GIZMO_FN_SLOT_LEN; i++) { data->fn_slots[i] = params.py_fn_slots[i]; diff --git a/source/blender/python/mathutils/mathutils_bvhtree.cc b/source/blender/python/mathutils/mathutils_bvhtree.cc index 54b9ab6374e..fc57d1be0a8 100644 --- a/source/blender/python/mathutils/mathutils_bvhtree.cc +++ b/source/blender/python/mathutils/mathutils_bvhtree.cc @@ -694,7 +694,7 @@ static PyObject *C_BVHTree_FromPolygons(PyObject * /*cls*/, PyObject *args, PyOb if (valid) { PyObject **py_coords_fast_items = PySequence_Fast_ITEMS(py_coords_fast); coords_len = uint(PySequence_Fast_GET_SIZE(py_coords_fast)); - coords = static_cast(MEM_mallocN(size_t(coords_len) * sizeof(*coords), __func__)); + coords = MEM_malloc_arrayN(size_t(coords_len), __func__); for (i = 0; i < coords_len; i++) { PyObject *py_vert = py_coords_fast_items[i]; @@ -713,7 +713,7 @@ static PyObject *C_BVHTree_FromPolygons(PyObject * /*cls*/, PyObject *args, PyOb /* all triangles, simple case */ PyObject **py_tris_fast_items = PySequence_Fast_ITEMS(py_tris_fast); tris_len = uint(PySequence_Fast_GET_SIZE(py_tris_fast)); - tris = static_cast(MEM_mallocN(size_t(tris_len) * sizeof(*tris), __func__)); + tris = MEM_malloc_arrayN(size_t(tris_len), __func__); for (i = 0; i < tris_len; i++) { PyObject *py_tricoords = py_tris_fast_items[i]; @@ -819,11 +819,10 @@ static PyObject *C_BVHTree_FromPolygons(PyObject * /*cls*/, PyObject *args, PyOb /* All NGON's are parsed, now tessellate. */ pf_arena = BLI_memarena_new(BLI_POLYFILL_ARENA_SIZE, __func__); - tris = static_cast(MEM_mallocN(sizeof(*tris) * size_t(tris_len), __func__)); + tris = MEM_malloc_arrayN(size_t(tris_len), __func__); - orig_index = static_cast(MEM_mallocN(sizeof(*orig_index) * size_t(tris_len), __func__)); - orig_normal = static_cast( - MEM_mallocN(sizeof(*orig_normal) * size_t(polys_len), __func__)); + orig_index = MEM_malloc_arrayN(size_t(tris_len), __func__); + orig_normal = MEM_malloc_arrayN(size_t(polys_len), __func__); for (plink = plink_first, poly_index = 0, i = 0; plink; plink = plink->next, poly_index++) { if (plink->len == 3) { @@ -966,8 +965,8 @@ static PyObject *C_BVHTree_FromBMesh(PyObject * /*cls*/, PyObject *args, PyObjec coords_len = uint(bm->totvert); tris_len = uint(poly_to_tri_count(bm->totface, bm->totloop)); - coords = static_cast(MEM_mallocN(sizeof(*coords) * size_t(coords_len), __func__)); - tris = static_cast(MEM_mallocN(sizeof(*tris) * size_t(tris_len), __func__)); + coords = MEM_malloc_arrayN(size_t(coords_len), __func__); + tris = MEM_malloc_arrayN(size_t(tris_len), __func__); blender::Array> corner_tris(tris_len); BM_mesh_calc_tessellation(bm, corner_tris); @@ -985,10 +984,8 @@ static PyObject *C_BVHTree_FromBMesh(PyObject * /*cls*/, PyObject *args, PyObjec BMFace *f; BMVert *v; - orig_index = static_cast( - MEM_mallocN(sizeof(*orig_index) * size_t(tris_len), __func__)); - orig_normal = static_cast( - MEM_mallocN(sizeof(*orig_normal) * size_t(bm->totface), __func__)); + orig_index = MEM_malloc_arrayN(size_t(tris_len), __func__); + orig_normal = MEM_malloc_arrayN(size_t(bm->totface), __func__); BM_ITER_MESH_INDEX (v, &iter, bm, BM_VERTS_OF_MESH, i) { copy_v3_v3(coords[i], v->co); @@ -1191,10 +1188,8 @@ static PyObject *C_BVHTree_FromObject(PyObject * /*cls*/, PyObject *args, PyObje const uint coords_len = uint(mesh->verts_num); - float(*coords)[3] = static_cast( - MEM_mallocN(sizeof(*coords) * size_t(coords_len), __func__)); - uint(*tris)[3] = static_cast( - MEM_mallocN(sizeof(*tris) * size_t(corner_tris.size()), __func__)); + float(*coords)[3] = MEM_malloc_arrayN(size_t(coords_len), __func__); + uint(*tris)[3] = MEM_malloc_arrayN(size_t(corner_tris.size()), __func__); memcpy(coords, mesh->vert_positions().data(), sizeof(float[3]) * size_t(mesh->verts_num)); BVHTree *tree; @@ -1205,12 +1200,10 @@ static PyObject *C_BVHTree_FromObject(PyObject * /*cls*/, PyObject *args, PyObje tree = BLI_bvhtree_new( int(corner_tris.size()), epsilon, PY_BVH_TREE_TYPE_DEFAULT, PY_BVH_AXIS_DEFAULT); if (tree) { - orig_index = static_cast( - MEM_mallocN(sizeof(*orig_index) * size_t(corner_tris.size()), __func__)); + orig_index = MEM_malloc_arrayN(size_t(corner_tris.size()), __func__); if (!BKE_mesh_face_normals_are_dirty(mesh)) { const blender::Span face_normals = mesh->face_normals(); - orig_normal = static_cast( - MEM_malloc_arrayN(size_t(mesh->faces_num), sizeof(blender::float3), __func__)); + orig_normal = MEM_malloc_arrayN(size_t(mesh->faces_num), __func__); blender::MutableSpan(orig_normal, face_normals.size()).copy_from(face_normals); } diff --git a/source/blender/python/mathutils/mathutils_geometry.cc b/source/blender/python/mathutils/mathutils_geometry.cc index 7b27f2d01c0..148b8567c19 100644 --- a/source/blender/python/mathutils/mathutils_geometry.cc +++ b/source/blender/python/mathutils/mathutils_geometry.cc @@ -1265,7 +1265,7 @@ static PyObject *M_Geometry_interpolate_bezier(PyObject * /*self*/, PyObject *ar return nullptr; } - coord_array = static_cast(MEM_callocN(dims * (resolu) * sizeof(float), error_prefix)); + coord_array = MEM_calloc_arrayN(size_t(dims) * size_t(resolu), error_prefix); for (i = 0; i < dims; i++) { BKE_curve_forward_diff_bezier( UNPACK4_EX(, data, [i]), coord_array + i, resolu - 1, sizeof(float) * dims); @@ -1327,15 +1327,14 @@ static PyObject *M_Geometry_tessellate_polygon(PyObject * /*self*/, PyObject *po len_polypoints = PySequence_Size(polyLine); if (len_polypoints > 0) { /* don't bother adding edges as polylines */ - dl = static_cast(MEM_callocN(sizeof(DispList), "poly disp")); + dl = MEM_callocN("poly disp"); BLI_addtail(&dispbase, dl); dl->nr = len_polypoints; dl->type = DL_POLY; dl->parts = 1; /* no faces, 1 edge loop */ dl->col = 0; /* no material */ - dl->verts = fp = static_cast( - MEM_mallocN(sizeof(float[3]) * len_polypoints, "dl verts")); - dl->index = static_cast(MEM_callocN(sizeof(int[3]) * len_polypoints, "dl index")); + dl->verts = fp = MEM_malloc_arrayN(3 * size_t(len_polypoints), "dl verts"); + dl->index = MEM_calloc_arrayN(3 * size_t(len_polypoints), "dl index"); for (int index = 0; index < len_polypoints; index++, fp += 3) { polyVec = PySequence_GetItem(polyLine, index); @@ -1409,7 +1408,7 @@ static int boxPack_FromPyObject(PyObject *value, BoxPack **r_boxarray) len = PyList_GET_SIZE(value); - boxarray = static_cast(MEM_mallocN(sizeof(BoxPack) * len, __func__)); + boxarray = MEM_malloc_arrayN(size_t(len), __func__); for (i = 0; i < len; i++) { list_item = PyList_GET_ITEM(value, i); @@ -1564,7 +1563,7 @@ static PyObject *M_Geometry_convex_hull_2d(PyObject * /*self*/, PyObject *pointl int *index_map; Py_ssize_t len_ret, i; - index_map = static_cast(MEM_mallocN(sizeof(*index_map) * len, __func__)); + index_map = MEM_malloc_arrayN(size_t(len), __func__); /* Non Python function */ len_ret = BLI_convexhull_2d(points, len, index_map); diff --git a/source/blender/python/mathutils/mathutils_interpolate.cc b/source/blender/python/mathutils/mathutils_interpolate.cc index 5f4a1cd1786..9c55f828e43 100644 --- a/source/blender/python/mathutils/mathutils_interpolate.cc +++ b/source/blender/python/mathutils/mathutils_interpolate.cc @@ -65,7 +65,7 @@ static PyObject *M_Interpolate_poly_3d_calc(PyObject * /*self*/, PyObject *args) } if (len) { - float *weights = static_cast(MEM_mallocN(sizeof(float) * len, __func__)); + float *weights = MEM_malloc_arrayN(size_t(len), __func__); interp_weights_poly_v3(weights, vecs, len, fp); diff --git a/source/blender/python/mathutils/mathutils_kdtree.cc b/source/blender/python/mathutils/mathutils_kdtree.cc index 0f0c51ef27c..84bfb310f1f 100644 --- a/source/blender/python/mathutils/mathutils_kdtree.cc +++ b/source/blender/python/mathutils/mathutils_kdtree.cc @@ -293,7 +293,7 @@ static PyObject *py_kdtree_find_n(PyKDTree *self, PyObject *args, PyObject *kwar return nullptr; } - nearest = static_cast(MEM_mallocN(sizeof(KDTreeNearest_3d) * n, __func__)); + nearest = MEM_malloc_arrayN(n, __func__); found = BLI_kdtree_3d_find_nearest_n(self->obj, co, nearest, n);