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
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>winapi_common: include/winapi/shmem.hpp Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">winapi_common
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
$(function() {
initMenu('',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div id="nav-path" class="navpath">
<ul>
<li class="navelem"><a class="el" href="dir_d44c64559bbebec7f509842c48db8b23.html">include</a></li><li class="navelem"><a class="el" href="dir_f1714771db42b7b5a0cd2ebe6e9736ed.html">winapi</a></li> </ul>
</div>
</div><!-- top -->
<div class="header">
<div class="headertitle">
<div class="title">shmem.hpp</div> </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span> <span class="comment">// Copyright (c) 2020 Egor Tensin <Egor.Tensin@gmail.com></span></div>
<div class="line"><a name="l00002"></a><span class="lineno"> 2</span> <span class="comment">// This file is part of the "winapi-common" project.</span></div>
<div class="line"><a name="l00003"></a><span class="lineno"> 3</span> <span class="comment">// For details, see https://github.com/egor-tensin/winapi-common.</span></div>
<div class="line"><a name="l00004"></a><span class="lineno"> 4</span> <span class="comment">// Distributed under the MIT License.</span></div>
<div class="line"><a name="l00005"></a><span class="lineno"> 5</span>  </div>
<div class="line"><a name="l00006"></a><span class="lineno"> 6</span> <span class="preprocessor">#pragma once</span></div>
<div class="line"><a name="l00007"></a><span class="lineno"> 7</span>  </div>
<div class="line"><a name="l00008"></a><span class="lineno"> 8</span> <span class="preprocessor">#include "handle.hpp"</span></div>
<div class="line"><a name="l00009"></a><span class="lineno"> 9</span>  </div>
<div class="line"><a name="l00010"></a><span class="lineno"> 10</span> <span class="preprocessor">#include <cstddef></span></div>
<div class="line"><a name="l00011"></a><span class="lineno"> 11</span> <span class="preprocessor">#include <memory></span></div>
<div class="line"><a name="l00012"></a><span class="lineno"> 12</span> <span class="preprocessor">#include <string></span></div>
<div class="line"><a name="l00013"></a><span class="lineno"> 13</span> <span class="preprocessor">#include <type_traits></span></div>
<div class="line"><a name="l00014"></a><span class="lineno"> 14</span> <span class="preprocessor">#include <utility></span></div>
<div class="line"><a name="l00015"></a><span class="lineno"> 15</span>  </div>
<div class="line"><a name="l00016"></a><span class="lineno"> 16</span> <span class="keyword">namespace </span>winapi {</div>
<div class="line"><a name="l00017"></a><span class="lineno"> 17</span>  </div>
<div class="line"><a name="l00019"></a><span class="lineno"><a class="line" href="classwinapi_1_1SharedMemory.html"> 19</a></span> <span class="keyword">class </span><a class="code" href="classwinapi_1_1SharedMemory.html">SharedMemory</a> {</div>
<div class="line"><a name="l00020"></a><span class="lineno"> 20</span> <span class="keyword">public</span>:</div>
<div class="line"><a name="l00026"></a><span class="lineno"> 26</span>  <span class="keyword">static</span> <a class="code" href="classwinapi_1_1SharedMemory.html">SharedMemory</a> <a class="code" href="classwinapi_1_1SharedMemory.html#a475d4dcdf99d777cb0968d7631cc6f7e">create</a>(<span class="keyword">const</span> std::string& name, std::size_t nb);</div>
<div class="line"><a name="l00031"></a><span class="lineno"> 31</span>  <span class="keyword">static</span> <a class="code" href="classwinapi_1_1SharedMemory.html">SharedMemory</a> <a class="code" href="classwinapi_1_1SharedMemory.html#a82eafb0e7c2c6b5af1a7ff8945af2027">open</a>(<span class="keyword">const</span> std::string& name);</div>
<div class="line"><a name="l00032"></a><span class="lineno"> 32</span>  </div>
<div class="line"><a name="l00034"></a><span class="lineno"><a class="line" href="classwinapi_1_1SharedMemory.html#ada3f0075608dd1541f54f004e18a4775"> 34</a></span>  <span class="keywordtype">void</span>* <a class="code" href="classwinapi_1_1SharedMemory.html#ada3f0075608dd1541f54f004e18a4775">get</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> m_addr.get(); }</div>
<div class="line"><a name="l00036"></a><span class="lineno"><a class="line" href="classwinapi_1_1SharedMemory.html#a44e75ba35ae3e470fb521f6274991673"> 36</a></span>  <span class="keywordtype">void</span>* <a class="code" href="classwinapi_1_1SharedMemory.html#a44e75ba35ae3e470fb521f6274991673">ptr</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> <a class="code" href="classwinapi_1_1SharedMemory.html#ada3f0075608dd1541f54f004e18a4775">get</a>(); }</div>
<div class="line"><a name="l00037"></a><span class="lineno"> 37</span>  </div>
<div class="line"><a name="l00038"></a><span class="lineno"> 38</span> <span class="keyword">private</span>:</div>
<div class="line"><a name="l00039"></a><span class="lineno"> 39</span>  <span class="keyword">struct </span>Unmap {</div>
<div class="line"><a name="l00040"></a><span class="lineno"> 40</span>  <span class="keywordtype">void</span> operator()(<span class="keywordtype">void</span>*) <span class="keyword">const</span>;</div>
<div class="line"><a name="l00041"></a><span class="lineno"> 41</span>  };</div>
<div class="line"><a name="l00042"></a><span class="lineno"> 42</span>  </div>
<div class="line"><a name="l00043"></a><span class="lineno"> 43</span>  SharedMemory() = <span class="keywordflow">default</span>;</div>
<div class="line"><a name="l00044"></a><span class="lineno"> 44</span>  </div>
<div class="line"><a name="l00045"></a><span class="lineno"> 45</span>  SharedMemory(Handle&& handle, <span class="keywordtype">void</span>* addr) : m_handle{std::move(handle)}, m_addr{addr} {}</div>
<div class="line"><a name="l00046"></a><span class="lineno"> 46</span>  </div>
<div class="line"><a name="l00047"></a><span class="lineno"> 47</span>  Handle m_handle;</div>
<div class="line"><a name="l00048"></a><span class="lineno"> 48</span>  std::unique_ptr<void, Unmap> m_addr;</div>
<div class="line"><a name="l00049"></a><span class="lineno"> 49</span> };</div>
<div class="line"><a name="l00050"></a><span class="lineno"> 50</span>  </div>
<div class="line"><a name="l00052"></a><span class="lineno"> 52</span> <span class="keyword">template</span> <<span class="keyword">typename</span> T></div>
<div class="line"><a name="l00053"></a><span class="lineno"><a class="line" href="classwinapi_1_1SharedObject.html"> 53</a></span> <span class="keyword">class </span><a class="code" href="classwinapi_1_1SharedObject.html">SharedObject</a> {</div>
<div class="line"><a name="l00054"></a><span class="lineno"> 54</span> <span class="keyword">public</span>:</div>
<div class="line"><a name="l00055"></a><span class="lineno"> 55</span>  <span class="keyword">typedef</span> <span class="keyword">typename</span> std::aligned_storage<<span class="keyword">sizeof</span>(T), __alignof(T)>::type AlignedType;</div>
<div class="line"><a name="l00056"></a><span class="lineno"> 56</span>  </div>
<div class="line"><a name="l00062"></a><span class="lineno"> 62</span>  <span class="keyword">template</span> <<span class="keyword">typename</span>... Args></div>
<div class="line"><a name="l00063"></a><span class="lineno"><a class="line" href="classwinapi_1_1SharedObject.html#acb4ef5283256d7463fe81c8b312b6745"> 63</a></span>  <span class="keyword">static</span> <a class="code" href="classwinapi_1_1SharedObject.html">SharedObject</a> <a class="code" href="classwinapi_1_1SharedObject.html#acb4ef5283256d7463fe81c8b312b6745">create</a>(<span class="keyword">const</span> std::string& name, Args&&... args) {</div>
<div class="line"><a name="l00064"></a><span class="lineno"> 64</span>  <a class="code" href="classwinapi_1_1SharedObject.html">SharedObject</a> obj{<a class="code" href="classwinapi_1_1SharedMemory.html#a475d4dcdf99d777cb0968d7631cc6f7e">SharedMemory::create</a>(name, <span class="keyword">sizeof</span>(AlignedType))};</div>
<div class="line"><a name="l00065"></a><span class="lineno"> 65</span>  <span class="keyword">new</span> (obj.ptr()) T(std::forward<Args>(args)...);</div>
<div class="line"><a name="l00066"></a><span class="lineno"> 66</span>  obj.m_destruct = <span class="keyword">true</span>;</div>
<div class="line"><a name="l00067"></a><span class="lineno"> 67</span>  <span class="keywordflow">return</span> obj;</div>
<div class="line"><a name="l00068"></a><span class="lineno"> 68</span>  }</div>
<div class="line"><a name="l00069"></a><span class="lineno"> 69</span>  </div>
<div class="line"><a name="l00074"></a><span class="lineno"><a class="line" href="classwinapi_1_1SharedObject.html#a08fda6ddde26e0a10170efaf07232761"> 74</a></span>  <span class="keyword">static</span> <a class="code" href="classwinapi_1_1SharedObject.html">SharedObject</a> <a class="code" href="classwinapi_1_1SharedObject.html#a08fda6ddde26e0a10170efaf07232761">open</a>(<span class="keyword">const</span> std::string& name) {</div>
<div class="line"><a name="l00075"></a><span class="lineno"> 75</span>  <a class="code" href="classwinapi_1_1SharedObject.html">SharedObject</a> obj{<a class="code" href="classwinapi_1_1SharedMemory.html#a82eafb0e7c2c6b5af1a7ff8945af2027">SharedMemory::open</a>(name)};</div>
<div class="line"><a name="l00076"></a><span class="lineno"> 76</span>  <span class="keywordflow">return</span> obj;</div>
<div class="line"><a name="l00077"></a><span class="lineno"> 77</span>  }</div>
<div class="line"><a name="l00078"></a><span class="lineno"> 78</span>  </div>
<div class="line"><a name="l00079"></a><span class="lineno"> 79</span>  <a class="code" href="classwinapi_1_1SharedObject.html">SharedObject</a>(<a class="code" href="classwinapi_1_1SharedObject.html">SharedObject</a>&& other) noexcept = <span class="keywordflow">default</span>;</div>
<div class="line"><a name="l00080"></a><span class="lineno"> 80</span>  <a class="code" href="classwinapi_1_1SharedObject.html">SharedObject</a>& operator=(<span class="keyword">const</span> <a class="code" href="classwinapi_1_1SharedObject.html">SharedObject</a>& other) noexcept = <span class="keywordflow">default</span>;</div>
<div class="line"><a name="l00081"></a><span class="lineno"> 81</span>  <a class="code" href="classwinapi_1_1SharedObject.html">SharedObject</a>(<span class="keyword">const</span> <a class="code" href="classwinapi_1_1SharedObject.html">SharedObject</a>&) = <span class="keyword">delete</span>;</div>
<div class="line"><a name="l00082"></a><span class="lineno"> 82</span>  </div>
<div class="line"><a name="l00083"></a><span class="lineno"> 83</span>  ~<a class="code" href="classwinapi_1_1SharedObject.html">SharedObject</a>() {</div>
<div class="line"><a name="l00084"></a><span class="lineno"> 84</span>  <span class="keywordflow">if</span> (m_destruct && <a class="code" href="classwinapi_1_1SharedObject.html#a0dfaad2c143d60af8dc6c010a13d513a">ptr</a>()) {</div>
<div class="line"><a name="l00085"></a><span class="lineno"> 85</span>  <a class="code" href="classwinapi_1_1SharedObject.html#a0dfaad2c143d60af8dc6c010a13d513a">ptr</a>()->~T();</div>
<div class="line"><a name="l00086"></a><span class="lineno"> 86</span>  }</div>
<div class="line"><a name="l00087"></a><span class="lineno"> 87</span>  }</div>
<div class="line"><a name="l00088"></a><span class="lineno"> 88</span>  </div>
<div class="line"><a name="l00090"></a><span class="lineno"><a class="line" href="classwinapi_1_1SharedObject.html#a0dfaad2c143d60af8dc6c010a13d513a"> 90</a></span>  T* <a class="code" href="classwinapi_1_1SharedObject.html#a0dfaad2c143d60af8dc6c010a13d513a">ptr</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> <span class="keyword">reinterpret_cast<</span>T*<span class="keyword">></span>(m_shmem.<a class="code" href="classwinapi_1_1SharedMemory.html#a44e75ba35ae3e470fb521f6274991673">ptr</a>()); }</div>
<div class="line"><a name="l00092"></a><span class="lineno"><a class="line" href="classwinapi_1_1SharedObject.html#a85d0943708ab0da78e1a189b1bbbd1eb"> 92</a></span>  T& <a class="code" href="classwinapi_1_1SharedObject.html#a85d0943708ab0da78e1a189b1bbbd1eb">get</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> *<a class="code" href="classwinapi_1_1SharedObject.html#a0dfaad2c143d60af8dc6c010a13d513a">ptr</a>(); }</div>
<div class="line"><a name="l00093"></a><span class="lineno"> 93</span>  </div>
<div class="line"><a name="l00094"></a><span class="lineno"> 94</span>  T* operator->()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> <a class="code" href="classwinapi_1_1SharedObject.html#a0dfaad2c143d60af8dc6c010a13d513a">ptr</a>(); }</div>
<div class="line"><a name="l00095"></a><span class="lineno"> 95</span>  T& operator*()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> <a class="code" href="classwinapi_1_1SharedObject.html#a85d0943708ab0da78e1a189b1bbbd1eb">get</a>(); }</div>
<div class="line"><a name="l00096"></a><span class="lineno"> 96</span>  </div>
<div class="line"><a name="l00097"></a><span class="lineno"> 97</span> <span class="keyword">private</span>:</div>
<div class="line"><a name="l00098"></a><span class="lineno"> 98</span>  <span class="keyword">explicit</span> SharedObject(SharedMemory&& shmem) : m_shmem(std::move(shmem)) {}</div>
<div class="line"><a name="l00099"></a><span class="lineno"> 99</span>  </div>
<div class="line"><a name="l00100"></a><span class="lineno"> 100</span>  SharedMemory m_shmem;</div>
<div class="line"><a name="l00101"></a><span class="lineno"> 101</span>  <span class="comment">// Destruct only once, no matter the number of mappings.</span></div>
<div class="line"><a name="l00102"></a><span class="lineno"> 102</span>  <span class="keywordtype">bool</span> m_destruct = <span class="keyword">false</span>;</div>
<div class="line"><a name="l00103"></a><span class="lineno"> 103</span> };</div>
<div class="line"><a name="l00104"></a><span class="lineno"> 104</span>  </div>
<div class="line"><a name="l00105"></a><span class="lineno"> 105</span> } <span class="comment">// namespace winapi</span></div>
<div class="ttc" id="aclasswinapi_1_1SharedMemory_html"><div class="ttname"><a href="classwinapi_1_1SharedMemory.html">winapi::SharedMemory</a></div><div class="ttdoc">Named shared memory region.</div><div class="ttdef"><b>Definition:</b> <a href="shmem_8hpp_source.html#l00019">shmem.hpp:19</a></div></div>
<div class="ttc" id="aclasswinapi_1_1SharedMemory_html_a44e75ba35ae3e470fb521f6274991673"><div class="ttname"><a href="classwinapi_1_1SharedMemory.html#a44e75ba35ae3e470fb521f6274991673">winapi::SharedMemory::ptr</a></div><div class="ttdeci">void * ptr() const</div><div class="ttdef"><b>Definition:</b> <a href="shmem_8hpp_source.html#l00036">shmem.hpp:36</a></div></div>
<div class="ttc" id="aclasswinapi_1_1SharedMemory_html_a475d4dcdf99d777cb0968d7631cc6f7e"><div class="ttname"><a href="classwinapi_1_1SharedMemory.html#a475d4dcdf99d777cb0968d7631cc6f7e">winapi::SharedMemory::create</a></div><div class="ttdeci">static SharedMemory create(const std::string &name, std::size_t nb)</div><div class="ttdef"><b>Definition:</b> <a href="shmem_8cpp_source.html#l00040">shmem.cpp:40</a></div></div>
<div class="ttc" id="aclasswinapi_1_1SharedMemory_html_a82eafb0e7c2c6b5af1a7ff8945af2027"><div class="ttname"><a href="classwinapi_1_1SharedMemory.html#a82eafb0e7c2c6b5af1a7ff8945af2027">winapi::SharedMemory::open</a></div><div class="ttdeci">static SharedMemory open(const std::string &name)</div><div class="ttdef"><b>Definition:</b> <a href="shmem_8cpp_source.html#l00059">shmem.cpp:59</a></div></div>
<div class="ttc" id="aclasswinapi_1_1SharedMemory_html_ada3f0075608dd1541f54f004e18a4775"><div class="ttname"><a href="classwinapi_1_1SharedMemory.html#ada3f0075608dd1541f54f004e18a4775">winapi::SharedMemory::get</a></div><div class="ttdeci">void * get() const</div><div class="ttdef"><b>Definition:</b> <a href="shmem_8hpp_source.html#l00034">shmem.hpp:34</a></div></div>
<div class="ttc" id="aclasswinapi_1_1SharedObject_html"><div class="ttname"><a href="classwinapi_1_1SharedObject.html">winapi::SharedObject</a></div><div class="ttdoc">Easy way to represent a C++ object as a shared memory region.</div><div class="ttdef"><b>Definition:</b> <a href="shmem_8hpp_source.html#l00053">shmem.hpp:53</a></div></div>
<div class="ttc" id="aclasswinapi_1_1SharedObject_html_a08fda6ddde26e0a10170efaf07232761"><div class="ttname"><a href="classwinapi_1_1SharedObject.html#a08fda6ddde26e0a10170efaf07232761">winapi::SharedObject::open</a></div><div class="ttdeci">static SharedObject open(const std::string &name)</div><div class="ttdef"><b>Definition:</b> <a href="shmem_8hpp_source.html#l00074">shmem.hpp:74</a></div></div>
<div class="ttc" id="aclasswinapi_1_1SharedObject_html_a0dfaad2c143d60af8dc6c010a13d513a"><div class="ttname"><a href="classwinapi_1_1SharedObject.html#a0dfaad2c143d60af8dc6c010a13d513a">winapi::SharedObject::ptr</a></div><div class="ttdeci">T * ptr() const</div><div class="ttdef"><b>Definition:</b> <a href="shmem_8hpp_source.html#l00090">shmem.hpp:90</a></div></div>
<div class="ttc" id="aclasswinapi_1_1SharedObject_html_a85d0943708ab0da78e1a189b1bbbd1eb"><div class="ttname"><a href="classwinapi_1_1SharedObject.html#a85d0943708ab0da78e1a189b1bbbd1eb">winapi::SharedObject::get</a></div><div class="ttdeci">T & get() const</div><div class="ttdef"><b>Definition:</b> <a href="shmem_8hpp_source.html#l00092">shmem.hpp:92</a></div></div>
<div class="ttc" id="aclasswinapi_1_1SharedObject_html_acb4ef5283256d7463fe81c8b312b6745"><div class="ttname"><a href="classwinapi_1_1SharedObject.html#acb4ef5283256d7463fe81c8b312b6745">winapi::SharedObject::create</a></div><div class="ttdeci">static SharedObject create(const std::string &name, Args &&... args)</div><div class="ttdef"><b>Definition:</b> <a href="shmem_8hpp_source.html#l00063">shmem.hpp:63</a></div></div>
</div><!-- fragment --></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.1
</small></address>
</body>
</html>
|