[Bf-blender-cvs] [7387e362678] functions: simplify allocating arrays and vectors from fixed array allocator

Jacques Lucke noreply at git.blender.org
Mon Jul 8 17:56:44 CEST 2019


Commit: 7387e362678ad2292e6ab3d1bfe85a90c6115293
Author: Jacques Lucke
Date:   Mon Jul 8 11:41:38 2019 +0200
Branches: functions
https://developer.blender.org/rB7387e362678ad2292e6ab3d1bfe85a90c6115293

simplify allocating arrays and vectors from fixed array allocator

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

M	source/blender/blenlib/BLI_fixed_array_allocator.hpp
M	source/blender/blenlib/BLI_vector_adaptor.hpp
M	source/blender/simulations/bparticles/simulate.cpp

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

diff --git a/source/blender/blenlib/BLI_fixed_array_allocator.hpp b/source/blender/blenlib/BLI_fixed_array_allocator.hpp
index 83eba60f418..5550adcb23a 100644
--- a/source/blender/blenlib/BLI_fixed_array_allocator.hpp
+++ b/source/blender/blenlib/BLI_fixed_array_allocator.hpp
@@ -7,6 +7,7 @@
  */
 
 #include "BLI_small_stack.hpp"
+#include "BLI_vector_adaptor.hpp"
 
 namespace BLI {
 
@@ -123,6 +124,11 @@ class FixedArrayAllocator {
     {
       return (T *)m_ptr;
     }
+
+    FixedArrayAllocator &allocator()
+    {
+      return m_allocator;
+    }
   };
 
   /**
@@ -143,6 +149,52 @@ class FixedArrayAllocator {
     return ScopedAllocation<T>(*this, this->allocate<T>(), sizeof(T));
   }
 
+  template<typename T> class Vector {
+   private:
+    ScopedAllocation<T> m_ptr;
+    VectorAdaptor<T> m_vector;
+
+   public:
+    Vector(FixedArrayAllocator &allocator)
+        : m_ptr(allocator.allocate_scoped<T>()), m_vector(m_ptr, allocator.array_size())
+    {
+    }
+
+    ~Vector() = default;
+
+    operator VectorAdaptor<T> &()
+    {
+      return m_vector;
+    }
+
+    operator ArrayRef<T>()
+    {
+      return m_vector;
+    }
+  };
+
+  template<typename T> class Array {
+   private:
+    ScopedAllocation<T> m_ptr;
+    uint m_size;
+
+   public:
+    Array(FixedArrayAllocator &allocator) : Array(allocator, allocator.array_size())
+    {
+    }
+
+    Array(FixedArrayAllocator &allocator, uint size)
+        : m_ptr(allocator.allocate_scoped<T>()), m_size(size)
+    {
+      BLI_assert(size <= allocator.array_size());
+    }
+
+    operator ArrayRef<T>()
+    {
+      return ArrayRef<T>(m_ptr, m_size);
+    }
+  };
+
  private:
   SmallStack<void *> &stack_for_element_size(uint element_size)
   {
diff --git a/source/blender/blenlib/BLI_vector_adaptor.hpp b/source/blender/blenlib/BLI_vector_adaptor.hpp
index 39a04afb9ac..11352f3e73c 100644
--- a/source/blender/blenlib/BLI_vector_adaptor.hpp
+++ b/source/blender/blenlib/BLI_vector_adaptor.hpp
@@ -75,9 +75,7 @@ template<typename T> class VectorAdaptor {
   /**
    * Elements should continue to live after the adapter is destructed.
    */
-  ~VectorAdaptor()
-  {
-  }
+  ~VectorAdaptor() = default;
 
   void clear()
   {
diff --git a/source/blender/simulations/bparticles/simulate.cpp b/source/blender/simulations/bparticles/simulate.cpp
index a5e3bc33f3b..3fd9973c89e 100644
--- a/source/blender/simulations/bparticles/simulate.cpp
+++ b/source/blender/simulations/bparticles/simulate.cpp
@@ -232,18 +232,12 @@ BLI_NOINLINE static void simulate_to_next_event(FixedArrayAllocator &array_alloc
                                                 VectorAdaptor<float> &r_remaining_durations)
 {
   uint amount = particles.size();
-
   BLI_assert(array_allocator.array_size() >= amount);
-  auto next_event_indices_array = array_allocator.allocate_scoped<int>();
-  auto time_factors_to_next_event_array = array_allocator.allocate_scoped<float>();
-  auto indices_with_event_array = array_allocator.allocate_scoped<uint>();
-  auto particle_indices_with_event_array = array_allocator.allocate_scoped<uint>();
-
-  VectorAdaptor<int> next_event_indices(next_event_indices_array, amount, amount);
-  VectorAdaptor<float> time_factors_to_next_event(
-      time_factors_to_next_event_array, amount, amount);
-  VectorAdaptor<uint> indices_with_event(indices_with_event_array, amount);
-  VectorAdaptor<uint> particle_indices_with_event(particle_indices_with_event_array, amount);
+
+  FixedArrayAllocator::Array<int> next_event_indices(array_allocator, amount);
+  FixedArrayAllocator::Array<float> time_factors_to_next_event(array_allocator, amount);
+  FixedArrayAllocator::Vector<uint> indices_with_event(array_allocator);
+  FixedArrayAllocator::Vector<uint> particle_indices_with_event(array_allocator);
 
   uint max_event_storage_size = std::max(get_max_event_storage_size(events), 1u);
   auto event_storage_array = array_allocator.allocate_scoped(max_event_storage_size);



More information about the Bf-blender-cvs mailing list