Blender V5.3
main_idmap.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <cstdlib>
6#include <cstring>
7
8#include "MEM_guardedalloc.h"
9
10#include "BLI_ghash.hh"
11#include "BLI_map.hh"
12#include "BLI_mempool.hh"
13#include "BLI_utildefines.hh"
14
15#include "DNA_ID.h"
16
17#include "BKE_idtype.hh"
18#include "BKE_lib_id.hh"
19#include "BKE_main.hh"
20#include "BKE_main_idmap.hh" /* own include */
21
22namespace blender {
23
29
30/* -------------------------------------------------------------------- */
41
44 const char *name;
46 const Library *lib;
47};
48
53
63
64 /* For storage of keys for the #TypeMap #GHash, avoids many single allocations. */
66};
67
69{
70 if (id_map->idmap_types & MAIN_IDMAP_TYPE_NAME) {
71 for (int i = 0; i < INDEX_ID_MAX; i++) {
72 if (id_map->type_maps[i].id_type == id_type) {
73 return &id_map->type_maps[i];
74 }
75 }
76 }
77 return nullptr;
78}
79
81 const bool create_valid_ids_set,
82 Main *old_bmain,
83 const int idmap_types)
84{
86 id_map->bmain = bmain;
87 id_map->idmap_types = idmap_types;
88
89 int index = 0;
90 while (index < INDEX_ID_MAX) {
91 IDNameLib_TypeMap *type_map = &id_map->type_maps[index];
92 type_map->map = nullptr;
94 BLI_assert(type_map->id_type != 0);
95 }
97 id_map->type_maps_keys_pool = nullptr;
98
99 if (idmap_types & MAIN_IDMAP_TYPE_UID) {
100 ID *id;
101 id_map->uid_map = MEM_new<Map<uint32_t, ID *>>(__func__);
102 FOREACH_MAIN_ID_BEGIN (bmain, id) {
103 BLI_assert(id->session_uid != MAIN_ID_SESSION_UID_UNSET);
104 id_map->uid_map->add_new(id->session_uid, id);
105 }
107 }
108 else {
109 id_map->uid_map = nullptr;
110 }
111
112 if (create_valid_ids_set) {
113 id_map->valid_id_pointers = BKE_main_set_create(bmain, nullptr);
114 if (old_bmain != nullptr) {
115 id_map->valid_id_pointers = BKE_main_set_create(old_bmain, id_map->valid_id_pointers);
116 }
117 }
118 else {
119 id_map->valid_id_pointers = nullptr;
120 }
121
122 return id_map;
123}
124
126{
127 if (id_map->idmap_types & MAIN_IDMAP_TYPE_NAME) {
128 const short id_type = GS(id->name);
129 IDNameLib_TypeMap *type_map = main_idmap_from_idcode(id_map, id_type);
130
131 /* No need to do anything if map has not been lazily created yet. */
132 if (LIKELY(type_map != nullptr) && type_map->map != nullptr) {
133 BLI_assert(id_map->type_maps_keys_pool != nullptr);
134
135 IDNameLib_Key *key = static_cast<IDNameLib_Key *>(
136 BLI_mempool_alloc(id_map->type_maps_keys_pool));
137 key->name = id->name + 2;
138 key->lib = id->lib;
139 BLI_ghash_insert(type_map->map, key, id);
140 }
141 }
142
143 if (id_map->idmap_types & MAIN_IDMAP_TYPE_UID) {
144 BLI_assert(id_map->uid_map != nullptr);
145 BLI_assert(id->session_uid != MAIN_ID_SESSION_UID_UNSET);
146 id_map->uid_map->add_new(id->session_uid, id);
147 }
148}
149
151{
152 if (id_map->idmap_types & MAIN_IDMAP_TYPE_NAME) {
153 const short id_type = GS(id->name);
154 IDNameLib_TypeMap *type_map = main_idmap_from_idcode(id_map, id_type);
155
156 /* No need to do anything if map has not been lazily created yet. */
157 if (LIKELY(type_map != nullptr) && type_map->map != nullptr) {
158 BLI_assert(id_map->type_maps_keys_pool != nullptr);
159
160 /* NOTE: We cannot free the key from the MemPool here, would need new API from GHash to also
161 * retrieve key pointer. Not a big deal for now */
162 IDNameLib_Key key{id->name + 2, id->lib};
163 BLI_ghash_remove(type_map->map, &key, nullptr, nullptr);
164 }
165 }
166
167 if (id_map->idmap_types & MAIN_IDMAP_TYPE_UID) {
168 BLI_assert(id_map->uid_map != nullptr);
169 BLI_assert(id->session_uid != MAIN_ID_SESSION_UID_UNSET);
170 id_map->uid_map->remove(id->session_uid);
171 }
172}
173
178
179static uint idkey_hash(const void *ptr)
180{
181 const IDNameLib_Key *idkey = static_cast<const IDNameLib_Key *>(ptr);
182 uint key = BLI_ghashutil_strhash(idkey->name);
183 if (idkey->lib) {
184 key ^= BLI_ghashutil_ptrhash(idkey->lib);
185 }
186 return key;
187}
188
189static bool idkey_cmp(const void *a, const void *b)
190{
191 const IDNameLib_Key *idkey_a = static_cast<const IDNameLib_Key *>(a);
192 const IDNameLib_Key *idkey_b = static_cast<const IDNameLib_Key *>(b);
193 return !STREQ(idkey_a->name, idkey_b->name) || (idkey_a->lib != idkey_b->lib);
194}
195
197 short id_type,
198 const char *name,
199 const Library *lib)
200{
201 IDNameLib_TypeMap *type_map = main_idmap_from_idcode(id_map, id_type);
202
203 if (type_map == nullptr) [[unlikely]] {
204 return nullptr;
205 }
206
207 /* Lazy init. */
208 if (type_map->map == nullptr) {
209 if (id_map->type_maps_keys_pool == nullptr) {
210 id_map->type_maps_keys_pool = BLI_mempool_create(
211 sizeof(IDNameLib_Key), 1024, 1024, BLI_MEMPOOL_NOP);
212 }
213
214 GHash *map = type_map->map = BLI_ghash_new(idkey_hash, idkey_cmp, __func__);
215 ListBaseT<ID> *lb = which_libbase(id_map->bmain, id_type);
216 for (ID *id = lb->first(); id; id = static_cast<ID *>(id->next)) {
217 IDNameLib_Key *key = static_cast<IDNameLib_Key *>(
218 BLI_mempool_alloc(id_map->type_maps_keys_pool));
219 key->name = id->name + 2;
220 key->lib = id->lib;
221 BLI_ghash_insert(map, key, id);
222 }
223 }
224
225 const IDNameLib_Key key_lookup = {name, lib};
226 return static_cast<ID *>(BLI_ghash_lookup(type_map->map, &key_lookup));
227}
228
230{
231 /* When used during undo/redo, this function cannot assume that given id points to valid memory
232 * (i.e. has not been freed),
233 * so it has to check that it does exist in 'old' (aka current) Main database.
234 * Otherwise, we cannot provide new ID pointer that way (would crash accessing freed memory
235 * when trying to get ID name).
236 */
237 if (id_map->valid_id_pointers == nullptr || id_map->valid_id_pointers->contains(id)) {
238 return BKE_main_idmap_lookup_name(id_map, GS(id->name), id->name + 2, id->lib);
239 }
240 return nullptr;
241}
242
244{
245 if (id_map->idmap_types & MAIN_IDMAP_TYPE_UID) {
246 return id_map->uid_map->lookup_default(session_uid, nullptr);
247 }
248 return nullptr;
249}
250
252{
253 if (id_map.idmap_types & MAIN_IDMAP_TYPE_NAME) {
254 for (IDNameLib_TypeMap &type_map : id_map.type_maps) {
255 if (type_map.map) {
256 BLI_ghash_clear(type_map.map, nullptr, nullptr);
257 }
258 }
259 }
260 if (id_map.idmap_types & MAIN_IDMAP_TYPE_UID) {
261 id_map.uid_map->clear();
262 }
263
264 if (id_map.valid_id_pointers != nullptr) {
265 id_map.valid_id_pointers->clear();
266 }
267}
268
270{
271 if (id_map->idmap_types & MAIN_IDMAP_TYPE_NAME) {
272 for (IDNameLib_TypeMap &type_map : id_map->type_maps) {
273 if (type_map.map) {
274 BLI_ghash_free(type_map.map, nullptr, nullptr);
275 type_map.map = nullptr;
276 }
277 }
278 if (id_map->type_maps_keys_pool != nullptr) {
279 BLI_mempool_destroy(id_map->type_maps_keys_pool);
280 id_map->type_maps_keys_pool = nullptr;
281 }
282 }
283 if (id_map->idmap_types & MAIN_IDMAP_TYPE_UID) {
284 MEM_delete(id_map->uid_map);
285 }
286
287 BLI_assert(id_map->type_maps_keys_pool == nullptr);
288
289 MEM_delete(id_map->valid_id_pointers);
290 MEM_delete(id_map);
291}
292
294
295} // namespace blender
#define MAIN_ID_SESSION_UID_UNSET
#define FOREACH_MAIN_ID_END
Definition BKE_main.hh:735
#define FOREACH_MAIN_ID_BEGIN(_bmain, _id)
Definition BKE_main.hh:729
#define BLI_ghashutil_strhash(key)
Definition BLI_ghash.hh:573
ID and Library types, which are fundamental for SDNA.
#define INDEX_ID_MAX
Definition DNA_ID.h:1362
Read Blender memory allocation and freeing.
#define STREQ(a, b)
#define LIKELY(x)
BMesh Iterators.
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned int uint
Definition BKE_text.h:55
static bool idkey_cmp(const void *a, const void *b)
void * BLI_ghash_lookup(const GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.cc:735
ID * BKE_main_idmap_lookup_name(IDNameLib_Map *id_map, short id_type, const char *name, const Library *lib) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
ID ID * BKE_main_idmap_lookup_id(IDNameLib_Map *id_map, const ID *id) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
unsigned int BLI_ghashutil_ptrhash(const void *key)
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition BLI_ghash.cc:711
void BLI_mempool_destroy(BLI_mempool *pool) ATTR_NONNULL(1)
static uint idkey_hash(const void *ptr)
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition BLI_ghash.cc:866
short BKE_idtype_idcode_iter_step(int *idtype_index)
Definition idtype.cc:483
BLI_mempool * BLI_mempool_create(unsigned int esize, unsigned int elem_num, unsigned int pchunk, unsigned int flag) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL
void BLI_ghash_clear(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition BLI_ghash.cc:861
static IDNameLib_TypeMap * main_idmap_from_idcode(IDNameLib_Map *id_map, short id_type)
Definition main_idmap.cc:68
Set< const ID * > * BKE_main_set_create(Main *bmain, Set< const ID * > *set)
Definition main.cc:788
void BKE_main_idmap_clear(IDNameLib_Map &id_map)
@ BLI_MEMPOOL_NOP
bool BLI_ghash_remove(GHash *gh, const void *key, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition BLI_ghash.cc:792
ID_Type GS(const char *name)
Definition DNA_ID.h:410
void BKE_main_idmap_insert_id(IDNameLib_Map *id_map, ID *id) ATTR_NONNULL()
IDNameLib_Map * BKE_main_idmap_create(Main *bmain, bool create_valid_ids_set, Main *old_bmain, int idmap_types) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition main_idmap.cc:80
@ MAIN_IDMAP_TYPE_UID
@ MAIN_IDMAP_TYPE_NAME
void * BLI_mempool_alloc(BLI_mempool *pool) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(1)
GHash * BLI_ghash_new(GHashHashFP hashfp, GHashCmpFP cmpfp, const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.cc:690
void BKE_main_idmap_remove_id(IDNameLib_Map *id_map, const ID *id) ATTR_NONNULL()
ID ID ID * BKE_main_idmap_lookup_uid(IDNameLib_Map *id_map, uint session_uid) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Main * BKE_main_idmap_main_get(IDNameLib_Map *id_map) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
ListBaseT< ID > * which_libbase(Main *bmain, short type)
Definition main.cc:1073
void BKE_main_idmap_destroy(IDNameLib_Map *id_map) ATTR_NONNULL()
const Library * lib
Definition main_idmap.cc:46
Map< uint32_t, ID * > * uid_map
Definition main_idmap.cc:59
IDNameLib_TypeMap type_maps[INDEX_ID_MAX]
Definition main_idmap.cc:58
BLI_mempool * type_maps_keys_pool
Definition main_idmap.cc:65
Set< const ID * > * valid_id_pointers
Definition main_idmap.cc:61