Commit: e3cdeb80b1c6e5d6c9bd159696e1ecfd37832ad2
Author: Jacques Lucke
Date: Sun Feb 17 14:17:48 2019 +0100
Branches: functions
https://developer.blender.org/rBe3cdeb80b1c6e5d6c9bd159696e1ecfd37832ad2
MemPool
===================================================================
A source/blender/blenlib/BLI_mempool.hpp
M source/blender/blenlib/BLI_small_vector.hpp
M source/blender/blenlib/CMakeLists.txt
A tests/gtests/blenlib/BLI_mempool_test.cc
M tests/gtests/blenlib/CMakeLists.txt
===================================================================
diff --git a/source/blender/blenlib/BLI_mempool.hpp
b/source/blender/blenlib/BLI_mempool.hpp
new file mode 100644
index 00000000000..f78bc6973db
--- /dev/null
+++ b/source/blender/blenlib/BLI_mempool.hpp
@@ -0,0 +1,59 @@
+#include "BLI_small_stack.hpp"
+
+namespace BLI {
+
+ class MemPool {
+ private:
+ SmallStack<void *> m_free_stack;
+ SmallVector<void *> m_start_pointers;
+ uint m_element_size;
+
+ public:
+ MemPool(uint element_size)
+ : m_element_size(element_size) {}
+
+ MemPool(MemPool &mempool) = delete;
+
+ ~MemPool()
+ {
+ for (void *ptr : m_start_pointers) {
+ std::free(ptr);
+ }
+ }
+
+ void *allocate()
+ {
+ if (m_free_stack.empty()) {
+ this->allocate_more();
+ }
+ return m_free_stack.pop();
+ }
+
+ void deallocate(void *ptr)
+ {
+ m_free_stack.push(ptr);
+ }
+
+ void print_stats() const
+ {
+ std::cout << "MemPool at " << (void *)this << std::endl;
+ std::cout << " Free Amount: " << m_free_stack.size()
<< std::endl;
+ std::cout << " Allocations: " <<
m_start_pointers.size() << std::endl;
+ }
+
+ private:
+ void allocate_more()
+ {
+ uint new_amount = 1 << (m_start_pointers.size() + 4);
+ uint byte_size = new_amount * m_element_size;
+ void *ptr = std::malloc(byte_size);
+
+ for (uint i = 0; i < new_amount; i++) {
+ m_free_stack.push((char *)ptr + i *
m_element_size);
+ }
+
+ m_start_pointers.append(ptr);
+ }
+ };
+
+} /* namespace BLI */
\ No newline at end of file
diff --git a/source/blender/blenlib/BLI_small_vector.hpp
b/source/blender/blenlib/BLI_small_vector.hpp
index 4b82ad418fa..ca5cb47b6d7 100644
--- a/source/blender/blenlib/BLI_small_vector.hpp
+++ b/source/blender/blenlib/BLI_small_vector.hpp
@@ -4,6 +4,7 @@
#include <cstdlib>
#include <cstring>
#include <memory>
+#include <iostream>
namespace BLI {
@@ -148,6 +149,14 @@ namespace BLI {
const T *cend() const
{ return this->end(); }
+ void print_stats() const
+ {
+ std::cout << "Small Vector at " << (void *)this << ":"
<< std::endl;
+ std::cout << " Elements: " << this->size() <<
std::endl;
+ std::cout << " Capacity: " << this->m_capacity <<
std::endl;
+ std::cout << " Small Elements: " << N << " Size on
Stack: " << sizeof(*this) << std::endl;
+ }
+
private:
T *small_buffer() const
{
diff --git a/source/blender/blenlib/CMakeLists.txt
b/source/blender/blenlib/CMakeLists.txt
index ee77bb7a42b..4ea5bfb2163 100644
--- a/source/blender/blenlib/CMakeLists.txt
+++ b/source/blender/blenlib/CMakeLists.txt
@@ -226,6 +226,7 @@ set(SRC
PIL_time_utildefines.h
BLI_composition.hpp
+ BLI_mempool.hpp
BLI_shared.hpp
BLI_small_vector.hpp
BLI_small_map.hpp
diff --git a/tests/gtests/blenlib/BLI_mempool_test.cc
b/tests/gtests/blenlib/BLI_mempool_test.cc
new file mode 100644
index 00000000000..689b4589a5b
--- /dev/null
+++ b/tests/gtests/blenlib/BLI_mempool_test.cc
@@ -0,0 +1,21 @@
+#include "testing/testing.h"
+#include "BLI_mempool.hpp"
+
+using namespace BLI;
+
+TEST(mempool, Test1)
+{
+ MemPool mempool(sizeof(int));
+ int *a = new(mempool.allocate()) int;
+ *a = 3;
+ EXPECT_EQ(*a, 3);
+ mempool.deallocate(a);
+}
+
+TEST(mempool, Test2)
+{
+ MemPool mempool(sizeof(int));
+ for (uint i = 0; i < 100000; i++) {
+ mempool.allocate();
+ }
+}
\ No newline at end of file
diff --git a/tests/gtests/blenlib/CMakeLists.txt
b/tests/gtests/blenlib/CMakeLists.txt
index 502fc137024..3c0ff77de20 100644
--- a/tests/gtests/blenlib/CMakeLists.txt
+++ b/tests/gtests/blenlib/CMakeLists.txt
@@ -53,6 +53,7 @@ BLENDER_TEST(BLI_math_base "bf_blenlib")
BLENDER_TEST(BLI_math_color "bf_blenlib")
BLENDER_TEST(BLI_math_geom "bf_blenlib")
BLENDER_TEST(BLI_memiter "bf_blenlib")
+BLENDER_TEST(BLI_mempool "bf_blenlib")
BLENDER_TEST(BLI_path_util "${BLI_path_util_extra_libs}")
BLENDER_TEST(BLI_polyfill_2d "bf_blenlib")
BLENDER_TEST(BLI_shared "bf_blenlib")
_______________________________________________
Bf-blender-cvs mailing list
[email protected]
https://lists.blender.org/mailman/listinfo/bf-blender-cvs