aboutsummaryrefslogtreecommitdiff
path: root/docs/doxygen/nel/a03382.html
blob: 1811e2638568f16456be72151a15b6e810dc1dea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>NeL: NLMISC::CSharedMemory class Reference</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.3.6 -->
<div class="qindex">  <form class="search" action="search.php" method="get">
<a class="qindex" href="main.html">Main&nbsp;Page</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class&nbsp;Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical&nbsp;List</a> | <a class="qindex" href="annotated.html">Data&nbsp;Structures</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="namespacemembers.html">Namespace&nbsp;Members</a> | <a class="qindex" href="functions.html">Data&nbsp;Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related&nbsp;Pages</a>  | <span class="search"><u>S</u>earch&nbsp;for&nbsp;<input class="search" type="text" name="query" value="" size="20" accesskey="s"/></span></form></div>
<h1>NLMISC::CSharedMemory Class Reference</h1><code>#include &lt;<a class="el" href="a06383.html">shared_memory.h</a>&gt;</code>
<p>
<hr><a name="_details"></a><h2>Detailed Description</h2>
Encapsulation of shared memory APIs. Using file mapping under Windows, System V shared memory (shm) under Linux.<p>
Note: under Linux, an option could be added to prevent a segment to be swapped out.<p>
<dl compact><dt><b>Author:</b></dt><dd>Olivier Cado <p>
Nevrax France </dd></dl>
<dl compact><dt><b>Date:</b></dt><dd>2002 </dd></dl>

<p>

<p>
Definition at line <a class="el" href="a06383.html#l00068">68</a> of file <a class="el" href="a06383.html">shared_memory.h</a>.<table border=0 cellpadding=0 cellspacing=0>
<tr><td></td></tr>
<tr><td colspan=2><br><h2>Static Public Member Functions</h2></td></tr>
<tr><td class="memItemLeft" nowrap align=right valign=top>void *&nbsp;</td><td class="memItemRight" valign=bottom><a class="el" href="a03382.html#NLMISC_1_1CSharedMemorye0">accessSharedMemory</a> (<a class="el" href="a05378.html#a232">TSharedMemId</a> sharedMemId)</td></tr>

<tr><td class="memItemLeft" nowrap align=right valign=top>bool&nbsp;</td><td class="memItemRight" valign=bottom><a class="el" href="a03382.html#NLMISC_1_1CSharedMemorye1">closeSharedMemory</a> (void *accessAddress)</td></tr>

<tr><td class="memItemLeft" nowrap align=right valign=top>void *&nbsp;</td><td class="memItemRight" valign=bottom><a class="el" href="a03382.html#NLMISC_1_1CSharedMemorye2">createSharedMemory</a> (<a class="el" href="a05378.html#a232">TSharedMemId</a> sharedMemId, <a class="el" href="a04558.html#a11">uint32</a> <a class="el" href="a04223.html#a587">size</a>)</td></tr>

<tr><td class="memItemLeft" nowrap align=right valign=top>void&nbsp;</td><td class="memItemRight" valign=bottom><a class="el" href="a03382.html#NLMISC_1_1CSharedMemorye3">destroySharedMemory</a> (<a class="el" href="a05378.html#a232">TSharedMemId</a> sharedMemId, bool force=false)</td></tr>

</table>
<hr><h2>Member Function Documentation</h2>
<a class="anchor" name="NLMISC_1_1CSharedMemorye0" doxytag="NLMISC::CSharedMemory::accessSharedMemory" ></a><p>
<table class="mdTable" width="100%" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" nowrap valign="top"> void * NLMISC::CSharedMemory::accessSharedMemory </td>
          <td class="md" valign="top">(&nbsp;</td>
          <td class="md" nowrap valign="top"><a class="el" href="a05378.html#a232">TSharedMemId</a>&nbsp;</td>
          <td class="mdname1" valign="top" nowrap> <em>sharedMemId</em>          </td>
          <td class="md" valign="top">&nbsp;)&nbsp;</td>
          <td class="md" nowrap><code> [static]</code></td>
        </tr>

      </table>
    </td>
  </tr>
</table>
<table cellspacing=5 cellpadding=0 border=0>
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
Get access to an existing shared memory segment. The id must be used. <dl compact><dt><b>Returns:</b></dt><dd>Access address of the segment. Its size was determined at the creation.</dd></dl>

