Cleanup: sequencer: 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/135747
This commit is contained in:
Bastien Montagne
2025-03-10 17:16:43 +01:00
committed by Bastien Montagne
parent 9104cc3406
commit 1d76cbab64
19 changed files with 54 additions and 69 deletions

View File

@@ -37,8 +37,7 @@ void channels_ensure(ListBase *channels)
{
/* Allocate channels. Channel 0 is never used, but allocated to prevent off by 1 issues. */
for (int i = 0; i < MAX_CHANNELS + 1; i++) {
SeqTimelineChannel *channel = static_cast<SeqTimelineChannel *>(
MEM_callocN(sizeof(SeqTimelineChannel), "seq timeline channel"));
SeqTimelineChannel *channel = MEM_callocN<SeqTimelineChannel>("seq timeline channel");
SNPRINTF(channel->name, DATA_("Channel %d"), i);
channel->index = i;
BLI_addtail(channels, channel);

View File

@@ -135,8 +135,7 @@ static DiskCacheFile *seq_disk_cache_add_file_to_list(SeqDiskCache *disk_cache,
const char *filepath)
{
DiskCacheFile *cache_file = static_cast<DiskCacheFile *>(
MEM_callocN(sizeof(DiskCacheFile), "SeqDiskCacheFile"));
DiskCacheFile *cache_file = MEM_callocN<DiskCacheFile>("SeqDiskCacheFile");
char dir[FILE_MAXDIR], file[FILE_MAX];
BLI_path_split_dir_file(filepath, dir, sizeof(dir), file, sizeof(file));
STRNCPY(cache_file->filepath, filepath);
@@ -669,8 +668,7 @@ ImBuf *seq_disk_cache_read_file(SeqDiskCache *disk_cache, SeqCacheKey *key)
SeqDiskCache *seq_disk_cache_create(Main *bmain, Scene *scene)
{
SeqDiskCache *disk_cache = static_cast<SeqDiskCache *>(
MEM_callocN(sizeof(SeqDiskCache), "SeqDiskCache"));
SeqDiskCache *disk_cache = MEM_callocN<SeqDiskCache>("SeqDiskCache");
disk_cache->bmain = bmain;
BLI_mutex_init(&disk_cache->read_write_mutex);
seq_disk_cache_handle_versioning(disk_cache);

View File

@@ -339,8 +339,10 @@ static void init_colormix_effect(Strip *strip)
if (strip->effectdata) {
MEM_freeN(strip->effectdata);
}
strip->effectdata = MEM_callocN(sizeof(ColorMixVars), "colormixvars");
ColorMixVars *data = (ColorMixVars *)strip->effectdata;
ColorMixVars *data = MEM_callocN<ColorMixVars>("colormixvars");
strip->effectdata = data;
data->blend_effect = STRIP_TYPE_OVERLAY;
data->factor = 1.0f;
}

View File

@@ -25,7 +25,7 @@ static void init_gaussian_blur_effect(Strip *strip)
MEM_freeN(strip->effectdata);
}
strip->effectdata = MEM_callocN(sizeof(GaussianBlurVars), "gaussianblurvars");
strip->effectdata = MEM_callocN<GaussianBlurVars>("gaussianblurvars");
}
static int num_inputs_gaussian_blur()

View File

@@ -130,9 +130,9 @@ static void init_glow_effect(Strip *strip)
MEM_freeN(strip->effectdata);
}
strip->effectdata = MEM_callocN(sizeof(GlowVars), "glowvars");
GlowVars *glow = MEM_callocN<GlowVars>("glowvars");
strip->effectdata = glow;
GlowVars *glow = (GlowVars *)strip->effectdata;
glow->fMini = 0.25;
glow->fClamp = 1.0;
glow->fBoost = 0.5;

View File

@@ -23,9 +23,9 @@ static void init_solid_color(Strip *strip)
MEM_freeN(strip->effectdata);
}
strip->effectdata = MEM_callocN(sizeof(SolidColorVars), "solidcolor");
SolidColorVars *cv = MEM_callocN<SolidColorVars>("solidcolor");
strip->effectdata = cv;
SolidColorVars *cv = (SolidColorVars *)strip->effectdata;
cv->col[0] = cv->col[1] = cv->col[2] = 0.5;
}

