2023-05-31 16:19:06 +02:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Foundation
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2016-06-07 16:07:13 +10:00
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2020-03-19 09:33:03 +01:00
|
|
|
#include <string.h>
|
2016-06-07 16:07:13 +10:00
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
|
|
#include "BLI_ghash.h"
|
|
|
|
|
#include "BLI_listbase.h"
|
2021-06-30 15:19:35 +02:00
|
|
|
#include "BLI_mempool.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_utildefines.h"
|
2016-06-07 16:07:13 +10:00
|
|
|
|
|
|
|
|
#include "DNA_ID.h"
|
|
|
|
|
|
2020-03-19 19:37:00 +01:00
|
|
|
#include "BKE_idtype.h"
|
2020-03-05 16:17:14 +01:00
|
|
|
#include "BKE_lib_id.h"
|
2018-11-07 16:06:36 +01:00
|
|
|
#include "BKE_main.h"
|
2020-02-07 16:29:02 +01:00
|
|
|
#include "BKE_main_idmap.h" /* own include */
|
2016-06-07 16:07:13 +10:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup bke
|
2016-06-07 16:07:13 +10:00
|
|
|
*
|
|
|
|
|
* Utility functions for faster ID lookups.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-10-27 21:32:09 +11:00
|
|
|
/* -------------------------------------------------------------------- */
|
2016-06-07 16:07:13 +10:00
|
|
|
/** \name BKE_main_idmap API
|
|
|
|
|
*
|
|
|
|
|
* Cache ID (name, library lookups).
|
|
|
|
|
* This doesn't account for adding/removing data-blocks,
|
|
|
|
|
* and should only be used when performing many lookups.
|
|
|
|
|
*
|
|
|
|
|
* \note GHash's are initialized on demand,
|
|
|
|
|
* since its likely some types will never have lookups run on them,
|
|
|
|
|
* so its a waste to create and never use.
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
struct IDNameLib_Key {
|
2021-07-20 22:52:31 +10:00
|
|
|
/** `ID.name + 2`: without the ID type prefix, since each id type gets its own 'map'. */
|
2016-06-07 16:07:13 +10:00
|
|
|
const char *name;
|
2021-07-20 22:52:31 +10:00
|
|
|
/** `ID.lib`: */
|
2016-06-07 16:07:13 +10:00
|
|
|
const Library *lib;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct IDNameLib_TypeMap {
|
|
|
|
|
GHash *map;
|
|
|
|
|
short id_type;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Opaque structure, external API users only see this.
|
|
|
|
|
*/
|
|
|
|
|
struct IDNameLib_Map {
|
2021-03-04 18:39:07 +01:00
|
|
|
struct IDNameLib_TypeMap type_maps[INDEX_ID_MAX];
|
2023-06-03 08:36:28 +10:00
|
|
|
GHash *uuid_map;
|
|
|
|
|
Main *bmain;
|
|
|
|
|
GSet *valid_id_pointers;
|
2020-03-05 16:17:14 +01:00
|
|
|
int idmap_types;
|
2021-06-30 15:19:35 +02:00
|
|
|
|
2023-02-03 12:41:01 +11:00
|
|
|
/* For storage of keys for the #TypeMap #GHash, avoids many single allocations. */
|
2021-06-30 15:19:35 +02:00
|
|
|
BLI_mempool *type_maps_keys_pool;
|
2016-06-07 16:07:13 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static struct IDNameLib_TypeMap *main_idmap_from_idcode(struct IDNameLib_Map *id_map,
|
|
|
|
|
short id_type)
|
|
|
|
|
{
|
2020-03-05 16:17:14 +01:00
|
|
|
if (id_map->idmap_types & MAIN_IDMAP_TYPE_NAME) {
|
2021-03-04 18:39:07 +01:00
|
|
|
for (int i = 0; i < INDEX_ID_MAX; i++) {
|
2020-03-05 16:17:14 +01:00
|
|
|
if (id_map->type_maps[i].id_type == id_type) {
|
|
|
|
|
return &id_map->type_maps[i];
|
|
|
|
|
}
|
2016-06-07 16:07:13 +10:00
|
|
|
}
|
|
|
|
|
}
|
2023-07-17 10:46:26 +02:00
|
|
|
return nullptr;
|
2016-06-07 16:07:13 +10:00
|
|
|
}
|
|
|
|
|
|
2023-06-03 08:36:28 +10:00
|
|
|
struct IDNameLib_Map *BKE_main_idmap_create(Main *bmain,
|
Fix (unreported) crash when undoing after ID deletion.
Yes, we do can undo an ID deletion now.
However, this requires extra care in UI 'remapping' to new IDs step
(when undoing, we do not fully reload the UI from saved .blend).
Otherwise, new UI (i.e. one from saved .blend file) might reference
IDs that where freed in old bmain (the one before the undo), we cannot
use those to get ID name then, that would be a nasty use-after-free!
To prevent this, we generate a GSet of all valid ID pointers at that
time (i.e. those found in both old and new Main's), and ensure any ID
we try to remap by its name is in that GSet. Otherwise, there is no
possible remapping, just return NULL.
2019-02-08 18:45:57 +01:00
|
|
|
const bool create_valid_ids_set,
|
2023-06-03 08:36:28 +10:00
|
|
|
Main *old_bmain,
|
2020-03-05 16:17:14 +01:00
|
|
|
const int idmap_types)
|
2016-06-07 16:07:13 +10:00
|
|
|
{
|
2023-07-17 10:46:26 +02:00
|
|
|
struct IDNameLib_Map *id_map = static_cast<IDNameLib_Map *>(
|
|
|
|
|
MEM_mallocN(sizeof(*id_map), __func__));
|
2020-03-05 16:17:14 +01:00
|
|
|
id_map->bmain = bmain;
|
|
|
|
|
id_map->idmap_types = idmap_types;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-06-07 16:07:13 +10:00
|
|
|
int index = 0;
|
2021-03-04 18:39:07 +01:00
|
|
|
while (index < INDEX_ID_MAX) {
|
2016-06-13 21:55:54 +10:00
|
|
|
struct IDNameLib_TypeMap *type_map = &id_map->type_maps[index];
|
2023-07-17 10:46:26 +02:00
|
|
|
type_map->map = nullptr;
|
2020-03-19 19:37:00 +01:00
|
|
|
type_map->id_type = BKE_idtype_idcode_iter_step(&index);
|
2016-06-13 21:55:54 +10:00
|
|
|
BLI_assert(type_map->id_type != 0);
|
2016-06-07 16:07:13 +10:00
|
|
|
}
|
2021-03-04 18:39:07 +01:00
|
|
|
BLI_assert(index == INDEX_ID_MAX);
|
2023-07-17 10:46:26 +02:00
|
|
|
id_map->type_maps_keys_pool = nullptr;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-03-05 16:17:14 +01:00
|
|
|
if (idmap_types & MAIN_IDMAP_TYPE_UUID) {
|
|
|
|
|
ID *id;
|
|
|
|
|
id_map->uuid_map = BLI_ghash_int_new(__func__);
|
|
|
|
|
FOREACH_MAIN_ID_BEGIN (bmain, id) {
|
|
|
|
|
BLI_assert(id->session_uuid != MAIN_ID_SESSION_UUID_UNSET);
|
|
|
|
|
void **id_ptr_v;
|
|
|
|
|
const bool existing_key = BLI_ghash_ensure_p(
|
|
|
|
|
id_map->uuid_map, POINTER_FROM_UINT(id->session_uuid), &id_ptr_v);
|
|
|
|
|
BLI_assert(existing_key == false);
|
2020-03-11 18:50:18 +01:00
|
|
|
UNUSED_VARS_NDEBUG(existing_key);
|
|
|
|
|
|
2020-03-05 16:17:14 +01:00
|
|
|
*id_ptr_v = id;
|
|
|
|
|
}
|
|
|
|
|
FOREACH_MAIN_ID_END;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2023-07-17 10:46:26 +02:00
|
|
|
id_map->uuid_map = nullptr;
|
2020-03-05 16:17:14 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
Fix (unreported) crash when undoing after ID deletion.
Yes, we do can undo an ID deletion now.
However, this requires extra care in UI 'remapping' to new IDs step
(when undoing, we do not fully reload the UI from saved .blend).
Otherwise, new UI (i.e. one from saved .blend file) might reference
IDs that where freed in old bmain (the one before the undo), we cannot
use those to get ID name then, that would be a nasty use-after-free!
To prevent this, we generate a GSet of all valid ID pointers at that
time (i.e. those found in both old and new Main's), and ensure any ID
we try to remap by its name is in that GSet. Otherwise, there is no
possible remapping, just return NULL.
2019-02-08 18:45:57 +01:00
|
|
|
if (create_valid_ids_set) {
|
2023-07-17 10:46:26 +02:00
|
|
|
id_map->valid_id_pointers = BKE_main_gset_create(bmain, nullptr);
|
|
|
|
|
if (old_bmain != nullptr) {
|
Fix (unreported) crash when undoing after ID deletion.
Yes, we do can undo an ID deletion now.
However, this requires extra care in UI 'remapping' to new IDs step
(when undoing, we do not fully reload the UI from saved .blend).
Otherwise, new UI (i.e. one from saved .blend file) might reference
IDs that where freed in old bmain (the one before the undo), we cannot
use those to get ID name then, that would be a nasty use-after-free!
To prevent this, we generate a GSet of all valid ID pointers at that
time (i.e. those found in both old and new Main's), and ensure any ID
we try to remap by its name is in that GSet. Otherwise, there is no
possible remapping, just return NULL.
2019-02-08 18:45:57 +01:00
|
|
|
id_map->valid_id_pointers = BKE_main_gset_create(old_bmain, id_map->valid_id_pointers);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2023-07-17 10:46:26 +02:00
|
|
|
id_map->valid_id_pointers = nullptr;
|
Fix (unreported) crash when undoing after ID deletion.
Yes, we do can undo an ID deletion now.
However, this requires extra care in UI 'remapping' to new IDs step
(when undoing, we do not fully reload the UI from saved .blend).
Otherwise, new UI (i.e. one from saved .blend file) might reference
IDs that where freed in old bmain (the one before the undo), we cannot
use those to get ID name then, that would be a nasty use-after-free!
To prevent this, we generate a GSet of all valid ID pointers at that
time (i.e. those found in both old and new Main's), and ensure any ID
we try to remap by its name is in that GSet. Otherwise, there is no
possible remapping, just return NULL.
2019-02-08 18:45:57 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-06-07 16:07:13 +10:00
|
|
|
return id_map;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-30 15:19:35 +02:00
|
|
|
void BKE_main_idmap_insert_id(struct IDNameLib_Map *id_map, ID *id)
|
|
|
|
|
{
|
|
|
|
|
if (id_map->idmap_types & MAIN_IDMAP_TYPE_NAME) {
|
|
|
|
|
const short id_type = GS(id->name);
|
|
|
|
|
struct IDNameLib_TypeMap *type_map = main_idmap_from_idcode(id_map, id_type);
|
|
|
|
|
|
2021-07-29 10:52:08 +10:00
|
|
|
/* No need to do anything if map has not been lazily created yet. */
|
2023-07-17 10:46:26 +02:00
|
|
|
if (LIKELY(type_map != nullptr) && type_map->map != nullptr) {
|
|
|
|
|
BLI_assert(id_map->type_maps_keys_pool != nullptr);
|
2021-06-30 15:19:35 +02:00
|
|
|
|
2023-07-17 10:46:26 +02:00
|
|
|
struct IDNameLib_Key *key = static_cast<IDNameLib_Key *>(
|
|
|
|
|
BLI_mempool_alloc(id_map->type_maps_keys_pool));
|
2021-06-30 15:19:35 +02:00
|
|
|
key->name = id->name + 2;
|
|
|
|
|
key->lib = id->lib;
|
|
|
|
|
BLI_ghash_insert(type_map->map, key, id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (id_map->idmap_types & MAIN_IDMAP_TYPE_UUID) {
|
2023-07-17 10:46:26 +02:00
|
|
|
BLI_assert(id_map->uuid_map != nullptr);
|
2021-06-30 15:19:35 +02:00
|
|
|
BLI_assert(id->session_uuid != MAIN_ID_SESSION_UUID_UNSET);
|
|
|
|
|
void **id_ptr_v;
|
|
|
|
|
const bool existing_key = BLI_ghash_ensure_p(
|
|
|
|
|
id_map->uuid_map, POINTER_FROM_UINT(id->session_uuid), &id_ptr_v);
|
|
|
|
|
BLI_assert(existing_key == false);
|
|
|
|
|
UNUSED_VARS_NDEBUG(existing_key);
|
|
|
|
|
|
|
|
|
|
*id_ptr_v = id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_main_idmap_remove_id(struct IDNameLib_Map *id_map, ID *id)
|
|
|
|
|
{
|
|
|
|
|
if (id_map->idmap_types & MAIN_IDMAP_TYPE_NAME) {
|
|
|
|
|
const short id_type = GS(id->name);
|
|
|
|
|
struct IDNameLib_TypeMap *type_map = main_idmap_from_idcode(id_map, id_type);
|
|
|
|
|
|
2021-07-29 10:52:08 +10:00
|
|
|
/* No need to do anything if map has not been lazily created yet. */
|
2023-07-17 10:46:26 +02:00
|
|
|
if (LIKELY(type_map != nullptr) && type_map->map != nullptr) {
|
|
|
|
|
BLI_assert(id_map->type_maps_keys_pool != nullptr);
|
2021-06-30 15:19:35 +02:00
|
|
|
|
|
|
|
|
/* NOTE: We cannot free the key from the MemPool here, would need new API from GHash to also
|
|
|
|
|
* retrieve key pointer. Not a big deal for now */
|
2023-07-17 10:46:26 +02:00
|
|
|
IDNameLib_Key key{id->name + 2, id->lib};
|
|
|
|
|
BLI_ghash_remove(type_map->map, &key, nullptr, nullptr);
|
2021-06-30 15:19:35 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (id_map->idmap_types & MAIN_IDMAP_TYPE_UUID) {
|
2023-07-17 10:46:26 +02:00
|
|
|
BLI_assert(id_map->uuid_map != nullptr);
|
2021-06-30 15:19:35 +02:00
|
|
|
BLI_assert(id->session_uuid != MAIN_ID_SESSION_UUID_UNSET);
|
|
|
|
|
|
2023-07-17 10:46:26 +02:00
|
|
|
BLI_ghash_remove(id_map->uuid_map, POINTER_FROM_UINT(id->session_uuid), nullptr, nullptr);
|
2021-06-30 15:19:35 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-03 08:36:28 +10:00
|
|
|
Main *BKE_main_idmap_main_get(struct IDNameLib_Map *id_map)
|
2016-06-07 16:07:13 +10:00
|
|
|
{
|
|
|
|
|
return id_map->bmain;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-25 17:04:52 +10:00
|
|
|
static uint idkey_hash(const void *ptr)
|
2016-06-07 16:07:13 +10:00
|
|
|
{
|
2023-07-17 10:46:26 +02:00
|
|
|
const struct IDNameLib_Key *idkey = static_cast<const IDNameLib_Key *>(ptr);
|
2022-09-25 17:04:52 +10:00
|
|
|
uint key = BLI_ghashutil_strhash(idkey->name);
|
2016-06-07 16:07:13 +10:00
|
|
|
if (idkey->lib) {
|
|
|
|
|
key ^= BLI_ghashutil_ptrhash(idkey->lib);
|
|
|
|
|
}
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool idkey_cmp(const void *a, const void *b)
|
|
|
|
|
{
|
2023-07-17 10:46:26 +02:00
|
|
|
const struct IDNameLib_Key *idkey_a = static_cast<const IDNameLib_Key *>(a);
|
|
|
|
|
const struct IDNameLib_Key *idkey_b = static_cast<const IDNameLib_Key *>(b);
|
2020-08-08 12:14:52 +10:00
|
|
|
return !STREQ(idkey_a->name, idkey_b->name) || (idkey_a->lib != idkey_b->lib);
|
2016-06-07 16:07:13 +10:00
|
|
|
}
|
|
|
|
|
|
2020-03-05 16:17:14 +01:00
|
|
|
ID *BKE_main_idmap_lookup_name(struct IDNameLib_Map *id_map,
|
|
|
|
|
short id_type,
|
|
|
|
|
const char *name,
|
|
|
|
|
const Library *lib)
|
2016-06-07 16:07:13 +10:00
|
|
|
{
|
|
|
|
|
struct IDNameLib_TypeMap *type_map = main_idmap_from_idcode(id_map, id_type);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-17 10:46:26 +02:00
|
|
|
if (UNLIKELY(type_map == nullptr)) {
|
|
|
|
|
return nullptr;
|
2016-06-07 16:07:13 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-06-30 15:19:35 +02:00
|
|
|
/* Lazy init. */
|
2023-07-17 10:46:26 +02:00
|
|
|
if (type_map->map == nullptr) {
|
|
|
|
|
if (id_map->type_maps_keys_pool == nullptr) {
|
2021-06-30 15:19:35 +02:00
|
|
|
id_map->type_maps_keys_pool = BLI_mempool_create(
|
|
|
|
|
sizeof(struct IDNameLib_Key), 1024, 1024, BLI_MEMPOOL_NOP);
|
2016-06-07 16:07:13 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-06-30 15:19:35 +02:00
|
|
|
GHash *map = type_map->map = BLI_ghash_new(idkey_hash, idkey_cmp, __func__);
|
|
|
|
|
ListBase *lb = which_libbase(id_map->bmain, id_type);
|
2023-07-17 10:46:26 +02:00
|
|
|
for (ID *id = static_cast<ID *>(lb->first); id; id = static_cast<ID *>(id->next)) {
|
|
|
|
|
struct IDNameLib_Key *key = static_cast<IDNameLib_Key *>(
|
|
|
|
|
BLI_mempool_alloc(id_map->type_maps_keys_pool));
|
2016-06-07 16:07:13 +10:00
|
|
|
key->name = id->name + 2;
|
|
|
|
|
key->lib = id->lib;
|
|
|
|
|
BLI_ghash_insert(map, key, id);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-06-07 16:07:13 +10:00
|
|
|
const struct IDNameLib_Key key_lookup = {name, lib};
|
2023-07-17 10:46:26 +02:00
|
|
|
return static_cast<ID *>(BLI_ghash_lookup(type_map->map, &key_lookup));
|
2016-06-07 16:07:13 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ID *BKE_main_idmap_lookup_id(struct IDNameLib_Map *id_map, const ID *id)
|
|
|
|
|
{
|
Fix (unreported) crash when undoing after ID deletion.
Yes, we do can undo an ID deletion now.
However, this requires extra care in UI 'remapping' to new IDs step
(when undoing, we do not fully reload the UI from saved .blend).
Otherwise, new UI (i.e. one from saved .blend file) might reference
IDs that where freed in old bmain (the one before the undo), we cannot
use those to get ID name then, that would be a nasty use-after-free!
To prevent this, we generate a GSet of all valid ID pointers at that
time (i.e. those found in both old and new Main's), and ensure any ID
we try to remap by its name is in that GSet. Otherwise, there is no
possible remapping, just return NULL.
2019-02-08 18:45:57 +01:00
|
|
|
/* When used during undo/redo, this function cannot assume that given id points to valid memory
|
2019-04-27 12:07:07 +10:00
|
|
|
* (i.e. has not been freed),
|
|
|
|
|
* so it has to check that it does exist in 'old' (aka current) Main database.
|
Fix (unreported) crash when undoing after ID deletion.
Yes, we do can undo an ID deletion now.
However, this requires extra care in UI 'remapping' to new IDs step
(when undoing, we do not fully reload the UI from saved .blend).
Otherwise, new UI (i.e. one from saved .blend file) might reference
IDs that where freed in old bmain (the one before the undo), we cannot
use those to get ID name then, that would be a nasty use-after-free!
To prevent this, we generate a GSet of all valid ID pointers at that
time (i.e. those found in both old and new Main's), and ensure any ID
we try to remap by its name is in that GSet. Otherwise, there is no
possible remapping, just return NULL.
2019-02-08 18:45:57 +01:00
|
|
|
* Otherwise, we cannot provide new ID pointer that way (would crash accessing freed memory
|
|
|
|
|
* when trying to get ID name).
|
|
|
|
|
*/
|
2023-07-17 10:46:26 +02:00
|
|
|
if (id_map->valid_id_pointers == nullptr || BLI_gset_haskey(id_map->valid_id_pointers, id)) {
|
2020-03-05 16:17:14 +01:00
|
|
|
return BKE_main_idmap_lookup_name(id_map, GS(id->name), id->name + 2, id->lib);
|
|
|
|
|
}
|
2023-07-17 10:46:26 +02:00
|
|
|
return nullptr;
|
2020-03-05 16:17:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ID *BKE_main_idmap_lookup_uuid(struct IDNameLib_Map *id_map, const uint session_uuid)
|
|
|
|
|
{
|
|
|
|
|
if (id_map->idmap_types & MAIN_IDMAP_TYPE_UUID) {
|
2023-07-17 10:46:26 +02:00
|
|
|
return static_cast<ID *>(BLI_ghash_lookup(id_map->uuid_map, POINTER_FROM_UINT(session_uuid)));
|
Fix (unreported) crash when undoing after ID deletion.
Yes, we do can undo an ID deletion now.
However, this requires extra care in UI 'remapping' to new IDs step
(when undoing, we do not fully reload the UI from saved .blend).
Otherwise, new UI (i.e. one from saved .blend file) might reference
IDs that where freed in old bmain (the one before the undo), we cannot
use those to get ID name then, that would be a nasty use-after-free!
To prevent this, we generate a GSet of all valid ID pointers at that
time (i.e. those found in both old and new Main's), and ensure any ID
we try to remap by its name is in that GSet. Otherwise, there is no
possible remapping, just return NULL.
2019-02-08 18:45:57 +01:00
|
|
|
}
|
2023-07-17 10:46:26 +02:00
|
|
|
return nullptr;
|
2016-06-07 16:07:13 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_main_idmap_destroy(struct IDNameLib_Map *id_map)
|
|
|
|
|
{
|
2020-03-05 16:17:14 +01:00
|
|
|
if (id_map->idmap_types & MAIN_IDMAP_TYPE_NAME) {
|
|
|
|
|
struct IDNameLib_TypeMap *type_map = id_map->type_maps;
|
2021-03-04 18:39:07 +01:00
|
|
|
for (int i = 0; i < INDEX_ID_MAX; i++, type_map++) {
|
2020-03-05 16:17:14 +01:00
|
|
|
if (type_map->map) {
|
2023-07-17 10:46:26 +02:00
|
|
|
BLI_ghash_free(type_map->map, nullptr, nullptr);
|
|
|
|
|
type_map->map = nullptr;
|
2020-03-05 16:17:14 +01:00
|
|
|
}
|
2016-06-07 16:07:13 +10:00
|
|
|
}
|
2023-07-17 10:46:26 +02:00
|
|
|
if (id_map->type_maps_keys_pool != nullptr) {
|
2021-06-30 15:19:35 +02:00
|
|
|
BLI_mempool_destroy(id_map->type_maps_keys_pool);
|
2023-07-17 10:46:26 +02:00
|
|
|
id_map->type_maps_keys_pool = nullptr;
|
2021-06-30 15:19:35 +02:00
|
|
|
}
|
2016-06-07 16:07:13 +10:00
|
|
|
}
|
2020-03-05 16:17:14 +01:00
|
|
|
if (id_map->idmap_types & MAIN_IDMAP_TYPE_UUID) {
|
2023-07-17 10:46:26 +02:00
|
|
|
BLI_ghash_free(id_map->uuid_map, nullptr, nullptr);
|
2020-03-05 16:17:14 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-17 10:46:26 +02:00
|
|
|
BLI_assert(id_map->type_maps_keys_pool == nullptr);
|
2021-06-30 15:19:35 +02:00
|
|
|
|
2023-07-17 10:46:26 +02:00
|
|
|
if (id_map->valid_id_pointers != nullptr) {
|
|
|
|
|
BLI_gset_free(id_map->valid_id_pointers, nullptr);
|
Fix (unreported) crash when undoing after ID deletion.
Yes, we do can undo an ID deletion now.
However, this requires extra care in UI 'remapping' to new IDs step
(when undoing, we do not fully reload the UI from saved .blend).
Otherwise, new UI (i.e. one from saved .blend file) might reference
IDs that where freed in old bmain (the one before the undo), we cannot
use those to get ID name then, that would be a nasty use-after-free!
To prevent this, we generate a GSet of all valid ID pointers at that
time (i.e. those found in both old and new Main's), and ensure any ID
we try to remap by its name is in that GSet. Otherwise, there is no
possible remapping, just return NULL.
2019-02-08 18:45:57 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-06-07 16:07:13 +10:00
|
|
|
MEM_freeN(id_map);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|