diff options
author | Jacques Lucke <jacques@blender.org> | 2021-03-07 16:24:52 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2021-03-07 16:24:52 +0300 |
commit | 649916f0983e4c59201672e6e28dcc7ae1f655b2 (patch) | |
tree | f8caa573963f0af1897b25cbda1352c5ea7474a1 /source/blender/blenlib | |
parent | 456d3cc85e9f408341b12eb0d4f2c3a925855e8c (diff) |
BLI: make it harder to forget to destruct a value
Instead of returning a raw pointer, `LinearAllocator.construct(...)` now returns
a `destruct_ptr`, which is similar to `unique_ptr`, but does not deallocate
the memory and only calls the destructor instead.
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r-- | source/blender/blenlib/BLI_linear_allocator.hh | 8 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_resource_collector.hh | 7 | ||||
-rw-r--r-- | source/blender/blenlib/tests/BLI_linear_allocator_test.cc | 2 |
3 files changed, 9 insertions, 8 deletions
diff --git a/source/blender/blenlib/BLI_linear_allocator.hh b/source/blender/blenlib/BLI_linear_allocator.hh index 11022a03d69..0bf4ad194e1 100644 --- a/source/blender/blenlib/BLI_linear_allocator.hh +++ b/source/blender/blenlib/BLI_linear_allocator.hh @@ -118,14 +118,14 @@ template<typename Allocator = GuardedAllocator> class LinearAllocator : NonCopya * * Arguments passed to this method will be forwarded to the constructor of T. * - * You must not call `delete` on the returned pointer. - * Instead, the destruct has to be called explicitly. + * You must not call `delete` on the returned value. + * Instead, only the destructor has to be called. */ - template<typename T, typename... Args> T *construct(Args &&... args) + template<typename T, typename... Args> destruct_ptr<T> construct(Args &&... args) { void *buffer = this->allocate(sizeof(T), alignof(T)); T *value = new (buffer) T(std::forward<Args>(args)...); - return value; + return destruct_ptr<T>(value); } /** diff --git a/source/blender/blenlib/BLI_resource_collector.hh b/source/blender/blenlib/BLI_resource_collector.hh index ecae9b8c682..70804ceb1f1 100644 --- a/source/blender/blenlib/BLI_resource_collector.hh +++ b/source/blender/blenlib/BLI_resource_collector.hh @@ -130,9 +130,10 @@ class ResourceCollector : NonCopyable, NonMovable { */ template<typename T, typename... Args> T &construct(const char *name, Args &&... args) { - T *value = m_allocator.construct<T>(std::forward<Args>(args)...); - this->add(destruct_ptr<T>(value), name); - return *value; + destruct_ptr<T> value_ptr = m_allocator.construct<T>(std::forward<Args>(args)...); + T &value_ref = *value_ptr; + this->add(std::move(value_ptr), name); + return value_ref; } /** diff --git a/source/blender/blenlib/tests/BLI_linear_allocator_test.cc b/source/blender/blenlib/tests/BLI_linear_allocator_test.cc index 95156ae5c0c..977e5dba497 100644 --- a/source/blender/blenlib/tests/BLI_linear_allocator_test.cc +++ b/source/blender/blenlib/tests/BLI_linear_allocator_test.cc @@ -79,7 +79,7 @@ TEST(linear_allocator, Construct) LinearAllocator<> allocator; std::array<int, 5> values = {1, 2, 3, 4, 5}; - Vector<int> *vector = allocator.construct<Vector<int>>(values); + Vector<int> *vector = allocator.construct<Vector<int>>(values).release(); EXPECT_EQ(vector->size(), 5); EXPECT_EQ((*vector)[3], 4); vector->~Vector(); |