Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
247 changes: 211 additions & 36 deletions stl/inc/memory

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions stl/inc/xmemory
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,12 @@ void _Construct_in_place(_Ty& _Obj, _Types&&... _Args) noexcept(is_nothrow_const
_Ty(_STD forward<_Types>(_Args)...);
}

// FUNCTION TEMPLATE _Construct_in_place_for_overwrite
template <class _Ty>
void _Construct_in_place_for_overwrite(_Ty& _Obj) noexcept(is_nothrow_constructible_v<_Ty>) {
::new (const_cast<void*>(static_cast<const volatile void*>(_STD addressof(_Obj)))) _Ty;
}

// FUNCTION TEMPLATE _Global_new
template <class _Ty, class... _Types>
_Ty* _Global_new(_Types&&... _Args) { // acts as "new" while disallowing user overload selection
Expand Down
1 change: 1 addition & 0 deletions stl/inc/yvals_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,7 @@
#define __cpp_lib_math_constants 201907L
#define __cpp_lib_remove_cvref 201711L
#define __cpp_lib_shift 201806L
#define __cpp_lib_smart_ptr_for_overwrite 201811L
Copy link
Member

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_CXX20 features at the top of yvals_core.h.

#define __cpp_lib_span 202002L
#define __cpp_lib_ssize 201902L
#define __cpp_lib_starts_ends_with 201711L
Expand Down
1 change: 1 addition & 0 deletions tests/std/test.lst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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
Expand Down
4 changes: 4 additions & 0 deletions tests/std/tests/P1020R1_smart_pointer_for_overwrite/env.lst
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 tests/std/tests/P1020R1_smart_pointer_for_overwrite/test.cpp
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();
}
14 changes: 14 additions & 0 deletions tests/std/tests/VSO_0157762_feature_test_macros/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,20 @@ STATIC_ASSERT(__cpp_lib_shift == 201806L);
#endif
#endif

#if _HAS_CXX20
#ifndef __cpp_lib_smart_ptr_for_overwrite
#error __cpp_lib_smart_ptr_for_overwrite is not defined
#elif __cpp_lib_smart_ptr_for_overwrite != 201811L
#error __cpp_lib_smart_ptr_for_overwrite is not 201811L
#else
STATIC_ASSERT(__cpp_lib_smart_ptr_for_overwrite == 201811L);
#endif
#else
#ifdef __cpp_lib_smart_ptr_for_overwrite
#error __cpp_lib_smart_ptr_for_overwrite is defined
#endif
#endif

#if _HAS_CXX20
#ifndef __cpp_lib_span
#error __cpp_lib_span is not defined
Expand Down