-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Smart Pointer Creation With Default Initialization #778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -254,6 +254,7 @@ tests\P0898R3_identity | |
| tests\P0919R3_heterogeneous_unordered_lookup | ||
| tests\P0966R1_string_reserve_should_not_shrink | ||
| tests\P1023R0_constexpr_for_array_comparisons | ||
| tests\P1020R1_smart_pointer_for_overwrite | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should appear in sorted order. |
||
| tests\P1165R1_consistently_propagating_stateful_allocators | ||
| tests\P1423R3_char8_t_remediation | ||
| tests\P1645R1_constexpr_numeric | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| RUNALL_INCLUDE ..\usual_latest_matrix.lst |
336 changes: 336 additions & 0 deletions
336
tests/std/tests/P1020R1_smart_pointer_for_overwrite/test.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,336 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include <assert.h> | ||
| #include <cstdlib> | ||
| #include <memory> | ||
| #include <new> | ||
| #include <stdexcept> | ||
| #include <type_traits> | ||
| #include <vector> | ||
|
|
||
| using namespace std; | ||
|
|
||
| int allocationCount = 0; | ||
| int canCreate = 10; // Counter to force an exception when constructing a | ||
| // sufficiently large ReportAddress array | ||
|
|
||
| struct ReportAddress; | ||
| vector<ReportAddress*> ascendingAddressBuffer; | ||
| vector<ReportAddress*> descendingAddressBuffer; | ||
|
|
||
| // According to N4849, the default behavior of operator new[](size) is to return | ||
| // operator new(size), so only the latter needs to be replaced. | ||
| void* operator new(size_t size) { | ||
| void* const p = ::operator new(size, nothrow); | ||
|
|
||
| if (p) { | ||
| return p; | ||
| } else { | ||
| throw bad_alloc(); | ||
| } | ||
| } | ||
|
|
||
| void* operator new(size_t size, const nothrow_t&) noexcept { | ||
| void* const result = malloc(size == 0 ? 1 : size); | ||
| ++allocationCount; | ||
| if (result) { | ||
| memset(result, 0xEE, size); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| void* operator new(size_t size, align_val_t align) { | ||
| void* const p = ::operator new(size, align, nothrow); | ||
|
|
||
| if (p) { | ||
| return p; | ||
| } else { | ||
| throw bad_alloc(); | ||
| } | ||
| } | ||
|
|
||
| void* operator new(size_t size, align_val_t align, const nothrow_t&) noexcept { | ||
| void* const result = ::_aligned_malloc(size, static_cast<size_t>(align)); | ||
| ++allocationCount; | ||
| if (result) { | ||
| memset(result, 0xEE, size); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| // Helper struct to check if type T is default initable without arguments. | ||
| template <typename T, typename = void> | ||
| struct unique_is_for_overwritable : false_type {}; | ||
|
|
||
| template <typename T> | ||
| struct unique_is_for_overwritable<T, void_t<decltype(make_unique_for_overwrite<T>())>> : true_type {}; | ||
|
|
||
| template <typename T> | ||
| constexpr bool unique_is_for_overwritable_v = unique_is_for_overwritable<T>::value; | ||
|
|
||
| struct DefaultInitableInt { | ||
| int value; | ||
| DefaultInitableInt() : value(106) {} | ||
| }; | ||
|
|
||
| struct alignas(32) HighlyAligned { | ||
| uint64_t a; | ||
| uint64_t b; | ||
| uint64_t c; | ||
| uint64_t d; | ||
| }; | ||
|
|
||
| struct ReportAddress { | ||
| ReportAddress() { | ||
| if (canCreate > 0) { | ||
| ascendingAddressBuffer.push_back(this); | ||
| --canCreate; | ||
| } else { | ||
| throw runtime_error("Can't create more ReportAddress objects."); | ||
| } | ||
| } | ||
|
|
||
| ~ReportAddress() { | ||
| ++canCreate; | ||
| descendingAddressBuffer.push_back(this); | ||
| } | ||
| }; | ||
|
|
||
| void assert_ascending_init() { | ||
| for (size_t i = 1; i < ascendingAddressBuffer.size(); ++i) { | ||
| assert(ascendingAddressBuffer[i - 1] < ascendingAddressBuffer[i]); | ||
| } | ||
|
|
||
| ascendingAddressBuffer.clear(); | ||
| } | ||
|
|
||
| void assert_descending_destruct() { | ||
| for (size_t i = 1; i < descendingAddressBuffer.size(); ++i) { | ||
| assert(descendingAddressBuffer[i - 1] > descendingAddressBuffer[i]); | ||
| } | ||
|
|
||
| descendingAddressBuffer.clear(); | ||
| } | ||
|
|
||
| void assert_uninitialized(void* p, size_t size) { | ||
| unsigned char* chPtr = reinterpret_cast<unsigned char*>(p); | ||
| for (unsigned int offset = 0; offset < size; ++offset) { | ||
| assert(*(chPtr + offset) == 0xEE); | ||
| } | ||
| } | ||
|
|
||
| template <class T> | ||
| void assert_shared_use_get(const shared_ptr<T>& sp) { | ||
| assert(sp.use_count() == 1); | ||
| assert(sp.get() != nullptr); | ||
| } | ||
|
|
||
| template <class T, class... Args> | ||
| shared_ptr<T> make_shared_for_overwrite_assert(Args&&... vals) { | ||
| int count = allocationCount; | ||
| shared_ptr<T> sp = make_shared_for_overwrite<T>(forward<Args>(vals)...); | ||
| assert_shared_use_get(sp); | ||
| assert(count + 1 == allocationCount); | ||
| return sp; | ||
| } | ||
|
|
||
| template <class T, class... Args> | ||
| void test_make_init_destruct_order(Args&&... vals) { | ||
| try { | ||
| shared_ptr<T> sp = make_shared_for_overwrite<T>(forward<Args>(vals)...); | ||
| assert_shared_use_get(sp); | ||
| } catch (const runtime_error& exc) { | ||
| assert(exc.what() == "Can't create more ReportAddress objects."sv); | ||
| } | ||
|
|
||
| assert_ascending_init(); | ||
| assert_descending_destruct(); | ||
| } | ||
|
|
||
| void test_make_unique_for_overwrite() { | ||
| static_assert(unique_is_for_overwritable_v<char>); | ||
| static_assert(!unique_is_for_overwritable_v<int[100]>); | ||
|
|
||
| auto p0 = make_unique_for_overwrite<int>(); | ||
| assert_uninitialized(addressof(*p0), sizeof(int)); | ||
|
|
||
| auto p1 = make_unique_for_overwrite<int[]>(100u); | ||
| assert_uninitialized(addressof(p1[0]), sizeof(int) * 100u); | ||
|
|
||
| auto p2 = make_unique_for_overwrite<DefaultInitableInt>(); | ||
| assert(p2->value == 106); | ||
|
|
||
| auto p3 = make_unique_for_overwrite<DefaultInitableInt[][89]>(2u); | ||
| for (int i = 0; i < 2; ++i) { | ||
| for (int j = 0; j < 89; ++j) { | ||
| assert(p3[i][j].value == 106); | ||
| } | ||
| } | ||
|
|
||
| auto p4 = make_unique_for_overwrite<int[]>(0u); // p4 cannot be dereferenced | ||
| } | ||
|
|
||
| void test_make_shared_for_overwrite() { | ||
| auto p0 = make_shared_for_overwrite_assert<int>(); | ||
| assert_uninitialized(addressof(*p0), sizeof(int)); | ||
|
|
||
| auto p1 = make_shared_for_overwrite_assert<DefaultInitableInt>(); | ||
| assert(p1->value == 106); | ||
|
|
||
| auto p2 = make_shared_for_overwrite_assert<HighlyAligned>(); | ||
| assert(reinterpret_cast<uintptr_t>(p2.get()) % alignof(HighlyAligned) == 0); | ||
| assert_uninitialized(addressof(*p2), sizeof(HighlyAligned)); | ||
|
|
||
| auto p3 = make_shared_for_overwrite_assert<int[100]>(); | ||
| assert_uninitialized(addressof(p3[0]), sizeof(int) * 100u); | ||
|
|
||
| auto p4 = make_shared_for_overwrite_assert<DefaultInitableInt[2][8]>(); | ||
| for (int i = 0; i < 2; ++i) { | ||
| for (int j = 0; j < 8; ++j) { | ||
| assert(p4[i][j].value == 106); | ||
| } | ||
| } | ||
|
|
||
| auto p5 = make_shared_for_overwrite_assert<HighlyAligned[10]>(); | ||
| assert(reinterpret_cast<uintptr_t>(p5.get()) % alignof(HighlyAligned) == 0); | ||
| assert_uninitialized(addressof(p5[0]), sizeof(HighlyAligned) * 10u); | ||
|
|
||
| auto p6 = make_shared_for_overwrite_assert<DefaultInitableInt[]>(100u); | ||
| for (int i = 0; i < 100; ++i) { | ||
| assert(p6[i].value == 106); | ||
| } | ||
|
|
||
| auto p7 = make_shared_for_overwrite_assert<DefaultInitableInt[][8][9]>(2u); | ||
| for (int i = 0; i < 2; ++i) { | ||
| for (int j = 0; j < 8; ++j) { | ||
| for (int k = 0; k < 9; ++k) { | ||
| assert(p7[i][j][k].value == 106); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| auto p8 = make_shared_for_overwrite_assert<int[]>(100u); | ||
| assert_uninitialized(addressof(p8[0]), sizeof(int) * 100u); | ||
|
|
||
| auto p9 = make_shared_for_overwrite_assert<int[]>(0u); // p9 cannot be dereferenced | ||
|
|
||
| auto p10 = make_shared_for_overwrite_assert<HighlyAligned[]>(10u); | ||
| assert(reinterpret_cast<uintptr_t>(p10.get()) % alignof(HighlyAligned) == 0); | ||
| assert_uninitialized(addressof(p10[0]), sizeof(HighlyAligned) * 10u); | ||
|
|
||
| test_make_init_destruct_order<ReportAddress[5]>(); // success one dimensional | ||
|
|
||
| test_make_init_destruct_order<ReportAddress[20]>(); // failure one dimensional | ||
|
|
||
| test_make_init_destruct_order<ReportAddress[2][2][2]>(); // success multidimensional | ||
|
|
||
| test_make_init_destruct_order<ReportAddress[3][3][3]>(); // failure multidimensional | ||
|
|
||
| test_make_init_destruct_order<ReportAddress[]>(5u); // success one dimensional | ||
|
|
||
| test_make_init_destruct_order<ReportAddress[]>(20u); // failure one dimensional | ||
|
|
||
| test_make_init_destruct_order<ReportAddress[][2][2]>(2u); // success multidimensional | ||
|
|
||
| test_make_init_destruct_order<ReportAddress[][3][3]>(3u); // failure multidimensional | ||
| } | ||
|
|
||
| template <class T, class... Args> | ||
| shared_ptr<T> allocate_shared_for_overwrite_assert(Args&&... vals) { | ||
| int aCount = allocationCount; | ||
| shared_ptr<T> sp = allocate_shared_for_overwrite<T>(forward<Args>(vals)...); | ||
| assert_shared_use_get(sp); | ||
| assert(aCount + 1 == allocationCount); | ||
| return sp; | ||
| } | ||
|
|
||
| template <class T, class... Args> | ||
| void test_allocate_init_destruct_order(Args&&... vals) { | ||
| allocator<remove_all_extents_t<T>> a{}; | ||
|
|
||
| try { | ||
| shared_ptr<T> sp = allocate_shared_for_overwrite<T>(a, forward<Args>(vals)...); | ||
| assert_shared_use_get(sp); | ||
| } catch (const runtime_error& exc) { | ||
| assert(exc.what() == "Can't create more ReportAddress objects."sv); | ||
| } | ||
|
|
||
| assert_ascending_init(); | ||
| assert_descending_destruct(); | ||
| } | ||
|
|
||
| void test_allocate_shared_for_overwrite() { | ||
| allocator<int> a0{}; | ||
| auto p0 = allocate_shared_for_overwrite_assert<int>(a0); | ||
| assert_uninitialized(addressof(*p0), sizeof(int)); | ||
|
|
||
| allocator<DefaultInitableInt> a1{}; | ||
| auto p1 = allocate_shared_for_overwrite_assert<DefaultInitableInt>(a1); | ||
| assert(p1->value == 106); | ||
|
|
||
| allocator<HighlyAligned> a2{}; | ||
| auto p2 = allocate_shared_for_overwrite_assert<HighlyAligned>(a2); | ||
| assert_uninitialized(addressof(*p2), sizeof(HighlyAligned)); | ||
|
|
||
| auto p3 = allocate_shared_for_overwrite_assert<int[100]>(a0); | ||
| assert_uninitialized(addressof(p3[0]), sizeof(int) * 100u); | ||
|
|
||
| auto p4 = allocate_shared_for_overwrite_assert<DefaultInitableInt[2][8]>(a1); | ||
| for (int i = 0; i < 2; ++i) { | ||
| for (int j = 0; j < 8; ++j) { | ||
| assert(p4[i][j].value == 106); | ||
| } | ||
| } | ||
|
|
||
| auto p5 = allocate_shared_for_overwrite_assert<HighlyAligned[10]>(a2); | ||
| assert(reinterpret_cast<uintptr_t>(p5.get()) % alignof(HighlyAligned) == 0); | ||
| assert_uninitialized(addressof(p5[0]), sizeof(HighlyAligned) * 10u); | ||
|
|
||
| auto p6 = allocate_shared_for_overwrite_assert<DefaultInitableInt[]>(a1, 100u); | ||
| for (int i = 0; i < 100; ++i) { | ||
| assert(p6[i].value == 106); | ||
| } | ||
|
|
||
| auto p7 = allocate_shared_for_overwrite_assert<DefaultInitableInt[][8][9]>(a1, 2u); | ||
| for (int i = 0; i < 2; ++i) { | ||
| for (int j = 0; j < 8; ++j) { | ||
| for (int k = 0; k < 9; ++k) { | ||
| assert(p7[i][j][k].value == 106); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| auto p8 = allocate_shared_for_overwrite_assert<int[]>(a0, 100u); | ||
| assert_uninitialized(addressof(p8[0]), sizeof(int) * 100u); | ||
|
|
||
| auto p9 = allocate_shared_for_overwrite_assert<int[]>(a0, 0u); // p9 cannot be dereferenced | ||
|
|
||
| auto p10 = allocate_shared_for_overwrite_assert<HighlyAligned[]>(a2, 10u); | ||
| assert(reinterpret_cast<uintptr_t>(p10.get()) % alignof(HighlyAligned) == 0); | ||
| assert_uninitialized(addressof(p10[0]), sizeof(HighlyAligned) * 10u); | ||
|
|
||
| test_allocate_init_destruct_order<ReportAddress[5]>(); // success one dimensional | ||
|
|
||
| test_allocate_init_destruct_order<ReportAddress[20]>(); // failure one dimensional | ||
|
|
||
| test_allocate_init_destruct_order<ReportAddress[2][2][2]>(); // success multidimensional | ||
|
|
||
| test_allocate_init_destruct_order<ReportAddress[3][3][3]>(); // failure multidimensional | ||
|
|
||
| test_allocate_init_destruct_order<ReportAddress[]>(5u); // success one dimensional | ||
|
|
||
| test_allocate_init_destruct_order<ReportAddress[]>(20u); // failure one dimensional | ||
|
|
||
| test_allocate_init_destruct_order<ReportAddress[][2][2]>(2u); // success multidimensional | ||
|
|
||
| test_allocate_init_destruct_order<ReportAddress[][3][3]>(3u); // failure multidimensional | ||
| } | ||
|
|
||
| int main() { | ||
| test_make_unique_for_overwrite(); | ||
| test_make_shared_for_overwrite(); | ||
| test_allocate_shared_for_overwrite(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feature should also be mentioned in the list of
_HAS_CXX20features at the top ofyvals_core.h.