Commit: 388e468a0429d3cc6eb8291f9ee94fa3bf498149 Author: Bastien Montagne Date: Fri Feb 14 18:58:01 2020 +0100 Branches: id-ensure-unique-memory-address https://developer.blender.org/rB388e468a0429d3cc6eb8291f9ee94fa3bf498149
Factorize unique ID mem address allocation into a util functions. =================================================================== M source/blender/blenkernel/BKE_main.h M source/blender/blenkernel/intern/lib_id.c M source/blender/blenkernel/intern/main.c =================================================================== diff --git a/source/blender/blenkernel/BKE_main.h b/source/blender/blenkernel/BKE_main.h index 03edc93ac16..aa72b85cc1c 100644 --- a/source/blender/blenkernel/BKE_main.h +++ b/source/blender/blenkernel/BKE_main.h @@ -169,6 +169,10 @@ void BKE_main_idmemset_release(struct Main *bmain); void BKE_main_idmemset_transfer_ownership(struct Main *bmain_dst, struct Main *bmain_src); void BKE_main_idmemset_usefrom(struct Main *bmain_user, struct Main *bmain_src); bool BKE_main_idmemset_register_id(struct Main *bmain, struct ID *id); +void *BKE_main_idmemset_unique_alloc(struct Main *bmain, + void *(*alloc_cb)(size_t len, const char *str), + size_t size, + const char *message); void BKE_main_relations_create(struct Main *bmain, const short flag); void BKE_main_relations_free(struct Main *bmain); diff --git a/source/blender/blenkernel/intern/lib_id.c b/source/blender/blenkernel/intern/lib_id.c index 0c8d7cf4777..8fbb54e3cda 100644 --- a/source/blender/blenkernel/intern/lib_id.c +++ b/source/blender/blenkernel/intern/lib_id.c @@ -1198,17 +1198,7 @@ void *BKE_libblock_alloc_notest(Main *bmain, short type) const char *name; size_t size = BKE_libblock_get_alloc_info(type, &name); if (size != 0) { - ID *id_mem = MEM_callocN(size, name); - if (bmain != NULL && bmain->used_id_memset != NULL) { - ListBase generated_ids = {.first = NULL}; - while (UNLIKELY(!BKE_main_idmemset_register_id(bmain, id_mem))) { - printf("Allocating ID re-used memory address %p, trying again...", id_mem); - BLI_addtail(&generated_ids, id_mem); - id_mem = MEM_callocN(size, name); - } - BLI_freelistN(&generated_ids); - } - return id_mem; + return BKE_main_idmemset_unique_alloc(bmain, MEM_callocN, size, name); } BLI_assert(!"Request to allocate unknown data type"); return NULL; diff --git a/source/blender/blenkernel/intern/main.c b/source/blender/blenkernel/intern/main.c index 30dff086f6c..732d57e622a 100644 --- a/source/blender/blenkernel/intern/main.c +++ b/source/blender/blenkernel/intern/main.c @@ -259,6 +259,25 @@ bool BKE_main_idmemset_register_id(Main *bmain, ID *id) return BLI_gset_add(bmain->used_id_memset, id); } +void *BKE_main_idmemset_unique_alloc(Main *bmain, + void *(*alloc_cb)(size_t len, const char *str), + size_t size, + const char *message) +{ + void *id_mem = alloc_cb(size, message); + if (bmain != NULL && bmain->used_id_memset != NULL) { + ListBase generated_ids = {.first = NULL}; + int count = 0; + while (UNLIKELY(!BKE_main_idmemset_register_id(bmain, id_mem))) { + printf("Allocating ID re-used memory address %p, trying again (%d)...\n", id_mem, ++count); + BLI_addtail(&generated_ids, id_mem); + id_mem = alloc_cb(size, message); + } + BLI_freelistN(&generated_ids); + } + return id_mem; +} + static int main_relations_create_idlink_cb(LibraryIDLinkCallbackData *cb_data) { MainIDRelations *rel = cb_data->user_data; _______________________________________________ Bf-blender-cvs mailing list Bf-blender-cvs@blender.org https://lists.blender.org/mailman/listinfo/bf-blender-cvs