diff options
author | Campbell Barton <ideasman42@gmail.com> | 2019-04-17 07:17:24 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2019-04-17 07:21:24 +0300 |
commit | e12c08e8d170b7ca40f204a5b0423c23a9fbc2c1 (patch) | |
tree | 8cf3453d12edb177a218ef8009357518ec6cab6a /source/blender/blenkernel/intern/library_idmap.c | |
parent | b3dabc200a4b0399ec6b81f2ff2730d07b44fcaa (diff) |
ClangFormat: apply to source, most of intern
Apply clang format as proposed in T53211.
For details on usage and instructions for migrating branches
without conflicts, see:
https://wiki.blender.org/wiki/Tools/ClangFormat
Diffstat (limited to 'source/blender/blenkernel/intern/library_idmap.c')
-rw-r--r-- | source/blender/blenkernel/intern/library_idmap.c | 219 |
1 files changed, 112 insertions, 107 deletions
diff --git a/source/blender/blenkernel/intern/library_idmap.c b/source/blender/blenkernel/intern/library_idmap.c index f03fa63843b..d520df31a75 100644 --- a/source/blender/blenkernel/intern/library_idmap.c +++ b/source/blender/blenkernel/intern/library_idmap.c @@ -26,7 +26,7 @@ #include "DNA_ID.h" #include "BKE_idcode.h" -#include "BKE_library_idmap.h" /* own include */ +#include "BKE_library_idmap.h" /* own include */ #include "BKE_main.h" /** \file @@ -47,36 +47,37 @@ * \{ */ struct IDNameLib_Key { - /** ``ID.name + 2``: without the ID type prefix, since each id type gets it's own 'map' */ - const char *name; - /** ``ID.lib``: */ - const Library *lib; + /** ``ID.name + 2``: without the ID type prefix, since each id type gets it's own 'map' */ + const char *name; + /** ``ID.lib``: */ + const Library *lib; }; struct IDNameLib_TypeMap { - GHash *map; - short id_type; - /* only for storage of keys in the ghash, avoid many single allocs */ - struct IDNameLib_Key *keys; + GHash *map; + short id_type; + /* only for storage of keys in the ghash, avoid many single allocs */ + struct IDNameLib_Key *keys; }; /** * Opaque structure, external API users only see this. */ struct IDNameLib_Map { - struct IDNameLib_TypeMap type_maps[MAX_LIBARRAY]; - struct Main *bmain; - struct GSet *valid_id_pointers; + struct IDNameLib_TypeMap type_maps[MAX_LIBARRAY]; + struct Main *bmain; + struct GSet *valid_id_pointers; }; -static struct IDNameLib_TypeMap *main_idmap_from_idcode(struct IDNameLib_Map *id_map, short id_type) +static struct IDNameLib_TypeMap *main_idmap_from_idcode(struct IDNameLib_Map *id_map, + short id_type) { - for (int i = 0; i < MAX_LIBARRAY; i++) { - if (id_map->type_maps[i].id_type == id_type) { - return &id_map->type_maps[i]; - } - } - return NULL; + for (int i = 0; i < MAX_LIBARRAY; i++) { + if (id_map->type_maps[i].id_type == id_type) { + return &id_map->type_maps[i]; + } + } + return NULL; } /** @@ -91,118 +92,122 @@ static struct IDNameLib_TypeMap *main_idmap_from_idcode(struct IDNameLib_Map *id * \param create_valid_ids_set: If \a true, generate a reference to prevent freed memory accesses. * \param old_bmain: If not NULL, its IDs will be added the valid references set. */ -struct IDNameLib_Map *BKE_main_idmap_create( - struct Main *bmain, const bool create_valid_ids_set, struct Main *old_bmain) +struct IDNameLib_Map *BKE_main_idmap_create(struct Main *bmain, + const bool create_valid_ids_set, + struct Main *old_bmain) { - struct IDNameLib_Map *id_map = MEM_mallocN(sizeof(*id_map), __func__); - - int index = 0; - while (index < MAX_LIBARRAY) { - struct IDNameLib_TypeMap *type_map = &id_map->type_maps[index]; - type_map->map = NULL; - type_map->id_type = BKE_idcode_iter_step(&index); - BLI_assert(type_map->id_type != 0); - } - BLI_assert(index == MAX_LIBARRAY); - - id_map->bmain = bmain; - - if (create_valid_ids_set) { - id_map->valid_id_pointers = BKE_main_gset_create(bmain, NULL); - if (old_bmain != NULL) { - id_map->valid_id_pointers = BKE_main_gset_create(old_bmain, id_map->valid_id_pointers); - } - } - else { - id_map->valid_id_pointers = NULL; - } - - return id_map; + struct IDNameLib_Map *id_map = MEM_mallocN(sizeof(*id_map), __func__); + + int index = 0; + while (index < MAX_LIBARRAY) { + struct IDNameLib_TypeMap *type_map = &id_map->type_maps[index]; + type_map->map = NULL; + type_map->id_type = BKE_idcode_iter_step(&index); + BLI_assert(type_map->id_type != 0); + } + BLI_assert(index == MAX_LIBARRAY); + + id_map->bmain = bmain; + + if (create_valid_ids_set) { + id_map->valid_id_pointers = BKE_main_gset_create(bmain, NULL); + if (old_bmain != NULL) { + id_map->valid_id_pointers = BKE_main_gset_create(old_bmain, id_map->valid_id_pointers); + } + } + else { + id_map->valid_id_pointers = NULL; + } + + return id_map; } struct Main *BKE_main_idmap_main_get(struct IDNameLib_Map *id_map) { - return id_map->bmain; + return id_map->bmain; } static unsigned int idkey_hash(const void *ptr) { - const struct IDNameLib_Key *idkey = ptr; - unsigned int key = BLI_ghashutil_strhash(idkey->name); - if (idkey->lib) { - key ^= BLI_ghashutil_ptrhash(idkey->lib); - } - return key; + const struct IDNameLib_Key *idkey = ptr; + unsigned int key = BLI_ghashutil_strhash(idkey->name); + if (idkey->lib) { + key ^= BLI_ghashutil_ptrhash(idkey->lib); + } + return key; } static bool idkey_cmp(const void *a, const void *b) { - const struct IDNameLib_Key *idkey_a = a; - const struct IDNameLib_Key *idkey_b = b; - return strcmp(idkey_a->name, idkey_b->name) || (idkey_a->lib != idkey_b->lib); + const struct IDNameLib_Key *idkey_a = a; + const struct IDNameLib_Key *idkey_b = b; + return strcmp(idkey_a->name, idkey_b->name) || (idkey_a->lib != idkey_b->lib); } -ID *BKE_main_idmap_lookup(struct IDNameLib_Map *id_map, short id_type, const char *name, const Library *lib) +ID *BKE_main_idmap_lookup(struct IDNameLib_Map *id_map, + short id_type, + const char *name, + const Library *lib) { - struct IDNameLib_TypeMap *type_map = main_idmap_from_idcode(id_map, id_type); - - if (UNLIKELY(type_map == NULL)) { - return NULL; - } - - /* lazy init */ - if (type_map->map == NULL) { - ListBase *lb = which_libbase(id_map->bmain, id_type); - const int lb_len = BLI_listbase_count(lb); - if (lb_len == 0) { - return NULL; - } - type_map->map = BLI_ghash_new_ex(idkey_hash, idkey_cmp, __func__, lb_len); - type_map->keys = MEM_mallocN(sizeof(struct IDNameLib_Key) * lb_len, __func__); - - GHash *map = type_map->map; - struct IDNameLib_Key *key = type_map->keys; - - for (ID *id = lb->first; id; id = id->next, key++) { - key->name = id->name + 2; - key->lib = id->lib; - BLI_ghash_insert(map, key, id); - } - } - - const struct IDNameLib_Key key_lookup = {name, lib}; - return BLI_ghash_lookup(type_map->map, &key_lookup); + struct IDNameLib_TypeMap *type_map = main_idmap_from_idcode(id_map, id_type); + + if (UNLIKELY(type_map == NULL)) { + return NULL; + } + + /* lazy init */ + if (type_map->map == NULL) { + ListBase *lb = which_libbase(id_map->bmain, id_type); + const int lb_len = BLI_listbase_count(lb); + if (lb_len == 0) { + return NULL; + } + type_map->map = BLI_ghash_new_ex(idkey_hash, idkey_cmp, __func__, lb_len); + type_map->keys = MEM_mallocN(sizeof(struct IDNameLib_Key) * lb_len, __func__); + + GHash *map = type_map->map; + struct IDNameLib_Key *key = type_map->keys; + + for (ID *id = lb->first; id; id = id->next, key++) { + key->name = id->name + 2; + key->lib = id->lib; + BLI_ghash_insert(map, key, id); + } + } + + const struct IDNameLib_Key key_lookup = {name, lib}; + return BLI_ghash_lookup(type_map->map, &key_lookup); } ID *BKE_main_idmap_lookup_id(struct IDNameLib_Map *id_map, const ID *id) { - /* When used during undo/redo, this function cannot assume that given id points to valid memory - * (i.e. has not been freed), so it has to check that it does exist in 'old' (aka current) Main database. - * Otherwise, we cannot provide new ID pointer that way (would crash accessing freed memory - * when trying to get ID name). - */ - if (id_map->valid_id_pointers == NULL || BLI_gset_haskey(id_map->valid_id_pointers, id)) { - return BKE_main_idmap_lookup(id_map, GS(id->name), id->name + 2, id->lib); - } - return NULL; + /* When used during undo/redo, this function cannot assume that given id points to valid memory + * (i.e. has not been freed), so it has to check that it does exist in 'old' (aka current) Main database. + * Otherwise, we cannot provide new ID pointer that way (would crash accessing freed memory + * when trying to get ID name). + */ + if (id_map->valid_id_pointers == NULL || BLI_gset_haskey(id_map->valid_id_pointers, id)) { + return BKE_main_idmap_lookup(id_map, GS(id->name), id->name + 2, id->lib); + } + return NULL; } void BKE_main_idmap_destroy(struct IDNameLib_Map *id_map) { - struct IDNameLib_TypeMap *type_map = id_map->type_maps; - for (int i = 0; i < MAX_LIBARRAY; i++, type_map++) { - if (type_map->map) { - BLI_ghash_free(type_map->map, NULL, NULL); - type_map->map = NULL; - MEM_freeN(type_map->keys); - } - } - - if (id_map->valid_id_pointers != NULL) { - BLI_gset_free(id_map->valid_id_pointers, NULL); - } - - MEM_freeN(id_map); + struct IDNameLib_TypeMap *type_map = id_map->type_maps; + for (int i = 0; i < MAX_LIBARRAY; i++, type_map++) { + if (type_map->map) { + BLI_ghash_free(type_map->map, NULL, NULL); + type_map->map = NULL; + MEM_freeN(type_map->keys); + } + } + + if (id_map->valid_id_pointers != NULL) { + BLI_gset_free(id_map->valid_id_pointers, NULL); + } + + MEM_freeN(id_map); } /** \} */ |