[Bf-blender-cvs] [2efdb20b0bc] functions-experimental-refactor: use allocator in MonotonicAllocator and use minimum allocation size

Jacques Lucke noreply at git.blender.org
Tue Oct 15 15:56:41 CEST 2019


Commit: 2efdb20b0bcee9eb6b88feb8ea68d226310679ae
Author: Jacques Lucke
Date:   Thu Oct 10 10:26:04 2019 +0200
Branches: functions-experimental-refactor
https://developer.blender.org/rB2efdb20b0bcee9eb6b88feb8ea68d226310679ae

use allocator in MonotonicAllocator and use minimum allocation size

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

M	source/blender/blenkernel/BKE_virtual_node_tree_cxx.h
M	source/blender/blenlib/BLI_allocator.h
M	source/blender/blenlib/BLI_monotonic_allocator.h
M	source/blender/blenlib/BLI_temporary_allocator.h
M	source/blender/blenlib/BLI_vector.h
M	source/blender/blenlib/intern/BLI_temporary_allocator.cc
M	source/blender/functions/core/data_graph.cpp
M	source/blender/functions/core/data_graph.hpp
M	source/blender/functions/core/data_graph_builder.cpp
M	source/blender/functions/core/data_graph_builder.hpp
M	source/blender/simulations/bparticles/node_frontend.cpp

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

