[Bf-blender-cvs] [738abb6d2a6] undo-caches: Move cache key struct (and its hashing and compare utils) to BKE_idtype.

Bastien Montagne noreply at git.blender.org
Thu Jul 2 18:03:57 CEST 2020


Commit: 738abb6d2a6eb5945570171e17657bc46ea37a8b
Author: Bastien Montagne
Date:   Thu Jul 2 18:02:39 2020 +0200
Branches: undo-caches
https://developer.blender.org/rB738abb6d2a6eb5945570171e17657bc46ea37a8b

Move cache key struct (and its hashing and compare utils) to BKE_idtype.

Since those are generic to IDTypeInfo callbacks, move them here, they do
not belong to BLO anymore for sure.

===================================================================

M	source/blender/blenkernel/BKE_idtype.h
M	source/blender/blenkernel/intern/idtype.c
M	source/blender/blenkernel/intern/sound.c
M	source/blender/blenloader/BLO_readfile.h
M	source/blender/blenloader/intern/readfile.c

===================================================================

diff --git a/source/blender/blenkernel/BKE_idtype.h b/source/blender/blenkernel/BKE_idtype.h
index 89082c99c2d..a823693e126 100644
--- a/source/blender/blenkernel/BKE_idtype.h
+++ b/source/blender/blenkernel/BKE_idtype.h
@@ -33,7 +33,6 @@ extern "C" {
 #endif
 
 struct ID;
-struct BLOCacheStorageKey;
 struct LibraryForeachIDData;
 struct Main;
 
@@ -47,6 +46,19 @@ enum {
   IDTYPE_FLAGS_NO_MAKELOCAL = 1 << 2,
 };
 
+typedef struct IDCacheKey {
+  /* The session uuid of the ID owning the cached data. */
+  unsigned int id_session_uuid;
+  /* Value uniquely indentifying the cache whithin its ID.
+   * Typically the offset of its member in the data-block struct, but can be anything. */
+  size_t offset_in_ID;
+  /* Actual address of the cached data to save and restore. */
+  void *cache_v;
+} IDCacheKey;
+
+uint BKE_idtype_cache_key_hash(const void *key_v);
+bool BKE_idtype_cache_key_cmp(const void *key_a_v, const void *key_b_v);
+
 /* ********** Prototypes for IDTypeInfo callbacks. ********** */
 
 typedef void (*IDTypeInitDataFunction)(struct ID *id);
@@ -65,7 +77,7 @@ typedef void (*IDTypeMakeLocalFunction)(struct Main *bmain, struct ID *id, const
 typedef void (*IDTypeForeachIDFunction)(struct ID *id, struct LibraryForeachIDData *data);
 
 typedef void (*IDTypeForeachCacheFunctionCallback)(struct ID *id,
-                                                   const struct BLOCacheStorageKey *cache_key,
+                                                   const struct IDCacheKey *cache_key,
                                                    void **cache_p,
                                                    void *user_data);
 typedef void (*IDTypeForeachCacheFunction)(struct ID *id,
diff --git a/source/blender/blenkernel/intern/idtype.c b/source/blender/blenkernel/intern/idtype.c
index fcd3bc9c5b4..2684e964eb1 100644
--- a/source/blender/blenkernel/intern/idtype.c
+++ b/source/blender/blenkernel/intern/idtype.c
@@ -28,6 +28,7 @@
 
 #include "MEM_guardedalloc.h"
 
+#include "BLI_ghash.h"
 #include "BLI_utildefines.h"
 
 #include "CLG_log.h"
@@ -42,6 +43,22 @@
 
 // static CLG_LogRef LOG = {"bke.idtype"};
 
+uint BKE_idtype_cache_key_hash(const void *key_v)
+{
+  const IDCacheKey *key = key_v;
+  size_t hash = BLI_ghashutil_uinthash(key->id_session_uuid);
+  hash = BLI_ghashutil_combine_hash(hash, BLI_ghashutil_uinthash((uint)key->offset_in_ID));
+  return (uint)BLI_ghashutil_combine_hash(hash, BLI_ghashutil_ptrhash(key->cache_v));
+}
+
+bool BKE_idtype_cache_key_cmp(const void *key_a_v, const void *key_b_v)
+{
+  const IDCacheKey *key_a = key_a_v;
+  const IDCacheKey *key_b = key_b_v;
+  return (key_a->id_session_uuid != key_b->id_session_uuid) ||
+         (key_a->offset_in_ID != key_b->offset_in_ID) || (key_a->cache_v != key_b->cache_v);
+}
+
 static IDTypeInfo *id_types[MAX_LIBARRAY] = {NULL};
 
 static void id_type_init(void)
diff --git a/source/blender/blenkernel/intern/sound.c b/source/blender/blenkernel/intern/sound.c
index 84299818845..6e292f46e0a 100644
--- a/source/blender/blenkernel/intern/sound.c
+++ b/source/blender/blenkernel/intern/sound.c
@@ -119,7 +119,7 @@ static void sound_foreach_cache(ID *id,
                                 void *user_data)
 {
   bSound *sound = (bSound *)id;
-  BLOCacheStorageKey key = {
+  IDCacheKey key = {
       .id_session_uuid = id->session_uuid,
       .offset_in_ID = offsetof(bSound, waveform),
       .cache_v = sound->waveform,
diff --git a/source/blender/blenloader/BLO_readfile.h b/source/blender/blenloader/BLO_readfile.h
index dfce23559d9..e4908eb7257 100644
--- a/source/blender/blenloader/BLO_readfile.h
+++ b/source/blender/blenloader/BLO_readfile.h
@@ -193,18 +193,6 @@ struct BlendThumbnail *BLO_thumbnail_from_file(const char *filepath);
 extern const struct bTheme U_theme_default;
 extern const struct UserDef U_default;
 
-/* Management of chaches that we want to preserve across undo's. */
-
-typedef struct BLOCacheStorageKey {
-  /* The session uuid of the ID owning the cached data. */
-  unsigned int id_session_uuid;
-  /* Value uniquely indentifying the cache whithin its ID.
-   * Typically the offset of its member in the data-block struct, but can be anything. */
-  size_t offset_in_ID;
-  /* Actual address of the cached data to save and restore. */
-  void *cache_v;
-} BLOCacheStorageKey;
-
 #ifdef __cplusplus
 }
 #endif
diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index 1273aec4592..03ae6aaa8e9 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -2325,7 +2325,7 @@ typedef struct BLOCacheStorage {
 
 /** Register a cache data entry to be preserved when reading some undo memfile. */
 static void blo_cache_storage_entry_register(ID *id,
-                                             const BLOCacheStorageKey *key,
+                                             const IDCacheKey *key,
                                              void **UNUSED(cache_p),
                                              void *cache_storage_v)
 {
@@ -2334,15 +2334,14 @@ static void blo_cache_storage_entry_register(ID *id,
   BLOCacheStorage *cache_storage = cache_storage_v;
   BLI_assert(!BLI_ghash_haskey(cache_storage->cache_map, key));
 
-  BLOCacheStorageKey *storage_key = BLI_memarena_alloc(cache_storage->memarena,
-                                                       sizeof(*storage_key));
+  IDCacheKey *storage_key = BLI_memarena_alloc(cache_storage->memarena, sizeof(*storage_key));
   *storage_key = *key;
   BLI_ghash_insert(cache_storage->cache_map, storage_key, POINTER_FROM_UINT(0));
 }
 
 /** Restore a cache data entry from old ID into new one, when reading some undo memfile. */
 static void blo_cache_storage_entry_restore_in_new(ID *UNUSED(id),
-                                                   const BLOCacheStorageKey *key,
+                                                   const IDCacheKey *key,
                                                    void **cache_p,
                                                    void *cache_storage_v)
 {
@@ -2364,7 +2363,7 @@ static void blo_cache_storage_entry_restore_in_new(ID *UNUSED(id),
 
 /** Clear as needed a cache data entry from old ID, when reading some undo memfile. */
 static void blo_cache_storage_entry_clear_in_old(ID *UNUSED(id),
-                                                 const BLOCacheStorageKey *key,
+                                                 const IDCacheKey *key,
                                                  void **cache_p,
                                                  void *cache_storage_v)
 {
@@ -2380,22 +2379,6 @@ static void blo_cache_storage_entry_clear_in_old(ID *UNUSED(id),
   *cache_p = POINTER_AS_UINT(*value) != 0 ? NULL : key->cache_v;
 }
 
-static uint blo_cache_storage_hash(const void *key_v)
-{
-  const BLOCacheStorageKey *key = key_v;
-  size_t hash = BLI_ghashutil_uinthash(key->id_session_uuid);
-  hash = BLI_ghashutil_combine_hash(hash, BLI_ghashutil_uinthash((uint)key->offset_in_ID));
-  return (uint)BLI_ghashutil_combine_hash(hash, BLI_ghashutil_ptrhash(key->cache_v));
-}
-
-static bool blo_cache_storage_cmp(const void *key_a_v, const void *key_b_v)
-{
-  const BLOCacheStorageKey *key_a = key_a_v;
-  const BLOCacheStorageKey *key_b = key_b_v;
-  return (key_a->id_session_uuid != key_b->id_session_uuid) ||
-         (key_a->offset_in_ID != key_b->offset_in_ID) || (key_a->cache_v != key_b->cache_v);
-}
-
 void blo_cache_storage_init(FileData *fd, Main *bmain)
 {
   if (fd->memfile != NULL) {
@@ -2403,7 +2386,7 @@ void blo_cache_storage_init(FileData *fd, Main *bmain)
     fd->cache_storage = MEM_mallocN(sizeof(*fd->cache_storage), __func__);
     fd->cache_storage->memarena = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);
     fd->cache_storage->cache_map = BLI_ghash_new(
-        blo_cache_storage_hash, blo_cache_storage_cmp, __func__);
+        BKE_idtype_cache_key_hash, BKE_idtype_cache_key_cmp, __func__);
 
     ListBase *lb;
     FOREACH_MAIN_LISTBASE_BEGIN (bmain, lb) {



More information about the Bf-blender-cvs mailing list