Cleanup: Avoid some void pointer freeing for type safety

Essentially add some API to properly free non-public data, instead of directly calling `MEM_freeN` on them.

Based on @brecht code from
https://projects.blender.org/mont29/blender/compare/tmp-guardedalloc-api...brecht:free-void

Co-authored-by: Brecht Van Lommel <brecht@blender.org>
Pull Request: https://projects.blender.org/blender/blender/pulls/134765
This commit is contained in:
Bastien Montagne
2025-02-20 11:24:34 +01:00
committed by Bastien Montagne
parent 3ca5f6f62e
commit 8045576c60
21 changed files with 49 additions and 44 deletions

View File

@@ -54,6 +54,8 @@
# include "BPY_extern.hh"
#endif
#include "WM_types.hh"
using blender::Span;
using blender::Vector;

View File

@@ -29,12 +29,13 @@
*
* angle = BLI_dial_angle(dial, current_position);
*
* MEM_freeN(dial);
* BLI_dial_free(dial);
* \endcode
*/
typedef struct Dial Dial;
Dial *BLI_dial_init(const float start_position[2], float threshold);
void BLI_dial_free(Dial *dial);
float BLI_dial_angle(Dial *dial, const float current_position[2]);

View File

@@ -45,4 +45,8 @@ void BLI_uvproject_from_view_ortho(float target[2], float source[3], const float
/**
* So we can adjust scale with keeping the struct private.
*/
void BLI_uvproject_camera_info_scale(struct ProjCameraInfo *uci, float scale_x, float scale_y);
void BLI_uvproject_camera_info_scale(ProjCameraInfo *uci, float scale_x, float scale_y);
/*
* Free info. */
void BLI_uvproject_camera_info_free(ProjCameraInfo *uci);

View File

@@ -44,6 +44,11 @@ Dial *BLI_dial_init(const float start_position[2], float threshold)
return dial;
}
void BLI_dial_free(Dial *dial)
{
MEM_freeN(dial);
}
float BLI_dial_angle(Dial *dial, const float current_position[2])
{
float current_direction[2];

View File

@@ -173,6 +173,11 @@ ProjCameraInfo *BLI_uvproject_camera_info(const Object *ob,
return nullptr;
}
void BLI_uvproject_camera_info_free(ProjCameraInfo *uci)
{
MEM_freeN(uci);
}
void BLI_uvproject_from_view_ortho(float target[2], float source[3], const float rotmat[4][4])
{
float pv[3];

View File

@@ -57,7 +57,6 @@ class FileListWrapper {
static void filelist_free_fn(FileList *list)
{
filelist_free(list);
MEM_freeN(static_cast<void *>(list));
}
std::unique_ptr<FileList, decltype(&filelist_free_fn)> file_list_;

View File

@@ -1290,9 +1290,8 @@ static void gizmo_cage2d_exit(bContext *C, wmGizmo *gz, const bool cancel)
{
RectTransformInteraction *data = static_cast<RectTransformInteraction *>(gz->interaction_data);
/* Cannot use MEM_SAFE_FREE, as #Dial type is only forward-declared in `BLI_dial_2d.h` */
if (data->dial) {
MEM_freeN(static_cast<void *>(data->dial));
BLI_dial_free(data->dial);
data->dial = nullptr;
}

View File

@@ -71,7 +71,7 @@ void region_reset(ARegion &region, const RegionViewData &data)
region.winrct = data.winrct;
ED_view3d_mats_rv3d_restore(&rv3d, data.rv3d_store);
MEM_freeN(static_cast<void *>(data.rv3d_store));
ED_view3D_mats_rv3d_free(data.rv3d_store);
}
GPUOffScreen *image_render_begin(const int2 &win_size)

View File

@@ -25,6 +25,8 @@ const char *ED_info_statusbar_string(Main *bmain, Scene *scene, ViewLayer *view_
const char *ED_info_statistics_string(Main *bmain, Scene *scene, ViewLayer *view_layer);
void ED_view3d_local_stats_free(View3D *v3d);
/**
* \param v3d_local: Pass this argument to calculate view-port local statistics.
* Note that this must only be used for local-view, otherwise report specific statistics

View File

@@ -1065,6 +1065,7 @@ void ED_view3d_check_mats_rv3d(RegionView3D *rv3d);
RV3DMatrixStore *ED_view3d_mats_rv3d_backup(RegionView3D *rv3d);
void ED_view3d_mats_rv3d_restore(RegionView3D *rv3d, RV3DMatrixStore *rv3dmat);
void ED_view3D_mats_rv3d_free(RV3DMatrixStore *rv3d_mat);
RenderEngineType *ED_view3d_engine_type(const Scene *scene, int drawtype);

View File

@@ -3905,10 +3905,8 @@ namespace blender::ed::sculpt_paint {
StrokeCache::~StrokeCache()
{
/* Cannot use MEM_SAFE_FREE, as #Dial type is only forward-declared in `BLI_dial_2d.h` */
if (this->dial) {
MEM_freeN(static_cast<void *>(this->dial));
this->dial = nullptr;
BLI_dial_free(this->dial);
}
}

View File

@@ -1975,6 +1975,8 @@ void filelist_free(FileList *filelist)
memset(&filelist->filter_data, 0, sizeof(filelist->filter_data));
filelist->flags &= ~(FL_NEED_SORTING | FL_NEED_FILTERING);
MEM_freeN(filelist);
}
blender::asset_system::AssetLibrary *filelist_asset_library(FileList *filelist)
@@ -4196,7 +4198,6 @@ static void filelist_readjob_free(void *flrjv)
filelist_freelib(flrj->tmp_filelist);
filelist_free(flrj->tmp_filelist);
MEM_freeN(flrj->tmp_filelist);
}
BLI_mutex_end(&flrj->lock);

View File

@@ -1329,7 +1329,6 @@ void ED_fileselect_exit(wmWindowManager *wm, SpaceFile *sfile)
if (sfile->files) {
ED_fileselect_clear(wm, sfile);
filelist_free(sfile->files);
MEM_freeN(static_cast<void *>(sfile->files));
sfile->files = nullptr;
}
}

