blob: 3125de793b0b4d6e28716d92210375d3908c0a58 (
plain)
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
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Examples</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../align.html" title="Chapter 2. Boost.Align">
<link rel="prev" href="tutorial.html" title="Tutorial">
<link rel="next" href="reference.html" title="Reference">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="tutorial.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../align.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="reference.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="align.examples"></a><a class="link" href="examples.html" title="Examples">Examples</a>
</h2></div></div></div>
<div class="toc"><dl>
<dt><span class="section"><a href="examples.html#align.examples.aligned_ptr">aligned_ptr and make_aligned</a></span></dt>
<dt><span class="section"><a href="examples.html#align.examples.aligned_vector">aligned_vector</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="align.examples.aligned_ptr"></a><a class="link" href="examples.html#align.examples.aligned_ptr" title="aligned_ptr and make_aligned">aligned_ptr and make_aligned</a>
</h3></div></div></div>
<p>
This example presents an alternative to <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_ptr</span></code>
for objects allocated with the aligned allocation function. It is defined
simply by providing an alias template which uses <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_ptr</span></code>
with our aligned deleter in place of the default <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">default_delete</span></code>
deleter. It also presents an alternative to <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">make_unique</span></code>
for the creation of these aligned unique pointer objects. It is implemented
using our aligned allocation function.
</p>
<h5>
<a name="align.examples.aligned_ptr.h0"></a>
<span><a name="align.examples.aligned_ptr.implementation"></a></span><a class="link" href="examples.html#align.examples.aligned_ptr.implementation">Implementation</a>
</h5>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">align</span><span class="special">/</span><span class="identifier">aligned_alloc</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">align</span><span class="special">/</span><span class="identifier">aligned_delete</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">align</span><span class="special">/</span><span class="identifier">alignment_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">memory</span><span class="special">></span>
<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span>
<span class="keyword">using</span> <span class="identifier">aligned_ptr</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_ptr</span><span class="special"><</span><span class="identifier">T</span><span class="special">,</span>
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">alignment</span><span class="special">::</span><span class="identifier">aligned_delete</span><span class="special">>;</span>
<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span><span class="special">...</span> <span class="identifier">Args</span><span class="special">></span>
<span class="keyword">inline</span> <span class="identifier">aligned_ptr</span><span class="special"><</span><span class="identifier">T</span><span class="special">></span> <span class="identifier">make_aligned</span><span class="special">(</span><span class="identifier">Args</span><span class="special">&&...</span> <span class="identifier">args</span><span class="special">)</span>
<span class="special">{</span>
<span class="keyword">auto</span> <span class="identifier">p</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">alignment</span><span class="special">::</span><span class="identifier">aligned_alloc</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span>
<span class="identifier">alignment</span><span class="special">::</span><span class="identifier">alignment_of</span><span class="special"><</span><span class="identifier">T</span><span class="special">>::</span><span class="identifier">value</span><span class="special">,</span> <span class="keyword">sizeof</span><span class="special">(</span><span class="identifier">T</span><span class="special">));</span>
<span class="keyword">if</span> <span class="special">(!</span><span class="identifier">p</span><span class="special">)</span> <span class="special">{</span>
<span class="keyword">throw</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">bad_alloc</span><span class="special">();</span>
<span class="special">}</span>
<span class="keyword">try</span> <span class="special">{</span>
<span class="keyword">auto</span> <span class="identifier">q</span> <span class="special">=</span> <span class="special">::</span><span class="keyword">new</span><span class="special">(</span><span class="identifier">p</span><span class="special">)</span> <span class="identifier">T</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special"><</span><span class="identifier">Args</span><span class="special">>(</span><span class="identifier">args</span><span class="special">)...);</span>
<span class="keyword">return</span> <span class="identifier">aligned_ptr</span><span class="special"><</span><span class="identifier">T</span><span class="special">>(</span><span class="identifier">q</span><span class="special">);</span>
<span class="special">}</span> <span class="keyword">catch</span> <span class="special">(...)</span> <span class="special">{</span>
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">alignment</span><span class="special">::</span><span class="identifier">aligned_free</span><span class="special">(</span><span class="identifier">p</span><span class="special">);</span>
<span class="keyword">throw</span><span class="special">;</span>
<span class="special">}</span>
<span class="special">}</span>
</pre>
<h5>
<a name="align.examples.aligned_ptr.h1"></a>
<span><a name="align.examples.aligned_ptr.usage"></a></span><a class="link" href="examples.html#align.examples.aligned_ptr.usage">Usage</a>
</h5>
<pre class="programlisting"><span class="keyword">struct</span> <span class="keyword">alignas</span><span class="special">(</span><span class="number">16</span><span class="special">)</span> <span class="identifier">type</span> <span class="special">{</span>
<span class="keyword">float</span> <span class="identifier">data</span><span class="special">[</span><span class="number">4</span><span class="special">];</span>
<span class="special">};</span>
<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
<span class="keyword">auto</span> <span class="identifier">p</span> <span class="special">=</span> <span class="identifier">make_aligned</span><span class="special"><</span><span class="identifier">type</span><span class="special">>();</span>
<span class="identifier">p</span><span class="special">.</span><span class="identifier">reset</span><span class="special">();</span>
<span class="special">}</span>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="align.examples.aligned_vector"></a><a class="link" href="examples.html#align.examples.aligned_vector" title="aligned_vector">aligned_vector</a>
</h3></div></div></div>
<p>
This example presents an alternative to <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span></code>
that can be used with over-aligned types, and allows specifying a minimum
alignment. It is defined simply by providing an alias template which uses
<code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span></code> with our aligned allocator.
</p>
<h5>
<a name="align.examples.aligned_vector.h0"></a>
<span><a name="align.examples.aligned_vector.implementation"></a></span><a class="link" href="examples.html#align.examples.aligned_vector.implementation">Implementation</a>
</h5>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">align</span><span class="special">/</span><span class="identifier">aligned_allocator</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">vector</span><span class="special">></span>
<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">Alignment</span> <span class="special">=</span> <span class="number">1</span><span class="special">></span>
<span class="keyword">using</span> <span class="identifier">aligned_vector</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">T</span><span class="special">,</span>
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">alignment</span><span class="special">::</span><span class="identifier">aligned_allocator</span><span class="special"><</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">Alignment</span><span class="special">></span> <span class="special">>;</span>
</pre>
<h5>
<a name="align.examples.aligned_vector.h1"></a>
<span><a name="align.examples.aligned_vector.usage"></a></span><a class="link" href="examples.html#align.examples.aligned_vector.usage">Usage</a>
</h5>
<pre class="programlisting"><span class="keyword">enum</span>
<span class="special">:</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="special">{</span>
<span class="identifier">cache_line</span> <span class="special">=</span> <span class="number">64</span>
<span class="special">};</span>
<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
<span class="identifier">aligned_vector</span><span class="special"><</span><span class="keyword">char</span><span class="special">,</span> <span class="identifier">cache_line</span><span class="special">></span> <span class="identifier">v</span><span class="special">;</span>
<span class="identifier">v</span><span class="special">.</span><span class="identifier">emplace_back</span><span class="special">();</span>
<span class="special">}</span>
</pre>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2014, 2015 Glen Joseph Fernandes<p>
Distributed under the Boost Software License, Version 1.0.
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="tutorial.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../align.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="reference.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
|