Cleanup: blenlib: 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/136268
This commit is contained in:
Bastien Montagne
2025-03-21 11:50:00 +01:00
committed by Bastien Montagne
parent 14913a447c
commit cef8de874b
31 changed files with 91 additions and 105 deletions

View File

@@ -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<char *>(MEM_mallocN(ds->curlen + 1, "dynstr_cstring"));
char *rets = MEM_malloc_arrayN<char>(size_t(ds->curlen) + 1, "dynstr_cstring");
BLI_dynstr_get_cstring_ex(ds, rets);
return rets;
}

View File

@@ -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<direntry>(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<direntry *>(MEM_mallocN(sizeof(**r_filelist), __func__));
*r_filelist = MEM_mallocN<direntry>(__func__);
}
return dir_ctx.files_num;
@@ -419,8 +419,7 @@ void BLI_filelist_duplicate(direntry **dest_filelist,
{
uint i;
*dest_filelist = static_cast<direntry *>(
MEM_mallocN(sizeof(**dest_filelist) * size_t(nrentries), __func__));
*dest_filelist = MEM_malloc_arrayN<direntry>(size_t(nrentries), __func__);
for (i = 0; i < nrentries; i++) {
const direntry *src = &src_filelist[i];
direntry *dst = &(*dest_filelist)[i];

View File

@@ -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<Entry *>(gh->nbuckets, __func__);
if (buckets_old) {
if (nbuckets > nbuckets_old) {

View File

@@ -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<GHashPair *>(MEM_mallocN(sizeof(GHashPair), "GHashPair"));
GHashPair *pair = MEM_mallocN<GHashPair>("GHashPair");
pair->first = first;
pair->second = second;
return pair;

View File

@@ -1405,8 +1405,7 @@ BVHTreeOverlap *BLI_bvhtree_overlap_ex(
total += BLI_stack_count(data[j].overlap);
}
to = overlap = static_cast<BVHTreeOverlap *>(
MEM_mallocN(sizeof(BVHTreeOverlap) * total, "BVHTreeOverlap"));
to = overlap = MEM_malloc_arrayN<BVHTreeOverlap>(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<int *>(MEM_mallocN(sizeof(int) * total, __func__));
intersect = MEM_malloc_arrayN<int>(total, __func__);
BLI_stack_pop_n(data.intersect, intersect, uint(total));
}
BLI_stack_free(data.intersect);

View File

@@ -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<LinkNode *>(MEM_mallocN(sizeof(*nlink), __func__));
LinkNode *nlink = MEM_mallocN<LinkNode>(__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<LinkNode *>(MEM_mallocN(sizeof(*nlink), __func__));
LinkNode *nlink = MEM_mallocN<LinkNode>(__func__);
BLI_linklist_append_nlink(list_pair, ptr, nlink);
}

View File

@@ -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<uchar *>(MEM_mallocN(data_len, __func__));
uchar *data_copy = MEM_malloc_arrayN<uchar>(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<uchar *>(MEM_mallocN(data_merge_len, __func__));
uchar *data_merge = MEM_malloc_arrayN<uchar>(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<uchar *>(MEM_mallocN(data_prev_len, __func__));
uchar *data_curr = static_cast<uchar *>(MEM_mallocN(data_curr_len, __func__));
uchar *data_prev = MEM_malloc_arrayN<uchar>(data_prev_len, __func__);
uchar *data_curr = MEM_malloc_arrayN<uchar>(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<uchar *>(MEM_mallocN(data_merge_len, __func__));
uchar *data_merge = MEM_malloc_arrayN<uchar>(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<hash_key *>(
MEM_mallocN(sizeof(*table_hash_array) * table_hash_array_len, __func__));
hash_key *table_hash_array = MEM_malloc_arrayN<hash_key>(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<BTableRef *>(
MEM_mallocN(chunk_list_reference_remaining_len * sizeof(BTableRef), __func__));
BTableRef *table_ref_stack = MEM_malloc_arrayN<BTableRef>(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<BTableRef **>(
MEM_callocN(table_len * sizeof(*table), __func__));
BTableRef **table = MEM_calloc_arrayN<BTableRef *>(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<hash_key *>(
MEM_mallocN(sizeof(hash_key) * hash_store_len, __func__));
hash_key *hash_store = MEM_malloc_arrayN<hash_key>(hash_store_len, __func__);
#endif
const BChunkRef *cref;

View File

@@ -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<int(*)[2]>(
MEM_mallocN(sizeof(*span_y) * size_t(verts.size()), __func__));
int(*span_y)[2] = MEM_malloc_arrayN<int[2]>(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<NodeX *>(
MEM_mallocN(sizeof(*node_x) * size_t(verts.size() + 1), __func__));
} *node_x = MEM_malloc_arrayN<NodeX>(size_t(verts.size() + 1), __func__);
int node_x_len = 0;
int span_y_index = 0;

View File

@@ -285,9 +285,8 @@ void BLI_box_pack_2d(
}
/* Add verts to the boxes, these are only used internally. */
vert = static_cast<BoxVert *>(MEM_mallocN(sizeof(BoxVert[4]) * size_t(len), "BoxPack Verts"));
vertex_pack_indices = static_cast<uint *>(
MEM_mallocN(sizeof(int[3]) * size_t(len), "BoxPack Indices"));
vert = MEM_malloc_arrayN<BoxVert>(4 * size_t(len), "BoxPack Verts");
vertex_pack_indices = MEM_malloc_arrayN<uint>(3 * size_t(len), "BoxPack Indices");
vs_ctx.vertarray = vert;

View File

@@ -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)

View File

@@ -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<int *>(MEM_mallocN(sizeof(int) * size_t(points_num), __func__));
float(*points_sort)[2] = static_cast<float(*)[2]>(
MEM_mallocN(sizeof(*points_sort) * size_t(points_num), __func__));
int *points_map = MEM_malloc_arrayN<int>(size_t(points_num), __func__);
float(*points_sort)[2] = MEM_malloc_arrayN<float[2]>(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<int *>(
MEM_mallocN(sizeof(*index_map) * size_t(points_num), __func__));
int *index_map = MEM_malloc_arrayN<int>(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<float(*)[2]>(
MEM_mallocN(sizeof(*points_hull) * size_t(points_hull_num), __func__));
float(*points_hull)[2] = MEM_malloc_arrayN<float[2]>(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]]);
}

View File

@@ -101,10 +101,8 @@ static bool zstd_read_seek_table(ZstdReader *zstd)
}
zstd->seek.frames_num = frames_num;
zstd->seek.compressed_ofs = static_cast<size_t *>(
MEM_malloc_arrayN(frames_num + 1, sizeof(size_t), __func__));
zstd->seek.uncompressed_ofs = static_cast<size_t *>(
MEM_malloc_arrayN(frames_num + 1, sizeof(size_t), __func__));
zstd->seek.compressed_ofs = MEM_malloc_arrayN<size_t>(frames_num + 1, __func__);
zstd->seek.uncompressed_ofs = MEM_malloc_arrayN<size_t>(frames_num + 1, __func__);
size_t compressed_ofs = 0;
size_t uncompressed_ofs = 0;

View File

@@ -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<float(*)[2]>(MEM_mallocN(12 + uint(num) * sizeof(float[2]), "initjit"));
jit2 = MEM_malloc_arrayN<float[2]>(2 + size_t(num), "initjit");
rad1 = 1.0f / number_fl_sqrt;
rad2 = 1.0f / number_fl;
rad3 = number_fl_sqrt / number_fl;

View File

@@ -95,6 +95,10 @@ KDTree *BLI_kdtree_nd_(new)(uint nodes_len_capacity)
KDTree *tree;
tree = MEM_callocN<KDTree>("KDTree");
/* NOTE: Cannot use `MEM_malloc_arrayN<KDTreeNode>()` 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<KDTreeNode *>(
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<uint *>(
MEM_mallocN((*stack_len_capacity + KD_NEAR_ALLOC_INC) * sizeof(uint), "KDTree.treestack"));
uint *stack_new = MEM_malloc_arrayN<uint>(*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) {

View File

@@ -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<double>(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<float>(size_t(count) * 2, "tridiagonal_ex");
if (!tmp) {
return false;
}

View File

@@ -2032,8 +2032,8 @@ static Array<Face *> 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<uint(*)[3]>(MEM_malloc_arrayN(totfilltri, sizeof(*tris), __func__));
projverts = static_cast<float(*)[2]>(MEM_malloc_arrayN(flen, sizeof(*projverts), __func__));
tris = MEM_malloc_arrayN<uint[3]>(size_t(totfilltri), __func__);
projverts = MEM_malloc_arrayN<float[2]>(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;

View File

@@ -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<ScanFillVertLink *>(
MEM_mallocN(sizeof(*scdata) * pf->verts, "Scanfill1"));
sc = scdata = MEM_malloc_arrayN<ScanFillVertLink>(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<PolyFill *>(MEM_mallocN(sizeof(*pflist) * size_t(poly), "edgefill"));
pflist = MEM_malloc_arrayN<PolyFill>(size_t(poly), "edgefill");
pf = pflist;
for (a = 0; a < poly; a++) {
pf->edges = pf->verts = 0;

View File

@@ -191,7 +191,7 @@ static bool scanfill_preprocess_self_isect(ScanFillContext *sf_ctx,
isect_hash = BLI_ghash_ptr_new(__func__);
}
isect = static_cast<ScanFillIsect *>(MEM_mallocN(sizeof(ScanFillIsect), __func__));
isect = MEM_mallocN<ScanFillIsect>(__func__);
BLI_addtail(&isect_lb, isect);

View File

@@ -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<char *>(MEM_mallocN(len + 1, "strdup"));
char *n = MEM_malloc_arrayN<char>(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<char *>(MEM_mallocN(sizeof(char) * size, __func__));
char *result = MEM_malloc_arrayN<char>(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<char *>(MEM_mallocN(sizeof(char) * size, __func__));
char *result = MEM_malloc_arrayN<char>(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<char *>(MEM_mallocN(sizeof(char) * size, __func__));
result = MEM_malloc_arrayN<char>(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<char>(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));

View File

@@ -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<ThreadSlot *>(MEM_callocN(sizeof(ThreadSlot), "threadslot"));
ThreadSlot *tslot = MEM_callocN<ThreadSlot>("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<ThreadMutex *>(MEM_callocN(sizeof(ThreadMutex), "ThreadMutex"));
ThreadMutex *mutex = MEM_callocN<ThreadMutex>("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<ThreadRWMutex *>(
MEM_callocN(sizeof(ThreadRWMutex), "ThreadRWMutex"));
ThreadRWMutex *mutex = MEM_callocN<ThreadRWMutex>("ThreadRWMutex");
BLI_rw_mutex_init(mutex);
return mutex;
}
@@ -510,8 +509,7 @@ struct TicketMutex {
TicketMutex *BLI_ticket_mutex_alloc()
{
TicketMutex *ticket = static_cast<TicketMutex *>(
MEM_callocN(sizeof(TicketMutex), "TicketMutex"));
TicketMutex *ticket = MEM_callocN<TicketMutex>("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<ThreadQueue *>(MEM_callocN(sizeof(ThreadQueue), "ThreadQueue"));
queue = MEM_callocN<ThreadQueue>("ThreadQueue");
queue->queue = BLI_gsqueue_new(sizeof(void *));
pthread_mutex_init(&queue->mutex, nullptr);

View File

@@ -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<ProjCameraInfo *>(MEM_mallocN(sizeof(ProjCameraInfo), __func__));
uci_pt = MEM_mallocN<ProjCameraInfo>(__func__);
*uci_pt = uci;
return uci_pt;
}

View File

@@ -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<DIR *>(MEM_mallocN(sizeof(DIR), "opendir"));
newd = MEM_mallocN<DIR>("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<char>(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<wchar_t>(bsize + add, "UTF-16 String");
conv_utf_8_to_16(in8, out16, bsize);
return out16;
}

View File

@@ -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<TestChunk>(__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<char>(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<char>(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<TestBuffer>(__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<char>(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<char>(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<char>(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<TestChunk *>(size_t(chunks_per_buffer), __func__);
{
TestChunk *tc = (TestChunk *)random_chunks.first;
for (int i = 0; i < chunks_per_buffer; i++, tc = tc->next) {

View File

@@ -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<int32_t>(12, __func__);
void *buffer = (void *)values;
GArray array(CPPType::get<int32_t>(), buffer, 4);
EXPECT_FALSE(array.data() == nullptr);

View File

@@ -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<float>(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++) {

View File

@@ -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<HeapNode *>(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<float>(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<HeapNode *>(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<HeapNode *>(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));
}

View File

@@ -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<float[3]>(size_t(points_len), __func__);
float(*points)[3] = (float(*)[3])mem;
for (int i = 0; i < points_len; i++) {

View File

@@ -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<IndexedNode>("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<bool>(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)

View File

@@ -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<Link>("link1");
void *link2 = MEM_callocN<Link>("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<TestLink>("link1");
STRNCPY(link1->name, link1_name);
link1->ptr = link1_ptr;
TestLink *link2 = (TestLink *)MEM_callocN(sizeof(TestLink), "link2");
TestLink *link2 = MEM_callocN<TestLink>("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<Link *>(MEM_callocN(sizeof(Link), "link1"));
Link *link2 = static_cast<Link *>(MEM_callocN(sizeof(Link), "link2"));
Link *link3 = static_cast<Link *>(MEM_callocN(sizeof(Link), "link3"));
Link *link1 = MEM_callocN<Link>("link1");
Link *link2 = MEM_callocN<Link>("link2");
Link *link3 = MEM_callocN<Link>("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<Link>("link1");
void *link2 = MEM_callocN<Link>("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<char *>(size_t(words_num), __func__);
words_linkdata_arr = (LinkData *)MEM_mallocN(sizeof(*words_linkdata_arr) * words_num, __func__);
words_linkdata_arr = MEM_malloc_arrayN<LinkData>(size_t(words_num), __func__);
/* create array */
w_step = words;

View File

@@ -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<int>(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<float[2]>(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<float[2]>(poly_num, id);
float tmp[2];
memcpy(poly_copy, poly, sizeof(float[2]) * poly_num);

View File

@@ -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<uint>(size_t(count), __func__);
uint *dt;
uint i;