<p>
Definition at line <a class="el" href="a06382.html#l00100">100</a> of file <a class="el" href="a06382.html">shared_memory.cpp</a>.
<p>
References <a class="el" href="a06383.html#l00041">NLMISC::TSharedMemId</a>.
<p>
<div class="fragment"><pre>00101 {
00102 <span class="preprocessor">#ifdef NL_OS_WINDOWS</span>
00103 <span class="preprocessor"></span>
00104         <span class="comment">// Open the existing file mapping by name</span>
00105         HANDLE hMapFile = OpenFileMapping( FILE_MAP_ALL_ACCESS, <span class="keyword">false</span>, sharedMemId );
00106         <span class="keywordflow">if</span> ( hMapFile == NULL )
00107                 <span class="keywordflow">return</span> NULL;
00108         <span class="comment">//nldebug( "SHDMEM: Opening smid %s --&gt; mapFile %p", sharedMemId, hMapFile );</span>
00109 
00110         <span class="comment">// Map the file into memory address space</span>
00111         <span class="keywordtype">void</span> *accessAddress = MapViewOfFile( hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0 );
00112         AccessAddressesToHandles.insert( make_pair( accessAddress, hMapFile ) );
00113         <span class="keywordflow">return</span> accessAddress;
00114         
00115 <span class="preprocessor">#else</span>
00116 <span class="preprocessor"></span>
00117         <span class="comment">// Open an existing shared memory segment</span>
00118         <span class="keywordtype">int</span> shmid = shmget( sharedMemId, 0, 0666 );
00119         <span class="keywordflow">if</span> ( shmid == -1 )
00120                 <span class="keywordflow">return</span> NULL;
00121 
00122         <span class="comment">// Map the segment into memory address space</span>
00123         <span class="keywordtype">void</span> *accessAddress = (<span class="keywordtype">void</span>*)shmat( shmid, 0, 0 );
00124         <span class="keywordflow">if</span> ( accessAddress == (<span class="keywordtype">void</span>*)-1 )
00125                 <span class="keywordflow">return</span> NULL;
00126         <span class="keywordflow">else</span>
00127                 <span class="keywordflow">return</span> accessAddress;
00128 
00129 <span class="preprocessor">#endif</span>
00130 <span class="preprocessor"></span>}
</pre></div>    </td>
  </tr>
</table>
<a class="anchor" name="NLMISC_1_1CSharedMemorye1" doxytag="NLMISC::CSharedMemory::closeSharedMemory" ></a><p>
<table class="mdTable" width="100%" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" nowrap valign="top"> bool NLMISC::CSharedMemory::closeSharedMemory </td>
          <td class="md" valign="top">(&nbsp;</td>
          <td class="md" nowrap valign="top">void *&nbsp;</td>
          <td class="mdname1" valign="top" nowrap> <em>accessAddress</em>          </td>
          <td class="md" valign="top">&nbsp;)&nbsp;</td>
          <td class="md" nowrap><code> [static]</code></td>
        </tr>

      </table>
    </td>
  </tr>
</table>
<table cellspacing=5 cellpadding=0 border=0>
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
Close a shared memory segment, given the address returned by <a class="el" href="a03382.html#NLMISC_1_1CSharedMemorye2">createSharedMemory()</a> or <a class="el" href="a03382.html#NLMISC_1_1CSharedMemorye0">accessSharedMemory()</a>. Must be called by each process that called <a class="el" href="a03382.html#NLMISC_1_1CSharedMemorye2">createSharedMemory()</a> or <a class="el" href="a03382.html#NLMISC_1_1CSharedMemorye0">accessSharedMemory()</a>.
<p>
Definition at line <a class="el" href="a06382.html#l00136">136</a> of file <a class="el" href="a06382.html">shared_memory.cpp</a>.
<p>
References <a class="el" href="a05622.html#l00135">nlwarning</a>.
<p>
<div class="fragment"><pre>00137 {
00138 <span class="preprocessor">#ifdef NL_OS_WINDOWS</span>
00139 <span class="preprocessor"></span>
00140         <span class="keywordtype">bool</span> result = <span class="keyword">true</span>;
00141 
00142         <span class="comment">// Unmap the file from memory address space</span>
00143         <span class="keywordflow">if</span> ( UnmapViewOfFile( accessAddress ) == 0 )
00144         {
00145                 <a class="code" href="a04199.html#a2">nlwarning</a>( <span class="stringliteral">"SHDMEM: UnmapViewOfFile failed: error %u"</span>, GetLastError() );
00146                 result = <span class="keyword">false</span>;
00147         }
00148 
00149         <span class="comment">// Close the corresponding handle</span>
00150         map&lt;void*,HANDLE&gt;::iterator im = AccessAddressesToHandles.find( accessAddress );
00151         <span class="keywordflow">if</span> ( im != AccessAddressesToHandles.end() )
00152         {
00153                 <span class="comment">//nldebug( "SHDMEM: CloseHandle mapFile %u", (*im).second );</span>
00154                 <span class="keywordflow">if</span> ( ! CloseHandle( (*im).second ) )
00155                         <a class="code" href="a04199.html#a2">nlwarning</a>( <span class="stringliteral">"SHDMEM: CloseHandle failed: error %u, mapFile %u"</span>, GetLastError(), (*im).second );
00156                 AccessAddressesToHandles.erase( im );
00157                 <span class="keywordflow">return</span> result;
00158         }
00159         <span class="keywordflow">else</span>
00160         {
00161                 <span class="keywordflow">return</span> <span class="keyword">false</span>;
00162         }
00163 
00164 <span class="preprocessor">#else</span>
00165 <span class="preprocessor"></span>
00166         <span class="comment">// Detach the shared memory segment</span>
00167         <span class="keywordflow">return</span> ( shmdt( accessAddress ) != -1 );
00168         
00169 <span class="preprocessor">#endif</span>
00170 <span class="preprocessor"></span>}
</pre></div>    </td>
  </tr>
