diff --git a/source/blender/blenlib/intern/BLI_dynstr.cc b/source/blender/blenlib/intern/BLI_dynstr.cc index b1a61fcfe64..85d0247511d 100644 --- a/source/blender/blenlib/intern/BLI_dynstr.cc +++ b/source/blender/blenlib/intern/BLI_dynstr.cc @@ -141,7 +141,7 @@ void BLI_dynstr_get_cstring_ex(const DynStr *__restrict ds, char *__restrict ret char *BLI_dynstr_get_cstring(const DynStr *ds) { - char *rets = static_cast(MEM_mallocN(ds->curlen + 1, "dynstr_cstring")); + char *rets = MEM_malloc_arrayN(size_t(ds->curlen) + 1, "dynstr_cstring"); BLI_dynstr_get_cstring_ex(ds, rets); return rets; } diff --git a/source/blender/blenlib/intern/BLI_filelist.cc b/source/blender/blenlib/intern/BLI_filelist.cc index 219b88c6dc5..57fb969e6f7 100644 --- a/source/blender/blenlib/intern/BLI_filelist.cc +++ b/source/blender/blenlib/intern/BLI_filelist.cc @@ -184,7 +184,7 @@ static void bli_builddir(BuildDirCtx *dir_ctx, const char *dirname) } if (dir_ctx->files == nullptr) { - dir_ctx->files = (direntry *)MEM_mallocN(newnum * sizeof(direntry), __func__); + dir_ctx->files = MEM_malloc_arrayN(size_t(newnum), __func__); } if (UNLIKELY(dir_ctx->files == nullptr)) { @@ -239,7 +239,7 @@ uint BLI_filelist_dir_contents(const char *dirname, direntry **r_filelist) else { /* Keep Blender happy. Blender stores this in a variable * where 0 has special meaning..... */ - *r_filelist = static_cast(MEM_mallocN(sizeof(**r_filelist), __func__)); + *r_filelist = MEM_mallocN(__func__); } return dir_ctx.files_num; @@ -419,8 +419,7 @@ void BLI_filelist_duplicate(direntry **dest_filelist, { uint i; - *dest_filelist = static_cast( - MEM_mallocN(sizeof(**dest_filelist) * size_t(nrentries), __func__)); + *dest_filelist = MEM_malloc_arrayN(size_t(nrentries), __func__); for (i = 0; i < nrentries; i++) { const direntry *src = &src_filelist[i]; direntry *dst = &(*dest_filelist)[i]; diff --git a/source/blender/blenlib/intern/BLI_ghash.cc b/source/blender/blenlib/intern/BLI_ghash.cc index 2df955b571f..cf37fc9cdcd 100644 --- a/source/blender/blenlib/intern/BLI_ghash.cc +++ b/source/blender/blenlib/intern/BLI_ghash.cc @@ -193,7 +193,7 @@ static void ghash_buckets_resize(GHash *gh, const uint nbuckets) gh->bucket_mask = nbuckets - 1; #endif - buckets_new = (Entry **)MEM_callocN(sizeof(*gh->buckets) * gh->nbuckets, __func__); + buckets_new = MEM_calloc_arrayN(gh->nbuckets, __func__); if (buckets_old) { if (nbuckets > nbuckets_old) { diff --git a/source/blender/blenlib/intern/BLI_ghash_utils.cc b/source/blender/blenlib/intern/BLI_ghash_utils.cc index c5df7cf4e73..1ecc1c20503 100644 --- a/source/blender/blenlib/intern/BLI_ghash_utils.cc +++ b/source/blender/blenlib/intern/BLI_ghash_utils.cc @@ -154,7 +154,7 @@ bool BLI_ghashutil_strcmp(const void *a, const void *b) GHashPair *BLI_ghashutil_pairalloc(const void *first, const void *second) { - GHashPair *pair = static_cast(MEM_mallocN(sizeof(GHashPair), "GHashPair")); + GHashPair *pair = MEM_mallocN("GHashPair"); pair->first = first; pair->second = second; return pair; diff --git a/source/blender/blenlib/intern/BLI_kdopbvh.cc b/source/blender/blenlib/intern/BLI_kdopbvh.cc index ddbee96847d..64a815c48fc 100644 --- a/source/blender/blenlib/intern/BLI_kdopbvh.cc +++ b/source/blender/blenlib/intern/BLI_kdopbvh.cc @@ -1405,8 +1405,7 @@ BVHTreeOverlap *BLI_bvhtree_overlap_ex( total += BLI_stack_count(data[j].overlap); } - to = overlap = static_cast( - MEM_mallocN(sizeof(BVHTreeOverlap) * total, "BVHTreeOverlap")); + to = overlap = MEM_malloc_arrayN(total, "BVHTreeOverlap"); for (j = 0; j < thread_num; j++) { uint count = uint(BLI_stack_count(data[j].overlap)); @@ -1510,7 +1509,7 @@ int *BLI_bvhtree_intersect_plane(const BVHTree *tree, float plane[4], uint *r_in total = BLI_stack_count(data.intersect); if (total) { - intersect = static_cast(MEM_mallocN(sizeof(int) * total, __func__)); + intersect = MEM_malloc_arrayN(total, __func__); BLI_stack_pop_n(data.intersect, intersect, uint(total)); } BLI_stack_free(data.intersect); diff --git a/source/blender/blenlib/intern/BLI_linklist.cc b/source/blender/blenlib/intern/BLI_linklist.cc index 175b601e57a..3c0ffed8f15 100644 --- a/source/blender/blenlib/intern/BLI_linklist.cc +++ b/source/blender/blenlib/intern/BLI_linklist.cc @@ -160,7 +160,7 @@ void BLI_linklist_prepend_nlink(LinkNode **listp, void *ptr, LinkNode *nlink) void BLI_linklist_prepend(LinkNode **listp, void *ptr) { - LinkNode *nlink = static_cast(MEM_mallocN(sizeof(*nlink), __func__)); + LinkNode *nlink = MEM_mallocN(__func__); BLI_linklist_prepend_nlink(listp, ptr, nlink); } @@ -195,7 +195,7 @@ void BLI_linklist_append_nlink(LinkNodePair *list_pair, void *ptr, LinkNode *nli void BLI_linklist_append(LinkNodePair *list_pair, void *ptr) { - LinkNode *nlink = static_cast(MEM_mallocN(sizeof(*nlink), __func__)); + LinkNode *nlink = MEM_mallocN(__func__); BLI_linklist_append_nlink(list_pair, ptr, nlink); } diff --git a/source/blender/blenlib/intern/array_store.cc b/source/blender/blenlib/intern/array_store.cc index 17f314b2574..c11df9cd988 100644 --- a/source/blender/blenlib/intern/array_store.cc +++ b/source/blender/blenlib/intern/array_store.cc @@ -367,7 +367,7 @@ static BChunk *bchunk_new(BArrayMemory *bs_mem, const uchar *data, const size_t static BChunk *bchunk_new_copydata(BArrayMemory *bs_mem, const uchar *data, const size_t data_len) { - uchar *data_copy = static_cast(MEM_mallocN(data_len, __func__)); + uchar *data_copy = MEM_malloc_arrayN(data_len, __func__); memcpy(data_copy, data, data_len); return bchunk_new(bs_mem, data_copy, data_len); } @@ -492,7 +492,7 @@ static void bchunk_list_ensure_min_size_last(const BArrayInfo *info, chunk_list->chunk_refs.last = cref->prev; chunk_list->chunk_refs_len -= 1; - uchar *data_merge = static_cast(MEM_mallocN(data_merge_len, __func__)); + uchar *data_merge = MEM_malloc_arrayN(data_merge_len, __func__); memcpy(data_merge, chunk_prev->data, chunk_prev->data_len); memcpy(&data_merge[chunk_prev->data_len], chunk_curr->data, chunk_curr->data_len); @@ -514,8 +514,8 @@ static void bchunk_list_ensure_min_size_last(const BArrayInfo *info, /* Merge and split. */ const size_t data_prev_len = split; const size_t data_curr_len = data_merge_len - split; - uchar *data_prev = static_cast(MEM_mallocN(data_prev_len, __func__)); - uchar *data_curr = static_cast(MEM_mallocN(data_curr_len, __func__)); + uchar *data_prev = MEM_malloc_arrayN(data_prev_len, __func__); + uchar *data_curr = MEM_malloc_arrayN(data_curr_len, __func__); if (data_prev_len <= chunk_prev->data_len) { const size_t data_curr_shrink_len = chunk_prev->data_len - data_prev_len; @@ -644,7 +644,7 @@ static void bchunk_list_append_data(const BArrayInfo *info, cref->link->data_len = data_merge_len; } else { - uchar *data_merge = static_cast(MEM_mallocN(data_merge_len, __func__)); + uchar *data_merge = MEM_malloc_arrayN(data_merge_len, __func__); memcpy(data_merge, chunk_prev->data, chunk_prev->data_len); memcpy(&data_merge[chunk_prev->data_len], data, data_len); cref->link = bchunk_new(bs_mem, data_merge, data_merge_len); @@ -1264,8 +1264,7 @@ static BChunkList *bchunk_list_from_data_merge(const BArrayInfo *info, #ifdef USE_HASH_TABLE_ACCUMULATE size_t i_table_start = i_prev; const size_t table_hash_array_len = (data_len - i_prev) / info->chunk_stride; - hash_key *table_hash_array = static_cast( - MEM_mallocN(sizeof(*table_hash_array) * table_hash_array_len, __func__)); + hash_key *table_hash_array = MEM_malloc_arrayN(table_hash_array_len, __func__); hash_array_from_data(info, &data[i_prev], data_len - i_prev, table_hash_array); hash_accum(table_hash_array, table_hash_array_len, info->accum_steps); @@ -1278,21 +1277,19 @@ static BChunkList *bchunk_list_from_data_merge(const BArrayInfo *info, const uint chunk_list_reference_remaining_len = (chunk_list_reference->chunk_refs_len - chunk_list_reference_skip_len) + 1; - BTableRef *table_ref_stack = static_cast( - MEM_mallocN(chunk_list_reference_remaining_len * sizeof(BTableRef), __func__)); + BTableRef *table_ref_stack = MEM_malloc_arrayN(chunk_list_reference_remaining_len, + __func__); uint table_ref_stack_n = 0; const size_t table_len = chunk_list_reference_remaining_len * BCHUNK_HASH_TABLE_MUL; - BTableRef **table = static_cast( - MEM_callocN(table_len * sizeof(*table), __func__)); + BTableRef **table = MEM_calloc_arrayN(table_len, __func__); /* Table_make - inline * include one matching chunk, to allow for repeating values. */ { #ifdef USE_HASH_TABLE_ACCUMULATE const size_t hash_store_len = info->accum_read_ahead_len; - hash_key *hash_store = static_cast( - MEM_mallocN(sizeof(hash_key) * hash_store_len, __func__)); + hash_key *hash_store = MEM_malloc_arrayN(hash_store_len, __func__); #endif const BChunkRef *cref; diff --git a/source/blender/blenlib/intern/bitmap_draw_2d.cc b/source/blender/blenlib/intern/bitmap_draw_2d.cc index 659dc8554f8..16513c2dfea 100644 --- a/source/blender/blenlib/intern/bitmap_draw_2d.cc +++ b/source/blender/blenlib/intern/bitmap_draw_2d.cc @@ -328,8 +328,7 @@ void BLI_bitmap_draw_2d_poly_v2i_n(const int xmin, /* Originally by Darel Rex Finley, 2007. * Optimized by Campbell Barton, 2016 to track sorted intersections. */ - int(*span_y)[2] = static_cast( - MEM_mallocN(sizeof(*span_y) * size_t(verts.size()), __func__)); + int(*span_y)[2] = MEM_malloc_arrayN(size_t(verts.size()), __func__); int span_y_len = 0; for (int i_curr = 0, i_prev = int(verts.size() - 1); i_curr < verts.size(); i_prev = i_curr++) { @@ -363,8 +362,7 @@ void BLI_bitmap_draw_2d_poly_v2i_n(const int xmin, struct NodeX { int span_y_index; int x; - } *node_x = static_cast( - MEM_mallocN(sizeof(*node_x) * size_t(verts.size() + 1), __func__)); + } *node_x = MEM_malloc_arrayN(size_t(verts.size() + 1), __func__); int node_x_len = 0; int span_y_index = 0; diff --git a/source/blender/blenlib/intern/boxpack_2d.cc b/source/blender/blenlib/intern/boxpack_2d.cc index 9cb05a2fb67..9aa157d29f5 100644 --- a/source/blender/blenlib/intern/boxpack_2d.cc +++ b/source/blender/blenlib/intern/boxpack_2d.cc @@ -285,9 +285,8 @@ void BLI_box_pack_2d( } /* Add verts to the boxes, these are only used internally. */ - vert = static_cast(MEM_mallocN(sizeof(BoxVert[4]) * size_t(len), "BoxPack Verts")); - vertex_pack_indices = static_cast( - MEM_mallocN(sizeof(int[3]) * size_t(len), "BoxPack Indices")); + vert = MEM_malloc_arrayN(4 * size_t(len), "BoxPack Verts"); + vertex_pack_indices = MEM_malloc_arrayN(3 * size_t(len), "BoxPack Indices"); vs_ctx.vertarray = vert; diff --git a/source/blender/blenlib/intern/buffer.cc b/source/blender/blenlib/intern/buffer.cc index c6d17782de9..eb408fb2828 100644 --- a/source/blender/blenlib/intern/buffer.cc +++ b/source/blender/blenlib/intern/buffer.cc @@ -40,7 +40,7 @@ static void *buffer_alloc(const BLI_Buffer *buffer, const size_t len) { - return MEM_mallocN(buffer->elem_size * len, "BLI_Buffer.data"); + return MEM_malloc_arrayN(len, buffer->elem_size, "BLI_Buffer.data"); } static void *buffer_realloc(BLI_Buffer *buffer, const size_t len) diff --git a/source/blender/blenlib/intern/convexhull_2d.cc b/source/blender/blenlib/intern/convexhull_2d.cc index 74a2e68f620..94e86ad7bc5 100644 --- a/source/blender/blenlib/intern/convexhull_2d.cc +++ b/source/blender/blenlib/intern/convexhull_2d.cc @@ -191,9 +191,8 @@ int BLI_convexhull_2d(const float (*points)[2], const int points_num, int r_poin } return points_num; } - int *points_map = static_cast(MEM_mallocN(sizeof(int) * size_t(points_num), __func__)); - float(*points_sort)[2] = static_cast( - MEM_mallocN(sizeof(*points_sort) * size_t(points_num), __func__)); + int *points_map = MEM_malloc_arrayN(size_t(points_num), __func__); + float(*points_sort)[2] = MEM_malloc_arrayN(size_t(points_num), __func__); for (int i = 0; i < points_num; i++) { points_map[i] = i; @@ -659,14 +658,12 @@ float BLI_convexhull_aabb_fit_points_2d(const float (*points)[2], int points_num BLI_assert(points_num >= 0); float angle = 0.0f; - int *index_map = static_cast( - MEM_mallocN(sizeof(*index_map) * size_t(points_num), __func__)); + int *index_map = MEM_malloc_arrayN(size_t(points_num), __func__); int points_hull_num = BLI_convexhull_2d(points, points_num, index_map); if (points_hull_num > 1) { - float(*points_hull)[2] = static_cast( - MEM_mallocN(sizeof(*points_hull) * size_t(points_hull_num), __func__)); + float(*points_hull)[2] = MEM_malloc_arrayN(size_t(points_hull_num), __func__); for (int j = 0; j < points_hull_num; j++) { copy_v2_v2(points_hull[j], points[index_map[j]]); } diff --git a/source/blender/blenlib/intern/filereader_zstd.cc b/source/blender/blenlib/intern/filereader_zstd.cc index 0de92aa6dca..f0ff4e8df8a 100644 --- a/source/blender/blenlib/intern/filereader_zstd.cc +++ b/source/blender/blenlib/intern/filereader_zstd.cc @@ -101,10 +101,8 @@ static bool zstd_read_seek_table(ZstdReader *zstd) } zstd->seek.frames_num = frames_num; - zstd->seek.compressed_ofs = static_cast( - MEM_malloc_arrayN(frames_num + 1, sizeof(size_t), __func__)); - zstd->seek.uncompressed_ofs = static_cast( - MEM_malloc_arrayN(frames_num + 1, sizeof(size_t), __func__)); + zstd->seek.compressed_ofs = MEM_malloc_arrayN(frames_num + 1, __func__); + zstd->seek.uncompressed_ofs = MEM_malloc_arrayN(frames_num + 1, __func__); size_t compressed_ofs = 0; size_t uncompressed_ofs = 0; diff --git a/source/blender/blenlib/intern/jitter_2d.cc b/source/blender/blenlib/intern/jitter_2d.cc index b376476ad30..6fdd45ac8bb 100644 --- a/source/blender/blenlib/intern/jitter_2d.cc +++ b/source/blender/blenlib/intern/jitter_2d.cc @@ -139,7 +139,7 @@ void BLI_jitter_init(float (*jitarr)[2], int num) number_fl = float(num); number_fl_sqrt = sqrtf(number_fl); - jit2 = static_cast(MEM_mallocN(12 + uint(num) * sizeof(float[2]), "initjit")); + jit2 = MEM_malloc_arrayN(2 + size_t(num), "initjit"); rad1 = 1.0f / number_fl_sqrt; rad2 = 1.0f / number_fl; rad3 = number_fl_sqrt / number_fl; diff --git a/source/blender/blenlib/intern/kdtree_impl.h b/source/blender/blenlib/intern/kdtree_impl.h index bd7ca4a350a..a8a6fe23f63 100644 --- a/source/blender/blenlib/intern/kdtree_impl.h +++ b/source/blender/blenlib/intern/kdtree_impl.h @@ -95,6 +95,10 @@ KDTree *BLI_kdtree_nd_(new)(uint nodes_len_capacity) KDTree *tree; tree = MEM_callocN("KDTree"); + /* NOTE: Cannot use `MEM_malloc_arrayN()` here, as `KDTreeNode` is not one type, but + * four (1D to 4D), differing by their `float co[KD_DIMS]` member. It seems like the code + * generating the templates does not distinguish these cases, and create a single code for all + * four cases, leading to invalid allocation sizes. */ tree->nodes = static_cast( MEM_mallocN(sizeof(KDTreeNode) * nodes_len_capacity, "KDTreeNode")); tree->nodes_len = 0; @@ -216,8 +220,8 @@ void BLI_kdtree_nd_(balance)(KDTree *tree) static uint *realloc_nodes(uint *stack, uint *stack_len_capacity, const bool is_alloc) { - uint *stack_new = static_cast( - MEM_mallocN((*stack_len_capacity + KD_NEAR_ALLOC_INC) * sizeof(uint), "KDTree.treestack")); + uint *stack_new = MEM_malloc_arrayN(*stack_len_capacity + KD_NEAR_ALLOC_INC, + "KDTree.treestack"); memcpy(stack_new, stack, *stack_len_capacity * sizeof(uint)); // memset(stack_new + *stack_len_capacity, 0, sizeof(uint) * KD_NEAR_ALLOC_INC); if (is_alloc) { diff --git a/source/blender/blenlib/intern/math_solvers.cc b/source/blender/blenlib/intern/math_solvers.cc index cc72f914a8f..7b269c02549 100644 --- a/source/blender/blenlib/intern/math_solvers.cc +++ b/source/blender/blenlib/intern/math_solvers.cc @@ -51,8 +51,7 @@ bool BLI_tridiagonal_solve( return false; } - size_t bytes = sizeof(double) * uint(count); - double *c1 = (double *)MEM_mallocN(bytes * 2, "tridiagonal_c1d1"); + double *c1 = MEM_malloc_arrayN(size_t(count) * 2, "tridiagonal_c1d1"); if (!c1) { return false; } @@ -118,7 +117,7 @@ bool BLI_tridiagonal_solve_cyclic( } size_t bytes = sizeof(float) * uint(count); - float *tmp = (float *)MEM_mallocN(bytes * 2, "tridiagonal_ex"); + float *tmp = MEM_malloc_arrayN(size_t(count) * 2, "tridiagonal_ex"); if (!tmp) { return false; } diff --git a/source/blender/blenlib/intern/mesh_intersect.cc b/source/blender/blenlib/intern/mesh_intersect.cc index 7409dac53ff..6b91b8ca549 100644 --- a/source/blender/blenlib/intern/mesh_intersect.cc +++ b/source/blender/blenlib/intern/mesh_intersect.cc @@ -2032,8 +2032,8 @@ static Array polyfill_triangulate_poly(Face *f, IMeshArena *arena) uint(*tris)[3]; const int totfilltri = flen - 2; /* Prepare projected vertices and array to receive triangles in tessellation. */ - tris = static_cast(MEM_malloc_arrayN(totfilltri, sizeof(*tris), __func__)); - projverts = static_cast(MEM_malloc_arrayN(flen, sizeof(*projverts), __func__)); + tris = MEM_malloc_arrayN(size_t(totfilltri), __func__); + projverts = MEM_malloc_arrayN(size_t(flen), __func__); axis_dominant_v3_to_m3_negate(axis_mat, no); for (int j = 0; j < flen; ++j) { const double3 &dco = (*f)[j]->co; diff --git a/source/blender/blenlib/intern/scanfill.cc b/source/blender/blenlib/intern/scanfill.cc index 71e22ccda32..09aadd7b22b 100644 --- a/source/blender/blenlib/intern/scanfill.cc +++ b/source/blender/blenlib/intern/scanfill.cc @@ -488,8 +488,7 @@ static uint scanfill(ScanFillContext *sf_ctx, PolyFill *pf, const int flag) /* STEP 1: make using FillVert and FillEdge lists a sorted * ScanFillVertLink list */ - sc = scdata = static_cast( - MEM_mallocN(sizeof(*scdata) * pf->verts, "Scanfill1")); + sc = scdata = MEM_malloc_arrayN(pf->verts, "Scanfill1"); verts = 0; LISTBASE_FOREACH (ScanFillVert *, eve, &sf_ctx->fillvertbase) { if (eve->poly_nr == nr) { @@ -1025,7 +1024,7 @@ uint BLI_scanfill_calc_ex(ScanFillContext *sf_ctx, const int flag, const float n */ /* STEP 3: MAKE POLYFILL STRUCT */ - pflist = static_cast(MEM_mallocN(sizeof(*pflist) * size_t(poly), "edgefill")); + pflist = MEM_malloc_arrayN(size_t(poly), "edgefill"); pf = pflist; for (a = 0; a < poly; a++) { pf->edges = pf->verts = 0; diff --git a/source/blender/blenlib/intern/scanfill_utils.cc b/source/blender/blenlib/intern/scanfill_utils.cc index cf023f18563..c0959568720 100644 --- a/source/blender/blenlib/intern/scanfill_utils.cc +++ b/source/blender/blenlib/intern/scanfill_utils.cc @@ -191,7 +191,7 @@ static bool scanfill_preprocess_self_isect(ScanFillContext *sf_ctx, isect_hash = BLI_ghash_ptr_new(__func__); } - isect = static_cast(MEM_mallocN(sizeof(ScanFillIsect), __func__)); + isect = MEM_mallocN(__func__); BLI_addtail(&isect_lb, isect); diff --git a/source/blender/blenlib/intern/string.cc b/source/blender/blenlib/intern/string.cc index 3d705ba7481..25ca3ddbd88 100644 --- a/source/blender/blenlib/intern/string.cc +++ b/source/blender/blenlib/intern/string.cc @@ -31,7 +31,7 @@ char *BLI_strdupn(const char *str, const size_t len) { BLI_assert_msg(BLI_strnlen(str, len) == len, "strlen(str) must be greater or equal to 'len'!"); - char *n = static_cast(MEM_mallocN(len + 1, "strdup")); + char *n = MEM_malloc_arrayN(len + 1, "strdup"); memcpy(n, str, len); n[len] = '\0'; @@ -256,7 +256,7 @@ char *BLI_sprintfN_with_buffer( /* `retval` doesn't include null terminator. */ const size_t size = size_t(retval) + 1; - char *result = static_cast(MEM_mallocN(sizeof(char) * size, __func__)); + char *result = MEM_malloc_arrayN(size, __func__); va_start(args, format); retval = vsnprintf(result, size, format, args); va_end(args); @@ -291,7 +291,7 @@ char *BLI_vsprintfN_with_buffer(char *fixed_buf, /* `retval` doesn't include null terminator. */ const size_t size = size_t(retval) + 1; - char *result = static_cast(MEM_mallocN(sizeof(char) * size, __func__)); + char *result = MEM_malloc_arrayN(size, __func__); retval = vsnprintf(result, size, format, args); BLI_assert((size_t)(retval + 1) == size); UNUSED_VARS_NDEBUG(retval); @@ -311,7 +311,7 @@ char *BLI_sprintfN(const char *__restrict format, ...) return result; } size_t size = result_len + 1; - result = static_cast(MEM_mallocN(sizeof(char) * size, __func__)); + result = MEM_malloc_arrayN(size, __func__); memcpy(result, fixed_buf, size); return result; } @@ -506,7 +506,7 @@ char *BLI_str_quoted_substrN(const char *__restrict str, const char *__restrict return nullptr; } const size_t escaped_len = (size_t)(end_match_ofs - start_match_ofs); - char *result = MEM_mallocN(sizeof(char) * (escaped_len + 1), __func__); + char *result = MEM_malloc_arrayN(escaped_len + 1, __func__); const size_t unescaped_len = BLI_str_unescape(result, str + start_match_ofs, escaped_len); if (unescaped_len != escaped_len) { result = MEM_reallocN(result, sizeof(char) * (unescaped_len + 1)); diff --git a/source/blender/blenlib/intern/threads.cc b/source/blender/blenlib/intern/threads.cc index 42f106aeee8..3797eb573bb 100644 --- a/source/blender/blenlib/intern/threads.cc +++ b/source/blender/blenlib/intern/threads.cc @@ -133,7 +133,7 @@ void BLI_threadpool_init(ListBase *threadbase, void *(*do_thread)(void *), int t } for (a = 0; a < tot; a++) { - ThreadSlot *tslot = static_cast(MEM_callocN(sizeof(ThreadSlot), "threadslot")); + ThreadSlot *tslot = MEM_callocN("threadslot"); BLI_addtail(threadbase, tslot); tslot->do_thread = do_thread; tslot->avail = 1; @@ -364,7 +364,7 @@ void BLI_mutex_end(ThreadMutex *mutex) ThreadMutex *BLI_mutex_alloc() { - ThreadMutex *mutex = static_cast(MEM_callocN(sizeof(ThreadMutex), "ThreadMutex")); + ThreadMutex *mutex = MEM_callocN("ThreadMutex"); BLI_mutex_init(mutex); return mutex; } @@ -486,8 +486,7 @@ void BLI_rw_mutex_end(ThreadRWMutex *mutex) ThreadRWMutex *BLI_rw_mutex_alloc() { - ThreadRWMutex *mutex = static_cast( - MEM_callocN(sizeof(ThreadRWMutex), "ThreadRWMutex")); + ThreadRWMutex *mutex = MEM_callocN("ThreadRWMutex"); BLI_rw_mutex_init(mutex); return mutex; } @@ -510,8 +509,7 @@ struct TicketMutex { TicketMutex *BLI_ticket_mutex_alloc() { - TicketMutex *ticket = static_cast( - MEM_callocN(sizeof(TicketMutex), "TicketMutex")); + TicketMutex *ticket = MEM_callocN("TicketMutex"); pthread_cond_init(&ticket->cond, nullptr); pthread_mutex_init(&ticket->mutex, nullptr); @@ -619,7 +617,7 @@ ThreadQueue *BLI_thread_queue_init() { ThreadQueue *queue; - queue = static_cast(MEM_callocN(sizeof(ThreadQueue), "ThreadQueue")); + queue = MEM_callocN("ThreadQueue"); queue->queue = BLI_gsqueue_new(sizeof(void *)); pthread_mutex_init(&queue->mutex, nullptr); diff --git a/source/blender/blenlib/intern/uvproject.cc b/source/blender/blenlib/intern/uvproject.cc index f18f8b7b981..26b8a680db8 100644 --- a/source/blender/blenlib/intern/uvproject.cc +++ b/source/blender/blenlib/intern/uvproject.cc @@ -165,7 +165,7 @@ ProjCameraInfo *BLI_uvproject_camera_info(const Object *ob, uci.shiftx = 0.5f - (camera->shiftx * uci.xasp); uci.shifty = 0.5f - (camera->shifty * uci.yasp); - uci_pt = static_cast(MEM_mallocN(sizeof(ProjCameraInfo), __func__)); + uci_pt = MEM_mallocN(__func__); *uci_pt = uci; return uci_pt; } diff --git a/source/blender/blenlib/intern/winstuff_dir.cc b/source/blender/blenlib/intern/winstuff_dir.cc index 63da8a2bf1a..2b5ce9c283d 100644 --- a/source/blender/blenlib/intern/winstuff_dir.cc +++ b/source/blender/blenlib/intern/winstuff_dir.cc @@ -58,7 +58,7 @@ DIR *opendir(const char *path) if ((GetFileAttributesW(path_16) & FILE_ATTRIBUTE_DIRECTORY) && ((path_len = strlen(path)) < (sizeof(newd->path) - PATH_SUFFIX_LEN))) { - newd = static_cast(MEM_mallocN(sizeof(DIR), "opendir")); + newd = MEM_mallocN("opendir"); newd->handle = INVALID_HANDLE_VALUE; memcpy(newd->path, path, path_len); memcpy(newd->path + path_len, PATH_SUFFIX, PATH_SUFFIX_LEN + 1); @@ -80,7 +80,7 @@ static char *BLI_alloc_utf_8_from_16(wchar_t *in16, size_t add) if (!bsize) { return nullptr; } - out8 = (char *)MEM_mallocN(sizeof(char) * (bsize + add), "UTF-8 String"); + out8 = MEM_malloc_arrayN(bsize + add, "UTF-8 String"); conv_utf_16_to_8(in16, out8, bsize); return out8; } @@ -92,7 +92,7 @@ static wchar_t *UNUSED_FUNCTION(BLI_alloc_utf16_from_8)(char *in8, size_t add) if (!bsize) { return nullptr; } - out16 = (wchar_t *)MEM_mallocN(sizeof(wchar_t) * (bsize + add), "UTF-16 String"); + out16 = MEM_malloc_arrayN(bsize + add, "UTF-16 String"); conv_utf_8_to_16(in8, out16, bsize); return out16; } diff --git a/source/blender/blenlib/tests/BLI_array_store_test.cc b/source/blender/blenlib/tests/BLI_array_store_test.cc index 8dcbfbbd888..b4fd19b9be4 100644 --- a/source/blender/blenlib/tests/BLI_array_store_test.cc +++ b/source/blender/blenlib/tests/BLI_array_store_test.cc @@ -42,7 +42,7 @@ struct TestChunk { static TestChunk *testchunk_list_add(ListBase *lb, const void *data, size_t data_len) { - TestChunk *tc = (TestChunk *)MEM_mallocN(sizeof(*tc), __func__); + TestChunk *tc = MEM_mallocN(__func__); tc->data = data; tc->data_len = data_len; BLI_addtail(lb, tc); @@ -76,7 +76,7 @@ static char *testchunk_as_data(ListBase *lb, size_t *r_data_len) for (TestChunk *tc = (TestChunk *)lb->first; tc; tc = tc->next) { data_len += tc->data_len; } - char *data = (char *)MEM_mallocN(data_len, __func__); + char *data = MEM_malloc_arrayN(data_len, __func__); size_t i = 0; for (TestChunk *tc = (TestChunk *)lb->first; tc; tc = tc->next) { memcpy(&data[i], tc->data, tc->data_len); @@ -96,7 +96,7 @@ static char *testchunk_as_data_array(TestChunk **tc_array, int tc_array_len, siz for (int tc_index = 0; tc_index < tc_array_len; tc_index++) { data_len += tc_array[tc_index]->data_len; } - char *data = (char *)MEM_mallocN(data_len, __func__); + char *data = MEM_malloc_arrayN(data_len, __func__); size_t i = 0; for (int tc_index = 0; tc_index < tc_array_len; tc_index++) { TestChunk *tc = tc_array[tc_index]; @@ -124,7 +124,7 @@ struct TestBuffer { static TestBuffer *testbuffer_list_add(ListBase *lb, const void *data, size_t data_len) { - TestBuffer *tb = (TestBuffer *)MEM_mallocN(sizeof(*tb), __func__); + TestBuffer *tb = MEM_mallocN(__func__); tb->data = data; tb->data_len = data_len; tb->state = nullptr; @@ -158,7 +158,7 @@ static void testbuffer_list_state_from_data__stride_expand(ListBase *lb, } else { const size_t data_stride_len = data_len * stride; - char *data_stride = (char *)MEM_mallocN(data_stride_len, __func__); + char *data_stride = MEM_malloc_arrayN(data_stride_len, __func__); for (size_t i = 0, i_stride = 0; i < data_len; i += 1, i_stride += stride) { memset(&data_stride[i_stride], data[i], stride); @@ -581,7 +581,7 @@ static void testbuffer_list_state_random_data(ListBase *lb, RNG *rng) { size_t data_len = rand_range_i(rng, data_min_len, data_max_len + stride, stride); - char *data = (char *)MEM_mallocN(data_len, __func__); + char *data = MEM_malloc_arrayN(data_len, __func__); if (lb->last == nullptr) { BLI_rng_get_char_n(rng, data, data_len); @@ -713,7 +713,7 @@ static void random_chunk_generate(ListBase *lb, RNG *rng = BLI_rng_new(random_seed); const size_t chunk_size_bytes = stride * chunk_count; for (int i = 0; i < chunks_per_buffer; i++) { - char *data_chunk = (char *)MEM_mallocN(chunk_size_bytes, __func__); + char *data_chunk = MEM_malloc_arrayN(chunk_size_bytes, __func__); BLI_rng_get_char_n(rng, data_chunk, chunk_size_bytes); testchunk_list_add(lb, data_chunk, chunk_size_bytes); } @@ -734,8 +734,7 @@ static void random_chunk_mutate_helper(const int chunks_per_buffer, ListBase random_chunks; BLI_listbase_clear(&random_chunks); random_chunk_generate(&random_chunks, chunks_per_buffer, stride, chunk_count, random_seed); - TestChunk **chunks_array = (TestChunk **)MEM_mallocN(chunks_per_buffer * sizeof(TestChunk *), - __func__); + TestChunk **chunks_array = MEM_malloc_arrayN(size_t(chunks_per_buffer), __func__); { TestChunk *tc = (TestChunk *)random_chunks.first; for (int i = 0; i < chunks_per_buffer; i++, tc = tc->next) { diff --git a/source/blender/blenlib/tests/BLI_generic_array_test.cc b/source/blender/blenlib/tests/BLI_generic_array_test.cc index 7c6b56cf280..abbfd955f8d 100644 --- a/source/blender/blenlib/tests/BLI_generic_array_test.cc +++ b/source/blender/blenlib/tests/BLI_generic_array_test.cc @@ -66,7 +66,7 @@ TEST(generic_array, CopyConstructor) TEST(generic_array, BufferAndSizeConstructor) { - int32_t *values = (int32_t *)MEM_malloc_arrayN(12, sizeof(int32_t), __func__); + int32_t *values = MEM_malloc_arrayN(12, __func__); void *buffer = (void *)values; GArray array(CPPType::get(), buffer, 4); EXPECT_FALSE(array.data() == nullptr); diff --git a/source/blender/blenlib/tests/BLI_heap_simple_test.cc b/source/blender/blenlib/tests/BLI_heap_simple_test.cc index dc4ce7f98fd..59816dd519c 100644 --- a/source/blender/blenlib/tests/BLI_heap_simple_test.cc +++ b/source/blender/blenlib/tests/BLI_heap_simple_test.cc @@ -95,7 +95,7 @@ TEST(heap, SimpleDuplicates) static void random_heapsimple_helper(const int items_total, const int random_seed) { HeapSimple *heap = BLI_heapsimple_new(); - float *values = (float *)MEM_mallocN(sizeof(float) * items_total, __func__); + float *values = MEM_malloc_arrayN(size_t(items_total), __func__); range_fl(values, items_total); BLI_array_randomize(values, sizeof(float), items_total, random_seed); for (int i = 0; i < items_total; i++) { diff --git a/source/blender/blenlib/tests/BLI_heap_test.cc b/source/blender/blenlib/tests/BLI_heap_test.cc index 59ec6a57e71..f6b020762fb 100644 --- a/source/blender/blenlib/tests/BLI_heap_test.cc +++ b/source/blender/blenlib/tests/BLI_heap_test.cc @@ -81,7 +81,7 @@ TEST(heap, RangeRemove) { const int items_total = SIZE; Heap *heap = BLI_heap_new(); - HeapNode **nodes = (HeapNode **)MEM_mallocN(sizeof(HeapNode *) * items_total, __func__); + HeapNode **nodes = MEM_malloc_arrayN(size_t(items_total), __func__); for (int in = 0; in < items_total; in++) { nodes[in] = BLI_heap_insert(heap, float(in), POINTER_FROM_INT(in)); } @@ -114,7 +114,7 @@ TEST(heap, Duplicates) static void random_heap_helper(const int items_total, const int random_seed) { Heap *heap = BLI_heap_new(); - float *values = (float *)MEM_mallocN(sizeof(float) * items_total, __func__); + float *values = MEM_malloc_arrayN(size_t(items_total), __func__); range_fl(values, items_total); BLI_array_randomize(values, sizeof(float), items_total, random_seed); for (int i = 0; i < items_total; i++) { @@ -145,7 +145,7 @@ TEST(heap, ReInsertSimple) { const int items_total = SIZE; Heap *heap = BLI_heap_new(); - HeapNode **nodes = (HeapNode **)MEM_mallocN(sizeof(HeapNode *) * items_total, __func__); + HeapNode **nodes = MEM_malloc_arrayN(size_t(items_total), __func__); for (int in = 0; in < items_total; in++) { nodes[in] = BLI_heap_insert(heap, float(in), POINTER_FROM_INT(in)); } @@ -165,7 +165,7 @@ TEST(heap, ReInsertSimple) static void random_heap_reinsert_helper(const int items_total, const int random_seed) { Heap *heap = BLI_heap_new(); - HeapNode **nodes = (HeapNode **)MEM_mallocN(sizeof(HeapNode *) * items_total, __func__); + HeapNode **nodes = MEM_malloc_arrayN(size_t(items_total), __func__); for (int in = 0; in < items_total; in++) { nodes[in] = BLI_heap_insert(heap, float(in), POINTER_FROM_INT(in)); } diff --git a/source/blender/blenlib/tests/BLI_kdopbvh_test.cc b/source/blender/blenlib/tests/BLI_kdopbvh_test.cc index b29eeee8a33..44b5c380025 100644 --- a/source/blender/blenlib/tests/BLI_kdopbvh_test.cc +++ b/source/blender/blenlib/tests/BLI_kdopbvh_test.cc @@ -74,7 +74,7 @@ static void find_nearest_points_test( RNG *rng = BLI_rng_new(random_seed); BVHTree *tree = BLI_bvhtree_new(points_len, 0.0, 8, 8); - void *mem = MEM_mallocN(sizeof(float[3]) * points_len, __func__); + void *mem = MEM_malloc_arrayN(size_t(points_len), __func__); float(*points)[3] = (float(*)[3])mem; for (int i = 0; i < points_len; i++) { diff --git a/source/blender/blenlib/tests/BLI_linklist_lockfree_test.cc b/source/blender/blenlib/tests/BLI_linklist_lockfree_test.cc index b1d531e7744..42584915121 100644 --- a/source/blender/blenlib/tests/BLI_linklist_lockfree_test.cc +++ b/source/blender/blenlib/tests/BLI_linklist_lockfree_test.cc @@ -70,7 +70,7 @@ void concurrent_insert(TaskPool *__restrict pool, void *taskdata) { LockfreeLinkList *list = (LockfreeLinkList *)BLI_task_pool_user_data(pool); CHECK_NOTNULL(list); - IndexedNode *node = (IndexedNode *)MEM_mallocN(sizeof(IndexedNode), "test node"); + IndexedNode *node = MEM_mallocN("test node"); node->index = POINTER_AS_INT(taskdata); BLI_linklist_lockfree_insert(list, (LockfreeLinkNode *)node); } @@ -93,7 +93,7 @@ TEST(LockfreeLinkList, InsertMultipleConcurrent) BLI_task_pool_work_and_wait(pool); /* Verify we've got all the data properly inserted. */ EXPECT_EQ(list.head, &list.dummy_node); - bool *visited_nodes = (bool *)MEM_callocN(sizeof(bool) * nodes_num, "visited nodes"); + bool *visited_nodes = MEM_calloc_arrayN(nodes_num, "visited nodes"); /* First, we make sure that none of the nodes are added twice. */ for (LockfreeLinkNode *node_v = BLI_linklist_lockfree_begin(&list); node_v != nullptr; node_v = node_v->next) diff --git a/source/blender/blenlib/tests/BLI_listbase_test.cc b/source/blender/blenlib/tests/BLI_listbase_test.cc index 3c2ec8297d2..127c7b528df 100644 --- a/source/blender/blenlib/tests/BLI_listbase_test.cc +++ b/source/blender/blenlib/tests/BLI_listbase_test.cc @@ -70,8 +70,8 @@ static int char_switch(char *string, char ch_src, char ch_dst) TEST(listbase, FindLinkOrIndex) { ListBase lb; - void *link1 = MEM_callocN(sizeof(Link), "link1"); - void *link2 = MEM_callocN(sizeof(Link), "link2"); + void *link1 = MEM_callocN("link1"); + void *link2 = MEM_callocN("link2"); /* Empty list */ BLI_listbase_clear(&lb); @@ -123,10 +123,10 @@ TEST(listbase, FindLinkFromStringOrPointer) const size_t ptr_offset = offsetof(TestLink, ptr); ListBase lb; - TestLink *link1 = (TestLink *)MEM_callocN(sizeof(TestLink), "link1"); + TestLink *link1 = MEM_callocN("link1"); STRNCPY(link1->name, link1_name); link1->ptr = link1_ptr; - TestLink *link2 = (TestLink *)MEM_callocN(sizeof(TestLink), "link2"); + TestLink *link2 = MEM_callocN("link2"); STRNCPY(link2->name, link2_name); link2->ptr = link2_ptr; @@ -167,9 +167,9 @@ TEST(listbase, FindLinkFromStringOrPointer) TEST(listbase, FromLink) { ListBase lb = {nullptr, nullptr}; - Link *link1 = static_cast(MEM_callocN(sizeof(Link), "link1")); - Link *link2 = static_cast(MEM_callocN(sizeof(Link), "link2")); - Link *link3 = static_cast(MEM_callocN(sizeof(Link), "link3")); + Link *link1 = MEM_callocN("link1"); + Link *link2 = MEM_callocN("link2"); + Link *link3 = MEM_callocN("link3"); /* Null safety. */ EXPECT_EQ(lb, BLI_listbase_from_link(nullptr)); @@ -193,8 +193,8 @@ TEST(listbase, SplitAfter) { ListBase lb; ListBase split_after_lb; - void *link1 = MEM_callocN(sizeof(Link), "link1"); - void *link2 = MEM_callocN(sizeof(Link), "link2"); + void *link1 = MEM_callocN("link1"); + void *link2 = MEM_callocN("link2"); /* Empty list */ BLI_listbase_clear(&lb); @@ -337,9 +337,9 @@ TEST(listbase, Sort) /* delimit words */ words_num = 1 + char_switch(words, ' ', '\0'); - words_arr = (char **)MEM_mallocN(sizeof(*words_arr) * words_num, __func__); + words_arr = MEM_malloc_arrayN(size_t(words_num), __func__); - words_linkdata_arr = (LinkData *)MEM_mallocN(sizeof(*words_linkdata_arr) * words_num, __func__); + words_linkdata_arr = MEM_malloc_arrayN(size_t(words_num), __func__); /* create array */ w_step = words; diff --git a/source/blender/blenlib/tests/BLI_polyfill_2d_test.cc b/source/blender/blenlib/tests/BLI_polyfill_2d_test.cc index 6ba0936e799..d889e516268 100644 --- a/source/blender/blenlib/tests/BLI_polyfill_2d_test.cc +++ b/source/blender/blenlib/tests/BLI_polyfill_2d_test.cc @@ -71,7 +71,7 @@ static void test_polyfill_simple(const float /*poly*/[][2], const uint tris_num) { uint i; - int *used_num = (int *)MEM_callocN(poly_num * sizeof(int), __func__); + int *used_num = MEM_calloc_arrayN(poly_num, __func__); for (i = 0; i < tris_num; i++) { uint j; for (j = 0; j < 3; j++) { @@ -238,7 +238,7 @@ static void test_polyfill_template_flip_sign(const char *id, uint tris[][3], const uint tris_num) { - float(*poly_copy)[2] = (float(*)[2])MEM_mallocN(sizeof(float[2]) * poly_num, id); + float(*poly_copy)[2] = MEM_malloc_arrayN(poly_num, id); for (int flip_x = 0; flip_x < 2; flip_x++) { for (int flip_y = 0; flip_y < 2; flip_y++) { float sign_x = flip_x ? -1.0f : 1.0f; @@ -263,7 +263,7 @@ static void test_polyfill_template_main(const char *id, { /* overkill? - try at _every_ offset & reverse */ uint poly_reverse; - float(*poly_copy)[2] = (float(*)[2])MEM_mallocN(sizeof(float[2]) * poly_num, id); + float(*poly_copy)[2] = MEM_malloc_arrayN(poly_num, id); float tmp[2]; memcpy(poly_copy, poly, sizeof(float[2]) * poly_num); diff --git a/source/blender/blenlib/tests/performance/BLI_map_performance_test.cc b/source/blender/blenlib/tests/performance/BLI_map_performance_test.cc index 343ab848737..9b089c32ba8 100644 --- a/source/blender/blenlib/tests/performance/BLI_map_performance_test.cc +++ b/source/blender/blenlib/tests/performance/BLI_map_performance_test.cc @@ -673,7 +673,7 @@ TEST(ghash, Int4Map20000000) static void multi_small_ghash_tests_one(GHash *ghash, RNG *rng, const uint count) { - uint *data = (uint *)MEM_mallocN(sizeof(*data) * size_t(count), __func__); + uint *data = MEM_malloc_arrayN(size_t(count), __func__); uint *dt; uint i;