• 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>Mapping Address Independent Pointer: offset_ptr</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="sharedmemorybetweenprocesses.html" title="Sharing memory between processes">
11<link rel="next" href="synchronization_mechanisms.html" title="Synchronization mechanisms">
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="sharedmemorybetweenprocesses.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="synchronization_mechanisms.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.offset_ptr"></a><a class="link" href="offset_ptr.html" title="Mapping Address Independent Pointer: offset_ptr">Mapping Address Independent Pointer:
29    offset_ptr</a>
30</h2></div></div></div>
31<p>
32      When creating shared memory and memory mapped files to communicate two processes
33      the memory segment can be mapped in a different address in each process:
34    </p>
35<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">shared_memory_object</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
36
37<span class="comment">// ...</span>
38
39<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">interprocess</span><span class="special">;</span>
40
41<span class="comment">//Open a shared memory segment</span>
42<span class="identifier">shared_memory_object</span> <span class="identifier">shm_obj</span>
43   <span class="special">(</span><span class="identifier">open_only</span>                    <span class="comment">//open or create</span>
44   <span class="special">,</span><span class="string">"shared_memory"</span>              <span class="comment">//name</span>
45   <span class="special">,</span><span class="identifier">read_only</span>   <span class="comment">//read-only mode</span>
46   <span class="special">);</span>
47
48<span class="comment">//Map the whole shared memory</span>
49<span class="identifier">mapped_region</span> <span class="identifier">region</span>
50   <span class="special">(</span> <span class="identifier">shm</span>                         <span class="comment">//Memory-mappable object</span>
51   <span class="special">,</span> <span class="identifier">read_write</span>                  <span class="comment">//Access mode</span>
52   <span class="special">);</span>
53
54<span class="comment">//This address can be different in each process</span>
55<span class="keyword">void</span> <span class="special">*</span><span class="identifier">addr</span> <span class="special">=</span> <span class="identifier">region</span><span class="special">.</span><span class="identifier">get_address</span><span class="special">();</span>
56</pre>
57<p>
58      This makes the creation of complex objects in mapped regions difficult: a C++
59      class instance placed in a mapped region might have a pointer pointing to another
60      object also placed in the mapped region. Since the pointer stores an absolute
61      address, that address is only valid for the process that placed the object
62      there unless all processes map the mapped region in the same address.
63    </p>
64<p>
65      To be able to simulate pointers in mapped regions, users must use <span class="bold"><strong>offsets</strong></span> (distance between objects) instead of absolute
66      addresses. The offset between two objects in a mapped region is the same for
67      any process that maps the mapped region, even if that region is placed in different
68      base addresses. To facilitate the use of offsets, <span class="bold"><strong>Boost.Interprocess</strong></span>
69      offers <code class="computeroutput"><a class="link" href="../boost/interprocess/offset_ptr.html" title="Class template offset_ptr">offset_ptr</a></code>.
70    </p>
71<p>
72      <code class="computeroutput"><a class="link" href="../boost/interprocess/offset_ptr.html" title="Class template offset_ptr">offset_ptr</a></code> wraps
73      all the background operations needed to offer a pointer-like interface. The
74      class interface is inspired in Boost Smart Pointers and this smart pointer
75      stores the offset (distance in bytes) between the pointee's address and it's
76      own <code class="computeroutput"><span class="keyword">this</span></code> pointer. Imagine a structure
77      in a common 32 bit processor:
78    </p>
79<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">structure</span>
80<span class="special">{</span>
81   <span class="keyword">int</span>               <span class="identifier">integer1</span><span class="special">;</span>   <span class="comment">//The compiler places this at offset 0 in the structure</span>
82   <span class="identifier">offset_ptr</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span>   <span class="identifier">ptr</span><span class="special">;</span>        <span class="comment">//The compiler places this at offset 4 in the structure</span>
83   <span class="keyword">int</span>               <span class="identifier">integer2</span><span class="special">;</span>   <span class="comment">//The compiler places this at offset 8 in the structure</span>
84<span class="special">};</span>
85
86<span class="comment">//...</span>
87
88<span class="identifier">structure</span> <span class="identifier">s</span><span class="special">;</span>
89
90<span class="comment">//Assign the address of "integer1" to "ptr".</span>
91<span class="comment">//"ptr" will store internally "-4":</span>
92<span class="comment">//    (char*)&amp;s.integer1 - (char*)&amp;s.ptr;</span>
93<span class="identifier">s</span><span class="special">.</span><span class="identifier">ptr</span> <span class="special">=</span> <span class="special">&amp;</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">integer1</span><span class="special">;</span>
94
95<span class="comment">//Assign the address of "integer2" to "ptr".</span>
96<span class="comment">//"ptr" will store internally "4":</span>
97<span class="comment">//    (char*)&amp;s.integer2 - (char*)&amp;s.ptr;</span>
98<span class="identifier">s</span><span class="special">.</span><span class="identifier">ptr</span> <span class="special">=</span> <span class="special">&amp;</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">integer2</span><span class="special">;</span>
99</pre>
100<p>
101      One of the big problems of <code class="computeroutput"><span class="identifier">offset_ptr</span></code>
102      is the representation of the null pointer. The null pointer can't be safely
103      represented like an offset, since the absolute address 0 is always outside
104      of the mapped region. Due to the fact that the segment can be mapped in a different
105      base address in each process the distance between the address 0 and <code class="computeroutput"><span class="identifier">offset_ptr</span></code> is different for every process.
106    </p>
107<p>
108      Some implementations choose the offset 0 (that is, an <code class="computeroutput"><span class="identifier">offset_ptr</span></code>
109      pointing to itself) as the null pointer pointer representation but this is
110      not valid for many use cases since many times structures like linked lists
111      or nodes from STL containers point to themselves (the end node in an empty
112      container, for example) and 0 offset value is needed. An alternative is to
113      store, in addition to the offset, a boolean to indicate if the pointer is null.
114      However, this increments the size of the pointer and hurts performance.
115    </p>
116<p>
117      In consequence, <code class="computeroutput"><a class="link" href="../boost/interprocess/offset_ptr.html" title="Class template offset_ptr">offset_ptr</a></code>
118      defines offset 1 as the null pointer, meaning that this class <span class="bold"><strong>can't</strong></span>
119      point to the byte after its own <span class="emphasis"><em>this</em></span> pointer:
120    </p>
121<pre class="programlisting"><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>
122
123<span class="identifier">offset_ptr</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">&gt;</span> <span class="identifier">ptr</span><span class="special">;</span>
124
125<span class="comment">//Pointing to the next byte of it's own address</span>
126<span class="comment">//marks the smart pointer as null.</span>
127<span class="identifier">ptr</span> <span class="special">=</span> <span class="special">(</span><span class="keyword">char</span><span class="special">*)&amp;</span><span class="identifier">ptr</span> <span class="special">+</span> <span class="number">1</span><span class="special">;</span>
128
129<span class="comment">//ptr is equal to null</span>
130<span class="identifier">assert</span><span class="special">(!</span><span class="identifier">ptr</span><span class="special">);</span>
131
132<span class="comment">//This is the same as assigning the null value...</span>
133<span class="identifier">ptr</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
134
135<span class="comment">//ptr is also equal to null</span>
136<span class="identifier">assert</span><span class="special">(!</span><span class="identifier">ptr</span><span class="special">);</span>
137</pre>
138<p>
139      In practice, this limitation is not important, since a user almost never wants
140      to point to this address.
141    </p>
142<p>
143      <code class="computeroutput"><a class="link" href="../boost/interprocess/offset_ptr.html" title="Class template offset_ptr">offset_ptr</a></code> offers
144      all pointer-like operations and random_access_iterator typedefs, so it can
145      be used in STL algorithms requiring random access iterators and detected via
146      traits. For more information about the members and operations of the class,
147      see <code class="computeroutput"><a class="link" href="../boost/interprocess/offset_ptr.html" title="Class template offset_ptr">offset_ptr reference</a></code>.
148    </p>
149</div>
150<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
151<td align="left"></td>
152<td align="right"><div class="copyright-footer">Copyright © 2005-2015 Ion Gaztanaga<p>
153        Distributed under the Boost Software License, Version 1.0. (See accompanying
154        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>)
155      </p>
156</div></td>
157</tr></table>
158<hr>
159<div class="spirit-nav">
160<a accesskey="p" href="sharedmemorybetweenprocesses.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="synchronization_mechanisms.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
161</div>
162</body>
163</html>
164