2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * Contains management of ID's for freeing & deletion.
23 #include "MEM_guardedalloc.h"
25 /* all types are needed here, in order to do memory operations */
26 #include "DNA_armature_types.h"
27 #include "DNA_brush_types.h"
28 #include "DNA_camera_types.h"
29 #include "DNA_cachefile_types.h"
30 #include "DNA_collection_types.h"
31 #include "DNA_gpencil_types.h"
32 #include "DNA_ipo_types.h"
33 #include "DNA_key_types.h"
34 #include "DNA_light_types.h"
35 #include "DNA_lattice_types.h"
36 #include "DNA_linestyle_types.h"
37 #include "DNA_material_types.h"
38 #include "DNA_mesh_types.h"
39 #include "DNA_meta_types.h"
40 #include "DNA_movieclip_types.h"
41 #include "DNA_mask_types.h"
42 #include "DNA_node_types.h"
43 #include "DNA_object_types.h"
44 #include "DNA_lightprobe_types.h"
45 #include "DNA_scene_types.h"
46 #include "DNA_screen_types.h"
47 #include "DNA_speaker_types.h"
48 #include "DNA_sound_types.h"
49 #include "DNA_text_types.h"
50 #include "DNA_vfont_types.h"
51 #include "DNA_windowmanager_types.h"
52 #include "DNA_workspace_types.h"
53 #include "DNA_world_types.h"
55 #include "BLI_utildefines.h"
57 #include "BLI_listbase.h"
59 #include "BKE_action.h"
60 #include "BKE_armature.h"
61 #include "BKE_brush.h"
62 #include "BKE_camera.h"
63 #include "BKE_cachefile.h"
64 #include "BKE_collection.h"
65 #include "BKE_curve.h"
67 #include "BKE_gpencil.h"
68 #include "BKE_idprop.h"
69 #include "BKE_image.h"
72 #include "BKE_light.h"
73 #include "BKE_lattice.h"
74 #include "BKE_lib_id.h"
75 #include "BKE_lib_override.h"
76 #include "BKE_lib_remap.h"
77 #include "BKE_library.h"
78 #include "BKE_linestyle.h"
80 #include "BKE_material.h"
83 #include "BKE_mball.h"
84 #include "BKE_movieclip.h"
86 #include "BKE_object.h"
87 #include "BKE_paint.h"
88 #include "BKE_particle.h"
89 #include "BKE_lightprobe.h"
90 #include "BKE_speaker.h"
91 #include "BKE_sound.h"
92 #include "BKE_screen.h"
93 #include "BKE_scene.h"
95 #include "BKE_texture.h"
96 #include "BKE_workspace.h"
97 #include "BKE_world.h"
99 #include "lib_intern.h"
101 #include "DEG_depsgraph.h"
104 # include "BPY_extern.h"
107 /* Not used currently. */
108 // static CLG_LogRef LOG = {.identifier = "bke.lib_id_delete"};
110 void BKE_libblock_free_data(ID *id, const bool do_id_user)
112 if (id->properties) {
113 IDP_FreePropertyContent_ex(id->properties, do_id_user);
114 MEM_freeN(id->properties);
117 if (id->override_library) {
118 BKE_lib_override_library_free(&id->override_library, do_id_user);
121 /* XXX TODO remove animdata handling from each type's freeing func,
122 * and do it here, like for copy! */
125 void BKE_libblock_free_datablock(ID *id, const int UNUSED(flag))
127 const short type = GS(id->name);
130 BKE_scene_free_ex((Scene *)id, false);
133 BKE_library_free((Library *)id);
136 BKE_object_free((Object *)id);
139 BKE_mesh_free((Mesh *)id);
142 BKE_curve_free((Curve *)id);
145 BKE_mball_free((MetaBall *)id);
148 BKE_material_free((Material *)id);
151 BKE_texture_free((Tex *)id);
154 BKE_image_free((Image *)id);
157 BKE_lattice_free((Lattice *)id);
160 BKE_light_free((Light *)id);
163 BKE_camera_free((Camera *)id);
165 case ID_IP: /* Deprecated. */
166 BKE_ipo_free((Ipo *)id);
169 BKE_key_free((Key *)id);
172 BKE_world_free((World *)id);
175 BKE_screen_free((bScreen *)id);
178 BKE_vfont_free((VFont *)id);
181 BKE_text_free((Text *)id);
184 BKE_speaker_free((Speaker *)id);
187 BKE_lightprobe_free((LightProbe *)id);
190 BKE_sound_free((bSound *)id);
193 BKE_collection_free((Collection *)id);
196 BKE_armature_free((bArmature *)id);
199 BKE_action_free((bAction *)id);
202 ntreeFreeTree((bNodeTree *)id);
205 BKE_brush_free((Brush *)id);
208 BKE_particlesettings_free((ParticleSettings *)id);
211 if (free_windowmanager_cb) {
212 free_windowmanager_cb(NULL, (wmWindowManager *)id);
216 BKE_gpencil_free((bGPdata *)id, true);
219 BKE_movieclip_free((MovieClip *)id);
222 BKE_mask_free((Mask *)id);
225 BKE_linestyle_free((FreestyleLineStyle *)id);
228 BKE_palette_free((Palette *)id);
231 BKE_paint_curve_free((PaintCurve *)id);
234 BKE_cachefile_free((CacheFile *)id);
237 BKE_workspace_free((WorkSpace *)id);
243 * Complete ID freeing, extended version for corner cases.
244 * Can override default (and safe!) freeing process, to gain some speed up.
246 * At that point, given id is assumed to not be used by any other data-block already
247 * (might not be actually true, in case e.g. several inter-related IDs get freed together...).
248 * However, they might still be using (referencing) other IDs, this code takes care of it if
249 * #LIB_TAG_NO_USER_REFCOUNT is not defined.
251 * \param bmain: #Main database containing the freed #ID,
252 * can be NULL in case it's a temp ID outside of any #Main.
253 * \param idv: Pointer to ID to be freed.
254 * \param flag: Set of \a LIB_ID_FREE_... flags controlling/overriding usual freeing process,
255 * 0 to get default safe behavior.
256 * \param use_flag_from_idtag: Still use freeing info flags from given #ID datablock,
257 * even if some overriding ones are passed in \a flag parameter.
259 void BKE_id_free_ex(Main *bmain, void *idv, int flag, const bool use_flag_from_idtag)
263 if (use_flag_from_idtag) {
264 if ((id->tag & LIB_TAG_NO_MAIN) != 0) {
265 flag |= LIB_ID_FREE_NO_MAIN | LIB_ID_FREE_NO_UI_USER | LIB_ID_FREE_NO_DEG_TAG;
268 flag &= ~LIB_ID_FREE_NO_MAIN;
271 if ((id->tag & LIB_TAG_NO_USER_REFCOUNT) != 0) {
272 flag |= LIB_ID_FREE_NO_USER_REFCOUNT;
275 flag &= ~LIB_ID_FREE_NO_USER_REFCOUNT;
278 if ((id->tag & LIB_TAG_NOT_ALLOCATED) != 0) {
279 flag |= LIB_ID_FREE_NOT_ALLOCATED;
282 flag &= ~LIB_ID_FREE_NOT_ALLOCATED;
286 BLI_assert((flag & LIB_ID_FREE_NO_MAIN) != 0 || bmain != NULL);
287 BLI_assert((flag & LIB_ID_FREE_NO_MAIN) != 0 || (flag & LIB_ID_FREE_NOT_ALLOCATED) == 0);
288 BLI_assert((flag & LIB_ID_FREE_NO_MAIN) != 0 || (flag & LIB_ID_FREE_NO_USER_REFCOUNT) == 0);
290 const short type = GS(id->name);
292 if (bmain && (flag & LIB_ID_FREE_NO_DEG_TAG) == 0) {
293 DEG_id_type_tag(bmain, type);
297 # ifdef WITH_PYTHON_SAFETY
300 if (id->py_instance) {
301 BPY_DECREF_RNA_INVALIDATE(id->py_instance);
305 if ((flag & LIB_ID_FREE_NO_USER_REFCOUNT) == 0) {
306 BKE_libblock_relink_ex(bmain, id, NULL, NULL, 0);
309 BKE_libblock_free_datablock(id, flag);
311 /* avoid notifying on removed data */
312 if ((flag & LIB_ID_FREE_NO_MAIN) == 0) {
313 BKE_main_lock(bmain);
316 if ((flag & LIB_ID_FREE_NO_UI_USER) == 0) {
317 if (free_notifier_reference_cb) {
318 free_notifier_reference_cb(id);
321 if (remap_editor_id_reference_cb) {
322 remap_editor_id_reference_cb(id, NULL);
326 if ((flag & LIB_ID_FREE_NO_MAIN) == 0) {
327 ListBase *lb = which_libbase(bmain, type);
331 BKE_libblock_free_data(id, (flag & LIB_ID_FREE_NO_USER_REFCOUNT) == 0);
333 if ((flag & LIB_ID_FREE_NO_MAIN) == 0) {
334 BKE_main_unlock(bmain);
337 if ((flag & LIB_ID_FREE_NOT_ALLOCATED) == 0) {
343 * Complete ID freeing, should be usable in most cases (even for out-of-Main IDs).
345 * See #BKE_id_free_ex description for full details.
347 * \param bmain: Main database containing the freed ID,
348 * can be NULL in case it's a temp ID outside of any Main.
349 * \param idv: Pointer to ID to be freed.
351 void BKE_id_free(Main *bmain, void *idv)
353 BKE_id_free_ex(bmain, idv, 0, true);
357 * Not really a freeing function by itself,
358 * it decrements usercount of given id, and only frees it if it reaches 0.
360 void BKE_id_free_us(Main *bmain, void *idv) /* test users */
366 /* XXX This is a temp (2.77) hack so that we keep same behavior as in 2.76 regarding collections
367 * when deleting an object. Since only 'user_one' usage of objects is collections,
368 * and only 'real user' usage of objects is scenes, removing that 'user_one' tag when there
369 * is no more real (scene) users of an object ensures it gets fully unlinked.
370 * But only for local objects, not linked ones!
371 * Otherwise, there is no real way to get rid of an object anymore -
372 * better handling of this is TODO.
374 if ((GS(id->name) == ID_OB) && (id->us == 1) && (id->lib == NULL)) {
375 id_us_clear_real(id);
379 BKE_libblock_unlink(bmain, id, false, false);
381 BKE_id_free(bmain, id);
385 static void id_delete(Main *bmain, const bool do_tagged_deletion)
387 const int tag = LIB_TAG_DOIT;
388 ListBase *lbarray[MAX_LIBARRAY];
389 Link dummy_link = {0};
392 /* Used by batch tagged deletion, when we call BKE_id_free then, id is no more in Main database,
393 * and has already properly unlinked its other IDs usages.
394 * UI users are always cleared in BKE_libblock_remap_locked() call, so we can always skip it. */
395 const int free_flag = LIB_ID_FREE_NO_UI_USER |
396 (do_tagged_deletion ? LIB_ID_FREE_NO_MAIN | LIB_ID_FREE_NO_USER_REFCOUNT :
398 ListBase tagged_deleted_ids = {NULL};
400 base_count = set_listbasepointers(bmain, lbarray);
402 BKE_main_lock(bmain);
403 if (do_tagged_deletion) {
404 /* Main idea of batch deletion is to remove all IDs to be deleted from Main database.
405 * This means that we won't have to loop over all deleted IDs to remove usages
406 * of other deleted IDs.
407 * This gives tremendous speed-up when deleting a large amount of IDs from a Main
408 * containing thousands of those.
409 * This also means that we have to be very careful here, as we by-pass many 'common'
410 * processing, hence risking to 'corrupt' at least user counts, if not IDs themselves. */
411 bool keep_looping = true;
412 while (keep_looping) {
414 ID *last_remapped_id = tagged_deleted_ids.last;
415 keep_looping = false;
417 /* First tag and remove from Main all datablocks directly from target lib.
418 * Note that we go forward here, since we want to check dependencies before users
419 * (e.g. meshes before objects). Avoids to have to loop twice. */
420 for (i = 0; i < base_count; i++) {
421 ListBase *lb = lbarray[i];
423 for (id = lb->first; id; id = id_next) {
425 /* Note: in case we delete a library, we also delete all its datablocks! */
426 if ((id->tag & tag) || (id->lib != NULL && (id->lib->id.tag & tag))) {
428 BLI_addtail(&tagged_deleted_ids, id);
429 /* Do not tag as no_main now, we want to unlink it first (lower-level ID management
430 * code has some specific handling of 'nom main'
431 * IDs that would be a problem in that case). */
437 if (last_remapped_id == NULL) {
438 dummy_link.next = tagged_deleted_ids.first;
439 last_remapped_id = (ID *)(&dummy_link);
441 for (id = last_remapped_id->next; id; id = id->next) {
442 /* Will tag 'never NULL' users of this ID too.
443 * Note that we cannot use BKE_libblock_unlink() here,
444 * since it would ignore indirect (and proxy!)
445 * links, this can lead to nasty crashing here in second, actual deleting loop.
446 * Also, this will also flag users of deleted data that cannot be unlinked
447 * (object using deleted obdata, etc.), so that they also get deleted. */
448 BKE_libblock_remap_locked(
449 bmain, id, NULL, ID_REMAP_FLAG_NEVER_NULL_USAGE | ID_REMAP_FORCE_NEVER_NULL_USAGE);
450 /* Since we removed ID from Main,
451 * we also need to unlink its own other IDs usages ourself. */
452 BKE_libblock_relink_ex(bmain, id, NULL, NULL, 0);
453 /* Now we can safely mark that ID as not being in Main database anymore. */
454 id->tag |= LIB_TAG_NO_MAIN;
455 /* This is needed because we may not have remapped usages
456 * of that ID by other deleted ones. */
457 // id->us = 0; /* Is it actually? */
462 /* First tag all datablocks directly from target lib.
463 * Note that we go forward here, since we want to check dependencies before users
464 * (e.g. meshes before objects).
465 * Avoids to have to loop twice. */
466 for (i = 0; i < base_count; i++) {
467 ListBase *lb = lbarray[i];
470 for (id = lb->first; id; id = id_next) {
472 /* Note: in case we delete a library, we also delete all its datablocks! */
473 if ((id->tag & tag) || (id->lib != NULL && (id->lib->id.tag & tag))) {
476 /* Will tag 'never NULL' users of this ID too.
477 * Note that we cannot use BKE_libblock_unlink() here, since it would ignore indirect
478 * (and proxy!) links, this can lead to nasty crashing here in second,
479 * actual deleting loop.
480 * Also, this will also flag users of deleted data that cannot be unlinked
481 * (object using deleted obdata, etc.), so that they also get deleted. */
482 BKE_libblock_remap_locked(
483 bmain, id, NULL, ID_REMAP_FLAG_NEVER_NULL_USAGE | ID_REMAP_FORCE_NEVER_NULL_USAGE);
488 BKE_main_unlock(bmain);
490 /* In usual reversed order, such that all usage of a given ID, even 'never NULL' ones,
491 * have been already cleared when we reach it
492 * (e.g. Objects being processed before meshes, they'll have already released their 'reference'
493 * over meshes when we come to freeing obdata). */
494 for (i = do_tagged_deletion ? 1 : base_count; i--;) {
495 ListBase *lb = lbarray[i];
498 for (id = do_tagged_deletion ? tagged_deleted_ids.first : lb->first; id; id = id_next) {
503 printf("%s: deleting %s (%d)\n", __func__, id->name, id->us);
505 BLI_assert(id->us == 0);
507 BKE_id_free_ex(bmain, id, free_flag, !do_tagged_deletion);
512 bmain->is_memfile_undo_written = false;
516 * Properly delete a single ID from given \a bmain database.
518 void BKE_id_delete(Main *bmain, void *idv)
520 BKE_main_id_tag_all(bmain, LIB_TAG_DOIT, false);
521 ((ID *)idv)->tag |= LIB_TAG_DOIT;
523 id_delete(bmain, false);
527 * Properly delete all IDs tagged with \a LIB_TAG_DOIT, in given \a bmain database.
529 * This is more efficient than calling #BKE_id_delete repetitively on a large set of IDs
530 * (several times faster when deleting most of the IDs at once)...
532 * \warning Considered experimental for now, seems to be working OK but this is
533 * risky code in a complicated area.
535 void BKE_id_multi_tagged_delete(Main *bmain)
537 id_delete(bmain, true);