From dd6dcf824793f62b70611cf588f52ab372697dbf Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Thu, 7 Feb 2019 21:52:54 +0100 Subject: Cleanup: Use new BKE_main_foreach_id() in a few more places... --- source/blender/blenkernel/intern/library_query.c | 52 +++++++++++------------- source/blender/blenkernel/intern/main.c | 19 ++++----- 2 files changed, 33 insertions(+), 38 deletions(-) diff --git a/source/blender/blenkernel/intern/library_query.c b/source/blender/blenkernel/intern/library_query.c index b73cd9251d7..7f743e7d61f 100644 --- a/source/blender/blenkernel/intern/library_query.c +++ b/source/blender/blenkernel/intern/library_query.c @@ -1328,6 +1328,27 @@ static int foreach_libblock_used_linked_data_tag_clear_cb( return IDWALK_RET_NOP; } +static bool unused_linked_data_tag_init_cb(Main *UNUSED(bmain), ID *id, void *UNUSED(user_data)) +{ + if (id->lib && (id->tag & LIB_TAG_INDIRECT) != 0) { + id->tag |= LIB_TAG_DOIT; + } + else { + id->tag &= ~LIB_TAG_DOIT; + } + return true; +} + +static bool unused_linked_data_check_cb(Main *bmain, ID *id, void *user_data) +{ + if ((id->tag & LIB_TAG_DOIT) == 0) { + BKE_library_foreach_ID_link( + bmain, id, foreach_libblock_used_linked_data_tag_clear_cb, user_data, IDWALK_READONLY); + } + /* Else it is an unused ID (so far), no need to check it further. */ + return true; +} + /** * Detect orphaned linked data blocks (i.e. linked data not used (directly or indirectly) in any way by any local data), * including complex cases like 'linked archipelagoes', i.e. linked datablocks that use each other in loops, @@ -1338,38 +1359,13 @@ static int foreach_libblock_used_linked_data_tag_clear_cb( */ void BKE_library_unused_linked_data_set_tag(Main *bmain, const bool do_init_tag) { - ListBase *lb_array[MAX_LIBARRAY]; - if (do_init_tag) { - int i = set_listbasepointers(bmain, lb_array); - - while (i--) { - for (ID *id = lb_array[i]->first; id; id = id->next) { - if (id->lib && (id->tag & LIB_TAG_INDIRECT) != 0) { - id->tag |= LIB_TAG_DOIT; - } - else { - id->tag &= ~LIB_TAG_DOIT; - } - } - } + BKE_main_foreach_id(bmain, true, unused_linked_data_tag_init_cb, NULL); } - bool do_loop = true; - while (do_loop) { - int i = set_listbasepointers(bmain, lb_array); + for (bool do_loop = true; do_loop; ) { do_loop = false; - - while (i--) { - for (ID *id = lb_array[i]->first; id; id = id->next) { - if (id->tag & LIB_TAG_DOIT) { - /* Unused ID (so far), no need to check it further. */ - continue; - } - BKE_library_foreach_ID_link( - bmain, id, foreach_libblock_used_linked_data_tag_clear_cb, &do_loop, IDWALK_READONLY); - } - } + BKE_main_foreach_id(bmain, true, unused_linked_data_check_cb, &do_loop); } } diff --git a/source/blender/blenkernel/intern/main.c b/source/blender/blenkernel/intern/main.c index 2d3988ed8f5..d5b9147b34f 100644 --- a/source/blender/blenkernel/intern/main.c +++ b/source/blender/blenkernel/intern/main.c @@ -140,7 +140,7 @@ void BKE_main_unlock(struct Main *bmain) } -static int main_relations_create_cb(void *user_data, ID *id_self, ID **id_pointer, int cb_flag) +static int main_relations_create_idlink_cb(void *user_data, ID *id_self, ID **id_pointer, int cb_flag) { MainIDRelations *rel = user_data; @@ -173,13 +173,15 @@ static int main_relations_create_cb(void *user_data, ID *id_self, ID **id_pointe return IDWALK_RET_NOP; } +static bool main_relations_create_id_cb(Main *bmain, ID *id, void *UNUSED(user_data)) +{ + BKE_library_foreach_ID_link(NULL, id, main_relations_create_idlink_cb, bmain->relations, IDWALK_READONLY); + return true; +} + /** Generate the mappings between used IDs and their users, and vice-versa. */ void BKE_main_relations_create(Main *bmain) { - ListBase *lbarray[MAX_LIBARRAY]; - ID *id; - int a; - if (bmain->relations != NULL) { BKE_main_relations_free(bmain); } @@ -189,11 +191,7 @@ void BKE_main_relations_create(Main *bmain) bmain->relations->id_user_to_used = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, __func__); bmain->relations->entry_pool = BLI_mempool_create(sizeof(MainIDRelationsEntry), 128, 128, BLI_MEMPOOL_NOP); - for (a = set_listbasepointers(bmain, lbarray); a--; ) { - for (id = lbarray[a]->first; id; id = id->next) { - BKE_library_foreach_ID_link(NULL, id, main_relations_create_cb, bmain->relations, IDWALK_READONLY); - } - } + BKE_main_foreach_id(bmain, false, main_relations_create_id_cb, NULL); } void BKE_main_relations_free(Main *bmain) @@ -237,6 +235,7 @@ bool BKE_main_listbase_foreach_id( * \param reverse_type_order Allow to reverse order in which ID *types* are handled * (i.e. does not reverse the order in which IDs themselves are handled * whithin a give listbase). + * Note that in most cases, you want to set that parameter to true. * \return false if the iteration was iterrupted by the callback. * * \warning \a callback may affect the ID, but DO NOT change the Main database (add/remove/reorder its IDs). -- cgit v1.2.3