2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2016-06-07 16:07:13 +10:00
|
|
|
|
2023-07-22 11:27:25 +10:00
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <cstring>
|
2016-06-07 16:07:13 +10:00
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
|
|
#include "BLI_ghash.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"
|
|
|
|
|
|
2024-01-20 19:17:36 +01:00
|
|
|
#include "BKE_idtype.hh"
|
2024-01-15 12:44:04 -05:00
|
|
|
#include "BKE_lib_id.hh"
|
2023-12-01 19:43:16 +01:00
|
|
|
#include "BKE_main.hh"
|
2023-11-27 16:21:49 +01:00
|
|
|
#include "BKE_main_idmap.hh" /* 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 {
|
2023-07-20 11:30:25 +10:00
|
|
|
IDNameLib_TypeMap type_maps[INDEX_ID_MAX];
|
2024-01-22 13:47:13 +01:00
|
|
|
GHash *uid_map;
|
2023-06-03 08:36:28 +10:00
|
|
|
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
|
|
|
};
|
|
|
|
|
|
2023-07-20 11:30:25 +10:00
|
|
|
static IDNameLib_TypeMap *main_idmap_from_idcode(IDNameLib_Map *id_map, short id_type)
|
2016-06-07 16:07:13 +10:00
|
|
|
{
|
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-07-20 11:30:25 +10:00
|
|
|
IDNameLib_Map *BKE_main_idmap_create(Main *bmain,
|
|
|
|
|
const bool create_valid_ids_set,
|
|
|
|
|
Main *old_bmain,
|
|
|
|
|
const int idmap_types)
|
2016-06-07 16:07:13 +10:00
|
|
|
{
|
2023-07-20 11:30:25 +10:00
|
|
|
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) {
|
2023-07-20 11:30:25 +10:00
|
|
|
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
|
|
|
|
2024-01-22 13:47:13 +01:00
|
|
|
if (idmap_types & MAIN_IDMAP_TYPE_UID) {
|
2020-03-05 16:17:14 +01:00
|
|
|
ID *id;
|
2024-01-22 13:47:13 +01:00
|
|
|
id_map->uid_map = BLI_ghash_int_new(__func__);
|
2020-03-05 16:17:14 +01:00
|
|
|
FOREACH_MAIN_ID_BEGIN (bmain, id) {
|
2024-01-22 13:47:13 +01:00
|
|
|
BLI_assert(id->session_uid != MAIN_ID_SESSION_UID_UNSET);
|
2020-03-05 16:17:14 +01:00
|
|
|
void **id_ptr_v;
|
|
|
|
|
const bool existing_key = BLI_ghash_ensure_p(
|
2024-01-22 13:47:13 +01:00
|
|
|
id_map->uid_map, POINTER_FROM_UINT(id->session_uid), &id_ptr_v);
|
2020-03-05 16:17:14 +01:00
|
|
|
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 {
|
2024-01-22 13:47:13 +01:00
|
|
|
id_map->uid_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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-20 11:30:25 +10:00
|
|
|
void BKE_main_idmap_insert_id(IDNameLib_Map *id_map, ID *id)
|
2021-06-30 15:19:35 +02:00
|
|
|
{
|
|
|
|
|
if (id_map->idmap_types & MAIN_IDMAP_TYPE_NAME) {
|
|
|
|
|
const short id_type = GS(id->name);
|
2023-07-20 11:30:25 +10:00
|
|
|
IDNameLib_TypeMap *type_map = main_idmap_from_idcode(id_map, id_type);
|
2021-06-30 15:19:35 +02:00
|
|
|
|
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-20 11:30:25 +10:00
|
|
|
IDNameLib_Key *key = static_cast<IDNameLib_Key *>(
|
2023-07-17 10:46:26 +02:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-22 13:47:13 +01:00
|
|
|
if (id_map->idmap_types & MAIN_IDMAP_TYPE_UID) {
|
|
|
|
|
BLI_assert(id_map->uid_map != nullptr);
|
|
|
|
|
BLI_assert(id->session_uid != MAIN_ID_SESSION_UID_UNSET);
|
2021-06-30 15:19:35 +02:00
|
|
|
void **id_ptr_v;
|
|
|
|
|
const bool existing_key = BLI_ghash_ensure_p(
|
2024-01-22 13:47:13 +01:00
|
|
|
id_map->uid_map, POINTER_FROM_UINT(id->session_uid), &id_ptr_v);
|
2021-06-30 15:19:35 +02:00
|
|
|
BLI_assert(existing_key == false);
|
|
|
|
|
UNUSED_VARS_NDEBUG(existing_key);
|
|
|
|
|
|
|
|
|
|
*id_ptr_v = id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-01 14:27:13 +02:00
|
|
|
void BKE_main_idmap_remove_id(IDNameLib_Map *id_map, const ID *id)
|
2021-06-30 15:19:35 +02:00
|
|
|
{
|
|
|
|
|
if (id_map->idmap_types & MAIN_IDMAP_TYPE_NAME) {
|
|
|
|
|
const short id_type = GS(id->name);
|
2023-07-20 11:30:25 +10:00
|
|
|
IDNameLib_TypeMap *type_map = main_idmap_from_idcode(id_map, id_type);
|
2021-06-30 15:19:35 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-22 13:47:13 +01:00
|
|
|
if (id_map->idmap_types & MAIN_IDMAP_TYPE_UID) {
|
|
|
|
|
BLI_assert(id_map->uid_map != nullptr);
|
|
|
|
|
BLI_assert(id->session_uid != MAIN_ID_SESSION_UID_UNSET);
|
2021-06-30 15:19:35 +02:00
|
|
|
|
2024-01-22 13:47:13 +01:00
|
|
|
BLI_ghash_remove(id_map->uid_map, POINTER_FROM_UINT(id->session_uid), nullptr, nullptr);
|
2021-06-30 15:19:35 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-20 11:30:25 +10:00
|
|
|
Main *BKE_main_idmap_main_get(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-20 11:30:25 +10:00
|
|
|
const 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-20 11:30:25 +10:00
|
|
|
const IDNameLib_Key *idkey_a = static_cast<const IDNameLib_Key *>(a);
|
|
|
|
|
const 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
|
|
|
}
|
|
|
|
|
|
2023-07-20 11:30:25 +10:00
|
|
|
ID *BKE_main_idmap_lookup_name(IDNameLib_Map *id_map,
|
2020-03-05 16:17:14 +01:00
|
|
|
short id_type,
|
|
|
|
|
const char *name,
|
|
|
|
|
const Library *lib)
|
2016-06-07 16:07:13 +10:00
|
|
|
{
|
2023-07-20 11:30:25 +10:00
|
|
|
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(
|
2023-07-18 14:18:07 +10:00
|
|
|
sizeof(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)) {
|
2023-07-20 11:30:25 +10:00
|
|
|
IDNameLib_Key *key = static_cast<IDNameLib_Key *>(
|
2023-07-17 10:46:26 +02:00
|
|
|
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
|
|
|
|
2023-07-20 11:30:25 +10:00
|
|
|
const 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
|
|
|
}
|
|
|
|
|
|
2023-07-20 11:30:25 +10:00
|
|
|
ID *BKE_main_idmap_lookup_id(IDNameLib_Map *id_map, const ID *id)
|
2016-06-07 16:07:13 +10: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
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
|
2024-01-22 13:47:13 +01:00
|
|
|
ID *BKE_main_idmap_lookup_uid(IDNameLib_Map *id_map, const uint session_uid)
|
2020-03-05 16:17:14 +01:00
|
|
|
{
|
2024-01-22 13:47:13 +01:00
|
|
|
if (id_map->idmap_types & MAIN_IDMAP_TYPE_UID) {
|
|
|
|
|
return static_cast<ID *>(BLI_ghash_lookup(id_map->uid_map, POINTER_FROM_UINT(session_uid)));
|
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
|
|
|
}
|
|
|
|
|
|
2024-07-01 14:27:13 +02:00
|
|
|
void BKE_main_idmap_clear(IDNameLib_Map &id_map)
|
|
|
|
|
{
|
|
|
|
|
if (id_map.idmap_types & MAIN_IDMAP_TYPE_NAME) {
|
|
|
|
|
for (IDNameLib_TypeMap &type_map : id_map.type_maps) {
|
|
|
|
|
if (type_map.map) {
|
|
|
|
|
BLI_ghash_clear(type_map.map, nullptr, nullptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (id_map.idmap_types & MAIN_IDMAP_TYPE_UID) {
|
|
|
|
|
BLI_ghash_clear(id_map.uid_map, nullptr, nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (id_map.valid_id_pointers != nullptr) {
|
|
|
|
|
BLI_gset_clear(id_map.valid_id_pointers, nullptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-20 11:30:25 +10:00
|
|
|
void BKE_main_idmap_destroy(IDNameLib_Map *id_map)
|
2016-06-07 16:07:13 +10:00
|
|
|
{
|
2020-03-05 16:17:14 +01:00
|
|
|
if (id_map->idmap_types & MAIN_IDMAP_TYPE_NAME) {
|
2024-07-01 14:27:13 +02:00
|
|
|
for (IDNameLib_TypeMap &type_map : id_map->type_maps) {
|
|
|
|
|
if (type_map.map) {
|
|
|
|
|
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
|
|
|
}
|
2024-01-22 13:47:13 +01:00
|
|
|
if (id_map->idmap_types & MAIN_IDMAP_TYPE_UID) {
|
|
|
|
|
BLI_ghash_free(id_map->uid_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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|