[Bf-blender-cvs] [0fd83de6c37] functions: new fixed array allocator

Jacques Lucke noreply at git.blender.org
Sun Jun 30 16:00:51 CEST 2019


Commit: 0fd83de6c379b925d4398a103e9afa31c0a99654
Author: Jacques Lucke
Date:   Sun Jun 30 11:11:34 2019 +0200
Branches: functions
https://developer.blender.org/rB0fd83de6c379b925d4398a103e9afa31c0a99654

new fixed array allocator

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

A	source/blender/blenlib/BLI_fixed_array_allocator.hpp
A	tests/gtests/blenlib/BLI_fixed_array_allocator_test.cc
M	tests/gtests/blenlib/CMakeLists.txt

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

diff --git a/source/blender/blenlib/BLI_fixed_array_allocator.hpp b/source/blender/blenlib/BLI_fixed_array_allocator.hpp
new file mode 100644
index 00000000000..2edcae2022d
--- /dev/null
+++ b/source/blender/blenlib/BLI_fixed_array_allocator.hpp
@@ -0,0 +1,68 @@
+#pragma once
+
+#include "BLI_small_stack.hpp"
+
+namespace BLI {
+
+class FixedArrayAllocator {
+ private:
+  SmallVector<void *, 16> m_all_pointers;
+  SmallVector<SmallStack<void *>, 16> m_pointer_stacks;
+  uint m_array_length;
+
+ public:
+  FixedArrayAllocator(uint array_length) : m_array_length(array_length)
+  {
+  }
+
+  ~FixedArrayAllocator()
+  {
+    for (void *ptr : m_all_pointers) {
+      MEM_freeN(ptr);
+    }
+  }
+
+  uint array_size() const
+  {
+    return m_array_length;
+  }
+
+  void *allocate_array(uint element_size)
+  {
+    SmallStack<void *> &stack = this->stack_for_element_size(element_size);
+    if (stack.size() > 0) {
+      return stack.pop();
+    }
+    void *ptr = MEM_mallocN_aligned(m_array_length * element_size, 64, __func__);
+    m_all_pointers.append(ptr);
+    return ptr;
+  }
+
+  void deallocate_array(void *ptr, uint element_size)
+  {
+    SmallStack<void *> &stack = this->stack_for_element_size(element_size);
+    stack.push(ptr);
+  }
+
+  template<typename T> T *allocate_array()
+  {
+    return this->allocate_array(sizeof(T));
+  }
+
+  template<typename T> void deallocate_array(T *ptr)
+  {
+    return this->deallocate_array(ptr, sizeof(T));
+  }
+
+  SmallStack<void *> &stack_for_element_size(uint element_size)
+  {
+    BLI_assert(element_size > 0);
+    uint index = element_size - 1;
+    if (index >= m_pointer_stacks.size()) {
+      m_pointer_stacks.append_n_times({}, index - m_pointer_stacks.size() + 1);
+    }
+    return m_pointer_stacks[index];
+  }
+};
+
+};  // namespace BLI
diff --git a/tests/gtests/blenlib/BLI_fixed_array_allocator_test.cc b/tests/gtests/blenlib/BLI_fixed_array_allocator_test.cc
new file mode 100644
index 00000000000..67648f083f8
--- /dev/null
+++ b/tests/gtests/blenlib/BLI_fixed_array_allocator_test.cc
@@ -0,0 +1,29 @@
+#include "testing/testing.h"
+#include "BLI_fixed_array_allocator.hpp"
+
+using BLI::FixedArrayAllocator;
+
+TEST(fixed_array_allocator, AllocateSameSize)
+{
+  FixedArrayAllocator allocator(42);
+  void *ptr1 = allocator.allocate_array(4);
+  void *ptr2 = allocator.allocate_array(4);
+  EXPECT_NE(ptr1, ptr2);
+}
+
+TEST(fixed_array_allocator, AllocateDifferentSizes)
+{
+  FixedArrayAllocator allocator(42);
+  void *ptr1 = allocator.allocate_array(3);
+  void *ptr2 = allocator.allocate_array(4);
+  EXPECT_NE(ptr1, ptr2);
+}
+
+TEST(fixed_array_allocator, AllocateSamePointerTwice)
+{
+  FixedArrayAllocator allocator(42);
+  void *ptr1 = allocator.allocate_array(10);
+  allocator.deallocate_array(ptr1, 10);
+  void *ptr2 = allocator.allocate_array(10);
+  EXPECT_EQ(ptr1, ptr2);
+}
diff --git a/tests/gtests/blenlib/CMakeLists.txt b/tests/gtests/blenlib/CMakeLists.txt
index a97efa81805..9672d07c32b 100644
--- a/tests/gtests/blenlib/CMakeLists.txt
+++ b/tests/gtests/blenlib/CMakeLists.txt
@@ -45,6 +45,7 @@ BLENDER_TEST(BLI_array_ref "bf_blenlib")
 BLENDER_TEST(BLI_chained_strings "bf_blenlib")
 BLENDER_TEST(BLI_expr_pylike_eval "bf_blenlib")
 BLENDER_TEST(BLI_edgehash "bf_blenlib")
+BLENDER_TEST(BLI_fixed_array_allocator "bf_blenlib")
 BLENDER_TEST(BLI_ghash "bf_blenlib")
 BLENDER_TEST(BLI_hash_mm2a "bf_blenlib")
 BLENDER_TEST(BLI_heap "bf_blenlib")



More information about the Bf-blender-cvs mailing list