From 8045576c60ec7c9f51f69a0db41d0bb1219ae99a Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Thu, 20 Feb 2025 11:24:34 +0100 Subject: [PATCH] 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 Pull Request: https://projects.blender.org/blender/blender/pulls/134765 --- source/blender/blenkernel/intern/screen.cc | 2 ++ source/blender/blenlib/BLI_dial_2d.h | 3 ++- source/blender/blenlib/BLI_uvproject.h | 6 ++++- source/blender/blenlib/intern/BLI_dial_2d.cc | 5 +++++ source/blender/blenlib/intern/uvproject.cc | 5 +++++ .../editors/asset/intern/asset_list.cc | 1 - .../gizmo_library/gizmo_types/cage2d_gizmo.cc | 3 +-- .../intern/grease_pencil_image_render.cc | 2 +- source/blender/editors/include/ED_info.hh | 2 ++ source/blender/editors/include/ED_view3d.hh | 1 + source/blender/editors/sculpt_paint/sculpt.cc | 4 +--- source/blender/editors/space_file/filelist.cc | 3 ++- source/blender/editors/space_file/filesel.cc | 1 - .../blender/editors/space_file/space_file.cc | 1 - .../blender/editors/space_info/info_stats.cc | 7 +++++- .../editors/space_view3d/space_view3d.cc | 22 ++++--------------- .../editors/space_view3d/view3d_draw.cc | 5 +++++ .../editors/space_view3d/view3d_navigate.cc | 4 ++-- .../editors/space_view3d/view3d_view.cc | 8 ++----- .../editors/uvedit/uvedit_unwrap_ops.cc | 2 +- .../windowmanager/intern/wm_operators.cc | 6 ++--- 21 files changed, 49 insertions(+), 44 deletions(-) diff --git a/source/blender/blenkernel/intern/screen.cc b/source/blender/blenkernel/intern/screen.cc index 891ac072154..0636fedc03c 100644 --- a/source/blender/blenkernel/intern/screen.cc +++ b/source/blender/blenkernel/intern/screen.cc @@ -54,6 +54,8 @@ # include "BPY_extern.hh" #endif +#include "WM_types.hh" + using blender::Span; using blender::Vector; diff --git a/source/blender/blenlib/BLI_dial_2d.h b/source/blender/blenlib/BLI_dial_2d.h index 9af9bc93395..4c75a435b64 100644 --- a/source/blender/blenlib/BLI_dial_2d.h +++ b/source/blender/blenlib/BLI_dial_2d.h @@ -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]); diff --git a/source/blender/blenlib/BLI_uvproject.h b/source/blender/blenlib/BLI_uvproject.h index 9938f1bebae..e696a3ac467 100644 --- a/source/blender/blenlib/BLI_uvproject.h +++ b/source/blender/blenlib/BLI_uvproject.h @@ -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); diff --git a/source/blender/blenlib/intern/BLI_dial_2d.cc b/source/blender/blenlib/intern/BLI_dial_2d.cc index 0223842b74d..5e85cc5b8b8 100644 --- a/source/blender/blenlib/intern/BLI_dial_2d.cc +++ b/source/blender/blenlib/intern/BLI_dial_2d.cc @@ -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]; diff --git a/source/blender/blenlib/intern/uvproject.cc b/source/blender/blenlib/intern/uvproject.cc index c335cd17981..f18f8b7b981 100644 --- a/source/blender/blenlib/intern/uvproject.cc +++ b/source/blender/blenlib/intern/uvproject.cc @@ -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]; diff --git a/source/blender/editors/asset/intern/asset_list.cc b/source/blender/editors/asset/intern/asset_list.cc index 9f85df6c425..7b2033c89b4 100644 --- a/source/blender/editors/asset/intern/asset_list.cc +++ b/source/blender/editors/asset/intern/asset_list.cc @@ -57,7 +57,6 @@ class FileListWrapper { static void filelist_free_fn(FileList *list) { filelist_free(list); - MEM_freeN(static_cast(list)); } std::unique_ptr file_list_; diff --git a/source/blender/editors/gizmo_library/gizmo_types/cage2d_gizmo.cc b/source/blender/editors/gizmo_library/gizmo_types/cage2d_gizmo.cc index a3471e3a179..85758d17800 100644 --- a/source/blender/editors/gizmo_library/gizmo_types/cage2d_gizmo.cc +++ b/source/blender/editors/gizmo_library/gizmo_types/cage2d_gizmo.cc @@ -1290,9 +1290,8 @@ static void gizmo_cage2d_exit(bContext *C, wmGizmo *gz, const bool cancel) { RectTransformInteraction *data = static_cast(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(data->dial)); + BLI_dial_free(data->dial); data->dial = nullptr; } diff --git a/source/blender/editors/grease_pencil/intern/grease_pencil_image_render.cc b/source/blender/editors/grease_pencil/intern/grease_pencil_image_render.cc index 9c3f6f21fe3..253d04ad42f 100644 --- a/source/blender/editors/grease_pencil/intern/grease_pencil_image_render.cc +++ b/source/blender/editors/grease_pencil/intern/grease_pencil_image_render.cc @@ -71,7 +71,7 @@ void region_reset(ARegion ®ion, const RegionViewData &data) region.winrct = data.winrct; ED_view3d_mats_rv3d_restore(&rv3d, data.rv3d_store); - MEM_freeN(static_cast(data.rv3d_store)); + ED_view3D_mats_rv3d_free(data.rv3d_store); } GPUOffScreen *image_render_begin(const int2 &win_size) diff --git a/source/blender/editors/include/ED_info.hh b/source/blender/editors/include/ED_info.hh index e385dfd38c5..b9747b9f747 100644 --- a/source/blender/editors/include/ED_info.hh +++ b/source/blender/editors/include/ED_info.hh @@ -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 diff --git a/source/blender/editors/include/ED_view3d.hh b/source/blender/editors/include/ED_view3d.hh index 59fdaaf9618..3da7f608a1b 100644 --- a/source/blender/editors/include/ED_view3d.hh +++ b/source/blender/editors/include/ED_view3d.hh @@ -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); diff --git a/source/blender/editors/sculpt_paint/sculpt.cc b/source/blender/editors/sculpt_paint/sculpt.cc index dbc1957dc2e..310b3886e81 100644 --- a/source/blender/editors/sculpt_paint/sculpt.cc +++ b/source/blender/editors/sculpt_paint/sculpt.cc @@ -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(this->dial)); - this->dial = nullptr; + BLI_dial_free(this->dial); } } diff --git a/source/blender/editors/space_file/filelist.cc b/source/blender/editors/space_file/filelist.cc index a549c23df10..e65b4760313 100644 --- a/source/blender/editors/space_file/filelist.cc +++ b/source/blender/editors/space_file/filelist.cc @@ -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); diff --git a/source/blender/editors/space_file/filesel.cc b/source/blender/editors/space_file/filesel.cc index bc507775e82..1e844233dbf 100644 --- a/source/blender/editors/space_file/filesel.cc +++ b/source/blender/editors/space_file/filesel.cc @@ -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(sfile->files)); sfile->files = nullptr; } } diff --git a/source/blender/editors/space_file/space_file.cc b/source/blender/editors/space_file/space_file.cc index bc84e7d9aed..9c045d27853 100644 --- a/source/blender/editors/space_file/space_file.cc +++ b/source/blender/editors/space_file/space_file.cc @@ -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(sfile->files)); sfile->files = nullptr; } diff --git a/source/blender/editors/space_info/info_stats.cc b/source/blender/editors/space_info/info_stats.cc index 89734ab5e57..f5c31e9ab2c 100644 --- a/source/blender/editors/space_info/info_stats.cc +++ b/source/blender/editors/space_info/info_stats.cc @@ -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) { diff --git a/source/blender/editors/space_view3d/space_view3d.cc b/source/blender/editors/space_view3d/space_view3d.cc index 94fdcd72e94..8b58fd8b59f 100644 --- a/source/blender/editors/space_view3d/space_view3d.cc +++ b/source/blender/editors/space_view3d/space_view3d.cc @@ -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(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(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(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(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), diff --git a/source/blender/editors/space_view3d/view3d_draw.cc b/source/blender/editors/space_view3d/view3d_draw.cc index a2083fe0c06..25eb3990e75 100644 --- a/source/blender/editors/space_view3d/view3d_draw.cc +++ b/source/blender/editors/space_view3d/view3d_draw.cc @@ -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); +} + /** \} */ /* -------------------------------------------------------------------- */ diff --git a/source/blender/editors/space_view3d/view3d_navigate.cc b/source/blender/editors/space_view3d/view3d_navigate.cc index e2295831c71..5df2c657c85 100644 --- a/source/blender/editors/space_view3d/view3d_navigate.cc +++ b/source/blender/editors/space_view3d/view3d_navigate.cc @@ -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(this->init.dial)); + BLI_dial_free(this->init.dial); this->init.dial = nullptr; } diff --git a/source/blender/editors/space_view3d/view3d_view.cc b/source/blender/editors/space_view3d/view3d_view.cc index 146f53f8b2c..79ac4ee6645 100644 --- a/source/blender/editors/space_view3d/view3d_view.cc +++ b/source/blender/editors/space_view3d/view3d_view.cc @@ -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(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) { diff --git a/source/blender/editors/uvedit/uvedit_unwrap_ops.cc b/source/blender/editors/uvedit/uvedit_unwrap_ops.cc index 1835947e5c7..bd24daad887 100644 --- a/source/blender/editors/uvedit/uvedit_unwrap_ops.cc +++ b/source/blender/editors/uvedit/uvedit_unwrap_ops.cc @@ -3507,7 +3507,7 @@ static int uv_from_view_exec(bContext *C, wmOperator *op) changed = true; } - MEM_freeN(static_cast(uci)); + BLI_uvproject_camera_info_free(uci); } } else { diff --git a/source/blender/windowmanager/intern/wm_operators.cc b/source/blender/windowmanager/intern/wm_operators.cc index 675ec7a570d..9fb37b1c994 100644 --- a/source/blender/windowmanager/intern/wm_operators.cc +++ b/source/blender/windowmanager/intern/wm_operators.cc @@ -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(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(rc->dial)); + BLI_dial_free(rc->dial); rc->dial = nullptr; } }