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.
25 #include "MEM_guardedalloc.h"
27 /* all types are needed here, in order to do memory operations */
28 #include "DNA_anim_types.h"
29 #include "DNA_armature_types.h"
30 #include "DNA_brush_types.h"
31 #include "DNA_camera_types.h"
32 #include "DNA_cachefile_types.h"
33 #include "DNA_collection_types.h"
34 #include "DNA_gpencil_types.h"
35 #include "DNA_ipo_types.h"
36 #include "DNA_key_types.h"
37 #include "DNA_light_types.h"
38 #include "DNA_lattice_types.h"
39 #include "DNA_linestyle_types.h"
40 #include "DNA_material_types.h"
41 #include "DNA_mesh_types.h"
42 #include "DNA_meta_types.h"
43 #include "DNA_movieclip_types.h"
44 #include "DNA_mask_types.h"
45 #include "DNA_node_types.h"
46 #include "DNA_object_types.h"
47 #include "DNA_lightprobe_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_screen_types.h"
50 #include "DNA_speaker_types.h"
51 #include "DNA_sound_types.h"
52 #include "DNA_text_types.h"
53 #include "DNA_vfont_types.h"
54 #include "DNA_windowmanager_types.h"
55 #include "DNA_workspace_types.h"
56 #include "DNA_world_types.h"
58 #include "BLI_utildefines.h"
60 #include "BLI_listbase.h"
62 #include "BKE_action.h"
63 #include "BKE_armature.h"
64 #include "BKE_brush.h"
65 #include "BKE_camera.h"
66 #include "BKE_cachefile.h"
67 #include "BKE_collection.h"
68 #include "BKE_curve.h"
69 #include "BKE_fcurve.h"
71 #include "BKE_gpencil.h"
72 #include "BKE_idprop.h"
73 #include "BKE_image.h"
76 #include "BKE_light.h"
77 #include "BKE_lattice.h"
78 #include "BKE_lib_id.h"
79 #include "BKE_lib_override.h"
80 #include "BKE_lib_remap.h"
81 #include "BKE_linestyle.h"
83 #include "BKE_material.h"
86 #include "BKE_mball.h"
87 #include "BKE_movieclip.h"
89 #include "BKE_object.h"
90 #include "BKE_paint.h"
91 #include "BKE_particle.h"
92 #include "BKE_lightprobe.h"
93 #include "BKE_speaker.h"
94 #include "BKE_sound.h"
95 #include "BKE_screen.h"
96 #include "BKE_scene.h"
98 #include "BKE_texture.h"
99 #include "BKE_workspace.h"
100 #include "BKE_world.h"
102 #include "lib_intern.h"
104 #include "DEG_depsgraph.h"
107 # include "BPY_extern.h"
110 /* Not used currently. */
111 // static CLG_LogRef LOG = {.identifier = "bke.lib_id_delete"};
113 void BKE_libblock_free_data(ID *id, const bool do_id_user)
115 if (id->properties) {
116 IDP_FreePropertyContent_ex(id->properties, do_id_user);
117 MEM_freeN(id->properties);
120 if (id->override_library) {
121 BKE_override_library_free(&id->override_library, do_id_user);
124 /* XXX TODO remove animdata handling from each type's freeing func,
125 * and do it here, like for copy! */
128 void BKE_libblock_free_datablock(ID *id, const int UNUSED(flag))
130 const short type = GS(id->name);
133 BKE_scene_free_ex((Scene *)id, false);
136 BKE_library_free((Library *)id);
139 BKE_object_free((Object *)id);
142 BKE_mesh_free((Mesh *)id);
145 BKE_curve_free((Curve *)id);
148 BKE_mball_free((MetaBall *)id);
151 BKE_material_free((Material *)id);
154 BKE_texture_free((Tex *)id);
157 BKE_image_free((Image *)id);
160 BKE_lattice_free((Lattice *)id);
163 BKE_light_free((Light *)id);
166 BKE_camera_free((Camera *)id);
168 case ID_IP: /* Deprecated. */
169 BKE_ipo_free((Ipo *)id);
172 BKE_key_free((Key *)id);
175 BKE_world_free((World *)id);
178 BKE_screen_free((bScreen *)id);
181 BKE_vfont_free((VFont *)id);
184 BKE_text_free((Text *)id);
187 BKE_speaker_free((Speaker *)id);
190 BKE_lightprobe_free((LightProbe *)id);
193 BKE_sound_free((bSound *)id);
196 BKE_collection_free((Collection *)id);
199 BKE_armature_free((bArmature *)id);
202 BKE_action_free((bAction *)id);
205 ntreeFreeTree((bNodeTree *)id);
208 BKE_brush_free((Brush *)id);
211 BKE_particlesettings_free((ParticleSettings *)id);
214 if (free_windowmanager_cb) {
215 free_windowmanager_cb(NULL, (wmWindowManager *)id);
219 BKE_gpencil_free((bGPdata *)id, true);
222 BKE_movieclip_free((MovieClip *)id);
225 BKE_mask_free((Mask *)id);
228 BKE_linestyle_free((FreestyleLineStyle *)id);
231 BKE_palette_free((Palette *)id);
234 BKE_paint_curve_free((PaintCurve *)id);
237 BKE_cachefile_free((CacheFile *)id);
240 BKE_workspace_free((WorkSpace *)id);
246 * Complete ID freeing, extended version for corner cases.
247 * Can override default (and safe!) freeing process, to gain some speed up.
249 * At that point, given id is assumed to not be used by any other data-block already
250 * (might not be actually true, in case e.g. several inter-related IDs get freed together...).
251 * However, they might still be using (referencing) other IDs, this code takes care of it if
252 * #LIB_TAG_NO_USER_REFCOUNT is not defined.
254 * \param bmain: #Main database containing the freed #ID,
255 * can be NULL in case it's a temp ID outside of any #Main.
256 * \param idv: Pointer to ID to be freed.
257 * \param flag: Set of \a LIB_ID_FREE_... flags controlling/overriding usual freeing process,
258 * 0 to get default safe behavior.
259 * \param use_flag_from_idtag: Still use freeing info flags from given #ID datablock,
260 * even if some overriding ones are passed in \a flag parameter.
262 void BKE_id_free_ex(Main *bmain, void *idv, int flag, const bool use_flag_from_idtag)
266 if (use_flag_from_idtag) {
267 if ((id->tag & LIB_TAG_NO_MAIN) != 0) {
268 flag |= LIB_ID_FREE_NO_MAIN | LIB_ID_FREE_NO_UI_USER | LIB_ID_FREE_NO_DEG_TAG;
271 flag &= ~LIB_ID_FREE_NO_MAIN;
274 if ((id->tag & LIB_TAG_NO_USER_REFCOUNT) != 0) {
275 flag |= LIB_ID_FREE_NO_USER_REFCOUNT;
278 flag &= ~LIB_ID_FREE_NO_USER_REFCOUNT;
281 if ((id->tag & LIB_TAG_NOT_ALLOCATED) != 0) {
282 flag |= LIB_ID_FREE_NOT_ALLOCATED;
285 flag &= ~LIB_ID_FREE_NOT_ALLOCATED;
289 BLI_assert((flag & LIB_ID_FREE_NO_MAIN) != 0 || bmain != NULL);
290 BLI_assert((flag & LIB_ID_FREE_NO_MAIN) != 0 || (flag & LIB_ID_FREE_NOT_ALLOCATED) == 0);
291 BLI_assert((flag & LIB_ID_FREE_NO_MAIN) != 0 || (flag & LIB_ID_FREE_NO_USER_REFCOUNT) == 0);
293 const short type = GS(id->name);
295 if (bmain && (flag & LIB_ID_FREE_NO_DEG_TAG) == 0) {
296 DEG_id_type_tag(bmain, type);
300 # ifdef WITH_PYTHON_SAFETY
303 if (id->py_instance) {
304 BPY_DECREF_RNA_INVALIDATE(id->py_instance);
308 if ((flag & LIB_ID_FREE_NO_USER_REFCOUNT) == 0) {
309 BKE_libblock_relink_ex(bmain, id, NULL, NULL, 0);
312 BKE_libblock_free_datablock(id, flag);
314 /* avoid notifying on removed data */
315 if ((flag & LIB_ID_FREE_NO_MAIN) == 0) {
316 BKE_main_lock(bmain);
319 if ((flag & LIB_ID_FREE_NO_UI_USER) == 0) {
320 if (free_notifier_reference_cb) {
321 free_notifier_reference_cb(id);
324 if (remap_editor_id_reference_cb) {
325 remap_editor_id_reference_cb(id, NULL);
329 if ((flag & LIB_ID_FREE_NO_MAIN) == 0) {
330 ListBase *lb = which_libbase(bmain, type);
334 BKE_libblock_free_data(id, (flag & LIB_ID_FREE_NO_USER_REFCOUNT) == 0);
336 if ((flag & LIB_ID_FREE_NO_MAIN) == 0) {
337 BKE_main_unlock(bmain);
340 if ((flag & LIB_ID_FREE_NOT_ALLOCATED) == 0) {
346 * Complete ID freeing, should be usable in most cases (even for out-of-Main IDs).
348 * See #BKE_id_free_ex description for full details.
350 * \param bmain: Main database containing the freed ID,
351 * can be NULL in case it's a temp ID outside of any Main.
352 * \param idv: Pointer to ID to be freed.
354 void BKE_id_free(Main *bmain, void *idv)
356 BKE_id_free_ex(bmain, idv, 0, true);
360 * Not really a freeing function by itself,
361 * it decrements usercount of given id, and only frees it if it reaches 0.
363 void BKE_id_free_us(Main *bmain, void *idv) /* test users */
369 /* XXX This is a temp (2.77) hack so that we keep same behavior as in 2.76 regarding collections
370 * when deleting an object. Since only 'user_one' usage of objects is collections,
371 * and only 'real user' usage of objects is scenes, removing that 'user_one' tag when there
372 * is no more real (scene) users of an object ensures it gets fully unlinked.
373 * But only for local objects, not linked ones!
374 * Otherwise, there is no real way to get rid of an object anymore -
375 * better handling of this is TODO.
377 if ((GS(id->name) == ID_OB) && (id->us == 1) && (id->lib == NULL)) {
378 id_us_clear_real(id);
382 BKE_libblock_unlink(bmain, id, false, false);
384 BKE_id_free(bmain, id);
388 static void id_delete(Main *bmain, const bool do_tagged_deletion)
390 const int tag = LIB_TAG_DOIT;
391 ListBase *lbarray[MAX_LIBARRAY];
392 Link dummy_link = {0};
395 /* Used by batch tagged deletion, when we call BKE_id_free then, id is no more in Main database,
396 * and has already properly unlinked its other IDs usages.
397 * UI users are always cleared in BKE_libblock_remap_locked() call, so we can always skip it. */
398 const int free_flag = LIB_ID_FREE_NO_UI_USER |
399 (do_tagged_deletion ? LIB_ID_FREE_NO_MAIN | LIB_ID_FREE_NO_USER_REFCOUNT :
401 ListBase tagged_deleted_ids = {NULL};
403 base_count = set_listbasepointers(bmain, lbarray);
405 BKE_main_lock(bmain);
406 if (do_tagged_deletion) {
407 /* Main idea of batch deletion is to remove all IDs to be deleted from Main database.
408 * This means that we won't have to loop over all deleted IDs to remove usages
409 * of other deleted IDs.
410 * This gives tremendous speed-up when deleting a large amount of IDs from a Main
411 * containing thousands of those.
412 * This also means that we have to be very careful here, as we by-pass many 'common'
413 * processing, hence risking to 'corrupt' at least user counts, if not IDs themselves. */
414 bool keep_looping = true;
415 while (keep_looping) {
417 ID *last_remapped_id = tagged_deleted_ids.last;
418 keep_looping = false;
420 /* First tag and remove from Main all datablocks directly from target lib.
421 * Note that we go forward here, since we want to check dependencies before users
422 * (e.g. meshes before objects). Avoids to have to loop twice. */
423 for (i = 0; i < base_count; i++) {
424 ListBase *lb = lbarray[i];
426 for (id = lb->first; id; id = id_next) {
428 /* Note: in case we delete a library, we also delete all its datablocks! */
429 if ((id->tag & tag) || (id->lib != NULL && (id->lib->id.tag & tag))) {
431 BLI_addtail(&tagged_deleted_ids, id);
432 /* Do not tag as no_main now, we want to unlink it first (lower-level ID management
433 * code has some specific handling of 'nom main'
434 * IDs that would be a problem in that case). */
440 if (last_remapped_id == NULL) {
441 dummy_link.next = tagged_deleted_ids.first;
442 last_remapped_id = (ID *)(&dummy_link);
444 for (id = last_remapped_id->next; id; id = id->next) {
445 /* Will tag 'never NULL' users of this ID too.
446 * Note that we cannot use BKE_libblock_unlink() here,
447 * since it would ignore indirect (and proxy!)
448 * links, this can lead to nasty crashing here in second, actual deleting loop.
449 * Also, this will also flag users of deleted data that cannot be unlinked
450 * (object using deleted obdata, etc.), so that they also get deleted. */
451 BKE_libblock_remap_locked(
452 bmain, id, NULL, ID_REMAP_FLAG_NEVER_NULL_USAGE | ID_REMAP_FORCE_NEVER_NULL_USAGE);
453 /* Since we removed ID from Main,
454 * we also need to unlink its own other IDs usages ourself. */
455 BKE_libblock_relink_ex(bmain, id, NULL, NULL, 0);
456 /* Now we can safely mark that ID as not being in Main database anymore. */
457 id->tag |= LIB_TAG_NO_MAIN;
458 /* This is needed because we may not have remapped usages
459 * of that ID by other deleted ones. */
460 // id->us = 0; /* Is it actually? */
465 /* First tag all datablocks directly from target lib.
466 * Note that we go forward here, since we want to check dependencies before users
467 * (e.g. meshes before objects).
468 * Avoids to have to loop twice. */
469 for (i = 0; i < base_count; i++) {
470 ListBase *lb = lbarray[i];
473 for (id = lb->first; id; id = id_next) {
475 /* Note: in case we delete a library, we also delete all its datablocks! */
476 if ((id->tag & tag) || (id->lib != NULL && (id->lib->id.tag & tag))) {
479 /* Will tag 'never NULL' users of this ID too.
480 * Note that we cannot use BKE_libblock_unlink() here, since it would ignore indirect
481 * (and proxy!) links, this can lead to nasty crashing here in second,
482 * actual deleting loop.
483 * Also, this will also flag users of deleted data that cannot be unlinked
484 * (object using deleted obdata, etc.), so that they also get deleted. */
485 BKE_libblock_remap_locked(
486 bmain, id, NULL, ID_REMAP_FLAG_NEVER_NULL_USAGE | ID_REMAP_FORCE_NEVER_NULL_USAGE);
491 BKE_main_unlock(bmain);
493 /* In usual reversed order, such that all usage of a given ID, even 'never NULL' ones,
494 * have been already cleared when we reach it
495 * (e.g. Objects being processed before meshes, they'll have already released their 'reference'
496 * over meshes when we come to freeing obdata). */
497 for (i = do_tagged_deletion ? 1 : base_count; i--;) {
498 ListBase *lb = lbarray[i];
501 for (id = do_tagged_deletion ? tagged_deleted_ids.first : lb->first; id; id = id_next) {
506 printf("%s: deleting %s (%d)\n", __func__, id->name, id->us);
508 BLI_assert(id->us == 0);
510 BKE_id_free_ex(bmain, id, free_flag, !do_tagged_deletion);
515 bmain->is_memfile_undo_written = false;
519 * Properly delete a single ID from given \a bmain database.
521 void BKE_id_delete(Main *bmain, void *idv)
523 BKE_main_id_tag_all(bmain, LIB_TAG_DOIT, false);
524 ((ID *)idv)->tag |= LIB_TAG_DOIT;
526 id_delete(bmain, false);
530 * Properly delete all IDs tagged with \a LIB_TAG_DOIT, in given \a bmain database.
532 * This is more efficient than calling #BKE_id_delete repetitively on a large set of IDs
533 * (several times faster when deleting most of the IDs at once)...
535 * \warning Considered experimental for now, seems to be working OK but this is
536 * risky code in a complicated area.
538 void BKE_id_multi_tagged_delete(Main *bmain)
540 id_delete(bmain, true);