1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2<html> 3<head> 4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5<title>Examples</title> 6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css"> 7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> 9<link rel="up" href="../align.html" title="Chapter 3. Boost.Align"> 10<link rel="prev" href="rationale.html" title="Rationale"> 11<link rel="next" href="reference.html" title="Reference"> 12</head> 13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 14<table cellpadding="2" width="100%"><tr> 15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td> 16<td align="center"><a href="../../../index.html">Home</a></td> 17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td> 18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 20<td align="center"><a href="../../../more/index.htm">More</a></td> 21</tr></table> 22<hr> 23<div class="spirit-nav"> 24<a accesskey="p" href="rationale.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> 25</div> 26<div class="section"> 27<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 28<a name="align.examples"></a><a class="link" href="examples.html" title="Examples">Examples</a> 29</h2></div></div></div> 30<h4> 31<a name="align.examples.h0"></a> 32 <span class="phrase"><a name="align.examples.aligned_allocation"></a></span><a class="link" href="examples.html#align.examples.aligned_allocation">Aligned 33 allocation</a> 34 </h4> 35<p> 36 To dynamically allocate storage with desired alignment, you can use the <code class="computeroutput"><span class="identifier">aligned_alloc</span></code> function: 37 </p> 38<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"> 39 <code class="computeroutput"><span class="keyword">void</span><span class="special">*</span> 40 <span class="identifier">storage</span> <span class="special">=</span> 41 <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">alignment</span><span class="special">,</span> 42 <span class="identifier">size</span><span class="special">);</span></code> 43 </li></ol></div> 44<p> 45 To deallocate storage allocated with the <code class="computeroutput"><span class="identifier">aligned_alloc</span></code> 46 function, use the <code class="computeroutput"><span class="identifier">aligned_free</span></code> 47 function: 48 </p> 49<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"> 50 <code class="computeroutput"><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">storage</span><span class="special">);</span></code> 51 </li></ol></div> 52<h4> 53<a name="align.examples.h1"></a> 54 <span class="phrase"><a name="align.examples.aligned_allocator"></a></span><a class="link" href="examples.html#align.examples.aligned_allocator">Aligned 55 allocator</a> 56 </h4> 57<p> 58 For C++ allocator aware code, you can use the <code class="computeroutput"><span class="identifier">aligned_allocator</span></code> 59 class template for an allocator that respects over-alignment: 60 </p> 61<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"> 62 <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">int128_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">int128_t</span><span class="special">></span> 63 <span class="special">></span> <span class="identifier">vector</span><span class="special">;</span></code> 64 </li></ol></div> 65<p> 66 This template allows specifying minimum alignment for all dynamic allocations: 67 </p> 68<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"> 69 <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="keyword">double</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="keyword">double</span><span class="special">,</span> <span class="number">64</span><span class="special">></span> <span class="special">></span> 70 <span class="identifier">vector</span><span class="special">;</span></code> 71 </li></ol></div> 72<h4> 73<a name="align.examples.h2"></a> 74 <span class="phrase"><a name="align.examples.aligned_allocator_adaptor"></a></span><a class="link" href="examples.html#align.examples.aligned_allocator_adaptor">Aligned 75 allocator adaptor</a> 76 </h4> 77<p> 78 To turn an allocator into an allocator that respects over-alignment, you can 79 use the <code class="computeroutput"><span class="identifier">aligned_allocator_adaptor</span></code> 80 class template: 81 </p> 82<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"> 83 <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">alignment</span><span class="special">::</span><span class="identifier">aligned_allocator_adaptor</span><span class="special"><</span><span class="identifier">First</span><span class="special">></span> <span class="identifier">second</span><span class="special">(</span><span class="identifier">first</span><span class="special">);</span></code> 84 </li></ol></div> 85<p> 86 This template allows specifying minimum alignment for all dynamic allocations: 87 </p> 88<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"> 89 <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">alignment</span><span class="special">::</span><span class="identifier">aligned_allocator_adaptor</span><span class="special"><</span><span class="identifier">First</span><span class="special">,</span> <span class="number">64</span><span class="special">></span> <span class="identifier">second</span><span class="special">(</span><span class="identifier">first</span><span class="special">);</span></code> 90 </li></ol></div> 91<h4> 92<a name="align.examples.h3"></a> 93 <span class="phrase"><a name="align.examples.aligned_deleter"></a></span><a class="link" href="examples.html#align.examples.aligned_deleter">Aligned 94 deleter</a> 95 </h4> 96<p> 97 For a deleter that can be paired with <code class="computeroutput"><span class="identifier">aligned_alloc</span></code>, 98 you can use the <code class="computeroutput"><span class="identifier">aligned_delete</span></code> 99 class: 100 </p> 101<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"> 102 <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_ptr</span><span class="special"><</span><span class="keyword">double</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> 103 <span class="identifier">pointer</span><span class="special">;</span></code> 104 </li></ol></div> 105<h4> 106<a name="align.examples.h4"></a> 107 <span class="phrase"><a name="align.examples.pointer_alignment"></a></span><a class="link" href="examples.html#align.examples.pointer_alignment">Pointer 108 alignment</a> 109 </h4> 110<p> 111 To advance a pointer to the next address with the desired alignment: 112 </p> 113<div class="orderedlist"><ol class="orderedlist" type="1"> 114<li class="listitem"> 115 <code class="computeroutput"><span class="keyword">void</span><span class="special">*</span> 116 <span class="identifier">pointer</span> <span class="special">=</span> 117 <span class="identifier">storage</span><span class="special">;</span></code> 118 </li> 119<li class="listitem"> 120 <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">space</span> 121 <span class="special">=</span> <span class="identifier">size</span><span class="special">;</span></code> 122 </li> 123<li class="listitem"> 124 <code class="computeroutput"><span class="keyword">void</span><span class="special">*</span> 125 <span class="identifier">result</span> <span class="special">=</span> 126 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">alignment</span><span class="special">::</span><span class="identifier">align</span><span class="special">(</span><span class="number">64</span><span class="special">,</span> <span class="keyword">sizeof</span><span class="special">(</span><span class="keyword">double</span><span class="special">),</span> <span class="identifier">pointer</span><span class="special">,</span> <span class="identifier">space</span><span class="special">);</span></code> 127 </li> 128</ol></div> 129<h4> 130<a name="align.examples.h5"></a> 131 <span class="phrase"><a name="align.examples.querying_alignment"></a></span><a class="link" href="examples.html#align.examples.querying_alignment">Querying 132 alignment</a> 133 </h4> 134<p> 135 To obtain the alignment of a given type at compie time, you can use: 136 </p> 137<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"> 138 <code class="computeroutput"><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">int128_t</span><span class="special">>::</span><span class="identifier">value</span></code> 139 </li></ol></div> 140<p> 141 If your compiler supports C++14 variable templates, you can also use: 142 </p> 143<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"> 144 <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">alignment</span><span class="special">::</span><span class="identifier">alignment_of_v</span><span class="special"><</span><span class="identifier">int128_t</span><span class="special">></span></code> 145 </li></ol></div> 146<h4> 147<a name="align.examples.h6"></a> 148 <span class="phrase"><a name="align.examples.hinting_alignment"></a></span><a class="link" href="examples.html#align.examples.hinting_alignment">Hinting 149 alignment</a> 150 </h4> 151<p> 152 To inform the compiler about the alignment of a pointer, you can use: 153 </p> 154<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"> 155 <code class="computeroutput"><span class="identifier">BOOST_ALIGN_ASSUME_ALIGNED</span><span class="special">(</span><span class="identifier">pointer</span><span class="special">,</span> <span class="number">64</span><span class="special">)</span></code> 156 </li></ol></div> 157<h4> 158<a name="align.examples.h7"></a> 159 <span class="phrase"><a name="align.examples.checking_alignment"></a></span><a class="link" href="examples.html#align.examples.checking_alignment">Checking 160 alignment</a> 161 </h4> 162<p> 163 To check alignment of a pointer you can use the <code class="computeroutput"><span class="identifier">is_aligned</span></code> 164 function: 165 </p> 166<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"> 167 <code class="computeroutput"><span class="identifier">assert</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">is_aligned</span><span class="special">(</span><span class="identifier">pointer</span><span class="special">,</span> <span class="number">64</span><span class="special">));</span></code> 168 </li></ol></div> 169</div> 170<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 171<td align="left"></td> 172<td align="right"><div class="copyright-footer">Copyright © 2014-2017 Glen 173 Joseph Fernandes<p> 174 Distributed under the Boost Software License, Version 1.0. 175 </p> 176</div></td> 177</tr></table> 178<hr> 179<div class="spirit-nav"> 180<a accesskey="p" href="rationale.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> 181</div> 182</body> 183</html> 184