</table>
<a class="anchor" name="NLMISC_1_1CSharedMemorye2" doxytag="NLMISC::CSharedMemory::createSharedMemory" ></a><p>
<table class="mdTable" width="100%" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" nowrap valign="top"> void * NLMISC::CSharedMemory::createSharedMemory </td>
          <td class="md" valign="top">(&nbsp;</td>
          <td class="md" nowrap valign="top"><a class="el" href="a05378.html#a232">TSharedMemId</a>&nbsp;</td>
          <td class="mdname" nowrap> <em>sharedMemId</em>, </td>
        </tr>
        <tr>
          <td class="md" nowrap align="right"></td>
          <td></td>
          <td class="md" nowrap><a class="el" href="a04558.html#a11">uint32</a>&nbsp;</td>
          <td class="mdname" nowrap> <em>size</em></td>
        </tr>
        <tr>
          <td></td>
          <td class="md">)&nbsp;</td>
          <td class="md" colspan="2"><code> [static]</code></td>
        </tr>

      </table>
    </td>
  </tr>
</table>
<table cellspacing=5 cellpadding=0 border=0>
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
Create a shared memory segment and get access to it. The id must not be used. The id 0x3a732235 is used by the NeL memory manager. <dl compact><dt><b>Returns:</b></dt><dd>Access address of the segment of the choosen size</dd></dl>

<p>
Definition at line <a class="el" href="a06382.html#l00055">55</a> of file <a class="el" href="a06382.html">shared_memory.cpp</a>.
<p>
References <a class="el" href="a05622.html#l00135">nlwarning</a>, <a class="el" href="a06382.html#l00048">NLMISC::SharedMemIdsToShmids</a>, <a class="el" href="a05646.html#l00645">size</a>, <a class="el" href="a06383.html#l00041">NLMISC::TSharedMemId</a>, and <a class="el" href="a05981.html#l00100">uint32</a>.
<p>
<div class="fragment"><pre>00056 {
00057 <span class="preprocessor">#ifdef NL_OS_WINDOWS</span>
00058 <span class="preprocessor"></span>
00059         <span class="comment">// Create a file mapping backed by the virtual memory swap file (not a data file)</span>
00060         HANDLE hMapFile = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, size, sharedMemId );
00061         <span class="keywordflow">if</span> ( (hMapFile == NULL) || (GetLastError() == ERROR_ALREADY_EXISTS) )
00062         {
00063                 <a class="code" href="a04199.html#a2">nlwarning</a>( <span class="stringliteral">"SHDMEM: Cannot create file mapping: error %u, mapFile %p"</span>, GetLastError(), hMapFile );
00064                 <span class="keywordflow">return</span> NULL;
00065         }
00066         <span class="comment">//else</span>
00067         <span class="comment">//      nldebug( "SHDMEM: Creating smid %s --&gt; mapFile %p", sharedMemId, hMapFile );</span>
00068         
00069 
00070         <span class="comment">// Map the file into memory address space</span>
00071         <span class="keywordtype">void</span> *accessAddress = MapViewOfFile( hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0 );
00072         AccessAddressesToHandles.insert( make_pair( accessAddress, hMapFile ) );
00073         <span class="comment">/*if ( accessAddress == NULL )</span>
00074 <span class="comment">        {</span>
00075 <span class="comment">                nlwarning( "SHDMEM: Cannot map view of file: error %u", GetLastError() );</span>
00076 <span class="comment">        }*/</span>
00077         <span class="keywordflow">return</span> accessAddress;
00078 
00079 <span class="preprocessor">#else</span>
00080 <span class="preprocessor"></span>
00081         <span class="comment">// Create a shared memory segment</span>
00082         <span class="keywordtype">int</span> shmid = shmget( sharedMemId, size, IPC_CREAT | IPC_EXCL | 0666 );
00083         <span class="keywordflow">if</span> ( shmid == -1 )
00084                 <span class="keywordflow">return</span> NULL;
00085         <a class="code" href="a05378.html#a36">SharedMemIdsToShmids</a>.insert( make_pair( sharedMemId, shmid ) );
00086 
00087         <span class="comment">// Map the segment into memory address space</span>
00088         <span class="keywordtype">void</span> *accessAddress = (<span class="keywordtype">void</span>*)shmat( shmid, 0, 0 );
00089         <span class="keywordflow">if</span> ( accessAddress == (<span class="keywordtype">void</span>*)-1 )
00090                 <span class="keywordflow">return</span> NULL;
00091         <span class="keywordflow">else</span>
00092                 <span class="keywordflow">return</span> accessAddress;
00093 <span class="preprocessor">#endif</span>
00094 <span class="preprocessor"></span>}
</pre></div>    </td>
  </tr>