View File

@@ -117,7 +117,6 @@ static void file_free(SpaceLink *sl)
/* XXX would need to do thumbnails_stop here, but no context available */
filelist_freelib(sfile->files);
filelist_free(sfile->files);
MEM_freeN(static_cast<void *>(sfile->files));
sfile->files = nullptr;
}

View File

@@ -450,13 +450,18 @@ void ED_info_stats_clear(wmWindowManager *wm, ViewLayer *view_layer)
if (area->spacetype == SPACE_VIEW3D) {
View3D *v3d = (View3D *)area->spacedata.first;
if (v3d->localvd) {
MEM_SAFE_FREE(v3d->runtime.local_stats);
ED_view3d_local_stats_free(v3d);
}
}
}
}
}
void ED_view3d_local_stats_free(View3D *v3d)
{
MEM_SAFE_FREE(v3d->runtime.local_stats);
}
static bool format_stats(
Main *bmain, Scene *scene, ViewLayer *view_layer, View3D *v3d_local, SceneStatsFmt *stats_fmt)
{

View File

@@ -50,6 +50,7 @@
#include "ED_asset_shelf.hh"
#include "ED_geometry.hh"
#include "ED_info.hh"
#include "ED_object.hh"
#include "ED_outliner.hh"
#include "ED_render.hh"
@@ -277,12 +278,7 @@ static void view3d_free(SpaceLink *sl)
MEM_freeN(vd->localvd);
}
/* Cannot use MEM_SAFE_FREE, as #SceneStats type is only forward-declared in `DNA_layer_types.h`
*/
if (vd->runtime.local_stats) {
MEM_freeN(static_cast<void *>(vd->runtime.local_stats));
vd->runtime.local_stats = nullptr;
}
ED_view3d_local_stats_free(vd);
if (vd->runtime.properties_storage_free) {
vd->runtime.properties_storage_free(vd->runtime.properties_storage);
@@ -304,12 +300,7 @@ static void view3d_exit(wmWindowManager * /*wm*/, ScrArea *area)
{
BLI_assert(area->spacetype == SPACE_VIEW3D);
View3D *v3d = static_cast<View3D *>(area->spacedata.first);
/* Cannot use MEM_SAFE_FREE, as #SceneStats type is only forward-declared in `DNA_layer_types.h`
*/
if (v3d->runtime.local_stats) {
MEM_freeN(static_cast<void *>(v3d->runtime.local_stats));
v3d->runtime.local_stats = nullptr;
}
ED_view3d_local_stats_free(v3d);
}
static SpaceLink *view3d_duplicate(SpaceLink *sl)
@@ -2016,12 +2007,7 @@ static void space_view3d_listener(const wmSpaceTypeListenerParams *params)
static void space_view3d_refresh(const bContext *C, ScrArea *area)
{
View3D *v3d = (View3D *)area->spacedata.first;
/* Cannot use MEM_SAFE_FREE, as #SceneStats type is only forward-declared in `DNA_layer_types.h`
*/
if (v3d->runtime.local_stats) {
MEM_freeN(static_cast<void *>(v3d->runtime.local_stats));
v3d->runtime.local_stats = nullptr;
}
ED_view3d_local_stats_free(v3d);
if (v3d->localvd && v3d->localvd->runtime.flag & V3D_RUNTIME_LOCAL_MAYBE_EMPTY) {
ED_localview_exit_if_empty(CTX_data_ensure_evaluated_depsgraph(C),

View File

@@ -2715,6 +2715,11 @@ void ED_view3d_mats_rv3d_restore(RegionView3D *rv3d, RV3DMatrixStore *rv3dmat_pt
rv3d->pixsize = rv3dmat->pixsize;
}
void ED_view3D_mats_rv3d_free(RV3DMatrixStore *rv3d_mat)
{
MEM_freeN(rv3d_mat);
}
/** \} */
/* -------------------------------------------------------------------- */

View File

@@ -8,6 +8,7 @@
#include "DNA_curve_types.h"
#include "BLI_dial_2d.h"
#include "BLI_listbase.h"
#include "BLI_math_geom.h"
#include "BLI_math_matrix.hh"
@@ -385,9 +386,8 @@ void ViewOpsData::end_navigation(bContext *C)
WM_event_timer_remove(CTX_wm_manager(C), this->timer->win, this->timer);
}
/* Cannot use MEM_SAFE_FREE, as #Dial type is only forward-declared in `BLI_dial_2d.h` */
if (this->init.dial) {
MEM_freeN(static_cast<void *>(this->init.dial));
BLI_dial_free(this->init.dial);
this->init.dial = nullptr;
}

View File

@@ -39,6 +39,7 @@
#include "WM_api.hh"
#include "ED_info.hh"
#include "ED_object.hh"
#include "ED_screen.hh"
@@ -985,12 +986,7 @@ static bool view3d_localview_exit(const Depsgraph *depsgraph,
MEM_freeN(v3d->localvd);
v3d->localvd = nullptr;
/* Cannot use MEM_SAFE_FREE, as #SceneStats type is only forward-declared in `DNA_layer_types.h`
*/
if (v3d->runtime.local_stats) {
MEM_freeN(static_cast<void *>(v3d->runtime.local_stats));
v3d->runtime.local_stats = nullptr;
}
ED_view3d_local_stats_free(v3d);
LISTBASE_FOREACH (ARegion *, region, &area->regionbase) {
if (region->regiontype == RGN_TYPE_WINDOW) {

View File

@@ -3507,7 +3507,7 @@ static int uv_from_view_exec(bContext *C, wmOperator *op)
changed = true;
}
MEM_freeN(static_cast<void *>(uci));
BLI_uvproject_camera_info_free(uci);
}
}
else {

View File

@@ -3208,9 +3208,8 @@ static void radial_control_cancel(bContext *C, wmOperator *op)
wmWindowManager *wm = CTX_wm_manager(C);
ScrArea *area = CTX_wm_area(C);
/* Cannot use MEM_SAFE_FREE, as #Dial type is only forward-declared in `BLI_dial_2d.h` */
if (rc->dial) {
MEM_freeN(static_cast<void *>(rc->dial));
BLI_dial_free(rc->dial);
rc->dial = nullptr;
}
@@ -3405,9 +3404,8 @@ static int radial_control_modal(bContext *C, wmOperator *op, const wmEvent *even
if (event->val == KM_RELEASE) {
rc->slow_mode = false;
handled = true;
/* Cannot use MEM_SAFE_FREE, as #Dial type is only forward-declared in `BLI_dial_2d.h` */
if (rc->dial) {
MEM_freeN(static_cast<void *>(rc->dial));
BLI_dial_free(rc->dial);
rc->dial = nullptr;
}
}