[Bf-blender-cvs] [de8a52dcee7] functions: low level memory utilities in blenlib
Jacques Lucke
noreply at git.blender.org
Sun Aug 18 12:13:05 CEST 2019
Commit: de8a52dcee782d1d2f0ace5507c64e1271cd85d8
Author: Jacques Lucke
Date: Sun Aug 18 12:12:39 2019 +0200
Branches: functions
https://developer.blender.org/rBde8a52dcee782d1d2f0ace5507c64e1271cd85d8
low level memory utilities in blenlib
===================================================================
M source/blender/blenlib/BLI_array_ref.hpp
A source/blender/blenlib/BLI_memory.hpp
M source/blender/blenlib/BLI_optional.hpp
M source/blender/blenlib/BLI_vector.hpp
M source/blender/blenlib/BLI_vector_adaptor.hpp
M source/blender/blenlib/CMakeLists.txt
M source/blender/functions/backends/cpp/cpp_type_info.hpp
M source/blender/functions/backends/cpp/tuple.hpp
===================================================================
diff --git a/source/blender/blenlib/BLI_array_ref.hpp b/source/blender/blenlib/BLI_array_ref.hpp
index e02be9109dd..f6ceb819dd8 100644
--- a/source/blender/blenlib/BLI_array_ref.hpp
+++ b/source/blender/blenlib/BLI_array_ref.hpp
@@ -34,6 +34,7 @@
#include "BLI_utildefines.h"
#include "BLI_string_ref.hpp"
+#include "BLI_memory.hpp"
namespace BLI {
@@ -138,7 +139,7 @@ template<typename T> class ArrayRef {
*/
void copy_from(const T *ptr)
{
- std::copy_n(ptr, m_size, m_start);
+ BLI::copy_n(ptr, m_size, m_start);
}
void copy_from(ArrayRef<T> other)
@@ -152,7 +153,7 @@ template<typename T> class ArrayRef {
*/
void copy_to(T *ptr)
{
- std::copy_n(m_start, m_size, ptr);
+ BLI::copy_n(m_start, m_size, ptr);
}
T *begin() const
diff --git a/source/blender/blenlib/BLI_memory.hpp b/source/blender/blenlib/BLI_memory.hpp
new file mode 100644
index 00000000000..4943ded779f
--- /dev/null
+++ b/source/blender/blenlib/BLI_memory.hpp
@@ -0,0 +1,61 @@
+#pragma once
+
+#include <memory>
+#include <algorithm>
+
+namespace BLI {
+
+using std::copy;
+using std::copy_n;
+using std::uninitialized_copy;
+using std::uninitialized_copy_n;
+using std::uninitialized_fill;
+using std::uninitialized_fill_n;
+
+template<typename T> void destruct(T *ptr)
+{
+ ptr->~T();
+}
+
+template<typename T> void descruct_n(T *ptr, uint n)
+{
+ for (uint i = 0; i < n; i++) {
+ ptr[i].~T();
+ }
+}
+
+template<typename T> void uninitialized_move_n(T *src, uint n, T *dst)
+{
+ std::uninitialized_copy_n(std::make_move_iterator(src), n, dst);
+}
+
+template<typename T> void move_n(T *src, uint n, T *dst)
+{
+ std::copy_n(std::make_move_iterator(src), n, dst);
+}
+
+template<typename T> void uninitialized_relocate(T *src, T *dst)
+{
+ new (dst) T(std::move(*src));
+ destruct(src);
+}
+
+template<typename T> void uninitialized_relocate_n(T *src, uint n, T *dst)
+{
+ uninitialized_move_n(src, n, dst);
+ descruct_n(src, n);
+}
+
+template<typename T> void relocate(T *src, T *dst)
+{
+ *dst = std::move(*src);
+ destruct(src);
+}
+
+template<typename T> void relocate_n(T *src, uint n, T *dst)
+{
+ move_n(src, n, dst);
+ descruct_n(src, n);
+}
+
+} // namespace BLI
diff --git a/source/blender/blenlib/BLI_optional.hpp b/source/blender/blenlib/BLI_optional.hpp
index b38f4165fbf..17400703ac6 100644
--- a/source/blender/blenlib/BLI_optional.hpp
+++ b/source/blender/blenlib/BLI_optional.hpp
@@ -111,7 +111,7 @@ template<typename T> class Optional {
return m_set;
}
- T &value() const
+ const T &value() const
{
if (m_set) {
return *this->value_ptr();
@@ -122,13 +122,24 @@ template<typename T> class Optional {
}
}
- void set(T &value)
+ T &value()
{
if (m_set) {
- std::copy_n(&value, 1, this->value_ptr());
+ return *this->value_ptr();
+ }
+ else {
+ BLI_assert(false);
+ return *(T *)nullptr;
+ }
+ }
+
+ void set(const T &value)
+ {
+ if (m_set) {
+ this->value() = value;
}
else {
- std::uninitialized_copy_n(&value, 1, this->value_ptr());
+ new (this->value_ptr()) T(value);
m_set = true;
}
}
@@ -136,10 +147,10 @@ template<typename T> class Optional {
void set(T &&value)
{
if (m_set) {
- std::copy_n(std::make_move_iterator(&value), 1, this->value_ptr());
+ this->value() = std::move(value);
}
else {
- std::uninitialized_copy_n(std::make_move_iterator(&value), 1, this->value_ptr());
+ new (this->value_ptr()) T(std::move(value));
m_set = true;
}
}
diff --git a/source/blender/blenlib/BLI_vector.hpp b/source/blender/blenlib/BLI_vector.hpp
index 1037a18a5c7..dd3ca90a0b7 100644
--- a/source/blender/blenlib/BLI_vector.hpp
+++ b/source/blender/blenlib/BLI_vector.hpp
@@ -33,6 +33,7 @@
#include <memory>
#include "BLI_utildefines.h"
+#include "BLI_memory.hpp"
#include "BLI_array_ref.hpp"
#include "BLI_listbase_wrapper.hpp"
#include "BLI_math_base.h"
@@ -41,14 +42,6 @@
namespace BLI {
-template<typename T> void uninitialized_relocate_n(T *src, uint n, T *dst)
-{
- std::uninitialized_copy_n(std::make_move_iterator(src), n, dst);
- for (uint i = 0; i < n; i++) {
- src[i].~T();
- }
-}
-
template<typename T, uint N = 4> class Vector {
private:
T *m_elements;
@@ -87,7 +80,7 @@ template<typename T, uint N = 4> class Vector {
Vector(uint size, const T &value) : Vector()
{
this->reserve(size);
- std::uninitialized_fill_n(m_elements, size, value);
+ BLI::uninitialized_fill_n(m_elements, size, value);
m_size = size;
}
@@ -104,7 +97,7 @@ template<typename T, uint N = 4> class Vector {
Vector(ArrayRef<T> values) : Vector()
{
this->reserve(values.size());
- std::uninitialized_copy_n(values.begin(), values.size(), this->begin());
+ BLI::uninitialized_copy_n(values.begin(), values.size(), this->begin());
m_size = values.size();
}
@@ -130,7 +123,7 @@ template<typename T, uint N = 4> class Vector {
{
this->reserve(values.size());
for (uint i = 0; i < values.size(); i++) {
- std::uninitialized_copy_n(&values[i], 1, m_elements + i);
+ new (m_elements + i) T(values[i]);
}
m_size = values.size();
}
@@ -252,14 +245,14 @@ template<typename T, uint N = 4> class Vector {
void append_unchecked(const T &value)
{
BLI_assert(m_size < m_capacity);
- std::uninitialized_copy_n(&value, 1, this->end());
+ new (this->end()) T(value);
m_size++;
}
void append_unchecked(T &&value)
{
BLI_assert(m_size < m_capacity);
- std::uninitialized_copy_n(std::make_move_iterator(&value), 1, this->end());
+ new (this->end()) T(std::move(value));
m_size++;
}
@@ -270,7 +263,7 @@ template<typename T, uint N = 4> class Vector {
void append_n_times(const T &value, uint n)
{
this->reserve(m_size + n);
- std::uninitialized_fill_n(this->end(), n, value);
+ BLI::uninitialized_fill_n(this->end(), n, value);
m_size += n;
}
@@ -302,7 +295,7 @@ template<typename T, uint N = 4> class Vector {
void extend_unchecked(const T *start, uint amount)
{
BLI_assert(m_size + amount <= m_capacity);
- std::uninitialized_copy_n(start, amount, this->end());
+ BLI::uninitialized_copy_n(start, amount, this->end());
m_size += amount;
}
@@ -376,13 +369,11 @@ template<typename T, uint N = 4> class Vector {
void remove_and_reorder(uint index)
{
BLI_assert(this->is_index_in_range(index));
- if (index < m_size - 1) {
- /* Move last element to index. */
- std::copy(std::make_move_iterator(this->end() - 1),
- std::make_move_iterator(this->end()),
- this->element_ptr(index));
+ uint last_index = m_size - 1;
+ if (index < last_index) {
+ m_elements[index] = std::move(m_elements[last_index]);
}
- this->destruct_element(m_size - 1);
+ this->destruct_element(last_index);
m_size--;
}
@@ -518,7 +509,7 @@ template<typename T, uint N = 4> class Vector {
m_elements = (T *)MEM_malloc_arrayN(other.m_capacity, sizeof(T), __func__);
}
- std::uninitialized_copy(other.begin(), other.end(), m_elements);
+ BLI::uninitialized_copy(other.begin(), other.end(), m_elements);
m_capacity = other.m_capacity;
m_size = other.m_size;
}
@@ -558,7 +549,7 @@ template<typename T, uint N = 4> class Vector {
void destruct_element(uint index)
{
- this->element_ptr(index)->~T();
+ destruct(this->element_ptr(index));
}
};
diff --git a/source/blender/blenlib/BLI_vector_adaptor.hpp b/source/blender/blenlib/BLI_vector_adaptor.hpp
index a2827a9ce7e..5c85cb0b0f8 100644
--- a/source/blender/blenlib/BLI_vector_adaptor.hpp
+++ b/source/blender/blenlib/BLI_vector_adaptor.hpp
@@ -85,14 +85,14 @@ template<typename T> class VectorAdaptor {
void append(const T &value)
{
BLI_assert(this->size() < m_capacity);
- std::uninitialized_copy_n(&value, 1, m_end);
+ new (m_end) T(value);
m_end += 1;
}
void append(T &&value)
{
BLI_assert(this->size() < m_capacity);
- std::uninitialized_copy_n(std::make_move_iterator(&value), 1, m_end);
+ new (m_end) T(std::move(value));
m_end += 1;
}
diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt
index 394767c8a97..54d9c66b73e 100644
--- a/source/blender/blenlib/CMakeLists.txt
+++ b/source/blender/blenlib/CMakeLists.txt
@@ -253,6 +253,7 @@ set(SRC
BLI_refcount.hpp
BLI_vector.hpp
BLI_map.hpp
+ BLI_memory.hpp
BLI_multi_map.hpp
BLI_set.hpp
BLI_set_vector.hpp
diff --git a/source/blender/functions/backends/cpp/cpp_type_info.hpp b/source/blender/functions/backends/cpp/cpp_type_info.hpp
index b8a5d652d03..38266e19010 100644
--- a/source/blender/functions/backends/cpp/cpp_type_info.hpp
+++ b/source/blender/functions/backends/cpp/cpp_type_info.hpp
@@ -120,80 +120,55 @@ template<typename T> class CPPTypeInfoForType : public CPPTypeInfo {
void destruct(void *ptr) const override
{
- T *ptr_ = (T *)ptr;
- ptr_->~T();
+ BLI::destruct((T *)ptr);
}
void destruct_n(void *ptr, uint n) const override
{
- T *ptr_ = (T *)ptr;
- for (uint i = 0; i < n; i++) {
- ptr_[i].~T();
- }
+ BLI::descruct_n((T *)ptr, n);
}
void copy_to_initialized(void *src, void *dst) const override
{
T *src_ = (T *)src;
T *dst_ = (T *)dst;
- std::copy_n(src_, 1, dst_);
+ *dst_ = *src_;
}
void copy_to_initialized_n(void *src, void *dst, uint n) const override
{
- T *src_ = (T *)src;
- T *dst_ = (T *)dst;
- std::copy_n(src_, n, dst_);
+ BLI::copy_n((T *)src, n, (T *)dst);
}
void copy_to_uninitialized(void *src, void *dst) const override
{
- T *src_ = (T *)src;
- T *dst_ = (T *)dst;
- std::uninitialized_copy_n(src_, 1, dst_);
+ T &src_ = *(T *)src;
+ new (dst) T(src_);
}
void copy_to_uninitialized_n(void *src, void *dst, uint n) const override
{
- T *src_ = (T *)src;
- T *dst_ = (T *)dst;
- std::uninitialized_copy_n(src_, n, dst_);
+ BLI::uninitialized_copy_n((T *)src, n, (T *)dst);
}
void relocate_to_initialized(void *src, voi
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list