-
Notifications
You must be signed in to change notification settings - Fork 13.3k
/
Copy pathallocators.h
256 lines (202 loc) · 6.74 KB
/
allocators.h
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#ifndef ALLOCATORS_H
#define ALLOCATORS_H
#include <cstddef>
#include <memory>
#include <new>
#include <type_traits>
#include <utility>
#include "test_macros.h"
#if TEST_STD_VER >= 11
template <class T>
class A1
{
int id_;
public:
explicit A1(int id = 0) TEST_NOEXCEPT : id_(id) {}
typedef T value_type;
int id() const {return id_;}
static bool copy_called;
static bool move_called;
static bool allocate_called;
static std::pair<T*, std::size_t> deallocate_called;
A1(const A1& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
A1(A1&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;}
A1& operator=(const A1& a) TEST_NOEXCEPT { id_ = a.id(); copy_called = true; return *this;}
A1& operator=(A1&& a) TEST_NOEXCEPT { id_ = a.id(); move_called = true; return *this;}
template <class U>
A1(const A1<U>& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
template <class U>
A1(A1<U>&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;}
T* allocate(std::size_t n)
{
allocate_called = true;
return (T*)n;
}
void deallocate(T* p, std::size_t n)
{
deallocate_called = std::pair<T*, std::size_t>(p, n);
}
std::size_t max_size() const {return id_;}
};
template <class T> bool A1<T>::copy_called = false;
template <class T> bool A1<T>::move_called = false;
template <class T> bool A1<T>::allocate_called = false;
template <class T> std::pair<T*, std::size_t> A1<T>::deallocate_called;
template <class T, class U>
inline
bool operator==(const A1<T>& x, const A1<U>& y)
{
return x.id() == y.id();
}
template <class T, class U>
inline
bool operator!=(const A1<T>& x, const A1<U>& y)
{
return !(x == y);
}
template <class T>
class A2
{
int id_;
public:
explicit A2(int id = 0) TEST_NOEXCEPT : id_(id) {}
typedef T value_type;
typedef unsigned size_type;
typedef int difference_type;
typedef std::true_type propagate_on_container_move_assignment;
int id() const {return id_;}
static bool copy_called;
static bool move_called;
static bool allocate_called;
A2(const A2& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
A2(A2&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;}
A2& operator=(const A2& a) TEST_NOEXCEPT { id_ = a.id(); copy_called = true; return *this;}
A2& operator=(A2&& a) TEST_NOEXCEPT { id_ = a.id(); move_called = true; return *this;}
T* allocate(std::size_t, const void* hint)
{
allocate_called = true;
return (T*) const_cast<void *>(hint);
}
};
template <class T> bool A2<T>::copy_called = false;
template <class T> bool A2<T>::move_called = false;
template <class T> bool A2<T>::allocate_called = false;
template <class T, class U>
inline
bool operator==(const A2<T>& x, const A2<U>& y)
{
return x.id() == y.id();
}
template <class T, class U>
inline
bool operator!=(const A2<T>& x, const A2<U>& y)
{
return !(x == y);
}
template <class T>
class A3
{
int id_;
public:
explicit A3(int id = 0) TEST_NOEXCEPT : id_(id) {}
typedef T value_type;
typedef std::true_type propagate_on_container_copy_assignment;
typedef std::true_type propagate_on_container_swap;
int id() const {return id_;}
static bool copy_called;
static bool move_called;
static bool constructed;
static bool destroy_called;
A3(const A3& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;}
A3(A3&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;}
A3& operator=(const A3& a) TEST_NOEXCEPT { id_ = a.id(); copy_called = true; return *this;}
A3& operator=(A3&& a) TEST_NOEXCEPT { id_ = a.id(); move_called = true; return *this;}
template <class U, class ...Args>
void construct(U* p, Args&& ...args)
{
::new (p) U(std::forward<Args>(args)...);
constructed = true;
}
template <class U>
void destroy(U* p)
{
p->~U();
destroy_called = true;
}
A3 select_on_container_copy_construction() const {return A3(-1);}
};
template <class T> bool A3<T>::copy_called = false;
template <class T> bool A3<T>::move_called = false;
template <class T> bool A3<T>::constructed = false;
template <class T> bool A3<T>::destroy_called = false;
template <class T, class U>
inline
bool operator==(const A3<T>& x, const A3<U>& y)
{
return x.id() == y.id();
}
template <class T, class U>
inline
bool operator!=(const A3<T>& x, const A3<U>& y)
{
return !(x == y);
}
template <class T, bool POCCAValue>
class MaybePOCCAAllocator {
int id_ = 0;
bool* copy_assigned_into_ = nullptr;
template<class, bool> friend class MaybePOCCAAllocator;
public:
typedef std::integral_constant<bool, POCCAValue> propagate_on_container_copy_assignment;
typedef T value_type;
template <class U>
struct rebind {
typedef MaybePOCCAAllocator<U, POCCAValue> other;
};
TEST_CONSTEXPR MaybePOCCAAllocator() = default;
TEST_CONSTEXPR MaybePOCCAAllocator(int id, bool* copy_assigned_into)
: id_(id), copy_assigned_into_(copy_assigned_into) {}
template <class U>
TEST_CONSTEXPR MaybePOCCAAllocator(const MaybePOCCAAllocator<U, POCCAValue>& that)
: id_(that.id_), copy_assigned_into_(that.copy_assigned_into_) {}
MaybePOCCAAllocator(const MaybePOCCAAllocator&) = default;
TEST_CONSTEXPR_CXX14 MaybePOCCAAllocator& operator=(const MaybePOCCAAllocator& a)
{
id_ = a.id();
if (copy_assigned_into_)
*copy_assigned_into_ = true;
return *this;
}
TEST_CONSTEXPR_CXX20 T* allocate(std::size_t n)
{
return std::allocator<T>().allocate(n);
}
TEST_CONSTEXPR_CXX20 void deallocate(T* ptr, std::size_t n)
{
std::allocator<T>().deallocate(ptr, n);
}
TEST_CONSTEXPR int id() const { return id_; }
template <class U>
TEST_CONSTEXPR friend bool operator==(const MaybePOCCAAllocator& lhs, const MaybePOCCAAllocator<U, POCCAValue>& rhs)
{
return lhs.id() == rhs.id();
}
template <class U>
TEST_CONSTEXPR friend bool operator!=(const MaybePOCCAAllocator& lhs, const MaybePOCCAAllocator<U, POCCAValue>& rhs)
{
return !(lhs == rhs);
}
};
template <class T>
using POCCAAllocator = MaybePOCCAAllocator<T, /*POCCAValue = */true>;
template <class T>
using NonPOCCAAllocator = MaybePOCCAAllocator<T, /*POCCAValue = */false>;
#endif // TEST_STD_VER >= 11
#endif // ALLOCATORS_H