View File

@@ -29,9 +29,9 @@ static void init_speed_effect(Strip *strip)
MEM_freeN(strip->effectdata);
}
strip->effectdata = MEM_callocN(sizeof(SpeedControlVars), "speedcontrolvars");
SpeedControlVars *v = MEM_callocN<SpeedControlVars>("speedcontrolvars");
strip->effectdata = v;
SpeedControlVars *v = (SpeedControlVars *)strip->effectdata;
v->speed_control_type = SEQ_SPEED_STRETCH;
v->speed_fader = 1.0f;
v->speed_fader_length = 0.0f;
@@ -95,7 +95,7 @@ void strip_effect_speed_rebuild_map(Scene *scene, Strip *strip)
MEM_freeN(v->frameMap);
}
v->frameMap = static_cast<float *>(MEM_mallocN(sizeof(float) * effect_strip_length, __func__));
v->frameMap = MEM_malloc_arrayN<float>(size_t(effect_strip_length), __func__);
v->frameMap[0] = 0.0f;
float target_frame = 0;

View File

@@ -174,8 +174,9 @@ static void init_text_effect(Strip *strip)
MEM_freeN(strip->effectdata);
}
TextVars *data = static_cast<TextVars *>(
strip->effectdata = MEM_callocN(sizeof(TextVars), "textvars"));
TextVars *data = MEM_callocN<TextVars>("textvars");
strip->effectdata = data;
data->text_font = nullptr;
data->text_blf_id = -1;
data->text_size = 60.0f;

View File

@@ -27,9 +27,8 @@ static void init_transform_effect(Strip *strip)
MEM_freeN(strip->effectdata);
}
strip->effectdata = MEM_callocN(sizeof(TransformVars), "transformvars");
TransformVars *transform = (TransformVars *)strip->effectdata;
TransformVars *transform = MEM_callocN<TransformVars>("transformvars");
strip->effectdata = transform;
transform->ScalexIni = 1.0f;
transform->ScaleyIni = 1.0f;

View File

@@ -288,7 +288,7 @@ static void init_wipe_effect(Strip *strip)
MEM_freeN(strip->effectdata);
}
strip->effectdata = MEM_callocN(sizeof(WipeVars), "wipevars");
strip->effectdata = MEM_callocN<WipeVars>("wipevars");
}
static int num_inputs_wipe()

View File