</table>
<a class="anchor" name="NLMISC_1_1CSharedMemorye3" doxytag="NLMISC::CSharedMemory::destroySharedMemory" ></a><p>
<table class="mdTable" width="100%" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" nowrap valign="top"> void NLMISC::CSharedMemory::destroySharedMemory </td>
          <td class="md" valign="top">(&nbsp;</td>
          <td class="md" nowrap valign="top"><a class="el" href="a05378.html#a232">TSharedMemId</a>&nbsp;</td>
          <td class="mdname" nowrap> <em>sharedMemId</em>, </td>
        </tr>
        <tr>
          <td class="md" nowrap align="right"></td>
          <td></td>
          <td class="md" nowrap>bool&nbsp;</td>
          <td class="mdname" nowrap> <em>force</em> = false</td>
        </tr>
        <tr>
          <td></td>
          <td class="md">)&nbsp;</td>
          <td class="md" colspan="2"><code> [static]</code></td>
        </tr>

      </table>
    </td>
  </tr>
</table>
<table cellspacing=5 cellpadding=0 border=0>
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
Destroy a shared memory segment (must be called by the process that created the segment, not by the accessors).<p>
"Rescue feature": set "force" to true if a segment was created and left out of control (meaning a new <a class="el" href="a03382.html#NLMISC_1_1CSharedMemorye2">createSharedMemory()</a> with the same sharedMemId fails), but before, make sure the segment really belongs to you!<p>
Note: this method does nothing under Windows, destroying is automatic. 
<p>
Definition at line <a class="el" href="a06382.html#l00177">177</a> of file <a class="el" href="a06382.html">shared_memory.cpp</a>.
<p>
References <a class="el" href="a06382.html#l00048">NLMISC::SharedMemIdsToShmids</a>, and <a class="el" href="a06383.html#l00041">NLMISC::TSharedMemId</a>.
<p>
<div class="fragment"><pre>00178 {
00179 <span class="preprocessor">#ifndef NL_OS_WINDOWS</span>
00180 <span class="preprocessor"></span>  <span class="comment">// Set the segment to auto-destroying (when the last process detaches)</span>
00181   map&lt;TSharedMemId,int&gt;::iterator im = <a class="code" href="a05378.html#a36">SharedMemIdsToShmids</a>.find( sharedMemId );
00182   <span class="keywordflow">if</span> ( im != <a class="code" href="a05378.html#a36">SharedMemIdsToShmids</a>.end() )
00183         {
00184           <span class="comment">// Destroy the segment created before</span>
00185           shmctl( (*im).second, IPC_RMID, 0 );
00186           <a class="code" href="a05378.html#a36">SharedMemIdsToShmids</a>.erase( im );
00187         }
00188   <span class="keywordflow">else</span> <span class="keywordflow">if</span> ( force )
00189         {
00190           <span class="comment">// Open and destroy the segment</span>
00191           <span class="keywordtype">int</span> shmid = shmget( sharedMemId, 0, 0666 );
00192           <span class="keywordflow">if</span> ( shmid != -1 )
00193                 {
00194                   <span class="comment">// Destroy the segment</span>
00195                   shmctl( shmid, IPC_RMID, 0 );
00196                 }
00197         }
00198 <span class="preprocessor">#endif</span>
00199 <span class="preprocessor"></span>}
</pre></div>    </td>
  </tr>
</table>
<hr>The documentation for this class was generated from the following files:<ul>
<li><a class="el" href="a06383.html">shared_memory.h</a><li><a class="el" href="a06382.html">shared_memory.cpp</a></ul>
<hr size="1"><address style="align: right;"><small>Generated on Tue Mar 16 13:32:41 2004 for NeL by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0 > 
</a>1.3.6 </small></address>
</body>
</html>