From 63b7ff9f4e5f5b0ff5d6bdb726fcd4e1d118e141 Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Thu, 4 Mar 2021 18:39:07 +0100 Subject: Cleanup: Main `foreach ID` code: Remove `MAX_LIBARRAY` and improve comments. The `MAX_LIBARRAY` define was an annoying doublon to the `INDEX_ID_MAX` enum value now defined in `DNA_ID.h`, and it is no more useful. And comments were somewhat outdated. Also added an explanation about chosen order for the `INDEX_ID_` order. --- source/blender/blenkernel/BKE_main.h | 16 ++++++++++------ source/blender/blenkernel/intern/blendfile.c | 2 +- source/blender/blenkernel/intern/bpath.c | 2 +- source/blender/blenkernel/intern/idtype.c | 2 +- source/blender/blenkernel/intern/lib_id.c | 6 +++--- source/blender/blenkernel/intern/lib_id_delete.c | 2 +- source/blender/blenkernel/intern/lib_query.c | 8 ++++---- source/blender/blenkernel/intern/main.c | 23 +++++++++++------------ source/blender/blenkernel/intern/main_idmap.c | 10 +++++----- 9 files changed, 37 insertions(+), 34 deletions(-) (limited to 'source/blender/blenkernel') diff --git a/source/blender/blenkernel/BKE_main.h b/source/blender/blenkernel/BKE_main.h index b6116b32ca5..2c6e5ed3873 100644 --- a/source/blender/blenkernel/BKE_main.h +++ b/source/blender/blenkernel/BKE_main.h @@ -223,7 +223,7 @@ struct GSet *BKE_main_gset_create(struct Main *bmain, struct GSet *gset); #define FOREACH_MAIN_LISTBASE_BEGIN(_bmain, _lb) \ { \ - ListBase *_lbarray[MAX_LIBARRAY]; \ + ListBase *_lbarray[INDEX_ID_MAX]; \ int _i = set_listbasepointers((_bmain), _lbarray); \ while (_i--) { \ (_lb) = _lbarray[_i]; @@ -234,9 +234,13 @@ struct GSet *BKE_main_gset_create(struct Main *bmain, struct GSet *gset); ((void)0) /** - * DO NOT use break statement with that macro, - * use #FOREACH_MAIN_LISTBASE and #FOREACH_MAIN_LISTBASE_ID instead - * if you need that kind of control flow. */ + * Top level `foreach`-like macro allowing to loop over all IDs in a given #Main data-base. + * + * NOTE: Order tries to go from 'user IDs' to 'used IDs' (e.g. collections will be processed + * before objects, which will be processed before obdata types, etc.). + * + * WARNING: DO NOT use break statement with that macro, use #FOREACH_MAIN_LISTBASE and + * #FOREACH_MAIN_LISTBASE_ID instead if you need that kind of control flow. */ #define FOREACH_MAIN_ID_BEGIN(_bmain, _id) \ { \ ListBase *_lb; \ @@ -259,8 +263,8 @@ const char *BKE_main_blendfile_path_from_global(void); struct ListBase *which_libbase(struct Main *bmain, short type); -#define MAX_LIBARRAY 41 -int set_listbasepointers(struct Main *main, struct ListBase *lb[MAX_LIBARRAY]); +//#define INDEX_ID_MAX 41 +int set_listbasepointers(struct Main *main, struct ListBase *lb[]); #define MAIN_VERSION_ATLEAST(main, ver, subver) \ ((main)->versionfile > (ver) || \ diff --git a/source/blender/blenkernel/intern/blendfile.c b/source/blender/blenkernel/intern/blendfile.c index 32710c4fa60..101f4b7caf6 100644 --- a/source/blender/blenkernel/intern/blendfile.c +++ b/source/blender/blenkernel/intern/blendfile.c @@ -865,7 +865,7 @@ bool BKE_blendfile_write_partial(Main *bmain_src, ReportList *reports) { Main *bmain_dst = MEM_callocN(sizeof(Main), "copybuffer"); - ListBase *lbarray_dst[MAX_LIBARRAY], *lbarray_src[MAX_LIBARRAY]; + ListBase *lbarray_dst[INDEX_ID_MAX], *lbarray_src[INDEX_ID_MAX]; int a, retval; void *path_list_backup = NULL; diff --git a/source/blender/blenkernel/intern/bpath.c b/source/blender/blenkernel/intern/bpath.c index 2ad0ac950d0..47427beccba 100644 --- a/source/blender/blenkernel/intern/bpath.c +++ b/source/blender/blenkernel/intern/bpath.c @@ -783,7 +783,7 @@ void BKE_bpath_traverse_main(Main *bmain, const int flag, void *bpath_user_data) { - ListBase *lbarray[MAX_LIBARRAY]; + ListBase *lbarray[INDEX_ID_MAX]; int a = set_listbasepointers(bmain, lbarray); while (a--) { BKE_bpath_traverse_id_list(bmain, lbarray[a], visit_cb, flag, bpath_user_data); diff --git a/source/blender/blenkernel/intern/idtype.c b/source/blender/blenkernel/intern/idtype.c index 1889d1c4eb0..fee70922570 100644 --- a/source/blender/blenkernel/intern/idtype.c +++ b/source/blender/blenkernel/intern/idtype.c @@ -63,7 +63,7 @@ bool BKE_idtype_cache_key_cmp(const void *key_a_v, const void *key_b_v) (key_a->offset_in_ID != key_b->offset_in_ID) || (key_a->cache_v != key_b->cache_v); } -static IDTypeInfo *id_types[MAX_LIBARRAY] = {NULL}; +static IDTypeInfo *id_types[INDEX_ID_MAX] = {NULL}; static void id_type_init(void) { diff --git a/source/blender/blenkernel/intern/lib_id.c b/source/blender/blenkernel/intern/lib_id.c index 54c2f5f5565..a511c1f9c4c 100644 --- a/source/blender/blenkernel/intern/lib_id.c +++ b/source/blender/blenkernel/intern/lib_id.c @@ -916,7 +916,7 @@ void BKE_main_id_tag_idcode(struct Main *mainvar, */ void BKE_main_id_tag_all(struct Main *mainvar, const int tag, const bool value) { - ListBase *lbarray[MAX_LIBARRAY]; + ListBase *lbarray[INDEX_ID_MAX]; int a; a = set_listbasepointers(mainvar, lbarray); @@ -949,7 +949,7 @@ void BKE_main_id_flag_listbase(ListBase *lb, const int flag, const bool value) */ void BKE_main_id_flag_all(Main *bmain, const int flag, const bool value) { - ListBase *lbarray[MAX_LIBARRAY]; + ListBase *lbarray[INDEX_ID_MAX]; int a; a = set_listbasepointers(bmain, lbarray); while (a--) { @@ -1870,7 +1870,7 @@ void BKE_library_make_local(Main *bmain, const bool untagged_only, const bool set_fake) { - ListBase *lbarray[MAX_LIBARRAY]; + ListBase *lbarray[INDEX_ID_MAX]; LinkNode *todo_ids = NULL; LinkNode *copied_ids = NULL; diff --git a/source/blender/blenkernel/intern/lib_id_delete.c b/source/blender/blenkernel/intern/lib_id_delete.c index 7c5032c97f4..1d7f89e1e8d 100644 --- a/source/blender/blenkernel/intern/lib_id_delete.c +++ b/source/blender/blenkernel/intern/lib_id_delete.c @@ -240,7 +240,7 @@ void BKE_id_free_us(Main *bmain, void *idv) /* test users */ static size_t id_delete(Main *bmain, const bool do_tagged_deletion) { const int tag = LIB_TAG_DOIT; - ListBase *lbarray[MAX_LIBARRAY]; + ListBase *lbarray[INDEX_ID_MAX]; Link dummy_link = {0}; int base_count, i; diff --git a/source/blender/blenkernel/intern/lib_query.c b/source/blender/blenkernel/intern/lib_query.c index 796bc3dc3d0..acd0c10040c 100644 --- a/source/blender/blenkernel/intern/lib_query.c +++ b/source/blender/blenkernel/intern/lib_query.c @@ -440,7 +440,7 @@ bool BKE_library_id_can_use_idtype(ID *id_owner, const short id_type_used) typedef struct IDUsersIter { ID *id; - ListBase *lb_array[MAX_LIBARRAY]; + ListBase *lb_array[INDEX_ID_MAX]; int lb_idx; ID *curr_id; @@ -514,7 +514,7 @@ int BKE_library_ID_use_ID(ID *id_user, ID *id_used) static bool library_ID_is_used(Main *bmain, void *idv, const bool check_linked) { IDUsersIter iter; - ListBase *lb_array[MAX_LIBARRAY]; + ListBase *lb_array[INDEX_ID_MAX]; ID *id = idv; int i = set_listbasepointers(bmain, lb_array); bool is_defined = false; @@ -567,7 +567,7 @@ bool BKE_library_ID_is_indirectly_used(Main *bmain, void *idv) void BKE_library_ID_test_usages(Main *bmain, void *idv, bool *is_used_local, bool *is_used_linked) { IDUsersIter iter; - ListBase *lb_array[MAX_LIBARRAY]; + ListBase *lb_array[INDEX_ID_MAX]; ID *id = idv; int i = set_listbasepointers(bmain, lb_array); bool is_defined = false; @@ -805,7 +805,7 @@ void BKE_library_unused_linked_data_set_tag(Main *bmain, const bool do_init_tag) */ void BKE_library_indirectly_used_data_tag_clear(Main *bmain) { - ListBase *lb_array[MAX_LIBARRAY]; + ListBase *lb_array[INDEX_ID_MAX]; bool do_loop = true; while (do_loop) { diff --git a/source/blender/blenkernel/intern/main.c b/source/blender/blenkernel/intern/main.c index 6f94b3355fa..ead1c5bf721 100644 --- a/source/blender/blenkernel/intern/main.c +++ b/source/blender/blenkernel/intern/main.c @@ -53,7 +53,7 @@ Main *BKE_main_new(void) void BKE_main_free(Main *mainvar) { /* also call when reading a file, erase all, etc */ - ListBase *lbarray[MAX_LIBARRAY]; + ListBase *lbarray[INDEX_ID_MAX]; int a; /* Since we are removing whole main, no need to bother 'properly' @@ -532,18 +532,17 @@ ListBase *which_libbase(Main *bmain, short type) } /** - * puts into array *lb pointers to all the #ListBase structs in main, - * and returns the number of them as the function result. This is useful for - * generic traversal of all the blocks in a Main (by traversing all the - * lists in turn), without worrying about block types. + * Put the pointers to all the #ListBase structs in given `bmain` into the `*lb[INDEX_ID_MAX]` + * array, and return the number of those for convinience. * - * \note #MAX_LIBARRAY define should match this code */ -int set_listbasepointers(Main *bmain, ListBase **lb) + * This is useful for generic traversal of all the blocks in a #Main (by traversing all the lists + * in turn), without worrying about block types. + * + * \note The order of each ID type #ListBase in the array is determined by the `INDEX_ID_` + * enum definitions in `DNA_ID.h`. See also the #FOREACH_MAIN_ID_BEGIN macro in `BKE_main.h` + */ +int set_listbasepointers(Main *bmain, ListBase *lb[INDEX_ID_MAX]) { - /* BACKWARDS! also watch order of free-ing! (mesh<->mat), first items freed last. - * This is important because freeing data decreases user-counts of other data-blocks, - * if this data is its self freed it can crash. */ - /* Libraries may be accessed from pretty much any other ID. */ lb[INDEX_ID_LI] = &(bmain->libraries); @@ -606,5 +605,5 @@ int set_listbasepointers(Main *bmain, ListBase **lb) lb[INDEX_ID_NULL] = NULL; - return (MAX_LIBARRAY - 1); + return (INDEX_ID_MAX - 1); } diff --git a/source/blender/blenkernel/intern/main_idmap.c b/source/blender/blenkernel/intern/main_idmap.c index 21f5e9c6fb2..1d362db4432 100644 --- a/source/blender/blenkernel/intern/main_idmap.c +++ b/source/blender/blenkernel/intern/main_idmap.c @@ -66,7 +66,7 @@ struct IDNameLib_TypeMap { * Opaque structure, external API users only see this. */ struct IDNameLib_Map { - struct IDNameLib_TypeMap type_maps[MAX_LIBARRAY]; + struct IDNameLib_TypeMap type_maps[INDEX_ID_MAX]; struct GHash *uuid_map; struct Main *bmain; struct GSet *valid_id_pointers; @@ -77,7 +77,7 @@ static struct IDNameLib_TypeMap *main_idmap_from_idcode(struct IDNameLib_Map *id short id_type) { if (id_map->idmap_types & MAIN_IDMAP_TYPE_NAME) { - for (int i = 0; i < MAX_LIBARRAY; i++) { + for (int i = 0; i < INDEX_ID_MAX; i++) { if (id_map->type_maps[i].id_type == id_type) { return &id_map->type_maps[i]; } @@ -108,13 +108,13 @@ struct IDNameLib_Map *BKE_main_idmap_create(struct Main *bmain, id_map->idmap_types = idmap_types; int index = 0; - while (index < MAX_LIBARRAY) { + while (index < INDEX_ID_MAX) { struct IDNameLib_TypeMap *type_map = &id_map->type_maps[index]; type_map->map = NULL; type_map->id_type = BKE_idtype_idcode_iter_step(&index); BLI_assert(type_map->id_type != 0); } - BLI_assert(index == MAX_LIBARRAY); + BLI_assert(index == INDEX_ID_MAX); if (idmap_types & MAIN_IDMAP_TYPE_UUID) { ID *id; @@ -231,7 +231,7 @@ void BKE_main_idmap_destroy(struct IDNameLib_Map *id_map) { if (id_map->idmap_types & MAIN_IDMAP_TYPE_NAME) { struct IDNameLib_TypeMap *type_map = id_map->type_maps; - for (int i = 0; i < MAX_LIBARRAY; i++, type_map++) { + for (int i = 0; i < INDEX_ID_MAX; i++, type_map++) { if (type_map->map) { BLI_ghash_free(type_map->map, NULL, NULL); type_map->map = NULL; -- cgit v1.2.3