From d7c644211898185579597588bb4fc08edc1a5093 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Tue, 29 Mar 2022 09:19:35 +0200 Subject: BLI: support value initialization in CPPType Value initialization differs from default-construction in that it also zero-initializes trivial types. --- source/blender/blenlib/BLI_cpp_type.hh | 28 +++++++++++++++++++ source/blender/blenlib/BLI_cpp_type_make.hh | 12 ++++++++ source/blender/blenlib/tests/BLI_cpp_type_test.cc | 34 +++++++++++++++++++++++ 3 files changed, 74 insertions(+) (limited to 'source/blender/blenlib') diff --git a/source/blender/blenlib/BLI_cpp_type.hh b/source/blender/blenlib/BLI_cpp_type.hh index 881408f460b..8e8175c2265 100644 --- a/source/blender/blenlib/BLI_cpp_type.hh +++ b/source/blender/blenlib/BLI_cpp_type.hh @@ -110,6 +110,9 @@ class CPPType : NonCopyable, NonMovable { void (*default_construct_)(void *ptr) = nullptr; void (*default_construct_indices_)(void *ptr, IndexMask mask) = nullptr; + void (*value_initialize_)(void *ptr) = nullptr; + void (*value_initialize_indices_)(void *ptr, IndexMask mask) = nullptr; + void (*destruct_)(void *ptr) = nullptr; void (*destruct_indices_)(void *ptr, IndexMask mask) = nullptr; @@ -325,6 +328,31 @@ class CPPType : NonCopyable, NonMovable { default_construct_indices_(ptr, mask); } + /** + * Same as #default_construct, but does zero initialization for trivial types. + * + * C++ equivalent: + * new (ptr) T(); + */ + void value_initialize(void *ptr) const + { + BLI_assert(this->pointer_can_point_to_instance(ptr)); + + value_initialize_(ptr); + } + + void value_initialize_n(void *ptr, int64_t n) const + { + this->value_initialize_indices(ptr, IndexMask(n)); + } + + void value_initialize_indices(void *ptr, IndexMask mask) const + { + BLI_assert(mask.size() == 0 || this->pointer_can_point_to_instance(ptr)); + + value_initialize_indices_(ptr, mask); + } + /** * Call the destructor on the given instance of this type. The pointer must not be nullptr. * diff --git a/source/blender/blenlib/BLI_cpp_type_make.hh b/source/blender/blenlib/BLI_cpp_type_make.hh index 9100b2b9a0f..2612348075b 100644 --- a/source/blender/blenlib/BLI_cpp_type_make.hh +++ b/source/blender/blenlib/BLI_cpp_type_make.hh @@ -20,6 +20,16 @@ template void default_construct_indices_cb(void *ptr, IndexMask mask mask.foreach_index([&](int64_t i) { new (static_cast(ptr) + i) T; }); } +template void value_initialize_cb(void *ptr) +{ + new (ptr) T(); +} + +template void value_initialize_indices_cb(void *ptr, IndexMask mask) +{ + mask.foreach_index([&](int64_t i) { new (static_cast(ptr) + i) T(); }); +} + template void destruct_cb(void *ptr) { (static_cast(ptr))->~T(); @@ -186,6 +196,8 @@ CPPType::CPPType(CPPTypeParam /* unused */, StringRef debug_name) if constexpr (std::is_default_constructible_v) { default_construct_ = default_construct_cb; default_construct_indices_ = default_construct_indices_cb; + value_initialize_ = value_initialize_cb; + value_initialize_indices_ = value_initialize_indices_cb; static T default_value; default_value_ = (void *)&default_value; } diff --git a/source/blender/blenlib/tests/BLI_cpp_type_test.cc b/source/blender/blenlib/tests/BLI_cpp_type_test.cc index 94456e1ee28..f00767eda8c 100644 --- a/source/blender/blenlib/tests/BLI_cpp_type_test.cc +++ b/source/blender/blenlib/tests/BLI_cpp_type_test.cc @@ -118,6 +118,40 @@ TEST(cpp_type, DefaultConstruction) EXPECT_EQ(buffer[8], 0); } +TEST(cpp_type, DefaultConstructTrivial) +{ + int value = 5; + CPPType::get().default_construct(&value); + EXPECT_EQ(value, 5); +} + +TEST(cpp_type, ValueInitialize) +{ + int buffer[10] = {0}; + CPPType_TestType.value_initialize((void *)buffer); + EXPECT_EQ(buffer[0], default_constructed_value); + EXPECT_EQ(buffer[1], 0); + CPPType_TestType.value_initialize_n((void *)buffer, 3); + EXPECT_EQ(buffer[0], default_constructed_value); + EXPECT_EQ(buffer[1], default_constructed_value); + EXPECT_EQ(buffer[2], default_constructed_value); + EXPECT_EQ(buffer[3], 0); + CPPType_TestType.value_initialize_indices((void *)buffer, {2, 5, 7}); + EXPECT_EQ(buffer[2], default_constructed_value); + EXPECT_EQ(buffer[4], 0); + EXPECT_EQ(buffer[5], default_constructed_value); + EXPECT_EQ(buffer[6], 0); + EXPECT_EQ(buffer[7], default_constructed_value); + EXPECT_EQ(buffer[8], 0); +} + +TEST(cpp_type, ValueInitializeTrivial) +{ + int value = 5; + CPPType::get().value_initialize(&value); + EXPECT_EQ(value, 0); +} + TEST(cpp_type, Destruct) { int buffer[10] = {0}; -- cgit v1.2.3