@@ -492,7 +492,7 @@ static void seq_cache_create(Main *bmain, Scene *scene)
{
BLI_mutex_lock(&cache_create_lock);
if (scene->ed->cache == nullptr) {
SeqCache *cache = static_cast<SeqCache *>(MEM_callocN(sizeof(SeqCache), "SeqCache"));
SeqCache *cache = MEM_callocN<SeqCache>("SeqCache");
cache->keys_pool = BLI_mempool_create(sizeof(SeqCacheKey), 0, 64, BLI_MEMPOOL_NOP);
cache->items_pool = BLI_mempool_create(sizeof(SeqCacheItem), 0, 64, BLI_MEMPOOL_NOP);
cache->hash = BLI_ghash_new(seq_cache_hashhash, seq_cache_hashcmp, "SeqCache hash");

View File

@@ -462,8 +462,7 @@ bool proxy_rebuild_context(Main *bmain,
relations_sequence_free_anim(strip);
context = static_cast<IndexBuildContext *>(
MEM_callocN(sizeof(IndexBuildContext), "strip proxy rebuild context"));
context = MEM_callocN<IndexBuildContext>("strip proxy rebuild context");
nseq = sequence_dupli_recursive(scene, scene, nullptr, strip, 0);

View File

@@ -73,7 +73,7 @@ ProxyJob *ED_seq_proxy_job_get(const bContext *C, wmJob *wm_job)
Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
ProxyJob *pj = static_cast<ProxyJob *>(WM_jobs_customdata_get(wm_job));
if (!pj) {
pj = static_cast<ProxyJob *>(MEM_callocN(sizeof(ProxyJob), "proxy rebuild job"));
pj = MEM_callocN<ProxyJob>("proxy rebuild job");
pj->depsgraph = depsgraph;
pj->scene = scene;
pj->main = CTX_data_main(C);

View File

@@ -830,7 +830,7 @@ static void convert_multilayer_ibuf(ImBuf *ibuf)
/* Combined layer might be non-4 channels, however the rest
* of sequencer assumes RGBA everywhere. Convert to 4 channel if needed. */
if (ibuf->float_buffer.data != nullptr && ibuf->channels != 4) {
float *dst = static_cast<float *>(MEM_mallocN(sizeof(float[4]) * ibuf->x * ibuf->y, __func__));
float *dst = MEM_malloc_arrayN<float>(4 * size_t(ibuf->x) * size_t(ibuf->y), __func__);
IMB_buffer_float_from_float_threaded(dst,
ibuf->float_buffer.data,
ibuf->channels,
@@ -958,8 +958,8 @@ static ImBuf *seq_render_image_strip(const RenderData *context,
if (is_multiview_render) {
int totviews = BKE_scene_multiview_num_views_get(&context->scene->r);
ImBuf **ibufs_arr = static_cast<ImBuf **>(
MEM_callocN(sizeof(ImBuf *) * totviews, "Sequence Image Views Imbufs"));
ImBuf **ibufs_arr = MEM_calloc_arrayN<ImBuf *>(size_t(totviews),
"Sequence Image Views Imbufs");
for (int view_id = 0; view_id < totfiles; view_id++) {
ibufs_arr[view_id] = seq_render_image_strip_view(
@@ -1114,8 +1114,7 @@ static ImBuf *seq_render_movie_strip(const RenderData *context,
if (is_multiview_render) {
ImBuf **ibuf_arr;
int totviews = BKE_scene_multiview_num_views_get(&context->scene->r);
ibuf_arr = static_cast<ImBuf **>(
MEM_callocN(sizeof(ImBuf *) * totviews, "Sequence Image Views Imbufs"));
ibuf_arr = MEM_calloc_arrayN<ImBuf *>(size_t(totviews), "Sequence Image Views Imbufs");
int ibuf_view_id;
for (ibuf_view_id = 0, sanim = static_cast<StripAnim *>(strip->anims.first); sanim;
@@ -1276,8 +1275,7 @@ ImBuf *seq_render_mask(const RenderData *context, Mask *mask, float frame_index,
context->depsgraph, mask->sfra + frame_index);
BKE_animsys_evaluate_animdata(&mask_temp->id, adt, &anim_eval_context, ADT_RECALC_ANIM, false);
maskbuf = static_cast<float *>(
MEM_mallocN(sizeof(float) * context->rectx * context->recty, __func__));
maskbuf = MEM_malloc_arrayN<float>(size_t(context->rectx) * size_t(context->recty), __func__);
mr_handle = BKE_maskrasterize_handle_new();
@@ -1520,8 +1518,7 @@ static ImBuf *seq_render_scene_strip(const RenderData *context,
goto finally;
}
ibufs_arr = static_cast<ImBuf **>(
MEM_callocN(sizeof(ImBuf *) * totviews, "Sequence Image Views Imbufs"));
ibufs_arr = MEM_calloc_arrayN<ImBuf *>(size_t(totviews), "Sequence Image Views Imbufs");
if (re == nullptr) {
re = RE_NewSceneRender(scene);

View File

@@ -66,8 +66,7 @@ namespace blender::seq {
StripProxy *seq_strip_proxy_alloc()
{
StripProxy *strip_proxy = static_cast<StripProxy *>(
MEM_callocN(sizeof(StripProxy), "StripProxy"));
StripProxy *strip_proxy = MEM_callocN<StripProxy>("StripProxy");
strip_proxy->quality = 50;
strip_proxy->build_tc_flags = SEQ_PROXY_TC_RECORD_RUN | SEQ_PROXY_TC_RECORD_RUN_NO_GAPS;
strip_proxy->tc = SEQ_PROXY_TC_RECORD_RUN;
@@ -76,17 +75,16 @@ StripProxy *seq_strip_proxy_alloc()
static StripData *seq_strip_alloc(int type)
{
StripData *data = static_cast<StripData *>(MEM_callocN(sizeof(StripData), "strip"));
StripData *data = MEM_callocN<StripData>("strip");
if (type != STRIP_TYPE_SOUND_RAM) {
data->transform = static_cast<StripTransform *>(
MEM_callocN(sizeof(StripTransform), "StripTransform"));
data->transform = MEM_callocN<StripTransform>("StripTransform");
data->transform->scale_x = 1;
data->transform->scale_y = 1;
data->transform->origin[0] = 0.5f;
data->transform->origin[1] = 0.5f;
data->transform->filter = SEQ_TRANSFORM_FILTER_AUTO;
data->crop = static_cast<StripCrop *>(MEM_callocN(sizeof(StripCrop), "StripCrop"));
data->crop = MEM_callocN<StripCrop>("StripCrop");
}
data->us = 1;
@@ -129,7 +127,7 @@ Strip *sequence_alloc(ListBase *lb, int timeline_frame, int machine, int type)
{
Strip *strip;
strip = static_cast<Strip *>(MEM_callocN(sizeof(Strip), "addseq"));
strip = MEM_callocN<Strip>("addseq");
BLI_addtail(lb, strip);
*((short *)strip->name) = ID_SEQ;
@@ -155,8 +153,7 @@ Strip *sequence_alloc(ListBase *lb, int timeline_frame, int machine, int type)
}
strip->data = seq_strip_alloc(type);
strip->stereo3d_format = static_cast<Stereo3dFormat *>(
MEM_callocN(sizeof(Stereo3dFormat), "Sequence Stereo Format"));
strip->stereo3d_format = MEM_callocN<Stereo3dFormat>("Sequence Stereo Format");
strip->color_tag = STRIP_COLOR_NONE;
@@ -274,7 +271,7 @@ Editing *editing_ensure(Scene *scene)
if (scene->ed == nullptr) {
Editing *ed;
ed = scene->ed = static_cast<Editing *>(MEM_callocN(sizeof(Editing), "addseq"));
ed = scene->ed = MEM_callocN<Editing>("addseq");
ed->seqbasep = &ed->seqbase;
ed->cache = nullptr;
ed->cache_flag = (SEQ_CACHE_STORE_FINAL_OUT | SEQ_CACHE_STORE_RAW);
@@ -339,8 +336,8 @@ static void seq_new_fix_links_recursive(Strip *strip, blender::Map<Strip *, Stri
SequencerToolSettings *tool_settings_init()
{
SequencerToolSettings *tool_settings = static_cast<SequencerToolSettings *>(
MEM_callocN(sizeof(SequencerToolSettings), "Sequencer tool settings"));
SequencerToolSettings *tool_settings = MEM_callocN<SequencerToolSettings>(
"Sequencer tool settings");
tool_settings->fit_method = SEQ_SCALE_TO_FIT;
tool_settings->snap_mode = SEQ_SNAP_TO_STRIPS | SEQ_SNAP_TO_CURRENT_FRAME |
SEQ_SNAP_TO_STRIP_HOLD | SEQ_SNAP_TO_MARKERS | SEQ_SNAP_TO_RETIMING |
@@ -429,7 +426,7 @@ static MetaStack *seq_meta_stack_alloc(const Scene *scene, Strip *strip_meta)
{
Editing *ed = editing_get(scene);
MetaStack *ms = static_cast<MetaStack *>(MEM_mallocN(sizeof(MetaStack), "metastack"));
MetaStack *ms = MEM_mallocN<MetaStack>("metastack");
BLI_addhead(&ed->metastack, ms);
ms->parseq = strip_meta;

View File

@@ -276,8 +276,7 @@ void *sound_equalizermodifier_recreator(Strip *strip, SequenceModifierData *smd,
return sound;
}
float *buf = (float *)MEM_callocN(sizeof(float) * SOUND_EQUALIZER_SIZE_DEFINITION,
"eqrecreator");
float *buf = MEM_calloc_arrayN<float>(SOUND_EQUALIZER_SIZE_DEFINITION, "eqrecreator");
CurveMapping *eq_mapping;
CurveMap *cm;

View File

@@ -237,8 +237,7 @@ Strip *add_image_strip(Main *bmain, Scene *scene, ListBase *seqbase, LoadData *l
seqbase, load_data->start_frame, load_data->channel, STRIP_TYPE_IMAGE);
strip->len = load_data->image.len;
StripData *data = strip->data;
data->stripdata = static_cast<StripElem *>(
MEM_callocN(load_data->image.len * sizeof(StripElem), "stripelem"));
data->stripdata = MEM_calloc_arrayN<StripElem>(size_t(load_data->image.len), "stripelem");
if (strip->len == 1) {
strip->flag |= SEQ_SINGLE_FRAME_CONTENT;
@@ -328,8 +327,7 @@ Strip *add_sound_strip(Main *bmain, Scene *scene, ListBase *seqbase, LoadData *l
StripData *data = strip->data;
/* We only need 1 element to store the filename. */
StripElem *se = data->stripdata = static_cast<StripElem *>(
MEM_callocN(sizeof(StripElem), "stripelem"));
StripElem *se = data->stripdata = MEM_callocN<StripElem>("stripelem");
BLI_path_split_dir_file(
load_data->path, data->dirpath, sizeof(data->dirpath), se->filename, sizeof(se->filename));
@@ -401,8 +399,7 @@ Strip *add_movie_strip(Main *bmain, Scene *scene, ListBase *seqbase, LoadData *l
char colorspace[64] = "\0"; /* MAX_COLORSPACE_NAME */
bool is_multiview_loaded = false;
const int totfiles = seq_num_files(scene, load_data->views_format, load_data->use_multiview);
MovieReader **anim_arr = static_cast<MovieReader **>(
MEM_callocN(sizeof(MovieReader *) * totfiles, "Video files"));
MovieReader **anim_arr = MEM_calloc_arrayN<MovieReader *>(size_t(totfiles), "Video files");
int i;
int orig_width = 0;
int orig_height = 0;
@@ -474,7 +471,7 @@ Strip *add_movie_strip(Main *bmain, Scene *scene, ListBase *seqbase, LoadData *l
for (i = 0; i < totfiles; i++) {
if (anim_arr[i]) {
StripAnim *sanim = static_cast<StripAnim *>(MEM_mallocN(sizeof(StripAnim), "Strip Anim"));
StripAnim *sanim = MEM_mallocN<StripAnim>("Strip Anim");
BLI_addtail(&strip->anims, sanim);
sanim->anim = anim_arr[i];
}
@@ -510,7 +507,7 @@ Strip *add_movie_strip(Main *bmain, Scene *scene, ListBase *seqbase, LoadData *l
StripData *data = strip->data;
/* We only need 1 element for MOVIE strips. */
StripElem *se;
data->stripdata = se = static_cast<StripElem *>(MEM_callocN(sizeof(StripElem), "stripelem"));
data->stripdata = se = MEM_callocN<StripElem>("stripelem");
data->stripdata->orig_width = orig_width;
data->stripdata->orig_height = orig_height;
data->stripdata->orig_fps = video_fps;
@@ -593,7 +590,7 @@ void add_reload_new_file(Main *bmain, Scene *scene, Strip *strip, const bool loc
if (anim) {
seq_anim_add_suffix(scene, anim, i);
sanim = static_cast<StripAnim *>(MEM_mallocN(sizeof(StripAnim), "Strip Anim"));
sanim = MEM_mallocN<StripAnim>("Strip Anim");
BLI_addtail(&strip->anims, sanim);
sanim->anim = anim;
}
@@ -609,7 +606,7 @@ void add_reload_new_file(Main *bmain, Scene *scene, Strip *strip, const bool loc
strip->streamindex,
strip->data->colorspace_settings.name);
if (anim) {
sanim = static_cast<StripAnim *>(MEM_mallocN(sizeof(StripAnim), "Strip Anim"));
sanim = MEM_mallocN<StripAnim>("Strip Anim");
BLI_addtail(&strip->anims, sanim);
sanim->anim = anim;
}

View File

@@ -111,7 +111,7 @@ void retiming_data_ensure(Strip *strip)
return;
}
strip->retiming_keys = (SeqRetimingKey *)MEM_calloc_arrayN(2, sizeof(SeqRetimingKey), __func__);
strip->retiming_keys = MEM_calloc_arrayN<SeqRetimingKey>(2, __func__);
SeqRetimingKey *key = strip->retiming_keys + 1;
key->strip_frame_index = strip->len - 1;
key->retiming_factor = 1.0f;
@@ -345,8 +345,7 @@ static SeqRetimingKey *strip_retiming_add_key(Strip *strip, float frame_index)
BLI_assert(new_key_index >= 0);
BLI_assert(new_key_index < keys_count);
SeqRetimingKey *new_keys = (SeqRetimingKey *)MEM_callocN(
(keys_count + 1) * sizeof(SeqRetimingKey), __func__);
SeqRetimingKey *new_keys = MEM_calloc_arrayN<SeqRetimingKey>(keys_count + 1, __func__);
if (new_key_index > 0) {
memcpy(new_keys, keys, new_key_index * sizeof(SeqRetimingKey));
}
@@ -443,8 +442,7 @@ void retiming_remove_multiple_keys(Strip *strip, blender::Vector<SeqRetimingKey
const size_t keys_count = retiming_keys_count(strip);
size_t new_keys_count = keys_count - keys_to_remove.size() - transitions.size() / 2;
SeqRetimingKey *new_keys = (SeqRetimingKey *)MEM_callocN(new_keys_count * sizeof(SeqRetimingKey),
__func__);
SeqRetimingKey *new_keys = MEM_calloc_arrayN<SeqRetimingKey>(new_keys_count, __func__);
int keys_copied = 0;
/* Copy keys to new array. */
@@ -480,8 +478,7 @@ static void strip_retiming_remove_key_ex(Strip *strip, SeqRetimingKey *key)
}
size_t keys_count = retiming_keys_count(strip);
SeqRetimingKey *keys = (SeqRetimingKey *)MEM_callocN((keys_count - 1) * sizeof(SeqRetimingKey),
__func__);
SeqRetimingKey *keys = MEM_calloc_arrayN<SeqRetimingKey>(keys_count - 1, __func__);
const int key_index = key - strip->retiming_keys;
memcpy(keys, strip->retiming_keys, (key_index) * sizeof(SeqRetimingKey));

View File

@@ -278,7 +278,7 @@ static bool open_anim_file_multiview(Scene *scene, Strip *strip, const char *fil
char filepath_view[FILE_MAX];
SNPRINTF(filepath_view, "%s%s%s", prefix, suffix, ext);
StripAnim *sanim = static_cast<StripAnim *>(MEM_mallocN(sizeof(StripAnim), "Strip Anim"));
StripAnim *sanim = MEM_mallocN<StripAnim>("Strip Anim");
/* Multiview files must be loaded, otherwise it is not possible to detect failure. */
open_anim_filepath(strip, sanim, filepath_view, true);
@@ -321,7 +321,7 @@ void strip_open_anim_file(Scene *scene, Strip *strip, bool openfile)
}
if (!is_multiview || !multiview_is_loaded) {
StripAnim *sanim = static_cast<StripAnim *>(MEM_mallocN(sizeof(StripAnim), "Strip Anim"));
StripAnim *sanim = MEM_mallocN<StripAnim>("Strip Anim");
BLI_addtail(&strip->anims, sanim);
open_anim_filepath(strip, sanim, filepath, openfile);
index_dir_set(ed, strip, sanim);