• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>Allocators, containers and memory allocation algorithms</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="../interprocess.html" title="Chapter 18. Boost.Interprocess">
10<link rel="prev" href="managed_memory_segments.html" title="Managed Memory Segments">
11<link rel="next" href="memory_algorithms.html" title="Memory allocation algorithms">
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="managed_memory_segments.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../interprocess.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="memory_algorithms.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="interprocess.allocators_containers"></a><a class="link" href="allocators_containers.html" title="Allocators, containers and memory allocation algorithms">Allocators, containers
29    and memory allocation algorithms</a>
30</h2></div></div></div>
31<div class="toc"><dl class="toc">
32<dt><span class="section"><a href="allocators_containers.html#interprocess.allocators_containers.allocator_introduction">Introduction
33      to Interprocess allocators</a></span></dt>
34<dt><span class="section"><a href="allocators_containers.html#interprocess.allocators_containers.stl_allocators_segregated_storage">Segregated
35      storage node allocators</a></span></dt>
36<dt><span class="section"><a href="allocators_containers.html#interprocess.allocators_containers.stl_allocators_adaptive">Adaptive
37      pool node allocators</a></span></dt>
38<dt><span class="section"><a href="allocators_containers.html#interprocess.allocators_containers.containers_explained">Interprocess
39      and containers in managed memory segments</a></span></dt>
40<dt><span class="section"><a href="allocators_containers.html#interprocess.allocators_containers.additional_containers">Boost
41      containers compatible with Boost.Interprocess</a></span></dt>
42</dl></div>
43<div class="section">
44<div class="titlepage"><div><div><h3 class="title">
45<a name="interprocess.allocators_containers.allocator_introduction"></a><a class="link" href="allocators_containers.html#interprocess.allocators_containers.allocator_introduction" title="Introduction to Interprocess allocators">Introduction
46      to Interprocess allocators</a>
47</h3></div></div></div>
48<div class="toc"><dl class="toc">
49<dt><span class="section"><a href="allocators_containers.html#interprocess.allocators_containers.allocator_introduction.allocator_properties">Properties
50        of <span class="bold"><strong>Boost.Interprocess</strong></span> allocators</a></span></dt>
51<dt><span class="section"><a href="allocators_containers.html#interprocess.allocators_containers.allocator_introduction.allocator_swapping">Swapping
52        Boost.Interprocess allocators</a></span></dt>
53<dt><span class="section"><a href="allocators_containers.html#interprocess.allocators_containers.allocator_introduction.allocator">allocator:
54        A general purpose allocator for managed memory segments</a></span></dt>
55</dl></div>
56<p>
57        As seen, <span class="bold"><strong>Boost.Interprocess</strong></span> offers raw memory
58        allocation and object construction using managed memory segments (managed
59        shared memory, managed mapped files...) and one of the first user requests
60        is the use of containers in managed shared memories. To achieve this, <span class="bold"><strong>Boost.Interprocess</strong></span> makes use of managed memory segment's
61        memory allocation algorithms to build several memory allocation schemes,
62        including general purpose and node allocators.
63      </p>
64<p>
65        <span class="bold"><strong>Boost.Interprocess</strong></span> STL compatible allocators
66        are configurable via template parameters. Allocators define their <code class="computeroutput"><span class="identifier">pointer</span></code> typedef based on the <code class="computeroutput"><span class="identifier">void_pointer</span></code> typedef of the segment manager
67        passed as template argument. When this <code class="computeroutput"><span class="identifier">segment_manager</span><span class="special">::</span><span class="identifier">void_pointer</span></code>
68        is a relative pointer, (for example, <code class="computeroutput"><span class="identifier">offset_ptr</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;</span></code>)
69        the user can place these allocators in memory mapped in different base addresses
70        in several processes.
71      </p>
72<div class="section">
73<div class="titlepage"><div><div><h4 class="title">
74<a name="interprocess.allocators_containers.allocator_introduction.allocator_properties"></a><a class="link" href="allocators_containers.html#interprocess.allocators_containers.allocator_introduction.allocator_properties" title="Properties of Boost.Interprocess allocators">Properties
75        of <span class="bold"><strong>Boost.Interprocess</strong></span> allocators</a>
76</h4></div></div></div>
77<p>
78          Container allocators are normally default-constructible because the are
79          stateless. <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">allocator</span></code> and <span class="bold"><strong>Boost.Pool's</strong></span>
80          <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">pool_allocator</span></code>/<code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fast_pool_allocator</span></code>
81          are examples of default-constructible allocators.
82        </p>
83<p>
84          On the other hand, <span class="bold"><strong>Boost.Interprocess</strong></span>
85          allocators need to allocate memory from a concrete memory segment and not
86          from a system-wide memory source (like the heap). <span class="bold"><strong>Boost.Interprocess</strong></span>
87          allocators are <span class="bold"><strong>stateful</strong></span>, which means that
88          they must be configured to tell them where the shared memory or the memory
89          mapped file is.
90        </p>
91<p>
92          This information is transmitted at compile-time and run-time: The allocators
93          receive a template parameter defining the type of the segment manager and
94          their constructor receive a pointer to the segment manager of the managed
95          memory segment where the user wants to allocate the values.
96        </p>
97<p>
98          <span class="bold"><strong>Boost.Interprocess</strong></span> allocators have <span class="bold"><strong>no default-constructors</strong></span> and containers must be explicitly
99          initialized with a configured allocator:
100        </p>
101<pre class="programlisting"><span class="comment">//The allocators must be templatized with the segment manager type</span>
102<span class="keyword">typedef</span> <span class="identifier">any_interprocess_allocator</span>
103   <span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">managed_shared_memory</span><span class="special">::</span><span class="identifier">segment_manager</span><span class="special">,</span> <span class="special">...&gt;</span> <span class="identifier">Allocator</span><span class="special">;</span>
104
105<span class="comment">//The allocator must be constructed with a pointer to the segment manager</span>
106<span class="identifier">Allocator</span> <span class="identifier">alloc_instance</span> <span class="special">(</span><span class="identifier">segment</span><span class="special">.</span><span class="identifier">get_segment_manager</span><span class="special">(),</span> <span class="special">...);</span>
107
108<span class="comment">//Containers must be initialized with a configured allocator</span>
109<span class="keyword">typedef</span> <span class="identifier">my_list</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;</span> <span class="identifier">MyIntList</span><span class="special">;</span>
110<span class="identifier">MyIntList</span> <span class="identifier">mylist</span><span class="special">(</span><span class="identifier">alloc_inst</span><span class="special">);</span>
111
112<span class="comment">//This would lead to a compilation error, because</span>
113<span class="comment">//the allocator has no default constructor</span>
114<span class="comment">//MyIntList mylist;</span>
115</pre>
116<p>
117          <span class="bold"><strong>Boost.Interprocess</strong></span> allocators also have
118          a <code class="computeroutput"><span class="identifier">get_segment_manager</span><span class="special">()</span></code>
119          function that returns the underlying segment manager that they have received
120          in the constructor:
121        </p>
122<pre class="programlisting"><span class="identifier">Allocator</span><span class="special">::</span><span class="identifier">segment_manager</span> <span class="identifier">s</span> <span class="special">=</span> <span class="identifier">alloc_instance</span><span class="special">.</span><span class="identifier">get_segment_manager</span><span class="special">();</span>
123<span class="identifier">AnotherType</span> <span class="special">*</span><span class="identifier">a</span> <span class="special">=</span> <span class="identifier">s</span><span class="special">-&gt;</span><span class="identifier">construct</span><span class="special">&lt;</span><span class="identifier">AnotherType</span><span class="special">&gt;(</span><span class="identifier">anonymous_instance</span><span class="special">)(/*</span><span class="identifier">Parameters</span><span class="special">*/);</span>
124</pre>
125</div>
126<div class="section">
127<div class="titlepage"><div><div><h4 class="title">
128<a name="interprocess.allocators_containers.allocator_introduction.allocator_swapping"></a><a class="link" href="allocators_containers.html#interprocess.allocators_containers.allocator_introduction.allocator_swapping" title="Swapping Boost.Interprocess allocators">Swapping
129        Boost.Interprocess allocators</a>
130</h4></div></div></div>
131<p>
132          When swapping STL containers, there is an active discussion on what to
133          do with the allocators. Some STL implementations, for example Dinkumware
134          from Visual .NET 2003, perform a deep swap of the whole container through
135          a temporary when allocators are not equal. The <a href="http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2004/n1599.html" target="_top">proposed
136          resolution</a> to container swapping is that allocators should be swapped
137          in a non-throwing way.
138        </p>
139<p>
140          Unfortunately, this approach is not valid with shared memory. Using heap
141          allocators, if Group1 of node allocators share a common segregated storage,
142          and Group2 share another common segregated storage, a simple pointer swapping
143          is needed to swap an allocator of Group1 and another allocator of Group2.
144          But when the user wants to swap two shared memory allocators, each one
145          placed in a different shared memory segment, this is not possible. As generally
146          shared memory is mapped in different addresses in each process, a pointer
147          placed in one segment can't point to any object placed in other shared
148          memory segment, since in each process, the distance between the segments
149          is different. However, if both shared memory allocators are in the same
150          segment, a non-throwing swap is possible, just like heap allocators.
151        </p>
152<p>
153          Until a final resolution is achieved. <span class="bold"><strong>Boost.Interprocess</strong></span>
154          allocators implement a non-throwing swap function that swaps internal pointers.
155          If an allocator placed in a shared memory segment is swapped with other
156          placed in a different shared memory segment, the result is undefined. But
157          a crash is quite sure.
158        </p>
159</div>
160<div class="section">
161<div class="titlepage"><div><div><h4 class="title">
162<a name="interprocess.allocators_containers.allocator_introduction.allocator"></a><a class="link" href="allocators_containers.html#interprocess.allocators_containers.allocator_introduction.allocator" title="allocator: A general purpose allocator for managed memory segments">allocator:
163        A general purpose allocator for managed memory segments</a>
164</h4></div></div></div>
165<p>
166          The <code class="computeroutput"><a class="link" href="../boost/interprocess/allocator.html" title="Class template allocator">allocator</a></code>
167          class defines an allocator class that uses the managed memory segment's
168          algorithm to allocate and deallocate memory. This is achieved through the
169          <span class="bold"><strong>segment manager</strong></span> of the managed memory
170          segment. This allocator is the equivalent for managed memory segments of
171          the standard <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">allocator</span></code>. <code class="computeroutput"><a class="link" href="../boost/interprocess/allocator.html" title="Class template allocator">allocator</a></code>
172          is templatized with the allocated type, and the segment manager.
173        </p>
174<p>
175          <span class="bold"><strong>Equality:</strong></span> Two <code class="computeroutput"><a class="link" href="../boost/interprocess/allocator.html" title="Class template allocator">allocator</a></code>
176          instances constructed with the same segment manager compare equal. If an
177          instance is created using copy constructor, that instance compares equal
178          with the original one.
179        </p>
180<p>
181          <span class="bold"><strong>Allocation thread-safety:</strong></span> Allocation and
182          deallocation are implemented as calls to the segment manager's allocation
183          function so the allocator offers the same thread-safety as the segment
184          manager.
185        </p>
186<p>
187          To use <code class="computeroutput"><a class="link" href="../boost/interprocess/allocator.html" title="Class template allocator">allocator</a></code>
188          you must include the following header:
189        </p>
190<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">allocators</span><span class="special">/</span><span class="identifier">allocator</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
191</pre>
192<p>
193          <code class="computeroutput"><a class="link" href="../boost/interprocess/allocator.html" title="Class template allocator">allocator</a></code> has
194          the following declaration:
195        </p>
196<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span>
197<span class="keyword">namespace</span> <span class="identifier">interprocess</span> <span class="special">{</span>
198
199<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">SegmentManager</span><span class="special">&gt;</span>
200<span class="keyword">class</span> <span class="identifier">allocator</span><span class="special">;</span>
201
202<span class="special">}</span>  <span class="comment">//namespace interprocess {</span>
203<span class="special">}</span>  <span class="comment">//namespace boost {</span>
204</pre>
205<p>
206          The allocator just provides the needed typedefs and forwards all allocation
207          and deallocation requests to the segment manager passed in the constructor,
208          just like <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">allocator</span></code> forwards the requests to <code class="computeroutput"><span class="keyword">operator</span> <span class="keyword">new</span><span class="special">[]</span></code>.
209        </p>
210<p>
211          Using <code class="computeroutput"><a class="link" href="../boost/interprocess/allocator.html" title="Class template allocator">allocator</a></code>
212          is straightforward:
213        </p>
214<p>
215</p>
216<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">managed_shared_memory</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
217<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">allocators</span><span class="special">/</span><span class="identifier">allocator</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
218<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>
219
220<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">interprocess</span><span class="special">;</span>
221
222<span class="keyword">int</span> <span class="identifier">main</span> <span class="special">()</span>
223<span class="special">{</span>
224   <span class="comment">//Remove shared memory on construction and destruction</span>
225   <span class="keyword">struct</span> <span class="identifier">shm_remove</span>
226   <span class="special">{</span>
227      <span class="identifier">shm_remove</span><span class="special">()</span> <span class="special">{</span> <span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"MySharedMemory"</span><span class="special">);</span> <span class="special">}</span>
228      <span class="special">~</span><span class="identifier">shm_remove</span><span class="special">(){</span> <span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"MySharedMemory"</span><span class="special">);</span> <span class="special">}</span>
229   <span class="special">}</span> <span class="identifier">remover</span><span class="special">;</span>
230
231   <span class="comment">//Create shared memory</span>
232   <span class="identifier">managed_shared_memory</span> <span class="identifier">segment</span><span class="special">(</span><span class="identifier">create_only</span><span class="special">,</span>
233                                 <span class="string">"MySharedMemory"</span><span class="special">,</span>  <span class="comment">//segment name</span>
234                                 <span class="number">65536</span><span class="special">);</span>
235
236   <span class="comment">//Create an allocator that allocates ints from the managed segment</span>
237   <span class="identifier">allocator</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">managed_shared_memory</span><span class="special">::</span><span class="identifier">segment_manager</span><span class="special">&gt;</span>
238      <span class="identifier">allocator_instance</span><span class="special">(</span><span class="identifier">segment</span><span class="special">.</span><span class="identifier">get_segment_manager</span><span class="special">());</span>
239
240   <span class="comment">//Copy constructed allocator is equal</span>
241   <span class="identifier">allocator</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">managed_shared_memory</span><span class="special">::</span><span class="identifier">segment_manager</span><span class="special">&gt;</span>
242      <span class="identifier">allocator_instance2</span><span class="special">(</span><span class="identifier">allocator_instance</span><span class="special">);</span>
243   <span class="identifier">assert</span><span class="special">(</span><span class="identifier">allocator_instance2</span> <span class="special">==</span> <span class="identifier">allocator_instance</span><span class="special">);</span>
244
245   <span class="comment">//Allocate and deallocate memory for 100 ints</span>
246   <span class="identifier">allocator_instance2</span><span class="special">.</span><span class="identifier">deallocate</span><span class="special">(</span><span class="identifier">allocator_instance</span><span class="special">.</span><span class="identifier">allocate</span><span class="special">(</span><span class="number">100</span><span class="special">),</span> <span class="number">100</span><span class="special">);</span>
247
248   <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
249<span class="special">}</span>
250</pre>
251<p>
252        </p>
253</div>
254</div>
255<div class="section">
256<div class="titlepage"><div><div><h3 class="title">
257<a name="interprocess.allocators_containers.stl_allocators_segregated_storage"></a><a class="link" href="allocators_containers.html#interprocess.allocators_containers.stl_allocators_segregated_storage" title="Segregated storage node allocators">Segregated
258      storage node allocators</a>
259</h3></div></div></div>
260<div class="toc"><dl class="toc">
261<dt><span class="section"><a href="allocators_containers.html#interprocess.allocators_containers.stl_allocators_segregated_storage.segregated_allocators_common">Additional
262        parameters and functions of segregated storage node allocators</a></span></dt>
263<dt><span class="section"><a href="allocators_containers.html#interprocess.allocators_containers.stl_allocators_segregated_storage.node_allocator">node_allocator:
264        A process-shared segregated storage</a></span></dt>
265<dt><span class="section"><a href="allocators_containers.html#interprocess.allocators_containers.stl_allocators_segregated_storage.private_node_allocator">private_node_allocator:
266        a private segregated storage</a></span></dt>
267<dt><span class="section"><a href="allocators_containers.html#interprocess.allocators_containers.stl_allocators_segregated_storage.cached_node_allocator">cached_node_allocator:
268        caching nodes to avoid overhead</a></span></dt>
269</dl></div>
270<p>
271        Variable size memory algorithms waste some space in management information
272        for each allocation. Sometimes, usually for small objects, this is not acceptable.
273        Memory algorithms can also fragment the managed memory segment under some
274        allocation and deallocation schemes, reducing their performance. When allocating
275        many objects of the same type, a simple segregated storage becomes a fast
276        and space-friendly allocator, as explained in the <a href="http://www.boost.org/libs/pool/" target="_top"><span class="bold"><strong>Boost.Pool</strong></span></a> library.
277      </p>
278<p>
279        Segregate storage node allocators allocate large memory chunks from a general
280        purpose memory allocator and divide that chunk into several nodes. No bookkeeping
281        information is stored in the nodes to achieve minimal memory waste: free
282        nodes are linked using a pointer constructed in the memory of the node.
283      </p>
284<p>
285        <span class="bold"><strong>Boost.Interprocess</strong></span> offers 3 allocators based
286        on this segregated storage algorithm: <code class="computeroutput"><a class="link" href="../boost/interprocess/node_allocator.html" title="Class template node_allocator">node_allocator</a></code>,
287        <code class="computeroutput"><a class="link" href="../boost/interprocess/private_node_allocator.html" title="Class template private_node_allocator">private_node_allocator</a></code>
288        and <code class="computeroutput"><a class="link" href="../boost/interprocess/cached_node_allocator.html" title="Class template cached_node_allocator">cached_node_allocator</a></code>.
289      </p>
290<p>
291        To know the details of the implementation of of the segregated storage pools
292        see the <a class="link" href="architecture.html#interprocess.architecture.allocators_containers.implementation_segregated_storage_pools" title="Implementation of Boost.Interprocess segregated storage pools">Implementation
293        of <span class="bold"><strong>Boost.Interprocess</strong></span> segregated storage
294        pools</a> section.
295      </p>
296<div class="section">
297<div class="titlepage"><div><div><h4 class="title">
298<a name="interprocess.allocators_containers.stl_allocators_segregated_storage.segregated_allocators_common"></a><a class="link" href="allocators_containers.html#interprocess.allocators_containers.stl_allocators_segregated_storage.segregated_allocators_common" title="Additional parameters and functions of segregated storage node allocators">Additional
299        parameters and functions of segregated storage node allocators</a>
300</h4></div></div></div>
301<p>
302          <code class="computeroutput"><a class="link" href="../boost/interprocess/node_allocator.html" title="Class template node_allocator">node_allocator</a></code>,
303          <code class="computeroutput"><a class="link" href="../boost/interprocess/private_node_allocator.html" title="Class template private_node_allocator">private_node_allocator</a></code>
304          and <code class="computeroutput"><a class="link" href="../boost/interprocess/cached_node_allocator.html" title="Class template cached_node_allocator">cached_node_allocator</a></code>
305          implement the standard allocator interface and the functions explained
306          in the <a class="link" href="allocators_containers.html#interprocess.allocators_containers.allocator_introduction.allocator_properties" title="Properties of Boost.Interprocess allocators">Properties
307          of Boost.Interprocess allocators</a>.
308        </p>
309<p>
310          All these allocators are templatized by 3 parameters:
311        </p>
312<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
313<li class="listitem">
314              <code class="computeroutput"><span class="keyword">class</span> <span class="identifier">T</span></code>:
315              The type to be allocated.
316            </li>
317<li class="listitem">
318              <code class="computeroutput"><span class="keyword">class</span> <span class="identifier">SegmentManager</span></code>:
319              The type of the segment manager that will be passed in the constructor.
320            </li>
321<li class="listitem">
322              <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">NodesPerChunk</span></code>:
323              The number of nodes that a memory chunk will contain. This value will
324              define the size of the memory the pool will request to the segment
325              manager when the pool runs out of nodes. This parameter has a default
326              value.
327            </li>
328</ul></div>
329<p>
330          These allocators also offer the <code class="computeroutput"><span class="identifier">deallocate_free_chunks</span><span class="special">()</span></code> function. This function will traverse
331          all the memory chunks of the pool and will return to the managed memory
332          segment the free chunks of memory. If this function is not used, deallocating
333          the free chunks does not happen until the pool is destroyed so the only
334          way to return memory allocated by the pool to the segment before destructing
335          the pool is calling manually this function. This function is quite time-consuming
336          because it has quadratic complexity (O(N^2)).
337        </p>
338</div>
339<div class="section">
340<div class="titlepage"><div><div><h4 class="title">
341<a name="interprocess.allocators_containers.stl_allocators_segregated_storage.node_allocator"></a><a class="link" href="allocators_containers.html#interprocess.allocators_containers.stl_allocators_segregated_storage.node_allocator" title="node_allocator: A process-shared segregated storage">node_allocator:
342        A process-shared segregated storage</a>
343</h4></div></div></div>
344<p>
345          For heap-memory node allocators (like <span class="bold"><strong>Boost.Pool's</strong></span>
346          <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fast_pool_allocator</span></code> usually a global,
347          thread-shared singleton pool is used for each node size. This is not possible
348          if you try to share a node allocator between processes. To achieve this
349          sharing <code class="computeroutput"><a class="link" href="../boost/interprocess/node_allocator.html" title="Class template node_allocator">node_allocator</a></code>
350          uses the segment manager's unique type allocation service (see <a class="link" href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_memory_segment_features.unique" title="Unique instance construction">Unique
351          instance construction</a> section).
352        </p>
353<p>
354          In the initialization, a <code class="computeroutput"><a class="link" href="../boost/interprocess/node_allocator.html" title="Class template node_allocator">node_allocator</a></code>
355          object searches this unique object in the segment. If it is not preset,
356          it builds one. This way, all <code class="computeroutput"><a class="link" href="../boost/interprocess/node_allocator.html" title="Class template node_allocator">node_allocator</a></code>
357          objects built inside a memory segment share a unique memory pool.
358        </p>
359<p>
360          The common segregated storage is not only shared between node_allocators
361          of the same type, but it is also shared between all node allocators that
362          allocate objects of the same size, for example, <span class="bold"><strong>node_allocator&lt;uint32&gt;</strong></span>
363          and <span class="bold"><strong>node_allocator&lt;float32&gt;</strong></span>. This
364          saves a lot of memory but also imposes an synchronization overhead for
365          each node allocation.
366        </p>
367<p>
368          The dynamically created common segregated storage integrates a reference
369          count so that a <code class="computeroutput"><a class="link" href="../boost/interprocess/node_allocator.html" title="Class template node_allocator">node_allocator</a></code>
370          can know if any other <code class="computeroutput"><a class="link" href="../boost/interprocess/node_allocator.html" title="Class template node_allocator">node_allocator</a></code>
371          is attached to the same common segregated storage. When the last allocator
372          attached to the pool is destroyed, the pool is destroyed.
373        </p>
374<p>
375          <span class="bold"><strong>Equality:</strong></span> Two <code class="computeroutput"><a class="link" href="../boost/interprocess/node_allocator.html" title="Class template node_allocator">node_allocator</a></code>
376          instances constructed with the same segment manager compare equal. If an
377          instance is created using copy constructor, that instance compares equal
378          with the original one.
379        </p>
380<p>
381          <span class="bold"><strong>Allocation thread-safety:</strong></span> Allocation and
382          deallocation are implemented as calls to the shared pool. The shared pool
383          offers the same synchronization guarantees as the segment manager.
384        </p>
385<p>
386          To use <code class="computeroutput"><a class="link" href="../boost/interprocess/node_allocator.html" title="Class template node_allocator">node_allocator</a></code>,
387          you must include the following header:
388        </p>
389<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">allocators</span><span class="special">/</span><span class="identifier">node_allocator</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
390</pre>
391<p>
392          <code class="computeroutput"><a class="link" href="../boost/interprocess/node_allocator.html" title="Class template node_allocator">node_allocator</a></code>
393          has the following declaration:
394        </p>
395<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span>
396<span class="keyword">namespace</span> <span class="identifier">interprocess</span> <span class="special">{</span>
397
398<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">SegmentManager</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">NodesPerChunk</span> <span class="special">=</span> <span class="special">...&gt;</span>
399<span class="keyword">class</span> <span class="identifier">node_allocator</span><span class="special">;</span>
400
401<span class="special">}</span>  <span class="comment">//namespace interprocess {</span>
402<span class="special">}</span>  <span class="comment">//namespace boost {</span>
403</pre>
404<p>
405          An example using <code class="computeroutput"><a class="link" href="../boost/interprocess/node_allocator.html" title="Class template node_allocator">node_allocator</a></code>:
406        </p>
407<p>
408</p>
409<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">managed_shared_memory</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
410<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">allocators</span><span class="special">/</span><span class="identifier">node_allocator</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
411<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>
412
413<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">interprocess</span><span class="special">;</span>
414
415<span class="keyword">int</span> <span class="identifier">main</span> <span class="special">()</span>
416<span class="special">{</span>
417   <span class="comment">//Remove shared memory on construction and destruction</span>
418   <span class="keyword">struct</span> <span class="identifier">shm_remove</span>
419   <span class="special">{</span>
420      <span class="identifier">shm_remove</span><span class="special">()</span> <span class="special">{</span> <span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"MySharedMemory"</span><span class="special">);</span> <span class="special">}</span>
421      <span class="special">~</span><span class="identifier">shm_remove</span><span class="special">(){</span> <span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"MySharedMemory"</span><span class="special">);</span> <span class="special">}</span>
422   <span class="special">}</span> <span class="identifier">remover</span><span class="special">;</span>
423
424   <span class="comment">//Create shared memory</span>
425   <span class="identifier">managed_shared_memory</span> <span class="identifier">segment</span><span class="special">(</span><span class="identifier">create_only</span><span class="special">,</span>
426                                 <span class="string">"MySharedMemory"</span><span class="special">,</span>  <span class="comment">//segment name</span>
427                                 <span class="number">65536</span><span class="special">);</span>
428
429   <span class="comment">//Create a node_allocator that allocates ints from the managed segment</span>
430   <span class="comment">//The number of chunks per segment is the default value</span>
431   <span class="keyword">typedef</span> <span class="identifier">node_allocator</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">managed_shared_memory</span><span class="special">::</span><span class="identifier">segment_manager</span><span class="special">&gt;</span>
432      <span class="identifier">node_allocator_t</span><span class="special">;</span>
433   <span class="identifier">node_allocator_t</span> <span class="identifier">allocator_instance</span><span class="special">(</span><span class="identifier">segment</span><span class="special">.</span><span class="identifier">get_segment_manager</span><span class="special">());</span>
434
435   <span class="comment">//Create another node_allocator. Since the segment manager address</span>
436   <span class="comment">//is the same, this node_allocator will be</span>
437   <span class="comment">//attached to the same pool so "allocator_instance2" can deallocate</span>
438   <span class="comment">//nodes allocated by "allocator_instance"</span>
439   <span class="identifier">node_allocator_t</span> <span class="identifier">allocator_instance2</span><span class="special">(</span><span class="identifier">segment</span><span class="special">.</span><span class="identifier">get_segment_manager</span><span class="special">());</span>
440
441   <span class="comment">//Create another node_allocator using copy-constructor. This</span>
442   <span class="comment">//node_allocator will also be attached to the same pool</span>
443   <span class="identifier">node_allocator_t</span> <span class="identifier">allocator_instance3</span><span class="special">(</span><span class="identifier">allocator_instance2</span><span class="special">);</span>
444
445   <span class="comment">//All allocators are equal</span>
446   <span class="identifier">assert</span><span class="special">(</span><span class="identifier">allocator_instance</span> <span class="special">==</span> <span class="identifier">allocator_instance2</span><span class="special">);</span>
447   <span class="identifier">assert</span><span class="special">(</span><span class="identifier">allocator_instance2</span> <span class="special">==</span> <span class="identifier">allocator_instance3</span><span class="special">);</span>
448
449   <span class="comment">//So memory allocated with one can be deallocated with another</span>
450   <span class="identifier">allocator_instance2</span><span class="special">.</span><span class="identifier">deallocate</span><span class="special">(</span><span class="identifier">allocator_instance</span><span class="special">.</span><span class="identifier">allocate</span><span class="special">(</span><span class="number">1</span><span class="special">),</span> <span class="number">1</span><span class="special">);</span>
451   <span class="identifier">allocator_instance3</span><span class="special">.</span><span class="identifier">deallocate</span><span class="special">(</span><span class="identifier">allocator_instance2</span><span class="special">.</span><span class="identifier">allocate</span><span class="special">(</span><span class="number">1</span><span class="special">),</span> <span class="number">1</span><span class="special">);</span>
452
453   <span class="comment">//The common pool will be destroyed here, since no allocator is</span>
454   <span class="comment">//attached to the pool</span>
455   <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
456<span class="special">}</span>
457</pre>
458<p>
459        </p>
460</div>
461<div class="section">
462<div class="titlepage"><div><div><h4 class="title">
463<a name="interprocess.allocators_containers.stl_allocators_segregated_storage.private_node_allocator"></a><a class="link" href="allocators_containers.html#interprocess.allocators_containers.stl_allocators_segregated_storage.private_node_allocator" title="private_node_allocator: a private segregated storage">private_node_allocator:
464        a private segregated storage</a>
465</h4></div></div></div>
466<p>
467          As said, the node_allocator shares a common segregated storage between
468          node_allocators that allocate objects of the same size and this optimizes
469          memory usage. However, it needs a unique/named object construction feature
470          so that this sharing can be possible. Also imposes a synchronization overhead
471          per node allocation because of this share. Sometimes, the unique object
472          service is not available (for example, when building index types to implement
473          the named allocation service itself) or the synchronization overhead is
474          not acceptable. Many times the programmer wants to make sure that the pool
475          is destroyed when the allocator is destroyed, to free the memory as soon
476          as possible.
477        </p>
478<p>
479          So <span class="bold"><strong>private_node_allocator</strong></span> uses the same
480          segregated storage as <code class="computeroutput"><span class="identifier">node_allocator</span></code>,
481          but each <span class="bold"><strong>private_node_allocator</strong></span> has its
482          own segregated storage pool. No synchronization is used when allocating
483          nodes, so there is far less overhead for an operation that usually involves
484          just a few pointer operations when allocating and deallocating a node.
485        </p>
486<p>
487          <span class="bold"><strong>Equality:</strong></span> Two <code class="computeroutput"><a class="link" href="../boost/interprocess/private_node_allocator.html" title="Class template private_node_allocator">private_node_allocator</a></code>
488          instances <span class="bold"><strong>never</strong></span> compare equal. Memory
489          allocated with one allocator <span class="bold"><strong>can't</strong></span> be
490          deallocated with another one.
491        </p>
492<p>
493          <span class="bold"><strong>Allocation thread-safety:</strong></span> Allocation and
494          deallocation are <span class="bold"><strong>not</strong></span> thread-safe.
495        </p>
496<p>
497          To use <code class="computeroutput"><a class="link" href="../boost/interprocess/private_node_allocator.html" title="Class template private_node_allocator">private_node_allocator</a></code>,
498          you must include the following header:
499        </p>
500<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">allocators</span><span class="special">/</span><span class="identifier">private_node_allocator</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
501</pre>
502<p>
503          <code class="computeroutput"><a class="link" href="../boost/interprocess/private_node_allocator.html" title="Class template private_node_allocator">private_node_allocator</a></code>
504          has the following declaration:
505        </p>
506<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span>
507<span class="keyword">namespace</span> <span class="identifier">interprocess</span> <span class="special">{</span>
508
509<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">SegmentManager</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">NodesPerChunk</span> <span class="special">=</span> <span class="special">...&gt;</span>
510<span class="keyword">class</span> <span class="identifier">private_node_allocator</span><span class="special">;</span>
511
512<span class="special">}</span>  <span class="comment">//namespace interprocess {</span>
513<span class="special">}</span>  <span class="comment">//namespace boost {</span>
514</pre>
515<p>
516          An example using <code class="computeroutput"><a class="link" href="../boost/interprocess/private_node_allocator.html" title="Class template private_node_allocator">private_node_allocator</a></code>:
517        </p>
518<p>
519</p>
520<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">managed_shared_memory</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
521<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">allocators</span><span class="special">/</span><span class="identifier">private_node_allocator</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
522<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>
523
524<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">interprocess</span><span class="special">;</span>
525
526<span class="keyword">int</span> <span class="identifier">main</span> <span class="special">()</span>
527<span class="special">{</span>
528   <span class="comment">//Remove shared memory on construction and destruction</span>
529   <span class="keyword">struct</span> <span class="identifier">shm_remove</span>
530   <span class="special">{</span>
531      <span class="identifier">shm_remove</span><span class="special">()</span> <span class="special">{</span> <span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"MySharedMemory"</span><span class="special">);</span> <span class="special">}</span>
532      <span class="special">~</span><span class="identifier">shm_remove</span><span class="special">(){</span> <span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"MySharedMemory"</span><span class="special">);</span> <span class="special">}</span>
533   <span class="special">}</span>  <span class="identifier">remover</span><span class="special">;</span>
534
535   <span class="comment">//Create shared memory</span>
536   <span class="identifier">managed_shared_memory</span> <span class="identifier">segment</span><span class="special">(</span><span class="identifier">create_only</span><span class="special">,</span>
537                                 <span class="string">"MySharedMemory"</span><span class="special">,</span>  <span class="comment">//segment name</span>
538                                 <span class="number">65536</span><span class="special">);</span>
539
540   <span class="comment">//Create a private_node_allocator that allocates ints from the managed segment</span>
541   <span class="comment">//The number of chunks per segment is the default value</span>
542   <span class="keyword">typedef</span> <span class="identifier">private_node_allocator</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">managed_shared_memory</span><span class="special">::</span><span class="identifier">segment_manager</span><span class="special">&gt;</span>
543      <span class="identifier">private_node_allocator_t</span><span class="special">;</span>
544   <span class="identifier">private_node_allocator_t</span> <span class="identifier">allocator_instance</span><span class="special">(</span><span class="identifier">segment</span><span class="special">.</span><span class="identifier">get_segment_manager</span><span class="special">());</span>
545
546   <span class="comment">//Create another private_node_allocator.</span>
547   <span class="identifier">private_node_allocator_t</span> <span class="identifier">allocator_instance2</span><span class="special">(</span><span class="identifier">segment</span><span class="special">.</span><span class="identifier">get_segment_manager</span><span class="special">());</span>
548
549   <span class="comment">//Although the segment manager address</span>
550   <span class="comment">//is the same, this private_node_allocator will have its own pool so</span>
551   <span class="comment">//"allocator_instance2" CAN'T deallocate nodes allocated by "allocator_instance".</span>
552   <span class="comment">//"allocator_instance2" is NOT equal to "allocator_instance"</span>
553   <span class="identifier">assert</span><span class="special">(</span><span class="identifier">allocator_instance</span> <span class="special">!=</span> <span class="identifier">allocator_instance2</span><span class="special">);</span>
554
555   <span class="comment">//Create another node_allocator using copy-constructor.</span>
556   <span class="identifier">private_node_allocator_t</span> <span class="identifier">allocator_instance3</span><span class="special">(</span><span class="identifier">allocator_instance2</span><span class="special">);</span>
557
558   <span class="comment">//This allocator is also unequal to allocator_instance2</span>
559   <span class="identifier">assert</span><span class="special">(</span><span class="identifier">allocator_instance2</span> <span class="special">!=</span> <span class="identifier">allocator_instance3</span><span class="special">);</span>
560
561   <span class="comment">//Pools are destroyed with the allocators</span>
562   <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
563<span class="special">}</span>
564</pre>
565<p>
566        </p>
567</div>
568<div class="section">
569<div class="titlepage"><div><div><h4 class="title">
570<a name="interprocess.allocators_containers.stl_allocators_segregated_storage.cached_node_allocator"></a><a class="link" href="allocators_containers.html#interprocess.allocators_containers.stl_allocators_segregated_storage.cached_node_allocator" title="cached_node_allocator: caching nodes to avoid overhead">cached_node_allocator:
571        caching nodes to avoid overhead</a>
572</h4></div></div></div>
573<p>
574          The total node sharing of <code class="computeroutput"><a class="link" href="../boost/interprocess/node_allocator.html" title="Class template node_allocator">node_allocator</a></code>
575          can impose a high overhead for some applications and the minimal synchronization
576          overhead of <code class="computeroutput"><a class="link" href="../boost/interprocess/private_node_allocator.html" title="Class template private_node_allocator">private_node_allocator</a></code>
577          can impose a unacceptable memory waste for other applications.
578        </p>
579<p>
580          To solve this, <span class="bold"><strong>Boost.Interprocess</strong></span> offers
581          an allocator, <code class="computeroutput"><a class="link" href="../boost/interprocess/cached_node_allocator.html" title="Class template cached_node_allocator">cached_node_allocator</a></code>,
582          that allocates nodes from the common pool but caches some of them privately
583          so that following allocations have no synchronization overhead. When the
584          cache is full, the allocator returns some cached nodes to the common pool,
585          and those will be available to other allocators.
586        </p>
587<p>
588          <span class="bold"><strong>Equality:</strong></span> Two <code class="computeroutput"><a class="link" href="../boost/interprocess/cached_node_allocator.html" title="Class template cached_node_allocator">cached_node_allocator</a></code>
589          instances constructed with the same segment manager compare equal. If an
590          instance is created using copy constructor, that instance compares equal
591          with the original one.
592        </p>
593<p>
594          <span class="bold"><strong>Allocation thread-safety:</strong></span> Allocation and
595          deallocation are <span class="bold"><strong>not</strong></span> thread-safe.
596        </p>
597<p>
598          To use <code class="computeroutput"><a class="link" href="../boost/interprocess/cached_node_allocator.html" title="Class template cached_node_allocator">cached_node_allocator</a></code>,
599          you must include the following header:
600        </p>
601<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">allocators</span><span class="special">/</span><span class="identifier">cached_node_allocator</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
602</pre>
603<p>
604          <code class="computeroutput"><a class="link" href="../boost/interprocess/cached_node_allocator.html" title="Class template cached_node_allocator">cached_node_allocator</a></code>
605          has the following declaration:
606        </p>
607<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span>
608<span class="keyword">namespace</span> <span class="identifier">interprocess</span> <span class="special">{</span>
609
610<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">SegmentManager</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">NodesPerChunk</span> <span class="special">=</span> <span class="special">...&gt;</span>
611<span class="keyword">class</span> <span class="identifier">cached_node_allocator</span><span class="special">;</span>
612
613<span class="special">}</span>  <span class="comment">//namespace interprocess {</span>
614<span class="special">}</span>  <span class="comment">//namespace boost {</span>
615</pre>
616<p>
617          A <code class="computeroutput"><a class="link" href="../boost/interprocess/cached_node_allocator.html" title="Class template cached_node_allocator">cached_node_allocator</a></code>
618          instance and a <code class="computeroutput"><a class="link" href="../boost/interprocess/node_allocator.html" title="Class template node_allocator">node_allocator</a></code>
619          instance share the same pool if both instances receive the same template
620          parameters. This means that nodes returned to the shared pool by one of
621          them can be reused by the other. Please note that this does not mean that
622          both allocators compare equal, this is just information for programmers
623          that want to maximize the use of the pool.
624        </p>
625<p>
626          <code class="computeroutput"><a class="link" href="../boost/interprocess/cached_node_allocator.html" title="Class template cached_node_allocator">cached_node_allocator</a></code>,
627          offers additional functions to control the cache (the cache can be controlled
628          per instance):
629        </p>
630<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
631<li class="listitem">
632              <code class="computeroutput"><span class="keyword">void</span> <span class="identifier">set_max_cached_nodes</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span>
633              <span class="identifier">n</span><span class="special">)</span></code>:
634              Sets the maximum cached nodes limit. If cached nodes reach the limit,
635              some are returned to the shared pool.
636            </li>
637<li class="listitem">
638              <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">get_max_cached_nodes</span><span class="special">()</span> <span class="keyword">const</span></code>:
639              Returns the maximum cached nodes limit.
640            </li>
641<li class="listitem">
642              <code class="computeroutput"><span class="keyword">void</span> <span class="identifier">deallocate_cache</span><span class="special">()</span></code>: Returns the cached nodes to the
643              shared pool.
644            </li>
645</ul></div>
646<p>
647          An example using <code class="computeroutput"><a class="link" href="../boost/interprocess/cached_node_allocator.html" title="Class template cached_node_allocator">cached_node_allocator</a></code>:
648        </p>
649<p>
650</p>
651<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">managed_shared_memory</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
652<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">allocators</span><span class="special">/</span><span class="identifier">cached_node_allocator</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
653<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>
654
655<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">interprocess</span><span class="special">;</span>
656
657<span class="keyword">int</span> <span class="identifier">main</span> <span class="special">()</span>
658<span class="special">{</span>
659   <span class="comment">//Remove shared memory on construction and destruction</span>
660   <span class="keyword">struct</span> <span class="identifier">shm_remove</span>
661   <span class="special">{</span>
662      <span class="identifier">shm_remove</span><span class="special">()</span> <span class="special">{</span> <span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"MySharedMemory"</span><span class="special">);</span> <span class="special">}</span>
663      <span class="special">~</span><span class="identifier">shm_remove</span><span class="special">(){</span> <span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"MySharedMemory"</span><span class="special">);</span> <span class="special">}</span>
664   <span class="special">}</span> <span class="identifier">remover</span><span class="special">;</span>
665
666   <span class="comment">//Create shared memory</span>
667   <span class="identifier">managed_shared_memory</span> <span class="identifier">segment</span><span class="special">(</span><span class="identifier">create_only</span><span class="special">,</span>
668                                 <span class="string">"MySharedMemory"</span><span class="special">,</span>  <span class="comment">//segment name</span>
669                                 <span class="number">65536</span><span class="special">);</span>
670
671   <span class="comment">//Create a cached_node_allocator that allocates ints from the managed segment</span>
672   <span class="comment">//The number of chunks per segment is the default value</span>
673   <span class="keyword">typedef</span> <span class="identifier">cached_node_allocator</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">managed_shared_memory</span><span class="special">::</span><span class="identifier">segment_manager</span><span class="special">&gt;</span>
674      <span class="identifier">cached_node_allocator_t</span><span class="special">;</span>
675   <span class="identifier">cached_node_allocator_t</span> <span class="identifier">allocator_instance</span><span class="special">(</span><span class="identifier">segment</span><span class="special">.</span><span class="identifier">get_segment_manager</span><span class="special">());</span>
676
677   <span class="comment">//The max cached nodes are configurable per instance</span>
678   <span class="identifier">allocator_instance</span><span class="special">.</span><span class="identifier">set_max_cached_nodes</span><span class="special">(</span><span class="number">3</span><span class="special">);</span>
679
680   <span class="comment">//Create another cached_node_allocator. Since the segment manager address</span>
681   <span class="comment">//is the same, this cached_node_allocator will be</span>
682   <span class="comment">//attached to the same pool so "allocator_instance2" can deallocate</span>
683   <span class="comment">//nodes allocated by "allocator_instance"</span>
684   <span class="identifier">cached_node_allocator_t</span> <span class="identifier">allocator_instance2</span><span class="special">(</span><span class="identifier">segment</span><span class="special">.</span><span class="identifier">get_segment_manager</span><span class="special">());</span>
685
686   <span class="comment">//The max cached nodes are configurable per instance</span>
687   <span class="identifier">allocator_instance2</span><span class="special">.</span><span class="identifier">set_max_cached_nodes</span><span class="special">(</span><span class="number">5</span><span class="special">);</span>
688
689   <span class="comment">//Create another cached_node_allocator using copy-constructor. This</span>
690   <span class="comment">//cached_node_allocator will also be attached to the same pool</span>
691   <span class="identifier">cached_node_allocator_t</span> <span class="identifier">allocator_instance3</span><span class="special">(</span><span class="identifier">allocator_instance2</span><span class="special">);</span>
692
693   <span class="comment">//We can clear the cache</span>
694   <span class="identifier">allocator_instance3</span><span class="special">.</span><span class="identifier">deallocate_cache</span><span class="special">();</span>
695
696   <span class="comment">//All allocators are equal</span>
697   <span class="identifier">assert</span><span class="special">(</span><span class="identifier">allocator_instance</span> <span class="special">==</span> <span class="identifier">allocator_instance2</span><span class="special">);</span>
698   <span class="identifier">assert</span><span class="special">(</span><span class="identifier">allocator_instance2</span> <span class="special">==</span> <span class="identifier">allocator_instance3</span><span class="special">);</span>
699
700   <span class="comment">//So memory allocated with one can be deallocated with another</span>
701   <span class="identifier">allocator_instance2</span><span class="special">.</span><span class="identifier">deallocate</span><span class="special">(</span><span class="identifier">allocator_instance</span><span class="special">.</span><span class="identifier">allocate</span><span class="special">(</span><span class="number">1</span><span class="special">),</span> <span class="number">1</span><span class="special">);</span>
702   <span class="identifier">allocator_instance3</span><span class="special">.</span><span class="identifier">deallocate</span><span class="special">(</span><span class="identifier">allocator_instance2</span><span class="special">.</span><span class="identifier">allocate</span><span class="special">(</span><span class="number">1</span><span class="special">),</span> <span class="number">1</span><span class="special">);</span>
703
704   <span class="comment">//The common pool will be destroyed here, since no allocator is</span>
705   <span class="comment">//attached to the pool</span>
706   <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
707<span class="special">}</span>
708</pre>
709<p>
710        </p>
711</div>
712</div>
713<div class="section">
714<div class="titlepage"><div><div><h3 class="title">
715<a name="interprocess.allocators_containers.stl_allocators_adaptive"></a><a class="link" href="allocators_containers.html#interprocess.allocators_containers.stl_allocators_adaptive" title="Adaptive pool node allocators">Adaptive
716      pool node allocators</a>
717</h3></div></div></div>
718<div class="toc"><dl class="toc">
719<dt><span class="section"><a href="allocators_containers.html#interprocess.allocators_containers.stl_allocators_adaptive.adaptive_allocators_common">Additional
720        parameters and functions of adaptive pool node allocators</a></span></dt>
721<dt><span class="section"><a href="allocators_containers.html#interprocess.allocators_containers.stl_allocators_adaptive.adaptive_pool">adaptive_pool:
722        a process-shared adaptive pool</a></span></dt>
723<dt><span class="section"><a href="allocators_containers.html#interprocess.allocators_containers.stl_allocators_adaptive.private_adaptive_pool">private_adaptive_pool:
724        a private adaptive pool</a></span></dt>
725<dt><span class="section"><a href="allocators_containers.html#interprocess.allocators_containers.stl_allocators_adaptive.cached_adaptive_pool">cached_adaptive_pool:
726        Avoiding synchronization overhead</a></span></dt>
727</dl></div>
728<p>
729        Node allocators based on simple segregated storage algorithm are both space-efficient
730        and fast but they have a problem: they only can grow. Every allocated node
731        avoids any payload to store additional data and that leads to the following
732        limitation: when a node is deallocated, it's stored in a free list of nodes
733        but memory is not returned to the segment manager so a deallocated node can
734        be only reused by other containers using the same node pool.
735      </p>
736<p>
737        This behaviour can be problematic if several containers use <code class="computeroutput"><a class="link" href="../boost/interprocess/node_allocator.html" title="Class template node_allocator">boost::interprocess::node_allocator</a></code>
738        to temporarily allocate a lot of objects but they end storing a few of them:
739        the node pool will be full of nodes that won't be reused wasting memory from
740        the segment.
741      </p>
742<p>
743        Adaptive pool based allocators trade some space (the overhead can be as low
744        as 1%) and performance (acceptable for many applications) with the ability
745        to return free chunks of nodes to the memory segment, so that they can be
746        used by any other container or managed object construction. To know the details
747        of the implementation of of "adaptive pools" see the <a class="link" href="architecture.html#interprocess.architecture.allocators_containers.implementation_adaptive_pools" title="Implementation of Boost.Interprocess adaptive pools">Implementation
748        of <span class="bold"><strong>Boost.Intrusive</strong></span> adaptive pools</a>
749        section.
750      </p>
751<p>
752        Like with segregated storage based node allocators, Boost.Interprocess offers
753        3 new allocators: <code class="computeroutput"><a class="link" href="../boost/interprocess/adaptive_pool.html" title="Class template adaptive_pool">adaptive_pool</a></code>,
754        <code class="computeroutput"><a class="link" href="../boost/interprocess/private_adaptive_pool.html" title="Class template private_adaptive_pool">private_adaptive_pool</a></code>,
755        <code class="computeroutput"><a class="link" href="../boost/interprocess/cached_adaptive_pool.html" title="Class template cached_adaptive_pool">cached_adaptive_pool</a></code>.
756      </p>
757<div class="section">
758<div class="titlepage"><div><div><h4 class="title">
759<a name="interprocess.allocators_containers.stl_allocators_adaptive.adaptive_allocators_common"></a><a class="link" href="allocators_containers.html#interprocess.allocators_containers.stl_allocators_adaptive.adaptive_allocators_common" title="Additional parameters and functions of adaptive pool node allocators">Additional
760        parameters and functions of adaptive pool node allocators</a>
761</h4></div></div></div>
762<p>
763          <code class="computeroutput"><a class="link" href="../boost/interprocess/adaptive_pool.html" title="Class template adaptive_pool">adaptive_pool</a></code>,
764          <code class="computeroutput"><a class="link" href="../boost/interprocess/private_adaptive_pool.html" title="Class template private_adaptive_pool">private_adaptive_pool</a></code>
765          and <code class="computeroutput"><a class="link" href="../boost/interprocess/cached_adaptive_pool.html" title="Class template cached_adaptive_pool">cached_adaptive_pool</a></code>
766          implement the standard allocator interface and the functions explained
767          in the <a class="link" href="allocators_containers.html#interprocess.allocators_containers.allocator_introduction.allocator_properties" title="Properties of Boost.Interprocess allocators">Properties
768          of Boost.Interprocess allocators</a>.
769        </p>
770<p>
771          All these allocators are templatized by 4 parameters:
772        </p>
773<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
774<li class="listitem">
775              <code class="computeroutput"><span class="keyword">class</span> <span class="identifier">T</span></code>:
776              The type to be allocated.
777            </li>
778<li class="listitem">
779              <code class="computeroutput"><span class="keyword">class</span> <span class="identifier">SegmentManager</span></code>:
780              The type of the segment manager that will be passed in the constructor.
781            </li>
782<li class="listitem">
783              <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">NodesPerChunk</span></code>:
784              The number of nodes that a memory chunk will contain. This value will
785              define the size of the memory the pool will request to the segment
786              manager when the pool runs out of nodes. This parameter has a default
787              value.
788            </li>
789<li class="listitem">
790              <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">MaxFreeChunks</span></code>:
791              The maximum number of free chunks that the pool will hold. If this
792              limit is reached the pool returns the chunks to the segment manager.
793              This parameter has a default value.
794            </li>
795</ul></div>
796<p>
797          These allocators also offer the <code class="computeroutput"><span class="identifier">deallocate_free_chunks</span><span class="special">()</span></code> function. This function will traverse
798          all the memory chunks of the pool and will return to the managed memory
799          segment the free chunks of memory. This function is much faster than for
800          segregated storage allocators, because the adaptive pool algorithm offers
801          constant-time access to free chunks.
802        </p>
803</div>
804<div class="section">
805<div class="titlepage"><div><div><h4 class="title">
806<a name="interprocess.allocators_containers.stl_allocators_adaptive.adaptive_pool"></a><a class="link" href="allocators_containers.html#interprocess.allocators_containers.stl_allocators_adaptive.adaptive_pool" title="adaptive_pool: a process-shared adaptive pool">adaptive_pool:
807        a process-shared adaptive pool</a>
808</h4></div></div></div>
809<p>
810          Just like <code class="computeroutput"><a class="link" href="../boost/interprocess/node_allocator.html" title="Class template node_allocator">node_allocator</a></code>
811          a global, process-thread pool is used for each node size. In the initialization,
812          <code class="computeroutput"><a class="link" href="../boost/interprocess/adaptive_pool.html" title="Class template adaptive_pool">adaptive_pool</a></code>
813          searches the pool in the segment. If it is not preset, it builds one. The
814          adaptive pool, is created using a unique name. The adaptive pool it is
815          also shared between all node_allocators that allocate objects of the same
816          size, for example, <span class="bold"><strong>adaptive_pool&lt;uint32&gt;</strong></span>
817          and <span class="bold"><strong>adaptive_pool&lt;float32&gt;</strong></span>.
818        </p>
819<p>
820          The common adaptive pool is destroyed when all the allocators attached
821          to the pool are destroyed.
822        </p>
823<p>
824          <span class="bold"><strong>Equality:</strong></span> Two <code class="computeroutput"><a class="link" href="../boost/interprocess/adaptive_pool.html" title="Class template adaptive_pool">adaptive_pool</a></code>
825          instances constructed with the same segment manager compare equal. If an
826          instance is created using copy constructor, that instance compares equal
827          with the original one.
828        </p>
829<p>
830          <span class="bold"><strong>Allocation thread-safety:</strong></span> Allocation and
831          deallocation are implemented as calls to the shared pool. The shared pool
832          offers the same synchronization guarantees as the segment manager.
833        </p>
834<p>
835          To use <code class="computeroutput"><a class="link" href="../boost/interprocess/adaptive_pool.html" title="Class template adaptive_pool">adaptive_pool</a></code>,
836          you must include the following header:
837        </p>
838<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">allocators</span><span class="special">/</span><span class="identifier">adaptive_pool</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
839</pre>
840<p>
841          <code class="computeroutput"><a class="link" href="../boost/interprocess/adaptive_pool.html" title="Class template adaptive_pool">adaptive_pool</a></code>
842          has the following declaration:
843        </p>
844<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span>
845<span class="keyword">namespace</span> <span class="identifier">interprocess</span> <span class="special">{</span>
846
847<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">SegmentManager</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">NodesPerChunk</span> <span class="special">=</span> <span class="special">...,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">MaxFreeChunks</span> <span class="special">=</span> <span class="special">...&gt;</span>
848<span class="keyword">class</span> <span class="identifier">adaptive_pool</span><span class="special">;</span>
849
850<span class="special">}</span>  <span class="comment">//namespace interprocess {</span>
851<span class="special">}</span>  <span class="comment">//namespace boost {</span>
852</pre>
853<p>
854          An example using <code class="computeroutput"><a class="link" href="../boost/interprocess/adaptive_pool.html" title="Class template adaptive_pool">adaptive_pool</a></code>:
855        </p>
856<p>
857</p>
858<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">managed_shared_memory</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
859<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">allocators</span><span class="special">/</span><span class="identifier">adaptive_pool</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
860<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>
861
862<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">interprocess</span><span class="special">;</span>
863
864<span class="keyword">int</span> <span class="identifier">main</span> <span class="special">()</span>
865<span class="special">{</span>
866   <span class="comment">//Remove shared memory on construction and destruction</span>
867   <span class="keyword">struct</span> <span class="identifier">shm_remove</span>
868   <span class="special">{</span>
869      <span class="identifier">shm_remove</span><span class="special">()</span> <span class="special">{</span> <span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"MySharedMemory"</span><span class="special">);</span> <span class="special">}</span>
870      <span class="special">~</span><span class="identifier">shm_remove</span><span class="special">(){</span> <span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"MySharedMemory"</span><span class="special">);</span> <span class="special">}</span>
871   <span class="special">}</span> <span class="identifier">remover</span><span class="special">;</span>
872
873   <span class="comment">//Create shared memory</span>
874   <span class="identifier">managed_shared_memory</span> <span class="identifier">segment</span><span class="special">(</span><span class="identifier">create_only</span><span class="special">,</span>
875                                 <span class="string">"MySharedMemory"</span><span class="special">,</span>  <span class="comment">//segment name</span>
876                                 <span class="number">65536</span><span class="special">);</span>
877
878   <span class="comment">//Create a adaptive_pool that allocates ints from the managed segment</span>
879   <span class="comment">//The number of chunks per segment is the default value</span>
880   <span class="keyword">typedef</span> <span class="identifier">adaptive_pool</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">managed_shared_memory</span><span class="special">::</span><span class="identifier">segment_manager</span><span class="special">&gt;</span>
881      <span class="identifier">adaptive_pool_t</span><span class="special">;</span>
882   <span class="identifier">adaptive_pool_t</span> <span class="identifier">allocator_instance</span><span class="special">(</span><span class="identifier">segment</span><span class="special">.</span><span class="identifier">get_segment_manager</span><span class="special">());</span>
883
884   <span class="comment">//Create another adaptive_pool. Since the segment manager address</span>
885   <span class="comment">//is the same, this adaptive_pool will be</span>
886   <span class="comment">//attached to the same pool so "allocator_instance2" can deallocate</span>
887   <span class="comment">//nodes allocated by "allocator_instance"</span>
888   <span class="identifier">adaptive_pool_t</span> <span class="identifier">allocator_instance2</span><span class="special">(</span><span class="identifier">segment</span><span class="special">.</span><span class="identifier">get_segment_manager</span><span class="special">());</span>
889
890   <span class="comment">//Create another adaptive_pool using copy-constructor. This</span>
891   <span class="comment">//adaptive_pool will also be attached to the same pool</span>
892   <span class="identifier">adaptive_pool_t</span> <span class="identifier">allocator_instance3</span><span class="special">(</span><span class="identifier">allocator_instance2</span><span class="special">);</span>
893
894   <span class="comment">//All allocators are equal</span>
895   <span class="identifier">assert</span><span class="special">(</span><span class="identifier">allocator_instance</span> <span class="special">==</span> <span class="identifier">allocator_instance2</span><span class="special">);</span>
896   <span class="identifier">assert</span><span class="special">(</span><span class="identifier">allocator_instance2</span> <span class="special">==</span> <span class="identifier">allocator_instance3</span><span class="special">);</span>
897
898   <span class="comment">//So memory allocated with one can be deallocated with another</span>
899   <span class="identifier">allocator_instance2</span><span class="special">.</span><span class="identifier">deallocate</span><span class="special">(</span><span class="identifier">allocator_instance</span><span class="special">.</span><span class="identifier">allocate</span><span class="special">(</span><span class="number">1</span><span class="special">),</span> <span class="number">1</span><span class="special">);</span>
900   <span class="identifier">allocator_instance3</span><span class="special">.</span><span class="identifier">deallocate</span><span class="special">(</span><span class="identifier">allocator_instance2</span><span class="special">.</span><span class="identifier">allocate</span><span class="special">(</span><span class="number">1</span><span class="special">),</span> <span class="number">1</span><span class="special">);</span>
901
902   <span class="comment">//The common pool will be destroyed here, since no allocator is</span>
903   <span class="comment">//attached to the pool</span>
904   <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
905<span class="special">}</span>
906</pre>
907<p>
908        </p>
909</div>
910<div class="section">
911<div class="titlepage"><div><div><h4 class="title">
912<a name="interprocess.allocators_containers.stl_allocators_adaptive.private_adaptive_pool"></a><a class="link" href="allocators_containers.html#interprocess.allocators_containers.stl_allocators_adaptive.private_adaptive_pool" title="private_adaptive_pool: a private adaptive pool">private_adaptive_pool:
913        a private adaptive pool</a>
914</h4></div></div></div>
915<p>
916          Just like <code class="computeroutput"><a class="link" href="../boost/interprocess/private_node_allocator.html" title="Class template private_node_allocator">private_node_allocator</a></code>
917          owns a private segregated storage pool, <code class="computeroutput"><a class="link" href="../boost/interprocess/private_adaptive_pool.html" title="Class template private_adaptive_pool">private_adaptive_pool</a></code>
918          owns its own adaptive pool. If the user wants to avoid the excessive node
919          allocation synchronization overhead in a container <code class="computeroutput"><a class="link" href="../boost/interprocess/private_adaptive_pool.html" title="Class template private_adaptive_pool">private_adaptive_pool</a></code>
920          is a good choice.
921        </p>
922<p>
923          <span class="bold"><strong>Equality:</strong></span> Two <code class="computeroutput"><a class="link" href="../boost/interprocess/private_adaptive_pool.html" title="Class template private_adaptive_pool">private_adaptive_pool</a></code>
924          instances <span class="bold"><strong>never</strong></span> compare equal. Memory
925          allocated with one allocator <span class="bold"><strong>can't</strong></span> be
926          deallocated with another one.
927        </p>
928<p>
929          <span class="bold"><strong>Allocation thread-safety:</strong></span> Allocation and
930          deallocation are <span class="bold"><strong>not</strong></span> thread-safe.
931        </p>
932<p>
933          To use <code class="computeroutput"><a class="link" href="../boost/interprocess/private_adaptive_pool.html" title="Class template private_adaptive_pool">private_adaptive_pool</a></code>,
934          you must include the following header:
935        </p>
936<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">allocators</span><span class="special">/</span><span class="identifier">private_adaptive_pool</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
937</pre>
938<p>
939          <code class="computeroutput"><a class="link" href="../boost/interprocess/private_adaptive_pool.html" title="Class template private_adaptive_pool">private_adaptive_pool</a></code>
940          has the following declaration:
941        </p>
942<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span>
943<span class="keyword">namespace</span> <span class="identifier">interprocess</span> <span class="special">{</span>
944
945<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">SegmentManager</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">NodesPerChunk</span> <span class="special">=</span> <span class="special">...,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">MaxFreeChunks</span> <span class="special">=</span> <span class="special">...&gt;</span>
946<span class="keyword">class</span> <span class="identifier">private_adaptive_pool</span><span class="special">;</span>
947
948<span class="special">}</span>  <span class="comment">//namespace interprocess {</span>
949<span class="special">}</span>  <span class="comment">//namespace boost {</span>
950</pre>
951<p>
952          An example using <code class="computeroutput"><a class="link" href="../boost/interprocess/private_adaptive_pool.html" title="Class template private_adaptive_pool">private_adaptive_pool</a></code>:
953        </p>
954<p>
955</p>
956<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">managed_shared_memory</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
957<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">allocators</span><span class="special">/</span><span class="identifier">private_adaptive_pool</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
958<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>
959
960<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">interprocess</span><span class="special">;</span>
961
962<span class="keyword">int</span> <span class="identifier">main</span> <span class="special">()</span>
963<span class="special">{</span>
964   <span class="comment">//Remove shared memory on construction and destruction</span>
965   <span class="keyword">struct</span> <span class="identifier">shm_remove</span>
966   <span class="special">{</span>
967      <span class="identifier">shm_remove</span><span class="special">()</span> <span class="special">{</span> <span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"MySharedMemory"</span><span class="special">);</span> <span class="special">}</span>
968      <span class="special">~</span><span class="identifier">shm_remove</span><span class="special">(){</span> <span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"MySharedMemory"</span><span class="special">);</span> <span class="special">}</span>
969   <span class="special">}</span> <span class="identifier">remover</span><span class="special">;</span>
970
971   <span class="comment">//Create shared memory</span>
972   <span class="identifier">managed_shared_memory</span> <span class="identifier">segment</span><span class="special">(</span><span class="identifier">create_only</span><span class="special">,</span>
973                                 <span class="string">"MySharedMemory"</span><span class="special">,</span>  <span class="comment">//segment name</span>
974                                 <span class="number">65536</span><span class="special">);</span>
975
976   <span class="comment">//Create a private_adaptive_pool that allocates ints from the managed segment</span>
977   <span class="comment">//The number of chunks per segment is the default value</span>
978   <span class="keyword">typedef</span> <span class="identifier">private_adaptive_pool</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">managed_shared_memory</span><span class="special">::</span><span class="identifier">segment_manager</span><span class="special">&gt;</span>
979         <span class="identifier">private_adaptive_pool_t</span><span class="special">;</span>
980   <span class="identifier">private_adaptive_pool_t</span> <span class="identifier">allocator_instance</span><span class="special">(</span><span class="identifier">segment</span><span class="special">.</span><span class="identifier">get_segment_manager</span><span class="special">());</span>
981
982   <span class="comment">//Create another private_adaptive_pool.</span>
983   <span class="identifier">private_adaptive_pool_t</span> <span class="identifier">allocator_instance2</span><span class="special">(</span><span class="identifier">segment</span><span class="special">.</span><span class="identifier">get_segment_manager</span><span class="special">());</span>
984
985   <span class="comment">//Although the segment manager address</span>
986   <span class="comment">//is the same, this private_adaptive_pool will have its own pool so</span>
987   <span class="comment">//"allocator_instance2" CAN'T deallocate nodes allocated by "allocator_instance".</span>
988   <span class="comment">//"allocator_instance2" is NOT equal to "allocator_instance"</span>
989   <span class="identifier">assert</span><span class="special">(</span><span class="identifier">allocator_instance</span> <span class="special">!=</span> <span class="identifier">allocator_instance2</span><span class="special">);</span>
990
991   <span class="comment">//Create another adaptive_pool using copy-constructor.</span>
992   <span class="identifier">private_adaptive_pool_t</span> <span class="identifier">allocator_instance3</span><span class="special">(</span><span class="identifier">allocator_instance2</span><span class="special">);</span>
993
994   <span class="comment">//This allocator is also unequal to allocator_instance2</span>
995   <span class="identifier">assert</span><span class="special">(</span><span class="identifier">allocator_instance2</span> <span class="special">!=</span> <span class="identifier">allocator_instance3</span><span class="special">);</span>
996
997   <span class="comment">//Pools are destroyed with the allocators</span>
998   <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
999<span class="special">}</span>
1000</pre>
1001<p>
1002        </p>
1003</div>
1004<div class="section">
1005<div class="titlepage"><div><div><h4 class="title">
1006<a name="interprocess.allocators_containers.stl_allocators_adaptive.cached_adaptive_pool"></a><a class="link" href="allocators_containers.html#interprocess.allocators_containers.stl_allocators_adaptive.cached_adaptive_pool" title="cached_adaptive_pool: Avoiding synchronization overhead">cached_adaptive_pool:
1007        Avoiding synchronization overhead</a>
1008</h4></div></div></div>
1009<p>
1010          Adaptive pools have also a cached version. In this allocator the allocator
1011          caches some nodes to avoid the synchronization and bookkeeping overhead
1012          of the shared adaptive pool. <code class="computeroutput"><a class="link" href="../boost/interprocess/cached_adaptive_pool.html" title="Class template cached_adaptive_pool">cached_adaptive_pool</a></code>
1013          allocates nodes from the common adaptive pool but caches some of them privately
1014          so that following allocations have no synchronization overhead. When the
1015          cache is full, the allocator returns some cached nodes to the common pool,
1016          and those will be available to other <code class="computeroutput"><a class="link" href="../boost/interprocess/cached_adaptive_pool.html" title="Class template cached_adaptive_pool">cached_adaptive_pools</a></code>
1017          or <code class="computeroutput"><a class="link" href="../boost/interprocess/adaptive_pool.html" title="Class template adaptive_pool">adaptive_pools</a></code>
1018          of the same managed segment.
1019        </p>
1020<p>
1021          <span class="bold"><strong>Equality:</strong></span> Two <code class="computeroutput"><a class="link" href="../boost/interprocess/cached_adaptive_pool.html" title="Class template cached_adaptive_pool">cached_adaptive_pool</a></code>
1022          instances constructed with the same segment manager compare equal. If an
1023          instance is created using copy constructor, that instance compares equal
1024          with the original one.
1025        </p>
1026<p>
1027          <span class="bold"><strong>Allocation thread-safety:</strong></span> Allocation and
1028          deallocation are <span class="bold"><strong>not</strong></span> thread-safe.
1029        </p>
1030<p>
1031          To use <code class="computeroutput"><a class="link" href="../boost/interprocess/cached_adaptive_pool.html" title="Class template cached_adaptive_pool">cached_adaptive_pool</a></code>,
1032          you must include the following header:
1033        </p>
1034<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">allocators</span><span class="special">/</span><span class="identifier">cached_adaptive_pool</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1035</pre>
1036<p>
1037          <code class="computeroutput"><a class="link" href="../boost/interprocess/cached_adaptive_pool.html" title="Class template cached_adaptive_pool">cached_adaptive_pool</a></code>
1038          has the following declaration:
1039        </p>
1040<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span>
1041<span class="keyword">namespace</span> <span class="identifier">interprocess</span> <span class="special">{</span>
1042
1043<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">SegmentManager</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">NodesPerChunk</span> <span class="special">=</span> <span class="special">...,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">MaxFreeNodes</span> <span class="special">=</span> <span class="special">...&gt;</span>
1044<span class="keyword">class</span> <span class="identifier">cached_adaptive_pool</span><span class="special">;</span>
1045
1046<span class="special">}</span>  <span class="comment">//namespace interprocess {</span>
1047<span class="special">}</span>  <span class="comment">//namespace boost {</span>
1048</pre>
1049<p>
1050          A <code class="computeroutput"><a class="link" href="../boost/interprocess/cached_adaptive_pool.html" title="Class template cached_adaptive_pool">cached_adaptive_pool</a></code>
1051          instance and an <code class="computeroutput"><a class="link" href="../boost/interprocess/adaptive_pool.html" title="Class template adaptive_pool">adaptive_pool</a></code>
1052          instance share the same pool if both instances receive the same template
1053          parameters. This means that nodes returned to the shared pool by one of
1054          them can be reused by the other. Please note that this does not mean that
1055          both allocators compare equal, this is just information for programmers
1056          that want to maximize the use of the pool.
1057        </p>
1058<p>
1059          <code class="computeroutput"><a class="link" href="../boost/interprocess/cached_adaptive_pool.html" title="Class template cached_adaptive_pool">cached_adaptive_pool</a></code>,
1060          offers additional functions to control the cache (the cache can be controlled
1061          per instance):
1062        </p>
1063<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
1064<li class="listitem">
1065              <code class="computeroutput"><span class="keyword">void</span> <span class="identifier">set_max_cached_nodes</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span>
1066              <span class="identifier">n</span><span class="special">)</span></code>:
1067              Sets the maximum cached nodes limit. If cached nodes reach the limit,
1068              some are returned to the shared pool.
1069            </li>
1070<li class="listitem">
1071              <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">get_max_cached_nodes</span><span class="special">()</span> <span class="keyword">const</span></code>:
1072              Returns the maximum cached nodes limit.
1073            </li>
1074<li class="listitem">
1075              <code class="computeroutput"><span class="keyword">void</span> <span class="identifier">deallocate_cache</span><span class="special">()</span></code>: Returns the cached nodes to the
1076              shared pool.
1077            </li>
1078</ul></div>
1079<p>
1080          An example using <code class="computeroutput"><a class="link" href="../boost/interprocess/cached_adaptive_pool.html" title="Class template cached_adaptive_pool">cached_adaptive_pool</a></code>:
1081        </p>
1082<p>
1083</p>
1084<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">managed_shared_memory</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1085<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">allocators</span><span class="special">/</span><span class="identifier">cached_adaptive_pool</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1086<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>
1087
1088<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">interprocess</span><span class="special">;</span>
1089
1090<span class="keyword">int</span> <span class="identifier">main</span> <span class="special">()</span>
1091<span class="special">{</span>
1092   <span class="comment">//Remove shared memory on construction and destruction</span>
1093   <span class="keyword">struct</span> <span class="identifier">shm_remove</span>
1094   <span class="special">{</span>
1095      <span class="identifier">shm_remove</span><span class="special">()</span> <span class="special">{</span> <span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"MySharedMemory"</span><span class="special">);</span> <span class="special">}</span>
1096      <span class="special">~</span><span class="identifier">shm_remove</span><span class="special">(){</span> <span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"MySharedMemory"</span><span class="special">);</span> <span class="special">}</span>
1097   <span class="special">}</span> <span class="identifier">remover</span><span class="special">;</span>
1098
1099   <span class="comment">//Create shared memory</span>
1100   <span class="identifier">managed_shared_memory</span> <span class="identifier">segment</span><span class="special">(</span><span class="identifier">create_only</span><span class="special">,</span>
1101                                 <span class="string">"MySharedMemory"</span><span class="special">,</span>  <span class="comment">//segment name</span>
1102                                 <span class="number">65536</span><span class="special">);</span>
1103
1104   <span class="comment">//Create a cached_adaptive_pool that allocates ints from the managed segment</span>
1105   <span class="comment">//The number of chunks per segment is the default value</span>
1106   <span class="keyword">typedef</span> <span class="identifier">cached_adaptive_pool</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">managed_shared_memory</span><span class="special">::</span><span class="identifier">segment_manager</span><span class="special">&gt;</span>
1107      <span class="identifier">cached_adaptive_pool_t</span><span class="special">;</span>
1108   <span class="identifier">cached_adaptive_pool_t</span> <span class="identifier">allocator_instance</span><span class="special">(</span><span class="identifier">segment</span><span class="special">.</span><span class="identifier">get_segment_manager</span><span class="special">());</span>
1109
1110   <span class="comment">//The max cached nodes are configurable per instance</span>
1111   <span class="identifier">allocator_instance</span><span class="special">.</span><span class="identifier">set_max_cached_nodes</span><span class="special">(</span><span class="number">3</span><span class="special">);</span>
1112
1113   <span class="comment">//Create another cached_adaptive_pool. Since the segment manager address</span>
1114   <span class="comment">//is the same, this cached_adaptive_pool will be</span>
1115   <span class="comment">//attached to the same pool so "allocator_instance2" can deallocate</span>
1116   <span class="comment">//nodes allocated by "allocator_instance"</span>
1117   <span class="identifier">cached_adaptive_pool_t</span> <span class="identifier">allocator_instance2</span><span class="special">(</span><span class="identifier">segment</span><span class="special">.</span><span class="identifier">get_segment_manager</span><span class="special">());</span>
1118
1119   <span class="comment">//The max cached nodes are configurable per instance</span>
1120   <span class="identifier">allocator_instance2</span><span class="special">.</span><span class="identifier">set_max_cached_nodes</span><span class="special">(</span><span class="number">5</span><span class="special">);</span>
1121
1122   <span class="comment">//Create another cached_adaptive_pool using copy-constructor. This</span>
1123   <span class="comment">//cached_adaptive_pool will also be attached to the same pool</span>
1124   <span class="identifier">cached_adaptive_pool_t</span> <span class="identifier">allocator_instance3</span><span class="special">(</span><span class="identifier">allocator_instance2</span><span class="special">);</span>
1125
1126   <span class="comment">//We can clear the cache</span>
1127   <span class="identifier">allocator_instance3</span><span class="special">.</span><span class="identifier">deallocate_cache</span><span class="special">();</span>
1128
1129   <span class="comment">//All allocators are equal</span>
1130   <span class="identifier">assert</span><span class="special">(</span><span class="identifier">allocator_instance</span> <span class="special">==</span> <span class="identifier">allocator_instance2</span><span class="special">);</span>
1131   <span class="identifier">assert</span><span class="special">(</span><span class="identifier">allocator_instance2</span> <span class="special">==</span> <span class="identifier">allocator_instance3</span><span class="special">);</span>
1132
1133   <span class="comment">//So memory allocated with one can be deallocated with another</span>
1134   <span class="identifier">allocator_instance2</span><span class="special">.</span><span class="identifier">deallocate</span><span class="special">(</span><span class="identifier">allocator_instance</span><span class="special">.</span><span class="identifier">allocate</span><span class="special">(</span><span class="number">1</span><span class="special">),</span> <span class="number">1</span><span class="special">);</span>
1135   <span class="identifier">allocator_instance3</span><span class="special">.</span><span class="identifier">deallocate</span><span class="special">(</span><span class="identifier">allocator_instance2</span><span class="special">.</span><span class="identifier">allocate</span><span class="special">(</span><span class="number">1</span><span class="special">),</span> <span class="number">1</span><span class="special">);</span>
1136
1137   <span class="comment">//The common pool will be destroyed here, since no allocator is</span>
1138   <span class="comment">//attached to the pool</span>
1139   <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
1140<span class="special">}</span>
1141</pre>
1142<p>
1143        </p>
1144</div>
1145</div>
1146<div class="section">
1147<div class="titlepage"><div><div><h3 class="title">
1148<a name="interprocess.allocators_containers.containers_explained"></a><a class="link" href="allocators_containers.html#interprocess.allocators_containers.containers_explained" title="Interprocess and containers in managed memory segments">Interprocess
1149      and containers in managed memory segments</a>
1150</h3></div></div></div>
1151<div class="toc"><dl class="toc">
1152<dt><span class="section"><a href="allocators_containers.html#interprocess.allocators_containers.containers_explained.stl_container_requirements">Container
1153        requirements for Boost.Interprocess allocators</a></span></dt>
1154<dt><span class="section"><a href="allocators_containers.html#interprocess.allocators_containers.containers_explained.containers">STL
1155        containers in managed memory segments</a></span></dt>
1156<dt><span class="section"><a href="allocators_containers.html#interprocess.allocators_containers.containers_explained.where_allocate">Where
1157        is this being allocated?</a></span></dt>
1158<dt><span class="section"><a href="allocators_containers.html#interprocess.allocators_containers.containers_explained.containers_and_move">Move
1159        semantics in Interprocess containers</a></span></dt>
1160<dt><span class="section"><a href="allocators_containers.html#interprocess.allocators_containers.containers_explained.containers_of_containers">Containers
1161        of containers</a></span></dt>
1162</dl></div>
1163<div class="section">
1164<div class="titlepage"><div><div><h4 class="title">
1165<a name="interprocess.allocators_containers.containers_explained.stl_container_requirements"></a><a class="link" href="allocators_containers.html#interprocess.allocators_containers.containers_explained.stl_container_requirements" title="Container requirements for Boost.Interprocess allocators">Container
1166        requirements for Boost.Interprocess allocators</a>
1167</h4></div></div></div>
1168<p>
1169          <span class="bold"><strong>Boost.Interprocess</strong></span> STL compatible allocators
1170          offer a STL compatible allocator interface and if they define their internal
1171          <span class="bold"><strong>pointer</strong></span> typedef as a relative pointer,
1172          they can be used to place STL containers in shared memory, memory mapped
1173          files or in a user defined memory segment.
1174        </p>
1175<p>
1176          However, as Scott Meyers mentions in his Effective STL book, Item 10,
1177          <span class="emphasis"><em>"Be aware of allocator conventions and restrictions"</em></span>:
1178        </p>
1179<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
1180<li class="listitem">
1181              <span class="emphasis"><em>"the Standard explicitly allows library implementers
1182              to assume that every allocator's pointer typedef is a synonym for T*"</em></span>
1183            </li>
1184<li class="listitem">
1185              <span class="emphasis"><em>"the Standard says that an implementation of the STL
1186              is permitted to assume that all allocator objects of the same type
1187              are equivalent and always compare equal"</em></span>
1188            </li>
1189</ul></div>
1190<p>
1191          Obviously, if any STL implementation ignores pointer typedefs, no smart
1192          pointer can be used as allocator::pointer. If STL implementations assume
1193          all allocator objects of the same type compare equal, it will assume that
1194          two allocators, each one allocating from a different memory pool are equal,
1195          which is a complete disaster.
1196        </p>
1197<p>
1198          STL containers that we want to place in shared memory or memory mapped
1199          files with <span class="bold"><strong>Boost.Interprocess</strong></span> can't make
1200          any of these assumptions, so:
1201        </p>
1202<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
1203<li class="listitem">
1204              STL containers may not assume that memory allocated with an allocator
1205              can be deallocated with other allocators of the same type. All allocators
1206              objects must compare equal only if memory allocated with one object
1207              can be deallocated with the other one, and this can only tested with
1208              operator==() at run-time.
1209            </li>
1210<li class="listitem">
1211              Containers' internal pointers should be of the type allocator::pointer
1212              and containers may not assume allocator::pointer is a raw pointer.
1213            </li>
1214<li class="listitem">
1215              All objects must be constructed-destroyed via allocator::construct
1216              and allocator::destroy functions.
1217            </li>
1218</ul></div>
1219</div>
1220<div class="section">
1221<div class="titlepage"><div><div><h4 class="title">
1222<a name="interprocess.allocators_containers.containers_explained.containers"></a><a class="link" href="allocators_containers.html#interprocess.allocators_containers.containers_explained.containers" title="STL containers in managed memory segments">STL
1223        containers in managed memory segments</a>
1224</h4></div></div></div>
1225<p>
1226          Unfortunately, many STL implementations use raw pointers for internal data
1227          and ignore allocator pointer typedefs and others suppose at some point
1228          that the allocator::typedef is T*. This is because in practice, there wasn't
1229          need of allocators with a pointer typedef different from T* for pooled/node
1230          memory allocators.
1231        </p>
1232<p>
1233          Until STL implementations handle allocator::pointer typedefs in a generic
1234          way, <span class="bold"><strong>Boost.Interprocess</strong></span> offers the following
1235          classes:
1236        </p>
1237<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
1238              <span class="bold"><strong>boost:interprocess::vector</strong></span> is the
1239              implementation of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span></code>
1240              ready to be used in managed memory segments like shared memory. To
1241              use it include:
1242            </li></ul></div>
1243<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">containers</span><span class="special">/</span><span class="identifier">vector</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1244</pre>
1245<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
1246              <span class="bold"><strong>boost:interprocess::deque</strong></span> is the implementation
1247              of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">deque</span></code> ready to be used in managed
1248              memory segments like shared memory. To use it include:
1249            </li></ul></div>
1250<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">containers</span><span class="special">/</span><span class="identifier">deque</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1251</pre>
1252<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
1253              <code class="computeroutput">list</code> is the
1254              implementation of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">list</span></code>
1255              ready to be used in managed memory segments like shared memory. To
1256              use it include:
1257            </li></ul></div>
1258<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">containers</span><span class="special">/</span><span class="identifier">list</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1259</pre>
1260<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
1261              <code class="computeroutput">slist</code> is the
1262              implementation of SGI's <code class="computeroutput"><span class="identifier">slist</span></code>
1263              container (singly linked list) ready to be used in managed memory segments
1264              like shared memory. To use it include:
1265            </li></ul></div>
1266<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">containers</span><span class="special">/</span><span class="identifier">slist</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1267</pre>
1268<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
1269              <code class="computeroutput">set</code>/ <code class="computeroutput">multiset</code>/ <code class="computeroutput">map</code>/ <code class="computeroutput">multimap</code>
1270              family is the implementation of std::set/multiset/map/multimap family
1271              ready to be used in managed memory segments like shared memory. To
1272              use them include:
1273            </li></ul></div>
1274<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">containers</span><span class="special">/</span><span class="identifier">set</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1275<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">containers</span><span class="special">/</span><span class="identifier">map</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1276</pre>
1277<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
1278              <code class="computeroutput">flat_set</code>/
1279              <code class="computeroutput">flat_multiset</code>/
1280              <code class="computeroutput">flat_map</code>/
1281              <code class="computeroutput">flat_multimap</code>
1282              classes are the adaptation and extension of Andrei Alexandrescu's famous
1283              AssocVector class from Loki library, ready for the shared memory. These
1284              classes offer the same functionality as <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">set</span><span class="special">/</span><span class="identifier">multiset</span><span class="special">/</span><span class="identifier">map</span><span class="special">/</span><span class="identifier">multimap</span></code>
1285              implemented with an ordered vector, which has faster lookups than the
1286              standard ordered associative containers based on red-black trees, but
1287              slower insertions. To use it include:
1288            </li></ul></div>
1289<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">containers</span><span class="special">/</span><span class="identifier">flat_set</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1290<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">containers</span><span class="special">/</span><span class="identifier">flat_map</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1291</pre>
1292<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
1293              <code class="computeroutput">basic_string</code>
1294              is the implementation of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">basic_string</span></code>
1295              ready to be used in managed memory segments like shared memory. It's
1296              implemented using a vector-like contiguous storage, so it has fast
1297              c string conversion and can be used with the <a class="link" href="streams.html#interprocess.streams.vectorstream" title="Formatting directly in your character vector: vectorstream">vectorstream</a>
1298              iostream formatting classes. To use it include:
1299            </li></ul></div>
1300<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">containers</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1301</pre>
1302<p>
1303          All these containers have the same default arguments as standard containers
1304          and they can be used with other, non <span class="bold"><strong>Boost.Interprocess</strong></span>
1305          allocators (std::allocator, or boost::pool_allocator, for example).
1306        </p>
1307<p>
1308          To place any of these containers in managed memory segments, we must define
1309          the allocator template parameter with a <span class="bold"><strong>Boost.Interprocess</strong></span>
1310          allocator so that the container allocates the values in the managed memory
1311          segment. To place the container itself in shared memory, we construct it
1312          in the managed memory segment just like any other object with <span class="bold"><strong>Boost.Interprocess</strong></span>:
1313        </p>
1314<p>
1315</p>
1316<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">containers</span><span class="special">/</span><span class="identifier">vector</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1317<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">allocators</span><span class="special">/</span><span class="identifier">allocator</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1318<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">managed_shared_memory</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1319
1320<span class="keyword">int</span> <span class="identifier">main</span> <span class="special">()</span>
1321<span class="special">{</span>
1322   <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">interprocess</span><span class="special">;</span>
1323   <span class="comment">//Remove shared memory on construction and destruction</span>
1324   <span class="keyword">struct</span> <span class="identifier">shm_remove</span>
1325   <span class="special">{</span>
1326      <span class="identifier">shm_remove</span><span class="special">()</span> <span class="special">{</span> <span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"MySharedMemory"</span><span class="special">);</span> <span class="special">}</span>
1327      <span class="special">~</span><span class="identifier">shm_remove</span><span class="special">(){</span> <span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"MySharedMemory"</span><span class="special">);</span> <span class="special">}</span>
1328   <span class="special">}</span> <span class="identifier">remover</span><span class="special">;</span>
1329
1330   <span class="comment">//A managed shared memory where we can construct objects</span>
1331   <span class="comment">//associated with a c-string</span>
1332   <span class="identifier">managed_shared_memory</span> <span class="identifier">segment</span><span class="special">(</span><span class="identifier">create_only</span><span class="special">,</span>
1333                                 <span class="string">"MySharedMemory"</span><span class="special">,</span>  <span class="comment">//segment name</span>
1334                                 <span class="number">65536</span><span class="special">);</span>
1335
1336   <span class="comment">//Alias an STL-like allocator of ints that allocates ints from the segment</span>
1337   <span class="keyword">typedef</span> <span class="identifier">allocator</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">managed_shared_memory</span><span class="special">::</span><span class="identifier">segment_manager</span><span class="special">&gt;</span>
1338      <span class="identifier">ShmemAllocator</span><span class="special">;</span>
1339
1340   <span class="comment">//Alias a vector that uses the previous STL-like allocator</span>
1341   <span class="keyword">typedef</span> <span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">ShmemAllocator</span><span class="special">&gt;</span> <span class="identifier">MyVector</span><span class="special">;</span>
1342
1343   <span class="keyword">int</span> <span class="identifier">initVal</span><span class="special">[]</span>        <span class="special">=</span> <span class="special">{</span><span class="number">0</span><span class="special">,</span> <span class="number">1</span><span class="special">,</span> <span class="number">2</span><span class="special">,</span> <span class="number">3</span><span class="special">,</span> <span class="number">4</span><span class="special">,</span> <span class="number">5</span><span class="special">,</span> <span class="number">6</span> <span class="special">};</span>
1344   <span class="keyword">const</span> <span class="keyword">int</span> <span class="special">*</span><span class="identifier">begVal</span>    <span class="special">=</span> <span class="identifier">initVal</span><span class="special">;</span>
1345   <span class="keyword">const</span> <span class="keyword">int</span> <span class="special">*</span><span class="identifier">endVal</span>    <span class="special">=</span> <span class="identifier">initVal</span> <span class="special">+</span> <span class="keyword">sizeof</span><span class="special">(</span><span class="identifier">initVal</span><span class="special">)/</span><span class="keyword">sizeof</span><span class="special">(</span><span class="identifier">initVal</span><span class="special">[</span><span class="number">0</span><span class="special">]);</span>
1346
1347   <span class="comment">//Initialize the STL-like allocator</span>
1348   <span class="keyword">const</span> <span class="identifier">ShmemAllocator</span> <span class="identifier">alloc_inst</span> <span class="special">(</span><span class="identifier">segment</span><span class="special">.</span><span class="identifier">get_segment_manager</span><span class="special">());</span>
1349
1350   <span class="comment">//Construct the vector in the shared memory segment with the STL-like allocator</span>
1351   <span class="comment">//from a range of iterators</span>
1352   <span class="identifier">MyVector</span> <span class="special">*</span><span class="identifier">myvector</span> <span class="special">=</span>
1353      <span class="identifier">segment</span><span class="special">.</span><span class="identifier">construct</span><span class="special">&lt;</span><span class="identifier">MyVector</span><span class="special">&gt;</span>
1354         <span class="special">(</span><span class="string">"MyVector"</span><span class="special">)/*</span><span class="identifier">object</span> <span class="identifier">name</span><span class="special">*/</span>
1355         <span class="special">(</span><span class="identifier">begVal</span>     <span class="comment">/*first ctor parameter*/</span><span class="special">,</span>
1356         <span class="identifier">endVal</span>     <span class="comment">/*second ctor parameter*/</span><span class="special">,</span>
1357         <span class="identifier">alloc_inst</span> <span class="comment">/*third ctor parameter*/</span><span class="special">);</span>
1358
1359   <span class="comment">//Use vector as your want</span>
1360   <span class="identifier">std</span><span class="special">::</span><span class="identifier">sort</span><span class="special">(</span><span class="identifier">myvector</span><span class="special">-&gt;</span><span class="identifier">rbegin</span><span class="special">(),</span> <span class="identifier">myvector</span><span class="special">-&gt;</span><span class="identifier">rend</span><span class="special">());</span>
1361   <span class="comment">// . . .</span>
1362   <span class="comment">//When done, destroy and delete vector from the segment</span>
1363   <span class="identifier">segment</span><span class="special">.</span><span class="identifier">destroy</span><span class="special">&lt;</span><span class="identifier">MyVector</span><span class="special">&gt;(</span><span class="string">"MyVector"</span><span class="special">);</span>
1364   <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
1365<span class="special">}</span>
1366</pre>
1367<p>
1368        </p>
1369<p>
1370          These containers also show how easy is to create/modify an existing container
1371          making possible to place it in shared memory.
1372        </p>
1373</div>
1374<div class="section">
1375<div class="titlepage"><div><div><h4 class="title">
1376<a name="interprocess.allocators_containers.containers_explained.where_allocate"></a><a class="link" href="allocators_containers.html#interprocess.allocators_containers.containers_explained.where_allocate" title="Where is this being allocated?">Where
1377        is this being allocated?</a>
1378</h4></div></div></div>
1379<p>
1380          <span class="bold"><strong>Boost.Interprocess</strong></span> containers are placed
1381          in shared memory/memory mapped files, etc... using two mechanisms <span class="bold"><strong>at the same time</strong></span>:
1382        </p>
1383<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
1384<li class="listitem">
1385              <span class="bold"><strong>Boost.Interprocess </strong></span><code class="computeroutput"><span class="identifier">construct</span><span class="special">&lt;&gt;</span></code>, <code class="computeroutput"><span class="identifier">find_or_construct</span><span class="special">&lt;&gt;</span></code>... functions. These functions
1386              place a C++ object in the shared memory/memory mapped file. But this
1387              places only the object, but <span class="bold"><strong>not</strong></span> the
1388              memory that this object may allocate dynamically.
1389            </li>
1390<li class="listitem">
1391              Shared memory allocators. These allow allocating shared memory/memory
1392              mapped file portions so that containers can allocate dynamically fragments
1393              of memory to store newly inserted elements.
1394            </li>
1395</ul></div>
1396<p>
1397          This means that to place any <span class="bold"><strong>Boost.Interprocess</strong></span>
1398          container (including <span class="bold"><strong>Boost.Interprocess</strong></span>
1399          strings) in shared memory or memory mapped files, containers <span class="bold"><strong>must</strong></span>:
1400        </p>
1401<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
1402<li class="listitem">
1403              Define their template allocator parameter to a <span class="bold"><strong>Boost.Interprocess</strong></span>
1404              allocator.
1405            </li>
1406<li class="listitem">
1407              Every container constructor must take the <span class="bold"><strong>Boost.Interprocess</strong></span>
1408              allocator as parameter.
1409            </li>
1410<li class="listitem">
1411              You must use construct&lt;&gt;/find_or_construct&lt;&gt;... functions
1412              to place the container in the managed memory.
1413            </li>
1414</ul></div>
1415<p>
1416          If you do the first two points but you don't use <code class="computeroutput"><span class="identifier">construct</span><span class="special">&lt;&gt;</span></code> or <code class="computeroutput"><span class="identifier">find_or_construct</span><span class="special">&lt;&gt;</span></code> you are creating a container placed
1417          <span class="bold"><strong>only</strong></span> in your process but that allocates
1418          memory for contained types from shared memory/memory mapped file.
1419        </p>
1420<p>
1421          Let's see an example:
1422        </p>
1423<p>
1424</p>
1425<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">managed_shared_memory</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1426<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">containers</span><span class="special">/</span><span class="identifier">vector</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1427<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">containers</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1428<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">allocators</span><span class="special">/</span><span class="identifier">allocator</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1429
1430<span class="keyword">int</span> <span class="identifier">main</span> <span class="special">()</span>
1431<span class="special">{</span>
1432   <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">interprocess</span><span class="special">;</span>
1433   <span class="comment">//Typedefs</span>
1434   <span class="keyword">typedef</span> <span class="identifier">allocator</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">,</span> <span class="identifier">managed_shared_memory</span><span class="special">::</span><span class="identifier">segment_manager</span><span class="special">&gt;</span>
1435      <span class="identifier">CharAllocator</span><span class="special">;</span>
1436   <span class="keyword">typedef</span> <span class="identifier">basic_string</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">char_traits</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">&gt;,</span> <span class="identifier">CharAllocator</span><span class="special">&gt;</span>
1437      <span class="identifier">MyShmString</span><span class="special">;</span>
1438   <span class="keyword">typedef</span> <span class="identifier">allocator</span><span class="special">&lt;</span><span class="identifier">MyShmString</span><span class="special">,</span> <span class="identifier">managed_shared_memory</span><span class="special">::</span><span class="identifier">segment_manager</span><span class="special">&gt;</span>
1439      <span class="identifier">StringAllocator</span><span class="special">;</span>
1440   <span class="keyword">typedef</span> <span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">MyShmString</span><span class="special">,</span> <span class="identifier">StringAllocator</span><span class="special">&gt;</span>
1441      <span class="identifier">MyShmStringVector</span><span class="special">;</span>
1442
1443   <span class="comment">//Open shared memory</span>
1444   <span class="comment">//Remove shared memory on construction and destruction</span>
1445   <span class="keyword">struct</span> <span class="identifier">shm_remove</span>
1446   <span class="special">{</span>
1447      <span class="identifier">shm_remove</span><span class="special">()</span> <span class="special">{</span> <span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"MySharedMemory"</span><span class="special">);</span> <span class="special">}</span>
1448      <span class="special">~</span><span class="identifier">shm_remove</span><span class="special">(){</span> <span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"MySharedMemory"</span><span class="special">);</span> <span class="special">}</span>
1449   <span class="special">}</span> <span class="identifier">remover</span><span class="special">;</span>
1450
1451   <span class="identifier">managed_shared_memory</span> <span class="identifier">shm</span><span class="special">(</span><span class="identifier">create_only</span><span class="special">,</span> <span class="string">"MySharedMemory"</span><span class="special">,</span> <span class="number">10000</span><span class="special">);</span>
1452
1453   <span class="comment">//Create allocators</span>
1454   <span class="identifier">CharAllocator</span>     <span class="identifier">charallocator</span>  <span class="special">(</span><span class="identifier">shm</span><span class="special">.</span><span class="identifier">get_segment_manager</span><span class="special">());</span>
1455   <span class="identifier">StringAllocator</span>   <span class="identifier">stringallocator</span><span class="special">(</span><span class="identifier">shm</span><span class="special">.</span><span class="identifier">get_segment_manager</span><span class="special">());</span>
1456
1457   <span class="comment">//This string is in only in this process (the pointer pointing to the</span>
1458   <span class="comment">//buffer that will hold the text is not in shared memory).</span>
1459   <span class="comment">//But the buffer that will hold "this is my text" is allocated from</span>
1460   <span class="comment">//shared memory</span>
1461   <span class="identifier">MyShmString</span> <span class="identifier">mystring</span><span class="special">(</span><span class="identifier">charallocator</span><span class="special">);</span>
1462   <span class="identifier">mystring</span> <span class="special">=</span> <span class="string">"this is my text"</span><span class="special">;</span>
1463
1464   <span class="comment">//This vector is only in this process (the pointer pointing to the</span>
1465   <span class="comment">//buffer that will hold the MyShmString-s is not in shared memory).</span>
1466   <span class="comment">//But the buffer that will hold 10 MyShmString-s is allocated from</span>
1467   <span class="comment">//shared memory using StringAllocator. Since strings use a shared</span>
1468   <span class="comment">//memory allocator (CharAllocator) the 10 buffers that hold</span>
1469   <span class="comment">//"this is my text" text are also in shared memory.</span>
1470   <span class="identifier">MyShmStringVector</span> <span class="identifier">myvector</span><span class="special">(</span><span class="identifier">stringallocator</span><span class="special">);</span>
1471   <span class="identifier">myvector</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">myvector</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="number">10</span><span class="special">,</span> <span class="identifier">mystring</span><span class="special">);</span>
1472
1473   <span class="comment">//This vector is fully constructed in shared memory. All pointers</span>
1474   <span class="comment">//buffers are constructed in the same shared memory segment</span>
1475   <span class="comment">//This vector can be safely accessed from other processes.</span>
1476   <span class="identifier">MyShmStringVector</span> <span class="special">*</span><span class="identifier">myshmvector</span> <span class="special">=</span>
1477      <span class="identifier">shm</span><span class="special">.</span><span class="identifier">construct</span><span class="special">&lt;</span><span class="identifier">MyShmStringVector</span><span class="special">&gt;(</span><span class="string">"myshmvector"</span><span class="special">)(</span><span class="identifier">stringallocator</span><span class="special">);</span>
1478   <span class="identifier">myshmvector</span><span class="special">-&gt;</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">myshmvector</span><span class="special">-&gt;</span><span class="identifier">begin</span><span class="special">(),</span> <span class="number">10</span><span class="special">,</span> <span class="identifier">mystring</span><span class="special">);</span>
1479
1480   <span class="comment">//Destroy vector. This will free all strings that the vector contains</span>
1481   <span class="identifier">shm</span><span class="special">.</span><span class="identifier">destroy_ptr</span><span class="special">(</span><span class="identifier">myshmvector</span><span class="special">);</span>
1482   <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
1483<span class="special">}</span>
1484</pre>
1485<p>
1486        </p>
1487</div>
1488<div class="section">
1489<div class="titlepage"><div><div><h4 class="title">
1490<a name="interprocess.allocators_containers.containers_explained.containers_and_move"></a><a class="link" href="allocators_containers.html#interprocess.allocators_containers.containers_explained.containers_and_move" title="Move semantics in Interprocess containers">Move
1491        semantics in Interprocess containers</a>
1492</h4></div></div></div>
1493<p>
1494          <span class="bold"><strong>Boost.Interprocess</strong></span> containers support
1495          move semantics, which means that the contents of a container can be moved
1496          from a container to another one, without any copying. The contents of the
1497          source container are transferred to the target container and the source
1498          container is left in default-constructed state.
1499        </p>
1500<p>
1501          When using containers of containers, we can also use move-semantics to
1502          insert objects in the container, avoiding unnecessary copies.
1503        </p>
1504<p>
1505          To transfer the contents of a container to another one, use <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">move</span><span class="special">()</span></code>
1506          function, as shown in the example. For more details about functions supporting
1507          move-semantics, see the reference section of Boost.Interprocess containers:
1508        </p>
1509<p>
1510</p>
1511<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">managed_shared_memory</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1512<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">containers</span><span class="special">/</span><span class="identifier">vector</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1513<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">containers</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1514<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">allocators</span><span class="special">/</span><span class="identifier">allocator</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1515<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>
1516
1517<span class="keyword">int</span> <span class="identifier">main</span> <span class="special">()</span>
1518<span class="special">{</span>
1519   <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">interprocess</span><span class="special">;</span>
1520
1521   <span class="comment">//Typedefs</span>
1522   <span class="keyword">typedef</span> <span class="identifier">managed_shared_memory</span><span class="special">::</span><span class="identifier">segment_manager</span>     <span class="identifier">SegmentManager</span><span class="special">;</span>
1523   <span class="keyword">typedef</span> <span class="identifier">allocator</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">,</span> <span class="identifier">SegmentManager</span><span class="special">&gt;</span>            <span class="identifier">CharAllocator</span><span class="special">;</span>
1524   <span class="keyword">typedef</span> <span class="identifier">basic_string</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">char_traits</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">&gt;</span>
1525                        <span class="special">,</span><span class="identifier">CharAllocator</span><span class="special">&gt;</span>                <span class="identifier">MyShmString</span><span class="special">;</span>
1526   <span class="keyword">typedef</span> <span class="identifier">allocator</span><span class="special">&lt;</span><span class="identifier">MyShmString</span><span class="special">,</span> <span class="identifier">SegmentManager</span><span class="special">&gt;</span>     <span class="identifier">StringAllocator</span><span class="special">;</span>
1527   <span class="keyword">typedef</span> <span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">MyShmString</span><span class="special">,</span> <span class="identifier">StringAllocator</span><span class="special">&gt;</span>       <span class="identifier">MyShmStringVector</span><span class="special">;</span>
1528
1529   <span class="comment">//Remove shared memory on construction and destruction</span>
1530   <span class="keyword">struct</span> <span class="identifier">shm_remove</span>
1531   <span class="special">{</span>
1532      <span class="identifier">shm_remove</span><span class="special">()</span> <span class="special">{</span> <span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"MySharedMemory"</span><span class="special">);</span> <span class="special">}</span>
1533      <span class="special">~</span><span class="identifier">shm_remove</span><span class="special">(){</span> <span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"MySharedMemory"</span><span class="special">);</span> <span class="special">}</span>
1534   <span class="special">}</span> <span class="identifier">remover</span><span class="special">;</span>
1535
1536   <span class="identifier">managed_shared_memory</span> <span class="identifier">shm</span><span class="special">(</span><span class="identifier">create_only</span><span class="special">,</span> <span class="string">"MySharedMemory"</span><span class="special">,</span> <span class="number">10000</span><span class="special">);</span>
1537
1538   <span class="comment">//Create allocators</span>
1539   <span class="identifier">CharAllocator</span>     <span class="identifier">charallocator</span>  <span class="special">(</span><span class="identifier">shm</span><span class="special">.</span><span class="identifier">get_segment_manager</span><span class="special">());</span>
1540   <span class="identifier">StringAllocator</span>   <span class="identifier">stringallocator</span><span class="special">(</span><span class="identifier">shm</span><span class="special">.</span><span class="identifier">get_segment_manager</span><span class="special">());</span>
1541
1542   <span class="comment">//Create a vector of strings in shared memory.</span>
1543   <span class="identifier">MyShmStringVector</span> <span class="special">*</span><span class="identifier">myshmvector</span> <span class="special">=</span>
1544      <span class="identifier">shm</span><span class="special">.</span><span class="identifier">construct</span><span class="special">&lt;</span><span class="identifier">MyShmStringVector</span><span class="special">&gt;(</span><span class="string">"myshmvector"</span><span class="special">)(</span><span class="identifier">stringallocator</span><span class="special">);</span>
1545
1546   <span class="comment">//Insert 50 strings in shared memory. The strings will be allocated</span>
1547   <span class="comment">//only once and no string copy-constructor will be called when inserting</span>
1548   <span class="comment">//strings, leading to a great performance.</span>
1549   <span class="identifier">MyShmString</span> <span class="identifier">string_to_compare</span><span class="special">(</span><span class="identifier">charallocator</span><span class="special">);</span>
1550   <span class="identifier">string_to_compare</span> <span class="special">=</span> <span class="string">"this is a long, long, long, long, long, long, string..."</span><span class="special">;</span>
1551
1552   <span class="identifier">myshmvector</span><span class="special">-&gt;</span><span class="identifier">reserve</span><span class="special">(</span><span class="number">50</span><span class="special">);</span>
1553   <span class="keyword">for</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="number">50</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">){</span>
1554      <span class="identifier">MyShmString</span> <span class="identifier">move_me</span><span class="special">(</span><span class="identifier">string_to_compare</span><span class="special">);</span>
1555      <span class="comment">//In the following line, no string copy-constructor will be called.</span>
1556      <span class="comment">//"move_me"'s contents will be transferred to the string created in</span>
1557      <span class="comment">//the vector</span>
1558      <span class="identifier">myshmvector</span><span class="special">-&gt;</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">move_me</span><span class="special">));</span>
1559
1560      <span class="comment">//The source string is in default constructed state</span>
1561      <span class="identifier">assert</span><span class="special">(</span><span class="identifier">move_me</span><span class="special">.</span><span class="identifier">empty</span><span class="special">());</span>
1562
1563      <span class="comment">//The newly created string will be equal to the "move_me"'s old contents</span>
1564      <span class="identifier">assert</span><span class="special">(</span><span class="identifier">myshmvector</span><span class="special">-&gt;</span><span class="identifier">back</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">string_to_compare</span><span class="special">);</span>
1565   <span class="special">}</span>
1566
1567   <span class="comment">//Now erase a string...</span>
1568   <span class="identifier">myshmvector</span><span class="special">-&gt;</span><span class="identifier">pop_back</span><span class="special">();</span>
1569
1570   <span class="comment">//...And insert one in the first position.</span>
1571   <span class="comment">//No string copy-constructor or assignments will be called, but</span>
1572   <span class="comment">//move constructors and move-assignments. No memory allocation</span>
1573   <span class="comment">//function will be called in this operations!!</span>
1574   <span class="identifier">myshmvector</span><span class="special">-&gt;</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">myshmvector</span><span class="special">-&gt;</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">string_to_compare</span><span class="special">));</span>
1575
1576   <span class="comment">//Destroy vector. This will free all strings that the vector contains</span>
1577   <span class="identifier">shm</span><span class="special">.</span><span class="identifier">destroy_ptr</span><span class="special">(</span><span class="identifier">myshmvector</span><span class="special">);</span>
1578   <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
1579<span class="special">}</span>
1580</pre>
1581<p>
1582        </p>
1583</div>
1584<div class="section">
1585<div class="titlepage"><div><div><h4 class="title">
1586<a name="interprocess.allocators_containers.containers_explained.containers_of_containers"></a><a class="link" href="allocators_containers.html#interprocess.allocators_containers.containers_explained.containers_of_containers" title="Containers of containers">Containers
1587        of containers</a>
1588</h4></div></div></div>
1589<p>
1590          When creating containers of containers, each container needs an allocator.
1591          To avoid using several allocators with complex type definitions, we can
1592          take advantage of the type erasure provided by void allocators and the
1593          ability to implicitly convert void allocators in allocators that allocate
1594          other types.
1595        </p>
1596<p>
1597          Here we have an example that builds a map in shared memory. Key is a string
1598          and the mapped type is a class that stores several containers:
1599        </p>
1600<p>
1601</p>
1602<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">managed_shared_memory</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1603<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">allocators</span><span class="special">/</span><span class="identifier">allocator</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1604<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">containers</span><span class="special">/</span><span class="identifier">map</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1605<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">containers</span><span class="special">/</span><span class="identifier">vector</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1606<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">containers</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1607
1608<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">interprocess</span><span class="special">;</span>
1609
1610<span class="comment">//Typedefs of allocators and containers</span>
1611<span class="keyword">typedef</span> <span class="identifier">managed_shared_memory</span><span class="special">::</span><span class="identifier">segment_manager</span>                       <span class="identifier">segment_manager_t</span><span class="special">;</span>
1612<span class="keyword">typedef</span> <span class="identifier">allocator</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">,</span> <span class="identifier">segment_manager_t</span><span class="special">&gt;</span>                           <span class="identifier">void_allocator</span><span class="special">;</span>
1613<span class="keyword">typedef</span> <span class="identifier">allocator</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">segment_manager_t</span><span class="special">&gt;</span>                            <span class="identifier">int_allocator</span><span class="special">;</span>
1614<span class="keyword">typedef</span> <span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">int_allocator</span><span class="special">&gt;</span>                                   <span class="identifier">int_vector</span><span class="special">;</span>
1615<span class="keyword">typedef</span> <span class="identifier">allocator</span><span class="special">&lt;</span><span class="identifier">int_vector</span><span class="special">,</span> <span class="identifier">segment_manager_t</span><span class="special">&gt;</span>                     <span class="identifier">int_vector_allocator</span><span class="special">;</span>
1616<span class="keyword">typedef</span> <span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">int_vector</span><span class="special">,</span> <span class="identifier">int_vector_allocator</span><span class="special">&gt;</span>                     <span class="identifier">int_vector_vector</span><span class="special">;</span>
1617<span class="keyword">typedef</span> <span class="identifier">allocator</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">,</span> <span class="identifier">segment_manager_t</span><span class="special">&gt;</span>                           <span class="identifier">char_allocator</span><span class="special">;</span>
1618<span class="keyword">typedef</span> <span class="identifier">basic_string</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">char_traits</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">&gt;,</span> <span class="identifier">char_allocator</span><span class="special">&gt;</span>   <span class="identifier">char_string</span><span class="special">;</span>
1619
1620<span class="keyword">class</span> <span class="identifier">complex_data</span>
1621<span class="special">{</span>
1622   <span class="keyword">int</span>               <span class="identifier">id_</span><span class="special">;</span>
1623   <span class="identifier">char_string</span>       <span class="identifier">char_string_</span><span class="special">;</span>
1624   <span class="identifier">int_vector_vector</span> <span class="identifier">int_vector_vector_</span><span class="special">;</span>
1625
1626   <span class="keyword">public</span><span class="special">:</span>
1627   <span class="comment">//Since void_allocator is convertible to any other allocator&lt;T&gt;, we can simplify</span>
1628   <span class="comment">//the initialization taking just one allocator for all inner containers.</span>
1629   <span class="identifier">complex_data</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">id</span><span class="special">,</span> <span class="keyword">const</span> <span class="keyword">char</span> <span class="special">*</span><span class="identifier">name</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">void_allocator</span> <span class="special">&amp;</span><span class="identifier">void_alloc</span><span class="special">)</span>
1630      <span class="special">:</span> <span class="identifier">id_</span><span class="special">(</span><span class="identifier">id</span><span class="special">),</span> <span class="identifier">char_string_</span><span class="special">(</span><span class="identifier">name</span><span class="special">,</span> <span class="identifier">void_alloc</span><span class="special">),</span> <span class="identifier">int_vector_vector_</span><span class="special">(</span><span class="identifier">void_alloc</span><span class="special">)</span>
1631   <span class="special">{}</span>
1632   <span class="comment">//Other members...</span>
1633<span class="special">};</span>
1634
1635<span class="comment">//Definition of the map holding a string as key and complex_data as mapped type</span>
1636<span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="keyword">const</span> <span class="identifier">char_string</span><span class="special">,</span> <span class="identifier">complex_data</span><span class="special">&gt;</span>                      <span class="identifier">map_value_type</span><span class="special">;</span>
1637<span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="identifier">char_string</span><span class="special">,</span> <span class="identifier">complex_data</span><span class="special">&gt;</span>                            <span class="identifier">movable_to_map_value_type</span><span class="special">;</span>
1638<span class="keyword">typedef</span> <span class="identifier">allocator</span><span class="special">&lt;</span><span class="identifier">map_value_type</span><span class="special">,</span> <span class="identifier">segment_manager_t</span><span class="special">&gt;</span>                    <span class="identifier">map_value_type_allocator</span><span class="special">;</span>
1639<span class="keyword">typedef</span> <span class="identifier">map</span><span class="special">&lt;</span> <span class="identifier">char_string</span><span class="special">,</span> <span class="identifier">complex_data</span>
1640           <span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">less</span><span class="special">&lt;</span><span class="identifier">char_string</span><span class="special">&gt;,</span> <span class="identifier">map_value_type_allocator</span><span class="special">&gt;</span>          <span class="identifier">complex_map_type</span><span class="special">;</span>
1641
1642<span class="keyword">int</span> <span class="identifier">main</span> <span class="special">()</span>
1643<span class="special">{</span>
1644   <span class="comment">//Remove shared memory on construction and destruction</span>
1645   <span class="keyword">struct</span> <span class="identifier">shm_remove</span>
1646   <span class="special">{</span>
1647      <span class="identifier">shm_remove</span><span class="special">()</span> <span class="special">{</span> <span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"MySharedMemory"</span><span class="special">);</span> <span class="special">}</span>
1648      <span class="special">~</span><span class="identifier">shm_remove</span><span class="special">(){</span> <span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"MySharedMemory"</span><span class="special">);</span> <span class="special">}</span>
1649   <span class="special">}</span> <span class="identifier">remover</span><span class="special">;</span>
1650
1651   <span class="comment">//Create shared memory</span>
1652   <span class="identifier">managed_shared_memory</span> <span class="identifier">segment</span><span class="special">(</span><span class="identifier">create_only</span><span class="special">,</span><span class="string">"MySharedMemory"</span><span class="special">,</span> <span class="number">65536</span><span class="special">);</span>
1653
1654   <span class="comment">//An allocator convertible to any allocator&lt;T, segment_manager_t&gt; type</span>
1655   <span class="identifier">void_allocator</span> <span class="identifier">alloc_inst</span> <span class="special">(</span><span class="identifier">segment</span><span class="special">.</span><span class="identifier">get_segment_manager</span><span class="special">());</span>
1656
1657   <span class="comment">//Construct the shared memory map and fill it</span>
1658   <span class="identifier">complex_map_type</span> <span class="special">*</span><span class="identifier">mymap</span> <span class="special">=</span> <span class="identifier">segment</span><span class="special">.</span><span class="identifier">construct</span><span class="special">&lt;</span><span class="identifier">complex_map_type</span><span class="special">&gt;</span>
1659      <span class="comment">//(object name), (first ctor parameter, second ctor parameter)</span>
1660         <span class="special">(</span><span class="string">"MyMap"</span><span class="special">)(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">less</span><span class="special">&lt;</span><span class="identifier">char_string</span><span class="special">&gt;(),</span> <span class="identifier">alloc_inst</span><span class="special">);</span>
1661
1662   <span class="keyword">for</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="number">100</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">){</span>
1663      <span class="comment">//Both key(string) and value(complex_data) need an allocator in their constructors</span>
1664      <span class="identifier">char_string</span>  <span class="identifier">key_object</span><span class="special">(</span><span class="identifier">alloc_inst</span><span class="special">);</span>
1665      <span class="identifier">complex_data</span> <span class="identifier">mapped_object</span><span class="special">(</span><span class="identifier">i</span><span class="special">,</span> <span class="string">"default_name"</span><span class="special">,</span> <span class="identifier">alloc_inst</span><span class="special">);</span>
1666      <span class="identifier">map_value_type</span> <span class="identifier">value</span><span class="special">(</span><span class="identifier">key_object</span><span class="special">,</span> <span class="identifier">mapped_object</span><span class="special">);</span>
1667      <span class="comment">//Modify values and insert them in the map</span>
1668      <span class="identifier">mymap</span><span class="special">-&gt;</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">value</span><span class="special">);</span>
1669   <span class="special">}</span>
1670   <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
1671<span class="special">}</span>
1672</pre>
1673<p>
1674        </p>
1675</div>
1676</div>
1677<div class="section">
1678<div class="titlepage"><div><div><h3 class="title">
1679<a name="interprocess.allocators_containers.additional_containers"></a><a class="link" href="allocators_containers.html#interprocess.allocators_containers.additional_containers" title="Boost containers compatible with Boost.Interprocess">Boost
1680      containers compatible with Boost.Interprocess</a>
1681</h3></div></div></div>
1682<div class="toc"><dl class="toc">
1683<dt><span class="section"><a href="allocators_containers.html#interprocess.allocators_containers.additional_containers.unordered">Boost
1684        unordered containers</a></span></dt>
1685<dt><span class="section"><a href="allocators_containers.html#interprocess.allocators_containers.additional_containers.multi_index">Boost.MultiIndex
1686        containers</a></span></dt>
1687</dl></div>
1688<p>
1689        As mentioned, container developers might need to change their implementation
1690        to make them compatible with Boost.Interprocess, because implementation usually
1691        ignore allocators with smart pointers. Hopefully several Boost containers
1692        are compatible with <span class="bold"><strong>Interprocess</strong></span>.
1693      </p>
1694<div class="section">
1695<div class="titlepage"><div><div><h4 class="title">
1696<a name="interprocess.allocators_containers.additional_containers.unordered"></a><a class="link" href="allocators_containers.html#interprocess.allocators_containers.additional_containers.unordered" title="Boost unordered containers">Boost
1697        unordered containers</a>
1698</h4></div></div></div>
1699<p>
1700          <span class="bold"><strong>Boost.Unordered</strong></span> containers are compatible
1701          with Interprocess, so programmers can store hash containers in shared memory
1702          and memory mapped files. Here is a small example storing <code class="computeroutput"><span class="identifier">unordered_map</span></code> in shared memory:
1703        </p>
1704<p>
1705</p>
1706<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">managed_shared_memory</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1707<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">allocators</span><span class="special">/</span><span class="identifier">allocator</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1708
1709
1710<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">unordered_map</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>     <span class="comment">//boost::unordered_map</span>
1711
1712
1713<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">functional</span><span class="special">&gt;</span>                  <span class="comment">//std::equal_to</span>
1714<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">functional</span><span class="special">/</span><span class="identifier">hash</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>   <span class="comment">//boost::hash</span>
1715
1716
1717<span class="keyword">int</span> <span class="identifier">main</span> <span class="special">()</span>
1718<span class="special">{</span>
1719   <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">interprocess</span><span class="special">;</span>
1720   <span class="comment">//Remove shared memory on construction and destruction</span>
1721   <span class="keyword">struct</span> <span class="identifier">shm_remove</span>
1722   <span class="special">{</span>
1723      <span class="identifier">shm_remove</span><span class="special">()</span> <span class="special">{</span> <span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"MySharedMemory"</span><span class="special">);</span> <span class="special">}</span>
1724      <span class="special">~</span><span class="identifier">shm_remove</span><span class="special">(){</span> <span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"MySharedMemory"</span><span class="special">);</span> <span class="special">}</span>
1725   <span class="special">}</span> <span class="identifier">remover</span><span class="special">;</span>
1726
1727   <span class="comment">//Create shared memory</span>
1728   <span class="identifier">managed_shared_memory</span> <span class="identifier">segment</span><span class="special">(</span><span class="identifier">create_only</span><span class="special">,</span> <span class="string">"MySharedMemory"</span><span class="special">,</span> <span class="number">65536</span><span class="special">);</span>
1729
1730   <span class="comment">//Note that unordered_map&lt;Key, MappedType&gt;'s value_type is std::pair&lt;const Key, MappedType&gt;,</span>
1731   <span class="comment">//so the allocator must allocate that pair.</span>
1732   <span class="keyword">typedef</span> <span class="keyword">int</span>    <span class="identifier">KeyType</span><span class="special">;</span>
1733   <span class="keyword">typedef</span> <span class="keyword">float</span>  <span class="identifier">MappedType</span><span class="special">;</span>
1734   <span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="keyword">const</span> <span class="keyword">int</span><span class="special">,</span> <span class="keyword">float</span><span class="special">&gt;</span> <span class="identifier">ValueType</span><span class="special">;</span>
1735
1736   <span class="comment">//Typedef the allocator</span>
1737   <span class="keyword">typedef</span> <span class="identifier">allocator</span><span class="special">&lt;</span><span class="identifier">ValueType</span><span class="special">,</span> <span class="identifier">managed_shared_memory</span><span class="special">::</span><span class="identifier">segment_manager</span><span class="special">&gt;</span> <span class="identifier">ShmemAllocator</span><span class="special">;</span>
1738
1739   <span class="comment">//Alias an unordered_map of ints that uses the previous STL-like allocator.</span>
1740   <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">unordered_map</span>
1741      <span class="special">&lt;</span> <span class="identifier">KeyType</span>               <span class="special">,</span> <span class="identifier">MappedType</span>
1742      <span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">hash</span><span class="special">&lt;</span><span class="identifier">KeyType</span><span class="special">&gt;</span>  <span class="special">,</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">equal_to</span><span class="special">&lt;</span><span class="identifier">KeyType</span><span class="special">&gt;</span>
1743      <span class="special">,</span> <span class="identifier">ShmemAllocator</span><span class="special">&gt;</span>
1744   <span class="identifier">MyHashMap</span><span class="special">;</span>
1745
1746   <span class="comment">//Construct a shared memory hash map.</span>
1747   <span class="comment">//Note that the first parameter is the initial bucket count and</span>
1748   <span class="comment">//after that, the hash function, the equality function and the allocator</span>
1749   <span class="identifier">MyHashMap</span> <span class="special">*</span><span class="identifier">myhashmap</span> <span class="special">=</span> <span class="identifier">segment</span><span class="special">.</span><span class="identifier">construct</span><span class="special">&lt;</span><span class="identifier">MyHashMap</span><span class="special">&gt;(</span><span class="string">"MyHashMap"</span><span class="special">)</span>  <span class="comment">//object name</span>
1750      <span class="special">(</span> <span class="number">3</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">hash</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;(),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">equal_to</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;()</span>                  <span class="comment">//</span>
1751      <span class="special">,</span> <span class="identifier">segment</span><span class="special">.</span><span class="identifier">get_allocator</span><span class="special">&lt;</span><span class="identifier">ValueType</span><span class="special">&gt;());</span>                         <span class="comment">//allocator instance</span>
1752
1753   <span class="comment">//Insert data in the hash map</span>
1754   <span class="keyword">for</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="number">100</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">){</span>
1755      <span class="identifier">myhashmap</span><span class="special">-&gt;</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">ValueType</span><span class="special">(</span><span class="identifier">i</span><span class="special">,</span> <span class="special">(</span><span class="keyword">float</span><span class="special">)</span><span class="identifier">i</span><span class="special">));</span>
1756   <span class="special">}</span>
1757   <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
1758<span class="special">}</span>
1759</pre>
1760<p>
1761        </p>
1762</div>
1763<div class="section">
1764<div class="titlepage"><div><div><h4 class="title">
1765<a name="interprocess.allocators_containers.additional_containers.multi_index"></a><a class="link" href="allocators_containers.html#interprocess.allocators_containers.additional_containers.multi_index" title="Boost.MultiIndex containers">Boost.MultiIndex
1766        containers</a>
1767</h4></div></div></div>
1768<p>
1769          The widely used <span class="bold"><strong>Boost.MultiIndex</strong></span> library
1770          is compatible with <span class="bold"><strong>Boost.Interprocess</strong></span>
1771          so we can construct pretty good databases in shared memory. Constructing
1772          databases in shared memory is a bit tougher than in normal memory, usually
1773          because those databases contain strings and those strings need to be placed
1774          in shared memory. Shared memory strings require an allocator in their constructors
1775          so this usually makes object insertion a bit more complicated.
1776        </p>
1777<p>
1778          Here is an example that shows how to put a multi index container in shared
1779          memory:
1780        </p>
1781<p>
1782</p>
1783<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">managed_shared_memory</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1784<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">allocators</span><span class="special">/</span><span class="identifier">allocator</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1785<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">containers</span><span class="special">/</span><span class="identifier">string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1786
1787
1788<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multi_index_container</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1789<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multi_index</span><span class="special">/</span><span class="identifier">member</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1790<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multi_index</span><span class="special">/</span><span class="identifier">ordered_index</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
1791
1792
1793<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">interprocess</span><span class="special">;</span>
1794<span class="keyword">namespace</span> <span class="identifier">bmi</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multi_index</span><span class="special">;</span>
1795
1796<span class="keyword">typedef</span> <span class="identifier">managed_shared_memory</span><span class="special">::</span><span class="identifier">allocator</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">&gt;::</span><span class="identifier">type</span>              <span class="identifier">char_allocator</span><span class="special">;</span>
1797<span class="keyword">typedef</span> <span class="identifier">basic_string</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">char_traits</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">&gt;,</span> <span class="identifier">char_allocator</span><span class="special">&gt;</span><span class="identifier">shm_string</span><span class="special">;</span>
1798
1799<span class="comment">//Data to insert in shared memory</span>
1800<span class="keyword">struct</span> <span class="identifier">employee</span>
1801<span class="special">{</span>
1802   <span class="keyword">int</span>         <span class="identifier">id</span><span class="special">;</span>
1803   <span class="keyword">int</span>         <span class="identifier">age</span><span class="special">;</span>
1804   <span class="identifier">shm_string</span>  <span class="identifier">name</span><span class="special">;</span>
1805   <span class="identifier">employee</span><span class="special">(</span> <span class="keyword">int</span> <span class="identifier">id_</span>
1806           <span class="special">,</span> <span class="keyword">int</span> <span class="identifier">age_</span>
1807           <span class="special">,</span> <span class="keyword">const</span> <span class="keyword">char</span> <span class="special">*</span><span class="identifier">name_</span>
1808           <span class="special">,</span> <span class="keyword">const</span> <span class="identifier">char_allocator</span> <span class="special">&amp;</span><span class="identifier">a</span><span class="special">)</span>
1809      <span class="special">:</span> <span class="identifier">id</span><span class="special">(</span><span class="identifier">id_</span><span class="special">),</span> <span class="identifier">age</span><span class="special">(</span><span class="identifier">age_</span><span class="special">),</span> <span class="identifier">name</span><span class="special">(</span><span class="identifier">name_</span><span class="special">,</span> <span class="identifier">a</span><span class="special">)</span>
1810   <span class="special">{}</span>
1811<span class="special">};</span>
1812
1813<span class="comment">//Tags</span>
1814<span class="keyword">struct</span> <span class="identifier">id</span><span class="special">{};</span>
1815<span class="keyword">struct</span> <span class="identifier">age</span><span class="special">{};</span>
1816<span class="keyword">struct</span> <span class="identifier">name</span><span class="special">{};</span>
1817
1818<span class="comment">// Define a multi_index_container of employees with following indices:</span>
1819<span class="comment">//   - a unique index sorted by employee::int,</span>
1820<span class="comment">//   - a non-unique index sorted by employee::name,</span>
1821<span class="comment">//   - a non-unique index sorted by employee::age.</span>
1822<span class="keyword">typedef</span> <span class="identifier">bmi</span><span class="special">::</span><span class="identifier">multi_index_container</span><span class="special">&lt;</span>
1823  <span class="identifier">employee</span><span class="special">,</span>
1824  <span class="identifier">bmi</span><span class="special">::</span><span class="identifier">indexed_by</span><span class="special">&lt;</span>
1825    <span class="identifier">bmi</span><span class="special">::</span><span class="identifier">ordered_unique</span>
1826      <span class="special">&lt;</span><span class="identifier">bmi</span><span class="special">::</span><span class="identifier">tag</span><span class="special">&lt;</span><span class="identifier">id</span><span class="special">&gt;,</span>  <span class="identifier">bmi</span><span class="special">::</span><span class="identifier">member</span><span class="special">&lt;</span><span class="identifier">employee</span><span class="special">,</span><span class="keyword">int</span><span class="special">,&amp;</span><span class="identifier">employee</span><span class="special">::</span><span class="identifier">id</span><span class="special">&gt;</span> <span class="special">&gt;,</span>
1827    <span class="identifier">bmi</span><span class="special">::</span><span class="identifier">ordered_non_unique</span><span class="special">&lt;</span>
1828      <span class="identifier">bmi</span><span class="special">::</span><span class="identifier">tag</span><span class="special">&lt;</span><span class="identifier">name</span><span class="special">&gt;,</span> <span class="identifier">bmi</span><span class="special">::</span><span class="identifier">member</span><span class="special">&lt;</span><span class="identifier">employee</span><span class="special">,</span><span class="identifier">shm_string</span><span class="special">,&amp;</span><span class="identifier">employee</span><span class="special">::</span><span class="identifier">name</span><span class="special">&gt;</span> <span class="special">&gt;,</span>
1829    <span class="identifier">bmi</span><span class="special">::</span><span class="identifier">ordered_non_unique</span>
1830      <span class="special">&lt;</span><span class="identifier">bmi</span><span class="special">::</span><span class="identifier">tag</span><span class="special">&lt;</span><span class="identifier">age</span><span class="special">&gt;,</span> <span class="identifier">bmi</span><span class="special">::</span><span class="identifier">member</span><span class="special">&lt;</span><span class="identifier">employee</span><span class="special">,</span><span class="keyword">int</span><span class="special">,&amp;</span><span class="identifier">employee</span><span class="special">::</span><span class="identifier">age</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="special">&gt;,</span>
1831  <span class="identifier">managed_shared_memory</span><span class="special">::</span><span class="identifier">allocator</span><span class="special">&lt;</span><span class="identifier">employee</span><span class="special">&gt;::</span><span class="identifier">type</span>
1832<span class="special">&gt;</span> <span class="identifier">employee_set</span><span class="special">;</span>
1833
1834<span class="keyword">int</span> <span class="identifier">main</span> <span class="special">()</span>
1835<span class="special">{</span>
1836   <span class="comment">//Remove shared memory on construction and destruction</span>
1837   <span class="keyword">struct</span> <span class="identifier">shm_remove</span>
1838   <span class="special">{</span>
1839      <span class="identifier">shm_remove</span><span class="special">()</span> <span class="special">{</span> <span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"MySharedMemory"</span><span class="special">);</span> <span class="special">}</span>
1840      <span class="special">~</span><span class="identifier">shm_remove</span><span class="special">(){</span> <span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="string">"MySharedMemory"</span><span class="special">);</span> <span class="special">}</span>
1841   <span class="special">}</span> <span class="identifier">remover</span><span class="special">;</span>
1842
1843   <span class="comment">//Create shared memory</span>
1844   <span class="identifier">managed_shared_memory</span> <span class="identifier">segment</span><span class="special">(</span><span class="identifier">create_only</span><span class="special">,</span><span class="string">"MySharedMemory"</span><span class="special">,</span> <span class="number">65536</span><span class="special">);</span>
1845
1846   <span class="comment">//Construct the multi_index in shared memory</span>
1847   <span class="identifier">employee_set</span> <span class="special">*</span><span class="identifier">es</span> <span class="special">=</span> <span class="identifier">segment</span><span class="special">.</span><span class="identifier">construct</span><span class="special">&lt;</span><span class="identifier">employee_set</span><span class="special">&gt;</span>
1848      <span class="special">(</span><span class="string">"My MultiIndex Container"</span><span class="special">)</span>            <span class="comment">//Container's name in shared memory</span>
1849      <span class="special">(</span> <span class="identifier">employee_set</span><span class="special">::</span><span class="identifier">ctor_args_list</span><span class="special">()</span>
1850      <span class="special">,</span> <span class="identifier">segment</span><span class="special">.</span><span class="identifier">get_allocator</span><span class="special">&lt;</span><span class="identifier">employee</span><span class="special">&gt;());</span>  <span class="comment">//Ctor parameters</span>
1851
1852   <span class="comment">//Now insert elements</span>
1853   <span class="identifier">char_allocator</span> <span class="identifier">ca</span><span class="special">(</span><span class="identifier">segment</span><span class="special">.</span><span class="identifier">get_allocator</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">&gt;());</span>
1854   <span class="identifier">es</span><span class="special">-&gt;</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">employee</span><span class="special">(</span><span class="number">0</span><span class="special">,</span><span class="number">31</span><span class="special">,</span> <span class="string">"Joe"</span><span class="special">,</span> <span class="identifier">ca</span><span class="special">));</span>
1855   <span class="identifier">es</span><span class="special">-&gt;</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">employee</span><span class="special">(</span><span class="number">1</span><span class="special">,</span><span class="number">27</span><span class="special">,</span> <span class="string">"Robert"</span><span class="special">,</span> <span class="identifier">ca</span><span class="special">));</span>
1856   <span class="identifier">es</span><span class="special">-&gt;</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">employee</span><span class="special">(</span><span class="number">2</span><span class="special">,</span><span class="number">40</span><span class="special">,</span> <span class="string">"John"</span><span class="special">,</span> <span class="identifier">ca</span><span class="special">));</span>
1857   <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
1858<span class="special">}</span>
1859</pre>
1860<p>
1861        </p>
1862</div>
1863<p>
1864        Programmers can place <span class="bold"><strong>Boost.CircularBuffer</strong></span>
1865        containers in sharecd memory provided they disable debugging facilities with
1866        defines <code class="computeroutput"><span class="identifier">BOOST_CB_DISABLE_DEBUG</span></code>
1867        or the more general <code class="computeroutput"><span class="identifier">NDEBUG</span></code>.
1868        The reason is that those debugging facilities are only compatible with raw
1869        pointers.
1870      </p>
1871</div>
1872</div>
1873<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
1874<td align="left"></td>
1875<td align="right"><div class="copyright-footer">Copyright © 2005-2015 Ion Gaztanaga<p>
1876        Distributed under the Boost Software License, Version 1.0. (See accompanying
1877        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
1878      </p>
1879</div></td>
1880</tr></table>
1881<hr>
1882<div class="spirit-nav">
1883<a accesskey="p" href="managed_memory_segments.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../interprocess.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="memory_algorithms.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
1884</div>
1885</body>
1886</html>
1887