1 /*
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.
6  *
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.
11  *
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.
15  */
16 
17 /** \file
18  * \ingroup bke
19  *
20  * Contains management of ID's for freeing & deletion.
21  */
22 
23 #include "MEM_guardedalloc.h"
24 
25 /* all types are needed here, in order to do memory operations */
26 #include "DNA_ID.h"
27 #include "DNA_key_types.h"
28 
29 #include "BLI_utildefines.h"
30 
31 #include "BLI_listbase.h"
32 
33 #include "BKE_anim_data.h"
34 #include "BKE_idprop.h"
35 #include "BKE_idtype.h"
36 #include "BKE_key.h"
37 #include "BKE_lib_id.h"
38 #include "BKE_lib_override.h"
39 #include "BKE_lib_remap.h"
40 #include "BKE_library.h"
41 #include "BKE_main.h"
42 
43 #include "lib_intern.h"
44 
45 #include "DEG_depsgraph.h"
46 
47 #ifdef WITH_PYTHON
48 #  include "BPY_extern.h"
49 #endif
50 
51 /* Not used currently. */
52 // static CLG_LogRef LOG = {.identifier = "bke.lib_id_delete"};
53 
BKE_libblock_free_data(ID * id,const bool do_id_user)54 void BKE_libblock_free_data(ID *id, const bool do_id_user)
55 {
56   if (id->properties) {
57     IDP_FreePropertyContent_ex(id->properties, do_id_user);
58     MEM_freeN(id->properties);
59     id->properties = NULL;
60   }
61 
62   if (id->override_library) {
63     BKE_lib_override_library_free(&id->override_library, do_id_user);
64     id->override_library = NULL;
65   }
66 
67   BKE_animdata_free(id, do_id_user);
68 }
69 
BKE_libblock_free_datablock(ID * id,const int UNUSED (flag))70 void BKE_libblock_free_datablock(ID *id, const int UNUSED(flag))
71 {
72   const IDTypeInfo *idtype_info = BKE_idtype_get_info_from_id(id);
73 
74   if (idtype_info != NULL) {
75     if (idtype_info->free_data != NULL) {
76       idtype_info->free_data(id);
77     }
78     return;
79   }
80 
81   BLI_assert(!"IDType Missing IDTypeInfo");
82 }
83 
84 /**
85  * Complete ID freeing, extended version for corner cases.
86  * Can override default (and safe!) freeing process, to gain some speed up.
87  *
88  * At that point, given id is assumed to not be used by any other data-block already
89  * (might not be actually true, in case e.g. several inter-related IDs get freed together...).
90  * However, they might still be using (referencing) other IDs, this code takes care of it if
91  * #LIB_TAG_NO_USER_REFCOUNT is not defined.
92  *
93  * \param bmain: #Main database containing the freed #ID,
94  * can be NULL in case it's a temp ID outside of any #Main.
95  * \param idv: Pointer to ID to be freed.
96  * \param flag: Set of \a LIB_ID_FREE_... flags controlling/overriding usual freeing process,
97  * 0 to get default safe behavior.
98  * \param use_flag_from_idtag: Still use freeing info flags from given #ID datablock,
99  * even if some overriding ones are passed in \a flag parameter.
100  */
BKE_id_free_ex(Main * bmain,void * idv,int flag,const bool use_flag_from_idtag)101 void BKE_id_free_ex(Main *bmain, void *idv, int flag, const bool use_flag_from_idtag)
102 {
103   ID *id = idv;
104 
105   if (use_flag_from_idtag) {
106     if ((id->tag & LIB_TAG_NO_MAIN) != 0) {
107       flag |= LIB_ID_FREE_NO_MAIN | LIB_ID_FREE_NO_UI_USER | LIB_ID_FREE_NO_DEG_TAG;
108     }
109     else {
110       flag &= ~LIB_ID_FREE_NO_MAIN;
111     }
112 
113     if ((id->tag & LIB_TAG_NO_USER_REFCOUNT) != 0) {
114       flag |= LIB_ID_FREE_NO_USER_REFCOUNT;
115     }
116     else {
117       flag &= ~LIB_ID_FREE_NO_USER_REFCOUNT;
118     }
119 
120     if ((id->tag & LIB_TAG_NOT_ALLOCATED) != 0) {
121       flag |= LIB_ID_FREE_NOT_ALLOCATED;
122     }
123     else {
124       flag &= ~LIB_ID_FREE_NOT_ALLOCATED;
125     }
126   }
127 
128   BLI_assert((flag & LIB_ID_FREE_NO_MAIN) != 0 || bmain != NULL);
129   BLI_assert((flag & LIB_ID_FREE_NO_MAIN) != 0 || (flag & LIB_ID_FREE_NOT_ALLOCATED) == 0);
130   BLI_assert((flag & LIB_ID_FREE_NO_MAIN) != 0 || (flag & LIB_ID_FREE_NO_USER_REFCOUNT) == 0);
131 
132   const short type = GS(id->name);
133 
134   if (bmain && (flag & LIB_ID_FREE_NO_DEG_TAG) == 0) {
135     BLI_assert(bmain->is_locked_for_linking == false);
136 
137     DEG_id_type_tag(bmain, type);
138   }
139 
140 #ifdef WITH_PYTHON
141 #  ifdef WITH_PYTHON_SAFETY
142   BPY_id_release(id);
143 #  endif
144   if (id->py_instance) {
145     BPY_DECREF_RNA_INVALIDATE(id->py_instance);
146   }
147 #endif
148 
149   Key *key = ((flag & LIB_ID_FREE_NO_MAIN) == 0) ? BKE_key_from_id(id) : NULL;
150 
151   if ((flag & LIB_ID_FREE_NO_USER_REFCOUNT) == 0) {
152     BKE_libblock_relink_ex(bmain, id, NULL, NULL, 0);
153   }
154 
155   if ((flag & LIB_ID_FREE_NO_MAIN) == 0 && key != NULL) {
156     BKE_id_free_ex(bmain, &key->id, flag, use_flag_from_idtag);
157   }
158 
159   BKE_libblock_free_datablock(id, flag);
160 
161   /* avoid notifying on removed data */
162   if ((flag & LIB_ID_FREE_NO_MAIN) == 0) {
163     BKE_main_lock(bmain);
164   }
165 
166   if ((flag & LIB_ID_FREE_NO_UI_USER) == 0) {
167     if (free_notifier_reference_cb) {
168       free_notifier_reference_cb(id);
169     }
170 
171     if (remap_editor_id_reference_cb) {
172       remap_editor_id_reference_cb(id, NULL);
173     }
174   }
175 
176   if ((flag & LIB_ID_FREE_NO_MAIN) == 0) {
177     ListBase *lb = which_libbase(bmain, type);
178     BLI_remlink(lb, id);
179   }
180 
181   BKE_libblock_free_data(id, (flag & LIB_ID_FREE_NO_USER_REFCOUNT) == 0);
182 
183   if ((flag & LIB_ID_FREE_NO_MAIN) == 0) {
184     BKE_main_unlock(bmain);
185   }
186 
187   if ((flag & LIB_ID_FREE_NOT_ALLOCATED) == 0) {
188     MEM_freeN(id);
189   }
190 }
191 
192 /**
193  * Complete ID freeing, should be usable in most cases (even for out-of-Main IDs).
194  *
195  * See #BKE_id_free_ex description for full details.
196  *
197  * \param bmain: Main database containing the freed ID,
198  * can be NULL in case it's a temp ID outside of any Main.
199  * \param idv: Pointer to ID to be freed.
200  */
BKE_id_free(Main * bmain,void * idv)201 void BKE_id_free(Main *bmain, void *idv)
202 {
203   BKE_id_free_ex(bmain, idv, 0, true);
204 }
205 
206 /**
207  * Not really a freeing function by itself,
208  * it decrements usercount of given id, and only frees it if it reaches 0.
209  */
BKE_id_free_us(Main * bmain,void * idv)210 void BKE_id_free_us(Main *bmain, void *idv) /* test users */
211 {
212   ID *id = idv;
213 
214   id_us_min(id);
215 
216   /* XXX This is a temp (2.77) hack so that we keep same behavior as in 2.76 regarding collections
217    *     when deleting an object. Since only 'user_one' usage of objects is collections,
218    *     and only 'real user' usage of objects is scenes, removing that 'user_one' tag when there
219    *     is no more real (scene) users of an object ensures it gets fully unlinked.
220    *     But only for local objects, not linked ones!
221    *     Otherwise, there is no real way to get rid of an object anymore -
222    *     better handling of this is TODO.
223    */
224   if ((GS(id->name) == ID_OB) && (id->us == 1) && (id->lib == NULL)) {
225     id_us_clear_real(id);
226   }
227 
228   if (id->us == 0) {
229     BKE_libblock_unlink(bmain, id, false, false);
230 
231     BKE_id_free(bmain, id);
232   }
233 }
234 
id_delete(Main * bmain,const bool do_tagged_deletion)235 static void id_delete(Main *bmain, const bool do_tagged_deletion)
236 {
237   const int tag = LIB_TAG_DOIT;
238   ListBase *lbarray[MAX_LIBARRAY];
239   Link dummy_link = {0};
240   int base_count, i;
241 
242   /* Used by batch tagged deletion, when we call BKE_id_free then, id is no more in Main database,
243    * and has already properly unlinked its other IDs usages.
244    * UI users are always cleared in BKE_libblock_remap_locked() call, so we can always skip it. */
245   const int free_flag = LIB_ID_FREE_NO_UI_USER |
246                         (do_tagged_deletion ? LIB_ID_FREE_NO_MAIN | LIB_ID_FREE_NO_USER_REFCOUNT :
247                                               0);
248   ListBase tagged_deleted_ids = {NULL};
249 
250   base_count = set_listbasepointers(bmain, lbarray);
251 
252   BKE_main_lock(bmain);
253   if (do_tagged_deletion) {
254     /* Main idea of batch deletion is to remove all IDs to be deleted from Main database.
255      * This means that we won't have to loop over all deleted IDs to remove usages
256      * of other deleted IDs.
257      * This gives tremendous speed-up when deleting a large amount of IDs from a Main
258      * containing thousands of those.
259      * This also means that we have to be very careful here, as we by-pass many 'common'
260      * processing, hence risking to 'corrupt' at least user counts, if not IDs themselves. */
261     bool keep_looping = true;
262     while (keep_looping) {
263       ID *id, *id_next;
264       ID *last_remapped_id = tagged_deleted_ids.last;
265       keep_looping = false;
266 
267       /* First tag and remove from Main all datablocks directly from target lib.
268        * Note that we go forward here, since we want to check dependencies before users
269        * (e.g. meshes before objects). Avoids to have to loop twice. */
270       for (i = 0; i < base_count; i++) {
271         ListBase *lb = lbarray[i];
272 
273         for (id = lb->first; id; id = id_next) {
274           id_next = id->next;
275           /* Note: in case we delete a library, we also delete all its datablocks! */
276           if ((id->tag & tag) || (id->lib != NULL && (id->lib->id.tag & tag))) {
277             BLI_remlink(lb, id);
278             BLI_addtail(&tagged_deleted_ids, id);
279             /* Do not tag as no_main now, we want to unlink it first (lower-level ID management
280              * code has some specific handling of 'no main' IDs that would be a problem in that
281              * case). */
282             id->tag |= tag;
283             keep_looping = true;
284           }
285         }
286       }
287       if (last_remapped_id == NULL) {
288         dummy_link.next = tagged_deleted_ids.first;
289         last_remapped_id = (ID *)(&dummy_link);
290       }
291       for (id = last_remapped_id->next; id; id = id->next) {
292         /* Will tag 'never NULL' users of this ID too.
293          * Note that we cannot use BKE_libblock_unlink() here,
294          * since it would ignore indirect (and proxy!)
295          * links, this can lead to nasty crashing here in second, actual deleting loop.
296          * Also, this will also flag users of deleted data that cannot be unlinked
297          * (object using deleted obdata, etc.), so that they also get deleted. */
298         BKE_libblock_remap_locked(
299             bmain, id, NULL, ID_REMAP_FLAG_NEVER_NULL_USAGE | ID_REMAP_FORCE_NEVER_NULL_USAGE);
300         /* Since we removed ID from Main,
301          * we also need to unlink its own other IDs usages ourself. */
302         BKE_libblock_relink_ex(bmain, id, NULL, NULL, 0);
303         /* Now we can safely mark that ID as not being in Main database anymore. */
304         id->tag |= LIB_TAG_NO_MAIN;
305         /* This is needed because we may not have remapped usages
306          * of that ID by other deleted ones. */
307         // id->us = 0;  /* Is it actually? */
308       }
309     }
310   }
311   else {
312     /* First tag all datablocks directly from target lib.
313      * Note that we go forward here, since we want to check dependencies before users
314      * (e.g. meshes before objects).
315      * Avoids to have to loop twice. */
316     for (i = 0; i < base_count; i++) {
317       ListBase *lb = lbarray[i];
318       ID *id, *id_next;
319 
320       for (id = lb->first; id; id = id_next) {
321         id_next = id->next;
322         /* Note: in case we delete a library, we also delete all its datablocks! */
323         if ((id->tag & tag) || (id->lib != NULL && (id->lib->id.tag & tag))) {
324           id->tag |= tag;
325 
326           /* Will tag 'never NULL' users of this ID too.
327            * Note that we cannot use BKE_libblock_unlink() here, since it would ignore indirect
328            * (and proxy!) links, this can lead to nasty crashing here in second,
329            * actual deleting loop.
330            * Also, this will also flag users of deleted data that cannot be unlinked
331            * (object using deleted obdata, etc.), so that they also get deleted. */
332           BKE_libblock_remap_locked(
333               bmain, id, NULL, ID_REMAP_FLAG_NEVER_NULL_USAGE | ID_REMAP_FORCE_NEVER_NULL_USAGE);
334         }
335       }
336     }
337   }
338   BKE_main_unlock(bmain);
339 
340   /* In usual reversed order, such that all usage of a given ID, even 'never NULL' ones,
341    * have been already cleared when we reach it
342    * (e.g. Objects being processed before meshes, they'll have already released their 'reference'
343    * over meshes when we come to freeing obdata). */
344   for (i = do_tagged_deletion ? 1 : base_count; i--;) {
345     ListBase *lb = lbarray[i];
346     ID *id, *id_next;
347 
348     for (id = do_tagged_deletion ? tagged_deleted_ids.first : lb->first; id; id = id_next) {
349       id_next = id->next;
350       if (id->tag & tag) {
351         if (id->us != 0) {
352 #ifdef DEBUG_PRINT
353           printf("%s: deleting %s (%d)\n", __func__, id->name, id->us);
354 #endif
355           BLI_assert(id->us == 0);
356         }
357         BKE_id_free_ex(bmain, id, free_flag, !do_tagged_deletion);
358       }
359     }
360   }
361 
362   bmain->is_memfile_undo_written = false;
363 }
364 
365 /**
366  * Properly delete a single ID from given \a bmain database.
367  */
BKE_id_delete(Main * bmain,void * idv)368 void BKE_id_delete(Main *bmain, void *idv)
369 {
370   BKE_main_id_tag_all(bmain, LIB_TAG_DOIT, false);
371   ((ID *)idv)->tag |= LIB_TAG_DOIT;
372 
373   id_delete(bmain, false);
374 }
375 
376 /**
377  * Properly delete all IDs tagged with \a LIB_TAG_DOIT, in given \a bmain database.
378  *
379  * This is more efficient than calling #BKE_id_delete repetitively on a large set of IDs
380  * (several times faster when deleting most of the IDs at once)...
381  *
382  * \warning Considered experimental for now, seems to be working OK but this is
383  *          risky code in a complicated area.
384  */
BKE_id_multi_tagged_delete(Main * bmain)385 void BKE_id_multi_tagged_delete(Main *bmain)
386 {
387   id_delete(bmain, true);
388 }
389