[Bf-blender-cvs] [aebddd0ed6f] functions: initial multi mempool
Jacques Lucke
noreply at git.blender.org
Tue Mar 26 17:34:52 CET 2019
Commit: aebddd0ed6f418c357d1adf9b219345a98edc7a8
Author: Jacques Lucke
Date: Tue Mar 26 17:34:12 2019 +0100
Branches: functions
https://developer.blender.org/rBaebddd0ed6f418c357d1adf9b219345a98edc7a8
initial multi mempool
===================================================================
A source/blender/blenlib/BLI_multipool.hpp
M source/blender/blenlib/CMakeLists.txt
===================================================================
diff --git a/source/blender/blenlib/BLI_multipool.hpp b/source/blender/blenlib/BLI_multipool.hpp
new file mode 100644
index 00000000000..5199e31ea1c
--- /dev/null
+++ b/source/blender/blenlib/BLI_multipool.hpp
@@ -0,0 +1,51 @@
+#pragma once
+
+#include "BLI_small_map.hpp"
+#include "BLI_mempool.hpp"
+
+namespace BLI {
+
+ class MemMultiPool {
+ private:
+ SmallMap<uint, MemPool *> m_pools;
+
+ public:
+ MemMultiPool() = default;
+ MemMultiPool(MemMultiPool &other) = delete;
+
+ ~MemMultiPool()
+ {
+ for (MemPool *pool : m_pools.values()) {
+ delete pool;
+ }
+ }
+
+ void *allocate(uint size)
+ {
+ uint alloc_size = size + sizeof(uint);
+ MemPool *pool = m_pools.lookup_default(alloc_size, NULL);
+
+ if (pool == NULL) {
+ pool = new MemPool(alloc_size);
+ m_pools.add(alloc_size, pool);
+ }
+
+ void *real_ptr = pool->allocate();
+ *(uint *)real_ptr = alloc_size;
+
+ void *ptr = (void *)((uint *)real_ptr + 1);
+ return ptr;
+ }
+
+ void deallocate(void *ptr)
+ {
+ void *real_ptr = (uint *)ptr - 1;
+ uint alloc_size = *(uint *)real_ptr;
+ BLI_assert(m_pools.contains(alloc_size));
+
+ MemPool *pool = m_pools.lookup(alloc_size);
+ pool->deallocate(ptr);
+ }
+ };
+
+} /* namespace BLI */
\ No newline at end of file
diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt
index f39b95accc9..764035ed95e 100644
--- a/source/blender/blenlib/CMakeLists.txt
+++ b/source/blender/blenlib/CMakeLists.txt
@@ -236,6 +236,7 @@ set(SRC
BLI_lazy_init.hpp
BLI_listbase_wrapper.hpp
BLI_mempool.hpp
+ BLI_multipool.hpp
BLI_optional.hpp
BLI_shared.hpp
BLI_small_vector.hpp
More information about the Bf-blender-cvs
mailing list