From 9506fed905b99d41ba95dfd9504deba9aefe496d Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Thu, 29 Aug 2024 13:11:42 +0200 Subject: [PATCH] Refactor: Move some Editors' data allocation from C alloc/free to C++ new/delete. Part of the effort to make PointerRNA non-trivial (#122431). --- source/blender/editors/io/io_cache.cc | 9 +++-- source/blender/editors/object/object_edit.cc | 11 +++--- .../editors/space_buttons/buttons_context.cc | 4 +-- .../editors/space_buttons/buttons_texture.cc | 11 +++--- .../editors/space_buttons/space_buttons.cc | 35 ++++++++++++++----- source/blender/editors/space_clip/clip_ops.cc | 9 +++-- .../blender/editors/space_image/image_ops.cc | 9 ++--- 7 files changed, 58 insertions(+), 30 deletions(-) diff --git a/source/blender/editors/io/io_cache.cc b/source/blender/editors/io/io_cache.cc index 4884318f952..959025ca3f5 100644 --- a/source/blender/editors/io/io_cache.cc +++ b/source/blender/editors/io/io_cache.cc @@ -43,7 +43,7 @@ static void cachefile_init(bContext *C, wmOperator *op) { PropertyPointerRNA *pprop; - op->customdata = pprop = MEM_cnew("OpenPropertyPointerRNA"); + op->customdata = pprop = MEM_new("OpenPropertyPointerRNA"); UI_context_active_but_prop_get_templateID(C, &pprop->ptr, &pprop->prop); } @@ -67,8 +67,11 @@ static int cachefile_open_invoke(bContext *C, wmOperator *op, const wmEvent * /* static void open_cancel(bContext * /*C*/, wmOperator *op) { - MEM_freeN(op->customdata); - op->customdata = nullptr; + if (op->customdata) { + PropertyPointerRNA *prop_ptr = static_cast(op->customdata); + op->customdata = nullptr; + MEM_delete(prop_ptr); + } } static int cachefile_open_exec(bContext *C, wmOperator *op) diff --git a/source/blender/editors/object/object_edit.cc b/source/blender/editors/object/object_edit.cc index 50023b780ae..9e2817f33e1 100644 --- a/source/blender/editors/object/object_edit.cc +++ b/source/blender/editors/object/object_edit.cc @@ -2229,7 +2229,7 @@ static int move_to_collection_menus_create(wmOperator *op, MoveToCollectionData int index = menu->index; LISTBASE_FOREACH (CollectionChild *, child, &menu->collection->children) { Collection *collection = child->collection; - MoveToCollectionData *submenu = MEM_cnew(__func__); + MoveToCollectionData *submenu = MEM_new(__func__); BLI_addtail(&menu->submenus, submenu); submenu->collection = collection; submenu->index = ++index; @@ -2241,10 +2241,11 @@ static int move_to_collection_menus_create(wmOperator *op, MoveToCollectionData static void move_to_collection_menus_free_recursive(MoveToCollectionData *menu) { - LISTBASE_FOREACH (MoveToCollectionData *, submenu, &menu->submenus) { + LISTBASE_FOREACH_MUTABLE (MoveToCollectionData *, submenu, &menu->submenus) { move_to_collection_menus_free_recursive(submenu); + MEM_delete(submenu); } - BLI_freelistN(&menu->submenus); + BLI_listbase_clear(&menu->submenus); } static void move_to_collection_menus_free(MoveToCollectionData **menu) @@ -2254,7 +2255,7 @@ static void move_to_collection_menus_free(MoveToCollectionData **menu) } move_to_collection_menus_free_recursive(*menu); - MEM_freeN(*menu); + MEM_delete(*menu); *menu = nullptr; } @@ -2353,7 +2354,7 @@ static int move_to_collection_invoke(bContext *C, wmOperator *op, const wmEvent * * So we are left with a memory that will necessarily leak. It's a small leak though. */ if (master_collection_menu == nullptr) { - master_collection_menu = MEM_cnew( + master_collection_menu = MEM_new( "MoveToCollectionData menu - expected eventual memleak"); } diff --git a/source/blender/editors/space_buttons/buttons_context.cc b/source/blender/editors/space_buttons/buttons_context.cc index 9c9449f7102..179a8a0e2c7 100644 --- a/source/blender/editors/space_buttons/buttons_context.cc +++ b/source/blender/editors/space_buttons/buttons_context.cc @@ -552,7 +552,7 @@ static bool buttons_context_path( Scene *scene = WM_window_get_active_scene(window); ViewLayer *view_layer = WM_window_get_active_view_layer(window); - memset(path, 0, sizeof(*path)); + *path = {}; path->flag = flag; /* If some ID datablock is pinned, set the root pointer. */ @@ -691,7 +691,7 @@ static int buttons_shading_new_context(const bContext *C, int flag) void buttons_context_compute(const bContext *C, SpaceProperties *sbuts) { if (!sbuts->path) { - sbuts->path = MEM_callocN(sizeof(ButsContextPath), "ButsContextPath"); + sbuts->path = MEM_new("ButsContextPath"); } ButsContextPath *path = static_cast(sbuts->path); diff --git a/source/blender/editors/space_buttons/buttons_texture.cc b/source/blender/editors/space_buttons/buttons_texture.cc index 865fe5563a5..d5da6e9b3be 100644 --- a/source/blender/editors/space_buttons/buttons_texture.cc +++ b/source/blender/editors/space_buttons/buttons_texture.cc @@ -70,7 +70,7 @@ static void buttons_texture_user_socket_property_add(ListBase *users, int icon, const char *name) { - ButsTextureUser *user = MEM_cnew("ButsTextureUser"); + ButsTextureUser *user = MEM_new("ButsTextureUser"); user->id = id; user->ptr = ptr; @@ -94,7 +94,7 @@ static void buttons_texture_user_property_add(ListBase *users, int icon, const char *name) { - ButsTextureUser *user = MEM_cnew("ButsTextureUser"); + ButsTextureUser *user = MEM_new("ButsTextureUser"); user->id = id; user->ptr = ptr; @@ -117,7 +117,7 @@ static void buttons_texture_user_node_add(ListBase *users, int icon, const char *name) { - ButsTextureUser *user = MEM_cnew("ButsTextureUser"); + ButsTextureUser *user = MEM_new("ButsTextureUser"); user->id = id; user->ntree = ntree; @@ -362,7 +362,10 @@ void buttons_texture_context_compute(const bContext *C, SpaceProperties *sbuts) sbuts->texuser = ct; } else { - BLI_freelistN(&ct->users); + LISTBASE_FOREACH_MUTABLE (ButsTextureUser *, user, &ct->users) { + MEM_delete(user); + } + BLI_listbase_clear(&ct->users); } buttons_texture_users_from_context(&ct->users, C, sbuts); diff --git a/source/blender/editors/space_buttons/space_buttons.cc b/source/blender/editors/space_buttons/space_buttons.cc index 5d4aabc0963..341fbe97ee9 100644 --- a/source/blender/editors/space_buttons/space_buttons.cc +++ b/source/blender/editors/space_buttons/space_buttons.cc @@ -91,12 +91,15 @@ static void buttons_free(SpaceLink *sl) SpaceProperties *sbuts = (SpaceProperties *)sl; if (sbuts->path) { - MEM_freeN(sbuts->path); + MEM_delete(static_cast(sbuts->path)); } if (sbuts->texuser) { ButsContextTexture *ct = static_cast(sbuts->texuser); - BLI_freelistN(&ct->users); + LISTBASE_FOREACH_MUTABLE (ButsTextureUser *, user, &ct->users) { + MEM_delete(user); + } + BLI_listbase_clear(&ct->users); MEM_freeN(ct); } @@ -881,7 +884,9 @@ static void buttons_id_remap(ScrArea * /*area*/, if (i != 0) { /* If the first item in the path is cleared, the whole path is cleared, so no need to * clear further items here, see also at the end of this block. */ - memset(&path->ptr[i], 0, sizeof(path->ptr[i]) * (path->len - i)); + for (int j = i; j < path->len; j++) { + path->ptr[j] = {}; + } } break; } @@ -890,7 +895,9 @@ static void buttons_id_remap(ScrArea * /*area*/, /* There is no easy way to check/make path downwards valid, just nullify it. * Next redraw will rebuild this anyway. */ i++; - memset(&path->ptr[i], 0, sizeof(path->ptr[i]) * (path->len - i)); + for (int j = i; j < path->len; j++) { + path->ptr[j] = {}; + } path->len = i; break; } @@ -902,15 +909,19 @@ static void buttons_id_remap(ScrArea * /*area*/, } } } - if (path->len == 0) { - MEM_SAFE_FREE(sbuts->path); + if (path->len == 0 && sbuts->path) { + MEM_delete(static_cast(sbuts->path)); + sbuts->path = nullptr; } } if (sbuts->texuser) { ButsContextTexture *ct = static_cast(sbuts->texuser); mappings.apply(reinterpret_cast(&ct->texture), ID_REMAP_APPLY_DEFAULT); - BLI_freelistN(&ct->users); + LISTBASE_FOREACH_MUTABLE (ButsTextureUser *, user, &ct->users) { + MEM_delete(user); + } + BLI_listbase_clear(&ct->users); ct->user = nullptr; } } @@ -928,7 +939,10 @@ static void buttons_foreach_id(SpaceLink *space_link, LibraryForeachIDData *data } /* NOTE: Restoring path pointers is complicated, if not impossible, because this contains * data pointers too, not just ID ones. See #40046. */ - MEM_SAFE_FREE(sbuts->path); + if (sbuts->path) { + MEM_delete(static_cast(sbuts->path)); + sbuts->path = nullptr; + } } if (sbuts->texuser) { @@ -936,7 +950,10 @@ static void buttons_foreach_id(SpaceLink *space_link, LibraryForeachIDData *data BKE_LIB_FOREACHID_PROCESS_IDSUPER(data, ct->texture, IDWALK_CB_DIRECT_WEAK_LINK); if (!is_readonly) { - BLI_freelistN(&ct->users); + LISTBASE_FOREACH_MUTABLE (ButsTextureUser *, user, &ct->users) { + MEM_delete(user); + } + BLI_listbase_clear(&ct->users); ct->user = nullptr; } } diff --git a/source/blender/editors/space_clip/clip_ops.cc b/source/blender/editors/space_clip/clip_ops.cc index 3956560b7b6..bc5793a9a2f 100644 --- a/source/blender/editors/space_clip/clip_ops.cc +++ b/source/blender/editors/space_clip/clip_ops.cc @@ -166,14 +166,17 @@ static void open_init(bContext *C, wmOperator *op) { PropertyPointerRNA *pprop; - op->customdata = pprop = MEM_cnew("OpenPropertyPointerRNA"); + op->customdata = pprop = MEM_new("OpenPropertyPointerRNA"); UI_context_active_but_prop_get_templateID(C, &pprop->ptr, &pprop->prop); } static void open_cancel(bContext * /*C*/, wmOperator *op) { - MEM_freeN(op->customdata); - op->customdata = nullptr; + if (op->customdata) { + PropertyPointerRNA *pprop = static_cast(op->customdata); + op->customdata = nullptr; + MEM_delete(pprop); + } } static int open_exec(bContext *C, wmOperator *op) diff --git a/source/blender/editors/space_image/image_ops.cc b/source/blender/editors/space_image/image_ops.cc index 2d238203eb4..7a9eb97b0c2 100644 --- a/source/blender/editors/space_image/image_ops.cc +++ b/source/blender/editors/space_image/image_ops.cc @@ -1253,8 +1253,7 @@ struct ImageOpenData { static void image_open_init(bContext *C, wmOperator *op) { ImageOpenData *iod; - op->customdata = iod = static_cast( - MEM_callocN(sizeof(ImageOpenData), __func__)); + op->customdata = iod = MEM_new(__func__); iod->iuser = static_cast( CTX_data_pointer_get_type(C, "image_user", &RNA_ImageUser).data); UI_context_active_but_prop_get_templateID(C, &iod->pprop.ptr, &iod->pprop.prop); @@ -1262,8 +1261,9 @@ static void image_open_init(bContext *C, wmOperator *op) static void image_open_cancel(bContext * /*C*/, wmOperator *op) { - MEM_freeN(op->customdata); + ImageOpenData *iod = static_cast(op->customdata); op->customdata = nullptr; + MEM_delete(iod); } static Image *image_open_single(Main *bmain, @@ -1442,7 +1442,8 @@ static int image_open_exec(bContext *C, wmOperator *op) BKE_image_signal(bmain, ima, iuser, IMA_SIGNAL_RELOAD); WM_event_add_notifier(C, NC_IMAGE | NA_EDITED, ima); - MEM_freeN(op->customdata); + op->customdata = nullptr; + MEM_delete(iod); return OPERATOR_FINISHED; }