boost-react-native-bundle
Version:
Boost library as in https://sourceforge.net/projects/boost/files/boost/1.57.0/
622 lines (616 loc) • 257 kB
HTML
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Managed Memory Segments</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../interprocess.html" title="Chapter 14. Boost.Interprocess">
<link rel="prev" href="synchronization_mechanisms.html" title="Synchronization mechanisms">
<link rel="next" href="allocators_containers.html" title="Allocators, containers and memory allocation algorithms">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="synchronization_mechanisms.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="allocators_containers.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="interprocess.managed_memory_segments"></a><a class="link" href="managed_memory_segments.html" title="Managed Memory Segments">Managed Memory Segments</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="managed_memory_segments.html#interprocess.managed_memory_segments.making_ipc_easy">Making
Interprocess Data Communication Easy</a></span></dt>
<dt><span class="section"><a href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_shared_memory">Managed
Shared Memory</a></span></dt>
<dt><span class="section"><a href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_mapped_files">Managed
Mapped File</a></span></dt>
<dt><span class="section"><a href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_memory_segment_features">Managed
Memory Segment Features</a></span></dt>
<dt><span class="section"><a href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_memory_segment_advanced_features">Managed
Memory Segment Advanced Features</a></span></dt>
<dt><span class="section"><a href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_heap_memory_external_buffer">Managed
Heap Memory And Managed External Buffer</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="interprocess.managed_memory_segments.making_ipc_easy"></a><a class="link" href="managed_memory_segments.html#interprocess.managed_memory_segments.making_ipc_easy" title="Making Interprocess Data Communication Easy">Making
Interprocess Data Communication Easy</a>
</h3></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="managed_memory_segments.html#interprocess.managed_memory_segments.making_ipc_easy.managed_memory_segments_intro">Introduction</a></span></dt>
<dt><span class="section"><a href="managed_memory_segments.html#interprocess.managed_memory_segments.making_ipc_easy.managed_memory_segment_int">Declaration
of managed memory segment classes</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="interprocess.managed_memory_segments.making_ipc_easy.managed_memory_segments_intro"></a><a class="link" href="managed_memory_segments.html#interprocess.managed_memory_segments.making_ipc_easy.managed_memory_segments_intro" title="Introduction">Introduction</a>
</h4></div></div></div>
<p>
As we have seen, <span class="bold"><strong>Boost.Interprocess</strong></span> offers
some basic classes to create shared memory objects and file mappings and
map those mappable classes to the process' address space.
</p>
<p>
However, managing those memory segments is not not easy for non-trivial
tasks. A mapped region is a fixed-length memory buffer and creating and
destroying objects of any type dynamically, requires a lot of work, since
it would require programming a memory management algorithm to allocate
portions of that segment. Many times, we also want to associate names to
objects created in shared memory, so all the processes can find the object
using the name.
</p>
<p>
<span class="bold"><strong>Boost.Interprocess</strong></span> offers 4 managed memory
segment classes:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
To manage a shared memory mapped region (<span class="bold"><strong>basic_managed_shared_memory</strong></span>
class).
</li>
<li class="listitem">
To manage a memory mapped file (<span class="bold"><strong>basic_managed_mapped_file</strong></span>).
</li>
<li class="listitem">
To manage a heap allocated (<code class="computeroutput"><span class="keyword">operator</span>
<span class="keyword">new</span></code>) memory buffer (<span class="bold"><strong>basic_managed_heap_memory</strong></span> class).
</li>
<li class="listitem">
To manage a user provided fixed size buffer (<span class="bold"><strong>basic_managed_external_buffer</strong></span>
class).
</li>
</ul></div>
<p>
The first two classes manage memory segments that can be shared between
processes. The third is useful to create complex data-bases to be sent
though other mechanisms like message queues to other processes. The fourth
class can manage any fixed size memory buffer. The first two classes will
be explained in the next two sections. <span class="bold"><strong>basic_managed_heap_memory</strong></span>
and <span class="bold"><strong>basic_managed_external_buffer</strong></span> will
be explained later.
</p>
<p>
The most important services of a managed memory segment are:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
Dynamic allocation of portions of a memory the segment.
</li>
<li class="listitem">
Construction of C++ objects in the memory segment. These objects can
be anonymous or we can associate a name to them.
</li>
<li class="listitem">
Searching capabilities for named objects.
</li>
<li class="listitem">
Customization of many features: memory allocation algorithm, index
types or character types.
</li>
<li class="listitem">
Atomic constructions and destructions so that if the segment is shared
between two processes it's impossible to create two objects associated
with the same name, simplifying synchronization.
</li>
</ul></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="interprocess.managed_memory_segments.making_ipc_easy.managed_memory_segment_int"></a><a class="link" href="managed_memory_segments.html#interprocess.managed_memory_segments.making_ipc_easy.managed_memory_segment_int" title="Declaration of managed memory segment classes">Declaration
of managed memory segment classes</a>
</h4></div></div></div>
<p>
All <span class="bold"><strong>Boost.Interprocess</strong></span> managed memory
segment classes are templatized classes that can be customized by the user:
</p>
<pre class="programlisting"><span class="keyword">template</span>
<span class="special"><</span>
<span class="keyword">class</span> <span class="identifier">CharType</span><span class="special">,</span>
<span class="keyword">class</span> <span class="identifier">MemoryAlgorithm</span><span class="special">,</span>
<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">IndexConfig</span><span class="special">></span> <span class="keyword">class</span> <span class="identifier">IndexType</span>
<span class="special">></span>
<span class="keyword">class</span> <span class="identifier">basic_managed_shared_memory</span> <span class="special">/</span> <span class="identifier">basic_managed_mapped_file</span> <span class="special">/</span>
<span class="identifier">basic_managed_heap_memory</span> <span class="special">/</span> <span class="identifier">basic_external_buffer</span><span class="special">;</span>
</pre>
<p>
These classes can be customized with the following template parameters:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
<span class="bold"><strong>CharType</strong></span> is the type of the character
that will be used to identify the created named objects (for example,
<span class="bold"><strong>char</strong></span> or <span class="bold"><strong>wchar_t</strong></span>)
</li>
<li class="listitem">
<span class="bold"><strong>MemoryAlgorithm</strong></span> is the memory algorithm
used to allocate portions of the segment (for example, rbtree_best_fit
). The internal typedefs of the memory algorithm also define:
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; ">
<li class="listitem">
The synchronization type (<code class="computeroutput"><span class="identifier">MemoryAlgorithm</span><span class="special">::</span><span class="identifier">mutex_family</span></code>)
to be used in all allocation operations. This allows the use
of user-defined mutexes or avoiding internal locking (maybe code
will be externally synchronized by the user).
</li>
<li class="listitem">
The Pointer type (<code class="computeroutput"><span class="identifier">MemoryAlgorithm</span><span class="special">::</span><span class="identifier">void_pointer</span></code>)
to be used by the memory allocation algorithm or additional helper
structures (like a map to maintain object/name associations).
All STL compatible allocators and containers to be used with
this managed memory segment will use this pointer type. The pointer
type will define if the managed memory segment can be mapped
between several processes. For example, if <code class="computeroutput"><span class="identifier">void_pointer</span></code>
is <code class="computeroutput"><span class="identifier">offset_ptr</span><span class="special"><</span><span class="keyword">void</span><span class="special">></span></code> we will be able to map the
managed segment in different base addresses in each process.
If <code class="computeroutput"><span class="identifier">void_pointer</span></code>
is <code class="computeroutput"><span class="keyword">void</span><span class="special">*</span></code>
only fixed address mapping could be used.
</li>
<li class="listitem">
See <a class="link" href="customizing_interprocess.html#interprocess.customizing_interprocess.custom_interprocess_alloc" title="Writing a new shared memory allocation algorithm">Writing
a new memory allocation algorithm</a> for more details about
memory algorithms.
</li>
</ul></div>
</li>
<li class="listitem">
<span class="bold"><strong>IndexType</strong></span> is the type of index that
will be used to store the name-object association (for example, a map,
a hash-map, or an ordered vector).
</li>
</ul></div>
<p>
This way, we can use <code class="computeroutput"><span class="keyword">char</span></code>
or <code class="computeroutput"><span class="keyword">wchar_t</span></code> strings to identify
created C++ objects in the memory segment, we can plug new shared memory
allocation algorithms, and use the index type that is best suited to our
needs.
</p>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="interprocess.managed_memory_segments.managed_shared_memory"></a><a class="link" href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_shared_memory" title="Managed Shared Memory">Managed
Shared Memory</a>
</h3></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_shared_memory.managed_memory_common_shm">Common
Managed Shared Memory Classes</a></span></dt>
<dt><span class="section"><a href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_shared_memory.constructing_managed_shared_memories">Constructing
Managed Shared Memory</a></span></dt>
<dt><span class="section"><a href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_shared_memory.windows_managed_memory_common_shm">Using
native windows shared memory</a></span></dt>
<dt><span class="section"><a href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_shared_memory.xsi_managed_memory_common_shm">Using
XSI (system V) shared memory</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="interprocess.managed_memory_segments.managed_shared_memory.managed_memory_common_shm"></a><a class="link" href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_shared_memory.managed_memory_common_shm" title="Common Managed Shared Memory Classes">Common
Managed Shared Memory Classes</a>
</h4></div></div></div>
<p>
As seen, <span class="bold"><strong>basic_managed_shared_memory</strong></span> offers
a great variety of customization. But for the average user, a common, default
shared memory named object creation is needed. Because of this, <span class="bold"><strong>Boost.Interprocess</strong></span> defines the most common managed
shared memory specializations:
</p>
<pre class="programlisting"><span class="comment">//!Defines a managed shared memory with c-strings as keys for named objects,</span>
<span class="comment">//!the default memory algorithm (with process-shared mutexes,</span>
<span class="comment">//!and offset_ptr as internal pointers) as memory allocation algorithm</span>
<span class="comment">//!and the default index type as the index.</span>
<span class="comment">//!This class allows the shared memory to be mapped in different base</span>
<span class="comment">//!in different processes</span>
<span class="keyword">typedef</span>
<span class="identifier">basic_managed_shared_memory</span><span class="special"><</span><span class="keyword">char</span>
<span class="special">,/*</span><span class="identifier">Default</span> <span class="identifier">memory</span> <span class="identifier">algorithm</span> <span class="identifier">defining</span> <span class="identifier">offset_ptr</span><span class="special"><</span><span class="keyword">void</span><span class="special">></span> <span class="identifier">as</span> <span class="identifier">void_pointer</span><span class="special">*/</span>
<span class="special">,/*</span><span class="identifier">Default</span> <span class="identifier">index</span> <span class="identifier">type</span><span class="special">*/></span>
<span class="identifier">managed_shared_memory</span><span class="special">;</span>
<span class="comment">//!Defines a managed shared memory with wide strings as keys for named objects,</span>
<span class="comment">//!the default memory algorithm (with process-shared mutexes,</span>
<span class="comment">//!and offset_ptr as internal pointers) as memory allocation algorithm</span>
<span class="comment">//!and the default index type as the index.</span>
<span class="comment">//!This class allows the shared memory to be mapped in different base</span>
<span class="comment">//!in different processes</span>
<span class="keyword">typedef</span>
<span class="identifier">basic_managed_shared_memory</span><span class="special"><</span><span class="keyword">wchar_t</span>
<span class="special">,/*</span><span class="identifier">Default</span> <span class="identifier">memory</span> <span class="identifier">algorithm</span> <span class="identifier">defining</span> <span class="identifier">offset_ptr</span><span class="special"><</span><span class="keyword">void</span><span class="special">></span> <span class="identifier">as</span> <span class="identifier">void_pointer</span><span class="special">*/</span>
<span class="special">,/*</span><span class="identifier">Default</span> <span class="identifier">index</span> <span class="identifier">type</span><span class="special">*/></span>
<span class="identifier">wmanaged_shared_memory</span><span class="special">;</span>
</pre>
<p>
<code class="computeroutput"><span class="identifier">managed_shared_memory</span></code> allocates
objects in shared memory associated with a c-string and <code class="computeroutput"><span class="identifier">wmanaged_shared_memory</span></code>
allocates objects in shared memory associated with a wchar_t null terminated
string. Both define the pointer type as <code class="computeroutput"><span class="identifier">offset_ptr</span><span class="special"><</span><span class="keyword">void</span><span class="special">></span></code> so they can be used to map the shared
memory at different base addresses in different processes.
</p>
<p>
If the user wants to map the shared memory in the same address in all processes
and want to use raw pointers internally instead of offset pointers, <span class="bold"><strong>Boost.Interprocess</strong></span> defines the following types:
</p>
<pre class="programlisting"><span class="comment">//!Defines a managed shared memory with c-strings as keys for named objects,</span>
<span class="comment">//!the default memory algorithm (with process-shared mutexes,</span>
<span class="comment">//!and offset_ptr as internal pointers) as memory allocation algorithm</span>
<span class="comment">//!and the default index type as the index.</span>
<span class="comment">//!This class allows the shared memory to be mapped in different base</span>
<span class="comment">//!in different processes*/</span>
<span class="keyword">typedef</span> <span class="identifier">basic_managed_shared_memory</span>
<span class="special"><</span><span class="keyword">char</span>
<span class="special">,/*</span><span class="identifier">Default</span> <span class="identifier">memory</span> <span class="identifier">algorithm</span> <span class="identifier">defining</span> <span class="keyword">void</span> <span class="special">*</span> <span class="identifier">as</span> <span class="identifier">void_pointer</span><span class="special">*/</span>
<span class="special">,/*</span><span class="identifier">Default</span> <span class="identifier">index</span> <span class="identifier">type</span><span class="special">*/></span>
<span class="identifier">fixed_managed_shared_memory</span><span class="special">;</span>
<span class="comment">//!Defines a managed shared memory with wide strings as keys for named objects,</span>
<span class="comment">//!the default memory algorithm (with process-shared mutexes,</span>
<span class="comment">//!and offset_ptr as internal pointers) as memory allocation algorithm</span>
<span class="comment">//!and the default index type as the index.</span>
<span class="comment">//!This class allows the shared memory to be mapped in different base</span>
<span class="comment">//!in different processes</span>
<span class="keyword">typedef</span> <span class="identifier">basic_managed_shared_memory</span>
<span class="special"><</span><span class="keyword">wchar_t</span>
<span class="special">,/*</span><span class="identifier">Default</span> <span class="identifier">memory</span> <span class="identifier">algorithm</span> <span class="identifier">defining</span> <span class="keyword">void</span> <span class="special">*</span> <span class="identifier">as</span> <span class="identifier">void_pointer</span><span class="special">*/</span>
<span class="special">,/*</span><span class="identifier">Default</span> <span class="identifier">index</span> <span class="identifier">type</span><span class="special">*/></span>
<span class="identifier">wfixed_managed_shared_memory</span><span class="special">;</span>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="interprocess.managed_memory_segments.managed_shared_memory.constructing_managed_shared_memories"></a><a class="link" href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_shared_memory.constructing_managed_shared_memories" title="Constructing Managed Shared Memory">Constructing
Managed Shared Memory</a>
</h4></div></div></div>
<p>
Managed shared memory is an advanced class that combines a shared memory
object and a mapped region that covers all the shared memory object. That
means that when we <span class="bold"><strong>create</strong></span> a new managed
shared memory:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
A new shared memory object is created.
</li>
<li class="listitem">
The whole shared memory object is mapped in the process' address space.
</li>
<li class="listitem">
Some helper objects are constructed (name-object index, internal synchronization
objects, internal variables...) in the mapped region to implement managed
memory segment features.
</li>
</ul></div>
<p>
When we <span class="bold"><strong>open</strong></span> a managed shared memory
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
A shared memory object is opened.
</li>
<li class="listitem">
The whole shared memory object is mapped in the process' address space.
</li>
</ul></div>
<p>
To use a managed shared memory, you must include the following header:
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</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">></span>
</pre>
<pre class="programlisting"><span class="comment">//1. Creates a new shared memory object</span>
<span class="comment">// called "MySharedMemory".</span>
<span class="comment">//2. Maps the whole object to this</span>
<span class="comment">// process' address space.</span>
<span class="comment">//3. Constructs some objects in shared memory</span>
<span class="comment">// to implement managed features.</span>
<span class="comment">//!! If anything fails, throws interprocess_exception</span>
<span class="comment">//</span>
<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="comment">//Shared memory object name</span>
<span class="special">,</span> <span class="number">65536</span><span class="special">);</span> <span class="comment">//Shared memory object size in bytes</span>
</pre>
<pre class="programlisting"><span class="comment">//1. Opens a shared memory object</span>
<span class="comment">// called "MySharedMemory".</span>
<span class="comment">//2. Maps the whole object to this</span>
<span class="comment">// process' address space.</span>
<span class="comment">//3. Obtains pointers to constructed internal objects</span>
<span class="comment">// to implement managed features.</span>
<span class="comment">//!! If anything fails, throws interprocess_exception</span>
<span class="comment">//</span>
<span class="identifier">managed_shared_memory</span> <span class="identifier">segment</span> <span class="special">(</span><span class="identifier">open_only</span><span class="special">,</span> <span class="string">"MySharedMemory"</span><span class="special">);//</span><span class="identifier">Shared</span> <span class="identifier">memory</span> <span class="identifier">object</span> <span class="identifier">name</span>
</pre>
<pre class="programlisting"><span class="comment">//1. If the segment was previously created</span>
<span class="comment">// equivalent to "open_only" (size is ignored).</span>
<span class="comment">//2. Otherwise, equivalent to "create_only"</span>
<span class="comment">//!! If anything fails, throws interprocess_exception</span>
<span class="comment">//</span>
<span class="identifier">managed_shared_memory</span> <span class="identifier">segment</span> <span class="special">(</span> <span class="identifier">open_or_create</span>
<span class="special">,</span> <span class="string">"MySharedMemory"</span> <span class="comment">//Shared memory object name</span>
<span class="special">,</span> <span class="number">65536</span><span class="special">);</span> <span class="comment">//Shared memory object size in bytes</span>
</pre>
<p>
When the <code class="computeroutput"><span class="identifier">managed_shared_memory</span></code>
object is destroyed, the shared memory object is automatically unmapped,
and all the resources are freed. To remove the shared memory object from
the system you must use the <code class="computeroutput"><span class="identifier">shared_memory_object</span><span class="special">::</span><span class="identifier">remove</span></code>
function. Shared memory object removing might fail if any process still
has the shared memory object mapped.
</p>
<p>
The user can also map the managed shared memory in a fixed address. This
option is essential when using using <code class="computeroutput"><span class="identifier">fixed_managed_shared_memory</span></code>.
To do this, just add the mapping address as an extra parameter:
</p>
<pre class="programlisting"><span class="identifier">fixed_managed_shared_memory</span> <span class="identifier">segment</span> <span class="special">(</span><span class="identifier">open_only</span> <span class="special">,</span><span class="string">"MyFixedAddressSharedMemory"</span> <span class="comment">//Shared memory object name</span>
<span class="special">,(</span><span class="keyword">void</span><span class="special">*)</span><span class="number">0x30000000</span> <span class="comment">//Mapping address</span>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="interprocess.managed_memory_segments.managed_shared_memory.windows_managed_memory_common_shm"></a><a class="link" href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_shared_memory.windows_managed_memory_common_shm" title="Using native windows shared memory">Using
native windows shared memory</a>
</h4></div></div></div>
<p>
Windows users might also want to use native windows shared memory instead
of the portable <code class="computeroutput"><a class="link" href="../boost/interprocess/shared_memory_object.html" title="Class shared_memory_object">shared_memory_object</a></code>
managed memory. This is achieved through the <code class="computeroutput"><a class="link" href="../boost/interprocess/basic_managed__idp65413104.html" title="Class template basic_managed_windows_shared_memory">basic_managed_windows_shared_memory</a></code>
class. To use it just include:
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">managed_windows_shared_memory</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
</pre>
<p>
This class has the same interface as <code class="computeroutput"><a class="link" href="../boost/interprocess/basic_managed__idp65355360.html" title="Class template basic_managed_shared_memory">basic_managed_shared_memory</a></code>
but uses native windows shared memory. Note that this managed class has
the same lifetime issues as the windows shared memory: when the last process
attached to the windows shared memory is detached from the memory (or ends/crashes)
the memory is destroyed. So there is no persistence support for windows
shared memory.
</p>
<p>
To communicate between system services and user applications using <code class="computeroutput"><span class="identifier">managed_windows_shared_memory</span></code>, please
read the explanations given in chapter <a class="link" href="sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.sharedmemory.windows_shared_memory" title="Native windows shared memory">Native
windows shared memory</a>.
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="interprocess.managed_memory_segments.managed_shared_memory.xsi_managed_memory_common_shm"></a><a class="link" href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_shared_memory.xsi_managed_memory_common_shm" title="Using XSI (system V) shared memory">Using
XSI (system V) shared memory</a>
</h4></div></div></div>
<p>
Unix users might also want to use XSI (system V) instead of the portable
<code class="computeroutput"><a class="link" href="../boost/interprocess/shared_memory_object.html" title="Class shared_memory_object">shared_memory_object</a></code>
managed memory. This is achieved through the <code class="computeroutput"><a class="link" href="../boost/interprocess/basic_managed__idp65461824.html" title="Class template basic_managed_xsi_shared_memory">basic_managed_xsi_shared_memory</a></code>
class. To use it just include:
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">managed_xsi_shared_memory</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
</pre>
<p>
This class has nearly the same interface as <code class="computeroutput"><a class="link" href="../boost/interprocess/basic_managed__idp65355360.html" title="Class template basic_managed_shared_memory">basic_managed_shared_memory</a></code>
but uses XSI shared memory as backend.
</p>
</div>
<p>
For more information about managed XSI shared memory capabilities, see <code class="computeroutput"><a class="link" href="../boost/interprocess/basic_managed__idp65461824.html" title="Class template basic_managed_xsi_shared_memory">basic_managed_xsi_shared_memory</a></code>
class reference.
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="interprocess.managed_memory_segments.managed_mapped_files"></a><a class="link" href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_mapped_files" title="Managed Mapped File">Managed
Mapped File</a>
</h3></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_mapped_files.managed_memory_common_mfile">Common
Managed Mapped Files</a></span></dt>
<dt><span class="section"><a href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_mapped_files.constructing_managed_mapped_files">Constructing
Managed Mapped Files</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="interprocess.managed_memory_segments.managed_mapped_files.managed_memory_common_mfile"></a><a class="link" href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_mapped_files.managed_memory_common_mfile" title="Common Managed Mapped Files">Common
Managed Mapped Files</a>
</h4></div></div></div>
<p>
As seen, <span class="bold"><strong>basic_managed_mapped_file</strong></span> offers
a great variety of customization. But for the average user, a common, default
shared memory named object creation is needed. Because of this, <span class="bold"><strong>Boost.Interprocess</strong></span> defines the most common managed
mapped file specializations:
</p>
<pre class="programlisting"><span class="comment">//Named object creation managed memory segment</span>
<span class="comment">//All objects are constructed in the memory-mapped file</span>
<span class="comment">// Names are c-strings,</span>
<span class="comment">// Default memory management algorithm(rbtree_best_fit with no mutexes)</span>
<span class="comment">// Name-object mappings are stored in the default index type (flat_map)</span>
<span class="keyword">typedef</span> <span class="identifier">basic_managed_mapped_file</span> <span class="special"><</span>
<span class="keyword">char</span><span class="special">,</span>
<span class="identifier">rbtree_best_fit</span><span class="special"><</span><span class="identifier">mutex_family</span><span class="special">,</span> <span class="identifier">offset_ptr</span><span class="special"><</span><span class="keyword">void</span><span class="special">></span> <span class="special">>,</span>
<span class="identifier">flat_map_index</span>
<span class="special">></span> <span class="identifier">managed_mapped_file</span><span class="special">;</span>
<span class="comment">//Named object creation managed memory segment</span>
<span class="comment">//All objects are constructed in the memory-mapped file</span>
<span class="comment">// Names are wide-strings,</span>
<span class="comment">// Default memory management algorithm(rbtree_best_fit with no mutexes)</span>
<span class="comment">// Name-object mappings are stored in the default index type (flat_map)</span>
<span class="keyword">typedef</span> <span class="identifier">basic_managed_mapped_file</span><span class="special"><</span>
<span class="keyword">wchar_t</span><span class="special">,</span>
<span class="identifier">rbtree_best_fit</span><span class="special"><</span><span class="identifier">mutex_family</span><span class="special">,</span> <span class="identifier">offset_ptr</span><span class="special"><</span><span class="keyword">void</span><span class="special">></span> <span class="special">>,</span>
<span class="identifier">flat_map_index</span>
<span class="special">></span> <span class="identifier">wmanaged_mapped_file</span><span class="special">;</span>
</pre>
<p>
<code class="computeroutput"><span class="identifier">managed_mapped_file</span></code> allocates
objects in a memory mapped files associated with a c-string and <code class="computeroutput"><span class="identifier">wmanaged_mapped_file</span></code> allocates objects
in a memory mapped file associated with a wchar_t null terminated string.
Both define the pointer type as <code class="computeroutput"><span class="identifier">offset_ptr</span><span class="special"><</span><span class="keyword">void</span><span class="special">></span></code> so they can be used to map the file
at different base addresses in different processes.
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="interprocess.managed_memory_segments.managed_mapped_files.constructing_managed_mapped_files"></a><a class="link" href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_mapped_files.constructing_managed_mapped_files" title="Constructing Managed Mapped Files">Constructing
Managed Mapped Files</a>
</h4></div></div></div>
<p>
Managed mapped file is an advanced class that combines a file and a mapped
region that covers all the file. That means that when we <span class="bold"><strong>create</strong></span>
a new managed mapped file:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
A new file is created.
</li>
<li class="listitem">
The whole file is mapped in the process' address space.
</li>
<li class="listitem">
Some helper objects are constructed (name-object index, internal synchronization
objects, internal variables...) in the mapped region to implement managed
memory segment features.
</li>
</ul></div>
<p>
When we <span class="bold"><strong>open</strong></span> a managed mapped file
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
A file is opened.
</li>
<li class="listitem">
The whole file is mapped in the process' address space.
</li>
</ul></div>
<p>
To use a managed mapped file, you must include the following header:
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">interprocess</span><span class="special">/</span><span class="identifier">managed_mapped_file</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
</pre>
<pre class="programlisting"><span class="comment">//1. Creates a new file</span>
<span class="comment">// called "MyMappedFile".</span>
<span class="comment">//2. Maps the whole file to this</span>
<span class="comment">// process' address space.</span>
<span class="comment">//3. Constructs some objects in the memory mapped</span>
<span class="comment">// file to implement managed features.</span>
<span class="comment">//!! If anything fails, throws interprocess_exception</span>
<span class="comment">//</span>
<span class="identifier">managed_mapped_file</span> <span class="identifier">mfile</span> <span class="special">(</span><span class="identifier">create_only</span><span class="special">,</span> <span class="string">"MyMappedFile"</span><span class="special">,</span> <span class="comment">//Mapped file name 65536); //Mapped file size</span>
</pre>
<pre class="programlisting"><span class="comment">//1. Opens a file</span>
<span class="comment">// called "MyMappedFile".</span>
<span class="comment">//2. Maps the whole file to this</span>
<span class="comment">// process' address space.</span>
<span class="comment">//3. Obtains pointers to constructed internal objects</span>
<span class="comment">// to implement managed features.</span>
<span class="comment">//!! If anything fails, throws interprocess_exception</span>
<span class="comment">//</span>
<span class="identifier">managed_mapped_file</span> <span class="identifier">mfile</span> <span class="special">(</span><span class="identifier">open_only</span><span class="special">,</span> <span class="string">"MyMappedFile"</span><span class="special">);</span> <span class="comment">//Mapped file name[c++]</span>
<span class="comment">//1. If the file was previously created</span>
<span class="comment">// equivalent to "open_only".</span>
<span class="comment">//2. Otherwise, equivalent to "open_only" (size is ignored)</span>
<span class="comment">//</span>
<span class="comment">//!! If anything fails, throws interprocess_exception</span>
<span class="comment">//</span>
<span class="identifier">managed_mapped_file</span> <span class="identifier">mfile</span> <span class="special">(</span><span class="identifier">open_or_create</span><span class="special">,</span> <span class="string">"MyMappedFile"</span><span class="special">,</span> <span class="comment">//Mapped file name 65536); //Mapped file size</span>
</pre>
<p>
When the <code class="computeroutput"><span class="identifier">managed_mapped_file</span></code>
object is destroyed, the file is automatically unmapped, and all the resources
are freed. To remove the file from the filesystem you could use standard
C <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">remove</span></code> or <span class="bold"><strong>Boost.Filesystem</strong></span>'s
<code class="computeroutput"><span class="identifier">remove</span><span class="special">()</span></code>
functions, but file removing might fail if any process still has the file
mapped in memory or the file is open by any process.
</p>
<p>
To obtain a more portable behaviour, use <code class="computeroutput"><span class="identifier">file_mapping</span><span class="special">::</span><span class="identifier">remove</span><span class="special">(</span><span class="keyword">const</span> <span class="keyword">char</span> <span class="special">*)</span></code>
operation, which will remove the file even if it's being mapped. However,
removal will fail in some OS systems if the file (eg. by C++ file streams)
and no delete share permission was granted to the file. But in most common
cases <code class="computeroutput"><span class="identifier">file_mapping</span><span class="special">::</span><span class="identifier">remove</span></code> is portable enough.
</p>
</div>
<p>
For more information about managed mapped file capabilities, see <code class="computeroutput"><a class="link" href="../boost/interprocess/basic_managed_mapped_file.html" title="Class template basic_managed_mapped_file">basic_managed_mapped_file</a></code>
class reference.
</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="interprocess.managed_memory_segments.managed_memory_segment_features"></a><a class="link" href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_memory_segment_features" title="Managed Memory Segment Features">Managed
Memory Segment Features</a>
</h3></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_memory_segment_features.allocate_deallocate">Allocating
fragments of a managed memory segment</a></span></dt>
<dt><span class="section"><a href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_memory_segment_features.segment_offset">Obtaining
handles to identify data</a></span></dt>
<dt><span class="section"><a href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_memory_segment_features.allocation_types">Object
construction function family</a></span></dt>
<dt><span class="section"><a href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_memory_segment_features.anonymous">Anonymous
instance construction</a></span></dt>
<dt><span class="section"><a href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_memory_segment_features.unique">Unique
instance construction</a></span></dt>
<dt><span class="section"><a href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_memory_segment_features.synchronization">Synchronization
guarantees</a></span></dt>
<dt><span class="section"><a href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_memory_segment_features.index_types">Index
types for name/object mappings</a></span></dt>
<dt><span class="section"><a href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_memory_segment_features.managed_memory_segment_segment_manager">Segment
Manager</a></span></dt>
<dt><span class="section"><a href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_memory_segment_features.managed_memory_segment_information">Obtaining
information about a constructed object</a></span></dt>
<dt><span class="section"><a href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_memory_segment_features.managed_memory_segment_atomic_func">Executing
an object function atomically</a></span></dt>
</dl></div>
<p>
The following features are common to all managed memory segment classes,
but we will use managed shared memory in our examples. We can do the same
with memory mapped files or other managed memory segment classes.
</p>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="interprocess.managed_memory_segments.managed_memory_segment_features.allocate_deallocate"></a><a class="link" href="managed_memory_segments.html#interprocess.managed_memory_segments.managed_memory_segment_features.allocate_deallocate" title="Allocating fragments of a managed memory segment">Allocating
fragments of a managed memory segment</a>
</h4></div></div></div>
<p>
If a basic raw-byte allocation is needed from a managed memory segment,
(for example, a managed shared memory), to implement top-level interprocess
communications, this class offers <span class="bold"><strong>allocate</strong></span>
and <span class="bold"><strong>deallocate</strong></span> functions. The allocation
function comes with throwing and no throwing versions. Throwing version
throws boost::interprocess::bad_alloc (which derives from <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">bad_alloc</span></code>) if there is no more memory
and the non-throwing version returns 0 pointer.
</p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</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">></span>
<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
<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>
<span class="comment">//Remove shared memory on construction and destruction</span>
<span class="keyword">struct</span> <span class="identifier">shm_remove</span>
<span class="special">{</span>
<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">"My