1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// <memory>
// To allow checking that self-move works correctly.
// ADDITIONAL_COMPILE_FLAGS: -Wno-self-move
// template<class _Alloc>
// struct __allocation_guard;
#include <__cxx03/__memory/allocation_guard.h>
#include <cassert>
#include <type_traits>
#include <utility>
#include "test_allocator.h"
using A = test_allocator<int>;
// A trimmed-down version of `test_allocator` that is copy-assignable (in general allocators don't have to support copy
// assignment).
template <class T>
struct AssignableAllocator {
using size_type = unsigned;
using difference_type = int;
using value_type = T;
using pointer = value_type*;
using const_pointer = const value_type*;
using reference = typename std::add_lvalue_reference<value_type>::type;
using const_reference = typename std::add_lvalue_reference<const value_type>::type;
template <class U>
struct rebind {
using other = test_allocator<U>;
};
test_allocator_statistics* stats_ = nullptr;
explicit AssignableAllocator(test_allocator_statistics& stats) : stats_(&stats) {
++stats_->count;
}
TEST_CONSTEXPR_CXX14 AssignableAllocator(const AssignableAllocator& rhs) TEST_NOEXCEPT
: stats_(rhs.stats_) {
if (stats_ != nullptr) {
++stats_->count;
++stats_->copied;
}
}
TEST_CONSTEXPR_CXX14 AssignableAllocator& operator=(const AssignableAllocator& rhs) TEST_NOEXCEPT {
stats_ = rhs.stats_;
if (stats_ != nullptr) {
++stats_->count;
++stats_->copied;
}
return *this;
}
TEST_CONSTEXPR_CXX14 pointer allocate(size_type n, const void* = nullptr) {
if (stats_ != nullptr) {
++stats_->alloc_count;
}
return std::allocator<value_type>().allocate(n);
}
TEST_CONSTEXPR_CXX14 void deallocate(pointer p, size_type s) {
if (stats_ != nullptr) {
--stats_->alloc_count;
}
std::allocator<value_type>().deallocate(p, s);
}
TEST_CONSTEXPR size_type max_size() const TEST_NOEXCEPT { return UINT_MAX / sizeof(T); }
template <class U>
TEST_CONSTEXPR_CXX20 void construct(pointer p, U&& val) {
if (stats_ != nullptr)
++stats_->construct_count;
#if TEST_STD_VER > 17
std::construct_at(std::to_address(p), std::forward<U>(val));
#else
::new (static_cast<void*>(p)) T(std::forward<U>(val));
#endif
}
TEST_CONSTEXPR_CXX14 void destroy(pointer p) {
if (stats_ != nullptr) {
++stats_->destroy_count;
}
p->~T();
}
};
// Move-only.
static_assert(!std::is_copy_constructible<std::__allocation_guard<A> >::value, "");
static_assert(std::is_move_constructible<std::__allocation_guard<A> >::value, "");
static_assert(!std::is_copy_assignable<std::__allocation_guard<A> >::value, "");
static_assert(std::is_move_assignable<std::__allocation_guard<A> >::value, "");
int main(int, char**) {
const int size = 42;
{ // The constructor allocates using the given allocator.
test_allocator_statistics stats;
std::__allocation_guard<A> guard(A(&stats), size);
assert(stats.alloc_count == 1);
assert(guard.__get() != nullptr);
}
{ // The destructor deallocates using the given allocator.
test_allocator_statistics stats;
{
std::__allocation_guard<A> guard(A(&stats), size);
assert(stats.alloc_count == 1);
}
assert(stats.alloc_count == 0);
}
{ // `__release_ptr` prevents deallocation.
test_allocator_statistics stats;
A alloc(&stats);
int* ptr = nullptr;
{
std::__allocation_guard<A> guard(alloc, size);
assert(stats.alloc_count == 1);
ptr = guard.__release_ptr();
}
assert(stats.alloc_count == 1);
alloc.deallocate(ptr, size);
}
{ // Using the move constructor doesn't lead to double deletion.
test_allocator_statistics stats;
{
std::__allocation_guard<A> guard1(A(&stats), size);
assert(stats.alloc_count == 1);
auto* ptr1 = guard1.__get();
std::__allocation_guard<A> guard2 = std::move(guard1);
assert(stats.alloc_count == 1);
assert(guard1.__get() == nullptr);
assert(guard2.__get() == ptr1);
}
assert(stats.alloc_count == 0);
}
{ // Using the move assignment operator doesn't lead to double deletion.
using A2 = AssignableAllocator<int>;
test_allocator_statistics stats;
{
std::__allocation_guard<A2> guard1(A2(stats), size);
assert(stats.alloc_count == 1);
std::__allocation_guard<A2> guard2(A2(stats), size);
assert(stats.alloc_count == 2);
auto* ptr1 = guard1.__get();
guard2 = std::move(guard1);
assert(stats.alloc_count == 1);
assert(guard1.__get() == nullptr);
assert(guard2.__get() == ptr1);
}
assert(stats.alloc_count == 0);
}
{ // Self-assignment is a no-op.
using A2 = AssignableAllocator<int>;
test_allocator_statistics stats;
{
std::__allocation_guard<A2> guard(A2(stats), size);
assert(stats.alloc_count == 1);
auto* ptr = guard.__get();
guard = std::move(guard);
assert(stats.alloc_count == 1);
assert(guard.__get() == ptr);
}
assert(stats.alloc_count == 0);
}
return 0;
}
|