[Bf-blender-cvs] [d8223bdc382] master: Cleanup: Improvements to Outliner tree writing code, add assert

Julian Eisel noreply at git.blender.org
Wed Aug 17 20:40:13 CEST 2022


Commit: d8223bdc38202c1ddd8e5cf2a4f3ec6136d9e316
Author: Julian Eisel
Date:   Wed Aug 17 19:20:52 2022 +0200
Branches: master
https://developer.blender.org/rBd8223bdc38202c1ddd8e5cf2a4f3ec6136d9e316

Cleanup: Improvements to Outliner tree writing code, add assert

Comments there weren't really helpful, took me a while to get what they
try to say. This attempts to add a better explanation. Also add an
assert for a previous, implicit (but commented) assumption, and some
minor cleanups.

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

M	source/blender/blenkernel/intern/screen.c

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

diff --git a/source/blender/blenkernel/intern/screen.c b/source/blender/blenkernel/intern/screen.c
index 66ec887b3d4..f1eba64e401 100644
--- a/source/blender/blenkernel/intern/screen.c
+++ b/source/blender/blenkernel/intern/screen.c
@@ -1123,32 +1123,43 @@ static void write_uilist(BlendWriter *writer, uiList *ui_list)
   }
 }
 
-static void write_space_outliner(BlendWriter *writer, SpaceOutliner *space_outliner)
+static void write_space_outliner(BlendWriter *writer, const SpaceOutliner *space_outliner)
 {
   BLI_mempool *ts = space_outliner->treestore;
 
   if (ts) {
-    int elems = BLI_mempool_len(ts);
+    const int elems = BLI_mempool_len(ts);
     /* linearize mempool to array */
     TreeStoreElem *data = elems ? BLI_mempool_as_arrayN(ts, "TreeStoreElem") : NULL;
 
     if (data) {
-      /* In this block we use the memory location of the treestore
-       * but _not_ its data, the addresses in this case are UUID's,
-       * since we can't rely on malloc giving us different values each time.
+      BLO_write_struct(writer, SpaceOutliner, space_outliner);
+
+      /* To store #TreeStore (instead of the mempool), two unique memory addresses are needed,
+       * which can be used to identify the data on read:
+       * 1) One for the #TreeStore data itself.
+       * 2) One for the array of #TreeStoreElem's inside #TreeStore (#TreeStore.data).
+       *
+       * For 1) we just use the mempool's address (#SpaceOutliner::treestore).
+       * For 2) we don't have such a direct choice. We can't just use the array's address from
+       * above, since that may not be unique over all Outliners. So instead use an address relative
+       * to 1).
        */
-      TreeStore ts_flat = {0};
+      /* TODO the mempool could be moved to #SpaceOutliner_Runtime so that #SpaceOutliner could
+       * hold the #TreeStore directly. */
 
-      /* we know the treestore is at least as big as a pointer,
-       * so offsetting works to give us a UUID. */
+      /* Address relative to the tree-store, as noted above.  */
       void *data_addr = (void *)POINTER_OFFSET(ts, sizeof(void *));
+      /* There should be plenty of memory addresses within the mempool data that we can point into,
+       * just double-check we don't potentially end up with a memory address that another DNA
+       * struct might use. Assumes BLI_mempool uses the guarded allocator. */
+      BLI_assert(MEM_allocN_len(ts) >= sizeof(void *) * 2);
 
+      TreeStore ts_flat = {0};
       ts_flat.usedelem = elems;
       ts_flat.totelem = elems;
       ts_flat.data = data_addr;
 
-      BLO_write_struct(writer, SpaceOutliner, space_outliner);
-
       BLO_write_struct_at_address(writer, TreeStore, ts, &ts_flat);
       BLO_write_struct_array_at_address(writer, TreeStoreElem, elems, data_addr, data);



More information about the Bf-blender-cvs mailing list