-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathallocator_boilerplate.html
361 lines (313 loc) · 12.1 KB
/
allocator_boilerplate.html
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Allocator Boilerplate</title>
<style type="text/css">
p {text-align:justify;}
li {text-align:justify;}
blockquote.note
{
background-color:#E0E0E0;
padding-left: 15px;
padding-right: 15px;
padding-top: 1px;
padding-bottom: 1px;
}
ins {background-color:#A0FFA0;}
del {background-color:#FFA0A0;}
address {text-align:right;}
h1 {text-align:center;}
span.comment {color:#C80000;}
table {border-width:1px; border-color:black; border-style:solid;}
td {border-width:1px; border-color:black; border-style:solid; padding:5px;}
</style>
</head>
<body>
<address>
<br>
<a href="mailto:howard.hinnant@gmail.com">Howard Hinnant</a><br>
2016-08-23<br/>
<a rel="license" href="http://creativecommons.org/licenses/by/4.0/">
<img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by/4.0/80x15.png" /></a><br />
This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution 4.0 International License</a>.
</address>
<hr>
<h1>Allocator Boilerplate</h1>
<h2>Contents</h2>
<ul>
<li><a href="#Introduction">Introduction</a></li>
<li><a href="#new">C++11 and forward</a></li>
<li><a href="#old">C++03 and backward</a></li>
</ul>
<h2><a name="Introduction"></a>Introduction</h2>
<p>
This paper does not introduce any novel ideas. Nor is it a complete tutorial
on allocators. The only purpose of this paper is to be a quick go-to site
for copy/pasting the code you need to create your custom allocator. There is
nothing at all novel in the code presented here. Just copy/paste the
allocator skeleton here and insert <i>your own</i> novel code! Do not worry
about copyright issues on copy/pasting code out of this document. There is
no code here that is worthy of copyright. It is <i>all</i> just boilerplate.
The hope is that by lowering the barrier just a little bit, more people can
more easily create truly useful custom allocators.
</p>
<p>
In case it is helpful, two allocator skeletons are presented:
</p>
<ul>
<li><a href="#new">C++11 and forward</a></li>
<li><a href="#old">C++03 and backward</a></li>
</ul>
<p>
If you are using a compiler/library that has only a partial implementation
for C++11 allocator support, you can easily copy/paste what you need from
both of these skeletons thus creating a custom hybrid C++03/C++11 solution.
</p>
<h2><a name="new"></a>C++11 and forward</h2>
<p>
Below much of the skeleton is commented out. The commented out code represents
functionality that <code>std::allocator_traits<allocator<T>></code>
defaults for you, if you do not provide it. The implementation in the code shows
you exactly what the defaults are. Thus if you simply uncomment the code, you
will not get any functionality changes. To get something different from what
the defaults provide, uncomment and change the implementation.
</p>
<blockquote><pre>
template <class T>
class allocator
{
public:
using value_type = T;
<font color="#C80000">// using pointer = value_type*;</font>
<font color="#C80000">// using const_pointer = typename std::pointer_traits<pointer>::template</font>
<font color="#C80000">// rebind<value_type const>;</font>
<font color="#C80000">// using void_pointer = typename std::pointer_traits<pointer>::template</font>
<font color="#C80000">// rebind<void>;</font>
<font color="#C80000">// using const_void_pointer = typename std::pointer_traits<pointer>::template</font>
<font color="#C80000">// rebind<const void>;</font>
<font color="#C80000">// using difference_type = typename std::pointer_traits<pointer>::difference_type;</font>
<font color="#C80000">// using size_type = std::make_unsigned_t<difference_type>;</font>
<font color="#C80000">// template <class U> struct rebind {typedef allocator<U> other;};</font>
allocator() noexcept {} <font color="#C80000">// not required, unless used</font>
template <class U> allocator(allocator<U> const&) noexcept {}
value_type* <font color="#C80000">// Use pointer if pointer is not a value_type*</font>
allocate(std::size_t n)
{
return static_cast<value_type*>(::operator new (n*sizeof(value_type)));
}
void
deallocate(value_type* p, std::size_t) noexcept <font color="#C80000">// Use pointer if pointer is not a value_type*</font>
{
::operator delete(p);
}
<font color="#C80000">// value_type*</font>
<font color="#C80000">// allocate(std::size_t n, const_void_pointer)</font>
<font color="#C80000">// {</font>
<font color="#C80000">// return allocate(n);</font>
<font color="#C80000">// }</font>
<font color="#C80000">// template <class U, class ...Args></font>
<font color="#C80000">// void</font>
<font color="#C80000">// construct(U* p, Args&& ...args)</font>
<font color="#C80000">// {</font>
<font color="#C80000">// ::new(p) U(std::forward<Args>(args)...);</font>
<font color="#C80000">// }</font>
<font color="#C80000">// template <class U></font>
<font color="#C80000">// void</font>
<font color="#C80000">// destroy(U* p) noexcept</font>
<font color="#C80000">// {</font>
<font color="#C80000">// p->~U();</font>
<font color="#C80000">// }</font>
<font color="#C80000">// std::size_t</font>
<font color="#C80000">// max_size() const noexcept</font>
<font color="#C80000">// {</font>
<font color="#C80000">// return std::numeric_limits<size_type>::max();</font>
<font color="#C80000">// }</font>
<font color="#C80000">// allocator</font>
<font color="#C80000">// select_on_container_copy_construction() const</font>
<font color="#C80000">// {</font>
<font color="#C80000">// return *this;</font>
<font color="#C80000">// }</font>
<font color="#C80000">// using propagate_on_container_copy_assignment = std::false_type;</font>
<font color="#C80000">// using propagate_on_container_move_assignment = std::false_type;</font>
<font color="#C80000">// using propagate_on_container_swap = std::false_type;</font>
<font color="#C80000">// using is_always_equal = std::is_empty<allocator>;</font>
};
template <class T, class U>
bool
operator==(allocator<T> const&, allocator<U> const&) noexcept
{
return true;
}
template <class T, class U>
bool
operator!=(allocator<T> const& x, allocator<U> const& y) noexcept
{
return !(x == y);
}
</pre></blockquote>
<p>
So copy the above into your code. Change the name from <code>allocator</code>
to whatever makes sense for you. Delete all of the comments for which the
defaults provided by <code>std::allocator_traits</code> meet your needs. For
whatever is left, fill in your implementation.
</p>
<p>
<i>Notes:</i>
</p>
<ul>
<li><p>
<code>is_always_equal</code> is new for C++1y (hopefully that will be C++17).
As I write this paper, it is not likely to actually be used, is not standard,
and is subject to change.
</p></li>
<li><p>
The default implementation for <code>max_size()</code> is not incredibly
useful. I have submitted
<a href="http://cplusplus.github.io/LWG/lwg-active.html#2466">an LWG issue</a>
to change the default to:
</p>
<blockquote><pre>
return std::numeric_limits<size_type>::max() / sizeof(value_type);
</pre></blockquote>
<p>
which makes a lot more sense when <code>sizeof(value_type) > 1</code>.
</p>
</li>
<li><p>
Under discussion is the possibility to remove the requirement that you
provide <code>operator==</code> and <code>operator!=</code> if
<code>is_always_equal{}</code> is <code>true</code>.
</p></li>
<li><p>
The nested types <code>reference</code> and <code>const_reference</code> are no
longer required in C++11 (as they were in C++03).
</p></li>
<li><p>
The member functions <code>address(reference)</code> and
<code>address(const_reference)</code> are no longer required in C++11 (as
they were in C++03).
</p></li>
<li><p>
Your allocator must be <code>CopyConstructible</code> and
<code>MoveConstructible</code>. If
<code>propagate_on_container_copy_assignment{}</code> is true, your
allocator must be <code>CopyAssignable</code>. If
<code>propagate_on_container_move_assignment{}</code> is true, your
allocator must be <code>MoveAssignable</code>. If
<code>propagate_on_container_swap{}</code> is true, your allocator must be
<code>Swappable</code>. If they exist, these operations should not propagate
an exception out. However they do not need to be marked with
<code>noexcept</code>. However I recommend marking them with
<code>noexcept</code> if the compiler does not implicitly do so, so that traits
such as <code>is_nothrow_copy_constructible<allocator<T>></code>
give the right answer.
</p></li>
<li><p>
If two allocators compare equal, that means that they can deallocate each
other's allocated pointers. If two instances of your allocators can't do
this, they must <b>not</b> compare equal to each other, else run time errors
will result. However copies, even converting copies, <i>are required</i> to
compare equal.
</p></li>
</ul>
<h2><a name="old"></a>C++03 and backward</h2>
<p>
Here is the C++98/C++03 allocator skeleton. In this case, there is no such
thing as <code>std::allocator_traits</code> and so nothing is defaulted for
you. You will know if you need anything from this as you will get
compile-time errors if your implementation is asking for it, and you don't
have it.
</p>
<blockquote><pre>
template <class T> class allocator;
template <>
class allocator<void>
{
public:
typedef void value_type;
typedef value_type* pointer;
typedef value_type const* const_pointer;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
template <class U>
struct rebind
{
typedef allocator<U> other;
};
};
template <class T>
class allocator
{
public:
typedef T value_type;
typedef value_type& reference;
typedef value_type const& const_reference;
typedef value_type* pointer;
typedef value_type const* const_pointer;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
template <class U>
struct rebind
{
typedef allocator<U> other;
};
allocator() throw() {} <font color="#C80000">// not required, unless used</font>
template <class U> allocator(allocator<U> const& u) throw() {}
pointer
allocate(size_type n, allocator<void>::const_pointer = 0)
{
return static_cast<pointer>(::operator new (n*sizeof(value_type)));
}
void
deallocate(pointer p, size_type)
{
::operator delete(p);
}
void
construct(pointer p, value_type const& val)
{
::new(p) value_type(val);
}
void
destroy(pointer p)
{
p->~value_type();
}
size_type
max_size() const throw()
{
return std::numeric_limits<size_type>::max() / sizeof(value_type);
}
pointer
address(reference x) const
{
return &x;
}
const_pointer
address(const_reference x) const
{
return &x;
}
};
template <class T, class U>
bool
operator==(allocator<T> const&, allocator<U> const&)
{
return true;
}
template <class T, class U>
bool
operator!=(allocator<T> const& x, allocator<U> const& y)
{
return !(x == y);
}
</pre></blockquote>
<p>
As is evident, there is a lot more boilerplate required in C++98/03 than in
C++11. Also C++98/03 allocators do not portably support pointer types that
are not <code>value_type*</code>. And support for allocators that do not
compare equal is not portable.
</p>
</body>
</html>