boost/doc/html/align/examples.html
2021-10-05 21:37:46 +02:00

184 lines
14 KiB
HTML
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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=UTF-8">
<title>Examples</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../align.html" title="Chapter 3. Boost.Align">
<link rel="prev" href="rationale.html" title="Rationale">
<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="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>
</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>
<h4>
<a name="align.examples.h0"></a>
<span class="phrase"><a name="align.examples.aligned_allocation"></a></span><a class="link" href="examples.html#align.examples.aligned_allocation">Aligned
allocation</a>
</h4>
<p>
To dynamically allocate storage with desired alignment, you can use the <code class="computeroutput"><span class="identifier">aligned_alloc</span></code> function:
</p>
<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">
<code class="computeroutput"><span class="keyword">void</span><span class="special">*</span>
<span class="identifier">storage</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">alignment</span><span class="special">,</span>
<span class="identifier">size</span><span class="special">);</span></code>
</li></ol></div>
<p>
To deallocate storage allocated with the <code class="computeroutput"><span class="identifier">aligned_alloc</span></code>
function, use the <code class="computeroutput"><span class="identifier">aligned_free</span></code>
function:
</p>
<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">
<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>
</li></ol></div>
<h4>
<a name="align.examples.h1"></a>
<span class="phrase"><a name="align.examples.aligned_allocator"></a></span><a class="link" href="examples.html#align.examples.aligned_allocator">Aligned
allocator</a>
</h4>
<p>
For C++ allocator aware code, you can use the <code class="computeroutput"><span class="identifier">aligned_allocator</span></code>
class template for an allocator that respects over-alignment:
</p>
<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">
<code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</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">&lt;</span><span class="identifier">int128_t</span><span class="special">&gt;</span>
<span class="special">&gt;</span> <span class="identifier">vector</span><span class="special">;</span></code>
</li></ol></div>
<p>
This template allows specifying minimum alignment for all dynamic allocations:
</p>
<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">
<code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</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">&lt;</span><span class="keyword">double</span><span class="special">,</span> <span class="number">64</span><span class="special">&gt;</span> <span class="special">&gt;</span>
<span class="identifier">vector</span><span class="special">;</span></code>
</li></ol></div>
<h4>
<a name="align.examples.h2"></a>
<span class="phrase"><a name="align.examples.aligned_allocator_adaptor"></a></span><a class="link" href="examples.html#align.examples.aligned_allocator_adaptor">Aligned
allocator adaptor</a>
</h4>
<p>
To turn an allocator into an allocator that respects over-alignment, you can
use the <code class="computeroutput"><span class="identifier">aligned_allocator_adaptor</span></code>
class template:
</p>
<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">
<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">&lt;</span><span class="identifier">First</span><span class="special">&gt;</span> <span class="identifier">second</span><span class="special">(</span><span class="identifier">first</span><span class="special">);</span></code>
</li></ol></div>
<p>
This template allows specifying minimum alignment for all dynamic allocations:
</p>
<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">
<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">&lt;</span><span class="identifier">First</span><span class="special">,</span> <span class="number">64</span><span class="special">&gt;</span> <span class="identifier">second</span><span class="special">(</span><span class="identifier">first</span><span class="special">);</span></code>
</li></ol></div>
<h4>
<a name="align.examples.h3"></a>
<span class="phrase"><a name="align.examples.aligned_deleter"></a></span><a class="link" href="examples.html#align.examples.aligned_deleter">Aligned
deleter</a>
</h4>
<p>
For a deleter that can be paired with <code class="computeroutput"><span class="identifier">aligned_alloc</span></code>,
you can use the <code class="computeroutput"><span class="identifier">aligned_delete</span></code>
class:
</p>
<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">
<code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_ptr</span><span class="special">&lt;</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">&gt;</span>
<span class="identifier">pointer</span><span class="special">;</span></code>
</li></ol></div>
<h4>
<a name="align.examples.h4"></a>
<span class="phrase"><a name="align.examples.pointer_alignment"></a></span><a class="link" href="examples.html#align.examples.pointer_alignment">Pointer
alignment</a>
</h4>
<p>
To advance a pointer to the next address with the desired alignment:
</p>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">
<code class="computeroutput"><span class="keyword">void</span><span class="special">*</span>
<span class="identifier">pointer</span> <span class="special">=</span>
<span class="identifier">storage</span><span class="special">;</span></code>
</li>
<li class="listitem">
<code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">space</span>
<span class="special">=</span> <span class="identifier">size</span><span class="special">;</span></code>
</li>
<li class="listitem">
<code class="computeroutput"><span class="keyword">void</span><span class="special">*</span>
<span class="identifier">result</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">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>
</li>
</ol></div>
<h4>
<a name="align.examples.h5"></a>
<span class="phrase"><a name="align.examples.querying_alignment"></a></span><a class="link" href="examples.html#align.examples.querying_alignment">Querying
alignment</a>
</h4>
<p>
To obtain the alignment of a given type at compie time, you can use:
</p>
<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">
<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">&lt;</span><span class="identifier">int128_t</span><span class="special">&gt;::</span><span class="identifier">value</span></code>
</li></ol></div>
<p>
If your compiler supports C++14 variable templates, you can also use:
</p>
<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">
<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">&lt;</span><span class="identifier">int128_t</span><span class="special">&gt;</span></code>
</li></ol></div>
<h4>
<a name="align.examples.h6"></a>
<span class="phrase"><a name="align.examples.hinting_alignment"></a></span><a class="link" href="examples.html#align.examples.hinting_alignment">Hinting
alignment</a>
</h4>
<p>
To inform the compiler about the alignment of a pointer, you can use:
</p>
<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">
<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>
</li></ol></div>
<h4>
<a name="align.examples.h7"></a>
<span class="phrase"><a name="align.examples.checking_alignment"></a></span><a class="link" href="examples.html#align.examples.checking_alignment">Checking
alignment</a>
</h4>
<p>
To check alignment of a pointer you can use the <code class="computeroutput"><span class="identifier">is_aligned</span></code>
function:
</p>
<div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">
<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>
</li></ol></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-2017 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="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>
</div>
</body>
</html>