[Bf-blender-cvs] [45fd15f824b] functions-experimental-refactor: initial attributes block container in functions2
Jacques Lucke
noreply at git.blender.org
Sat Nov 2 21:33:02 CET 2019
Commit: 45fd15f824b75c9a56f720e276793416546d88bd
Author: Jacques Lucke
Date: Sat Nov 2 20:48:35 2019 +0100
Branches: functions-experimental-refactor
https://developer.blender.org/rB45fd15f824b75c9a56f720e276793416546d88bd
initial attributes block container in functions2
===================================================================
M source/blender/functions2/CMakeLists.txt
A source/blender/functions2/FN_attributes_block_container.h
M source/blender/functions2/FN_cpp_type.h
A source/blender/functions2/intern/attributes_block_container.cc
M source/blender/functions2/intern/cpp_types.cc
===================================================================
diff --git a/source/blender/functions2/CMakeLists.txt b/source/blender/functions2/CMakeLists.txt
index ce8531a4ee0..bb7272e192e 100644
--- a/source/blender/functions2/CMakeLists.txt
+++ b/source/blender/functions2/CMakeLists.txt
@@ -29,6 +29,7 @@ set(SRC
intern/vtree_multi_function_network/mappings_nodes.cc
intern/vtree_multi_function_network/mappings_sockets.cc
intern/vtree_multi_function_network/mappings.cc
+ intern/attributes_block_container.cc
intern/attributes_ref.cc
intern/cpp_type.cc
intern/cpp_types.cc
@@ -36,6 +37,7 @@ set(SRC
intern/initialize.cc
intern/multi_function_network.cc
+ FN_attributes_block_container.h
FN_attributes_ref.h
FN_cpp_type.h
FN_generic_array_ref.h
diff --git a/source/blender/functions2/FN_attributes_block_container.h b/source/blender/functions2/FN_attributes_block_container.h
new file mode 100644
index 00000000000..1f350277317
--- /dev/null
+++ b/source/blender/functions2/FN_attributes_block_container.h
@@ -0,0 +1,91 @@
+#ifndef __FN_ATTRIBUTES_BLOCK_CONTAINER_H__
+#define __FN_ATTRIBUTES_BLOCK_CONTAINER_H__
+
+#include <mutex>
+
+#include "FN_attributes_ref.h"
+
+namespace FN {
+
+class AttributesBlock;
+
+class AttributesBlockContainer : BLI::NonCopyable, BLI::NonMovable {
+ private:
+ AttributesInfo m_attributes_info;
+ uint m_block_size;
+ VectorSet<AttributesBlock *> m_active_blocks;
+ std::mutex m_blocks_mutex;
+
+ public:
+ AttributesBlockContainer(AttributesInfo attributes_info, uint block_size);
+ ~AttributesBlockContainer();
+
+ const AttributesInfo &info() const
+ {
+ return m_attributes_info;
+ }
+
+ uint block_size() const
+ {
+ return m_block_size;
+ }
+
+ AttributesBlock &new_block();
+ void release_block(AttributesBlock &block);
+};
+
+class AttributesBlock : BLI::NonCopyable, BLI::NonMovable {
+ private:
+ AttributesBlockContainer &m_owner;
+ Vector<void *> m_buffers;
+ uint m_used_size;
+
+ public:
+ AttributesBlock(AttributesBlockContainer &owner);
+ ~AttributesBlock();
+
+ const AttributesInfo &info() const
+ {
+ return m_owner.info();
+ }
+
+ uint used_size() const
+ {
+ return m_used_size;
+ }
+
+ uint capacity() const
+ {
+ return m_owner.block_size();
+ }
+
+ IndexRange used_range() const
+ {
+ return IndexRange(m_used_size);
+ }
+
+ void set_used_size(uint new_used_size)
+ {
+ BLI_assert(new_used_size <= this->capacity());
+ m_used_size = new_used_size;
+ }
+
+ AttributesBlockContainer &owner()
+ {
+ return m_owner;
+ }
+
+ AttributesRef as_ref()
+ {
+ return AttributesRef(m_owner.info(), m_buffers, m_used_size);
+ }
+
+ AttributesRef as_ref__all()
+ {
+ return AttributesRef(m_owner.info(), m_buffers, this->capacity());
+ }
+};
+
+} // namespace FN
+
+#endif /* __FN_ATTRIBUTES_BLOCK_CONTAINER_H__ */
diff --git a/source/blender/functions2/FN_cpp_type.h b/source/blender/functions2/FN_cpp_type.h
index 76bc76c307e..d9610d78cd9 100644
--- a/source/blender/functions2/FN_cpp_type.h
+++ b/source/blender/functions2/FN_cpp_type.h
@@ -14,6 +14,7 @@ class CPPType {
public:
using ConstructDefaultF = void (*)(const CPPType *self, void *ptr);
using DestructF = void (*)(void *ptr);
+ using DestructNF = void (*)(void *ptr, uint n);
using CopyToInitializedF = void (*)(const void *src, void *dst);
using CopyToUninitializedF = void (*)(const void *src, void *dst);
using RelocateToInitializedF = void (*)(void *src, void *dst);
@@ -25,6 +26,7 @@ class CPPType {
bool trivially_destructible,
ConstructDefaultF construct_default,
DestructF destruct,
+ DestructNF destruct_n,
CopyToInitializedF copy_to_initialized,
CopyToUninitializedF copy_to_uninitialized,
RelocateToInitializedF relocate_to_initialized,
@@ -35,6 +37,7 @@ class CPPType {
m_trivially_destructible(trivially_destructible),
m_construct_default(construct_default),
m_destruct(destruct),
+ m_destruct_n(destruct_n),
m_copy_to_initialized(copy_to_initialized),
m_copy_to_uninitialized(copy_to_uninitialized),
m_relocate_to_initialized(relocate_to_initialized),
@@ -95,6 +98,13 @@ class CPPType {
m_destruct(ptr);
}
+ void destruct_n(void *ptr, uint n) const
+ {
+ BLI_assert(this->pointer_has_valid_alignment(ptr));
+
+ m_destruct_n(ptr, n);
+ }
+
void copy_to_initialized(const void *src, void *dst) const
{
BLI_assert(this->pointer_has_valid_alignment(src));
@@ -155,6 +165,7 @@ class CPPType {
bool m_trivially_destructible;
ConstructDefaultF m_construct_default;
DestructF m_destruct;
+ DestructNF m_destruct_n;
CopyToInitializedF m_copy_to_initialized;
CopyToUninitializedF m_copy_to_uninitialized;
RelocateToInitializedF m_relocate_to_initialized;
diff --git a/source/blender/functions2/intern/attributes_block_container.cc b/source/blender/functions2/intern/attributes_block_container.cc
new file mode 100644
index 00000000000..9450ff74fa0
--- /dev/null
+++ b/source/blender/functions2/intern/attributes_block_container.cc
@@ -0,0 +1,52 @@
+#include "FN_attributes_block_container.h"
+
+namespace FN {
+
+AttributesBlockContainer::AttributesBlockContainer(AttributesInfo attributes_info, uint block_size)
+ : m_attributes_info(std::move(attributes_info)), m_block_size(block_size)
+{
+}
+
+AttributesBlockContainer::~AttributesBlockContainer()
+{
+ while (m_active_blocks.size() > 0) {
+ this->release_block(**m_active_blocks.begin());
+ }
+}
+
+AttributesBlock &AttributesBlockContainer::new_block()
+{
+ AttributesBlock *block = new AttributesBlock(*this);
+ {
+ std::lock_guard<std::mutex> lock(m_blocks_mutex);
+ m_active_blocks.add(block);
+ }
+ return *block;
+}
+
+void AttributesBlockContainer::release_block(AttributesBlock &block)
+{
+ {
+ std::lock_guard<std::mutex> lock(m_blocks_mutex);
+ m_active_blocks.remove(&block);
+ }
+ delete █
+}
+
+AttributesBlock::AttributesBlock(AttributesBlockContainer &owner) : m_owner(owner), m_used_size(0)
+{
+ for (const CPPType *type : owner.info().types()) {
+ void *buffer = MEM_malloc_arrayN(owner.block_size(), type->size(), __func__);
+ m_buffers.append(buffer);
+ }
+}
+
+AttributesBlock::~AttributesBlock()
+{
+ for (uint attribute_index : m_owner.info().indices()) {
+ const CPPType &type = m_owner.info().type_of(attribute_index);
+ type.destruct_n(m_buffers[attribute_index], m_used_size);
+ }
+}
+
+} // namespace FN
diff --git a/source/blender/functions2/intern/cpp_types.cc b/source/blender/functions2/intern/cpp_types.cc
index 54d78882991..3a0b35d4a6f 100644
--- a/source/blender/functions2/intern/cpp_types.cc
+++ b/source/blender/functions2/intern/cpp_types.cc
@@ -42,6 +42,10 @@ template<typename T> void Destruct_CB(void *ptr)
{
BLI::destruct((T *)ptr);
}
+template<typename T> void DestructN_CB(void *ptr, uint n)
+{
+ BLI::destruct_n((T *)ptr, n);
+}
template<typename T> void CopyToInitialized_CB(const void *src, void *dst)
{
*(T *)dst = *(T *)src;
@@ -83,6 +87,7 @@ void init_cpp_types()
DefaultConstructor<TYPE_NAME, \
std::is_default_constructible<TYPE_NAME>::value>::get_callback(), \
Destruct_CB<TYPE_NAME>, \
+ DestructN_CB<TYPE_NAME>, \
CopyToInitialized_CB<TYPE_NAME>, \
CopyToUninitialized_CB<TYPE_NAME>, \
RelocateToInitialized_CB<TYPE_NAME>, \
More information about the Bf-blender-cvs
mailing list