diff --git a/source/blender/blenkernel/BKE_virtual_node_tree_cxx.h b/source/blender/blenkernel/BKE_virtual_node_tree_cxx.h
index d050b7652ac..3291fc49ae6 100644
--- a/source/blender/blenkernel/BKE_virtual_node_tree_cxx.h
+++ b/source/blender/blenkernel/BKE_virtual_node_tree_cxx.h
@@ -61,7 +61,7 @@ class VirtualNodeTree {
   Vector<VirtualSocket *> m_inputs_with_links;
   MultiMap<std::string, VirtualNode *> m_nodes_by_idname;
   uint m_socket_counter = 0;
-  MonotonicAllocator m_allocator;
+  MonotonicAllocator<> m_allocator;
 
  public:
   void add_all_of_tree(bNodeTree *btree);
diff --git a/source/blender/blenlib/BLI_allocator.h b/source/blender/blenlib/BLI_allocator.h
index 52fa8d2b705..98dbe969966 100644
--- a/source/blender/blenlib/BLI_allocator.h
+++ b/source/blender/blenlib/BLI_allocator.h
@@ -60,6 +60,11 @@ class GuardedAllocator {
   {
     MEM_freeN(ptr);
   }
+
+  uint min_allocated_size() const
+  {
+    return 0;
+  }
 };
 
 /**
@@ -99,6 +104,11 @@ class RawAllocator {
     void *actual_pointer = POINTER_OFFSET(ptr, offset);
     free(actual_pointer);
   }
+
+  uint min_allocated_size() const
+  {
+    return 0;
+  }
 };
 
 /**
@@ -122,6 +132,11 @@ class TemporaryAllocator {
   {
     BLI_temporary_deallocate(ptr);
   }
+
+  uint min_allocated_size() const
+  {
+    return BLI_TEMPORARY_MINIMUM_SIZE;
+  }
 };
 
 }  // namespace BLI
diff --git a/source/blender/blenlib/BLI_monotonic_allocator.h b/source/blender/blenlib/BLI_monotonic_allocator.h
index 5970c717367..15169d397fc 100644
--- a/source/blender/blenlib/BLI_monotonic_allocator.h
+++ b/source/blender/blenlib/BLI_monotonic_allocator.h
@@ -29,8 +29,10 @@
 
 namespace BLI {
 
+template<typename Allocator = GuardedAllocator>
 class MonotonicAllocator : NonCopyable, NonMovable {
  private:
+  Allocator m_allocator;
   Vector<void *> m_pointers;
 
   void *m_current_buffer;
@@ -46,7 +48,7 @@ class MonotonicAllocator : NonCopyable, NonMovable {
   ~MonotonicAllocator()
   {
     for (void *ptr : m_pointers) {
-      MEM_freeN(ptr);
+      m_allocator.deallocate(ptr);
     }
   }
 
@@ -59,8 +61,9 @@ class MonotonicAllocator : NonCopyable, NonMovable {
       return ptr;
     }
     else {
-      uint byte_size = std::max(m_next_min_alloc_size, size);
-      void *ptr = MEM_mallocN(byte_size, __func__);
+      uint byte_size = std::max({m_next_min_alloc_size, size, m_allocator.min_allocated_size()});
+
+      void *ptr = m_allocator.allocate(byte_size, __func__);
       m_pointers.append(ptr);
 
       m_current_buffer = POINTER_OFFSET(ptr, size);
diff --git a/source/blender/blenlib/BLI_temporary_allocator.h b/source/blender/blenlib/BLI_temporary_allocator.h
index b378e5869c0..6bb15f9f695 100644
--- a/source/blender/blenlib/BLI_temporary_allocator.h
+++ b/source/blender/blenlib/BLI_temporary_allocator.h
@@ -53,6 +53,7 @@ extern "C" {
 #endif
 
 #define BLI_TEMPORARY_BUFFER_ALIGNMENT 64
+#define BLI_TEMPORARY_MINIMUM_SIZE (64 * 1024)
 
 void *BLI_temporary_allocate(uint size);
 void BLI_temporary_deallocate(void *buffer);
diff --git a/source/blender/blenlib/BLI_vector.h b/source/blender/blenlib/BLI_vector.h
index 46c46a1440f..e5ec6fb1269 100644
--- a/source/blender/blenlib/BLI_vector.h
+++ b/source/blender/blenlib/BLI_vector.h
@@ -555,6 +555,11 @@ template<typename T, uint N = 4, typename Allocator = GuardedAllocator> class Ve
     /* Round up to the next power of two. Otherwise consecutive calls to grow can cause a
      * reallocation every time even though the min_capacity only increments. */
     min_capacity = power_of_2_max_u(min_capacity);
+
+    /* Use a larger capacity if the allocator cannot allocate small buffers. */
+    uint min_allocation_capacity = m_allocator.min_allocated_size() / sizeof(T);
+    min_capacity = std::max(min_capacity, min_allocation_capacity);
+
     uint size = this->size();
 
     T *new_array = (T *)m_allocator.allocate_aligned(
diff --git a/source/blender/blenlib/intern/BLI_temporary_allocator.cc b/source/blender/blenlib/intern/BLI_temporary_allocator.cc
index b145e65530d..2040313fcfb 100644
--- a/source/blender/blenlib/intern/BLI_temporary_allocator.cc
+++ b/source/blender/blenlib/intern/BLI_temporary_allocator.cc
@@ -23,7 +23,7 @@
 using namespace BLI;
 
 constexpr uint ALIGNMENT = BLI_TEMPORARY_BUFFER_ALIGNMENT;
-constexpr uint SMALL_BUFFER_SIZE = 64 * 1024;
+constexpr uint SMALL_BUFFER_SIZE = BLI_TEMPORARY_MINIMUM_SIZE;
 constexpr uintptr_t ALIGNMENT_MASK = ~(uintptr_t)(ALIGNMENT - 1);
 
 enum TemporaryBufferType {
diff --git a/source/blender/functions/core/data_graph.cpp b/source/blender/functions/core/data_graph.cpp
index c4781a5705a..258ca3cd93f 100644
--- a/source/blender/functions/core/data_graph.cpp
+++ b/source/blender/functions/core/data_graph.cpp
@@ -7,7 +7,7 @@ DataGraph::DataGraph(std::unique_ptr<OwnedResources> resources,
                      Vector<InputSocket> inputs,
                      Vector<OutputSocket> outputs,
                      Vector<uint> targets,
-                     std::unique_ptr<MonotonicAllocator> source_info_allocator)
+                     std::unique_ptr<MonotonicAllocator<>> source_info_allocator)
     : m_resources(std::move(resources)),
       m_nodes(std::move(nodes)),
       m_inputs(std::move(inputs)),
diff --git a/source/blender/functions/core/data_graph.hpp b/source/blender/functions/core/data_graph.hpp
index 93d9f3e7cfa..b4f1a4de1ae 100644
--- a/source/blender/functions/core/data_graph.hpp
+++ b/source/blender/functions/core/data_graph.hpp
@@ -200,7 +200,7 @@ class DataGraph {
   Vector<InputSocket> m_inputs;
   Vector<OutputSocket> m_outputs;
   Vector<uint> m_targets;
-  std::unique_ptr<MonotonicAllocator> m_source_info_allocator;
+  std::unique_ptr<MonotonicAllocator<>> m_source_info_allocator;
 
  public:
   DataGraph(std::unique_ptr<OwnedResources> m_resources,
@@ -208,7 +208,7 @@ class DataGraph {
             Vector<InputSocket> inputs,
             Vector<OutputSocket> outputs,
             Vector<uint> targets,
-            std::unique_ptr<MonotonicAllocator> source_info_allocator);
+            std::unique_ptr<MonotonicAllocator<>> source_info_allocator);
 
   DataGraph(DataGraph &other) = delete;
   ~DataGraph();
diff --git a/source/blender/functions/core/data_graph_builder.cpp b/source/blender/functions/core/data_graph_builder.cpp
index c410e52f0c1..4171c046531 100644
--- a/source/blender/functions/core/data_graph_builder.cpp
+++ b/source/blender/functions/core/data_graph_builder.cpp
@@ -2,7 +2,7 @@
 
 namespace FN {
 
-DataGraphBuilder::DataGraphBuilder() : m_source_info_allocator(new MonotonicAllocator())
+DataGraphBuilder::DataGraphBuilder() : m_source_info_allocator(new MonotonicAllocator<>())
 {
 }
 
diff --git a/source/blender/functions/core/data_graph_builder.hpp b/source/blender/functions/core/data_graph_builder.hpp
index c52c48507e6..9dff2adc1ff 100644
--- a/source/blender/functions/core/data_graph_builder.hpp
+++ b/source/blender/functions/core/data_graph_builder.hpp
@@ -145,8 +145,8 @@ class DataGraphBuilder {
   uint m_link_counter = 0;
   uint m_input_socket_counter = 0;
   uint m_output_socket_counter = 0;
-  std::unique_ptr<MonotonicAllocator> m_source_info_allocator;
-  MonotonicAllocator m_allocator;
+  std::unique_ptr<MonotonicAllocator<>> m_source_info_allocator;
+  MonotonicAllocator<> m_allocator;
 
  public:
   DataGraphBuilder();
diff --git a/source/blender/simulations/bparticles/node_frontend.cpp b/source/blender/simulations/bparticles/node_frontend.cpp
index 016de9bf482..4fa390ac1d8 100644
--- a/source/blender/simulations/bparticles/node_frontend.cpp
+++ b/source/blender/simulations/bparticles/node_frontend.cpp
@@ -62,7 +62,7 @@ template<typename T> struct DestructFunc {
 class VTreeData {
  private:
   /* Keep this at the beginning, so that it is destructed last. */
-  MonotonicAllocator m_allocator;
+  MonotonicAllocator<> m_allocator;
 
   template<typename T> using destruct_ptr = std::unique_ptr<T, DestructFunc<T>>;



More information about the Bf-blender-cvs mailing list