OSVR-Core
SharedMemory.h
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_SharedMemory_h_GUID_4EAFB440_5023_439B_9413_0E54BC240DBD
26 #define INCLUDED_SharedMemory_h_GUID_4EAFB440_5023_439B_9413_0E54BC240DBD
27 
30 #define BOOST_CB_DISABLE_DEBUG
31 
33 #define BOOST_DATE_TIME_NO_LIB
34 
37 #include <boost/version.hpp>
38 #if defined(__clang__) && (BOOST_VERSION < 105000) && \
39  !defined(BOOST_NO_TEMPLATE_ALIASES)
40 #define OSVR_NEEDS_BOOST_CLANG_WORKAROUND
41 #endif
42 
43 // Internal Includes
44 #include <osvr/Util/StdInt.h>
45 
46 // Library/third-party includes
47 #ifdef OSVR_NEEDS_BOOST_CLANG_WORKAROUND
48 #define BOOST_NO_TEMPLATE_ALIASES
49 #endif
50 #include <boost/interprocess/mem_algo/rbtree_best_fit.hpp>
51 #ifdef OSVR_NEEDS_BOOST_CLANG_WORKAROUND
52 #undef BOOST_NO_TEMPLATE_ALIASES
53 #endif
54 
55 #include <boost/interprocess/interprocess_fwd.hpp>
56 #include <boost/interprocess/offset_ptr.hpp>
57 
58 // Standard includes
59 #include <cctype>
60 #include <string>
61 
62 #undef OSVR_USE_PLATFORM_SPECIALIZED_SHM
63 
64 #if defined(BOOST_INTERPROCESS_WINDOWS)
65 // Currently only supporting mixing 32/64 on Windows
66 #define OSVR_SHM_3264
67 #define OSVR_HAVE_WINDOWS_SHM
68 #include <boost/interprocess/managed_windows_shared_memory.hpp>
69 #elif defined(BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS)
70 // Disabled - can't compute a correct key at this time.
71 #undef OSVR_HAVE_XSI_SHM
72 #ifdef OSVR_USE_PLATFORM_SPECIALIZED_SHM
73 #undef OSVR_USE_PLATFORM_SPECIALIZED_SHM
74 #endif
75 #include <boost/interprocess/managed_xsi_shared_memory.hpp>
76 #else
77 #ifdef OSVR_USE_PLATFORM_SPECIALIZED_SHM
78 #undef OSVR_USE_PLATFORM_SPECIALIZED_SHM
79 #endif
80 #endif
81 
82 #include <boost/interprocess/managed_shared_memory.hpp>
83 
84 namespace osvr {
85 namespace common {
86 
87  namespace ipc {
88 #ifdef OSVR_SHM_3264
89  namespace detail {
93  typedef int64_t my_ptrdiff;
94  typedef uint64_t my_uintptr;
95  static_assert(
96  sizeof(my_ptrdiff) == sizeof(my_uintptr),
97  "These types must be the same size - the largest "
98  "possible pointer size on a given platform that you want "
99  "to interoperate!");
100  static_assert(sizeof(my_ptrdiff) >= sizeof(::ptrdiff_t),
101  "my_ptrdiff must be at least as big as ptrdiff_t!");
102  static_assert(sizeof(my_uintptr) >= sizeof(::uintptr_t),
103  "my_uintptr must be at least as big as uintptr_t!");
104 
105  template <typename T>
106  using my_offset_ptr =
107  boost::interprocess::offset_ptr<T, my_ptrdiff, my_uintptr>;
108 
109  } // namespace detail
110  template <typename T> using ipc_offset_ptr = detail::my_offset_ptr<T>;
111 
112 #else
113  template <typename T>
114  using ipc_offset_ptr = boost::interprocess::offset_ptr<T>;
115 #endif
116 
117  template <class MutexFamily>
118  using my_mem_alloc_algo =
119  boost::interprocess::rbtree_best_fit<MutexFamily,
120  ipc_offset_ptr<void> >;
121 
122  template <class IndexConfig>
123  using index_type = boost::interprocess::iset_index<IndexConfig>;
124 
128  template <typename ManagedMemory> struct device_type {
129  static void remove(const char *name) {
130  typedef typename ManagedMemory::device_type device_type;
131  device_type::remove(name);
132  }
133  };
134 
135  typedef uint8_t SharedMemoryBackendType;
136  static const SharedMemoryBackendType BASIC_MANAGED_SHM_ID = 0;
137  static const SharedMemoryBackendType WINDOWS_MANAGED_SHM_ID = 1;
138  static const SharedMemoryBackendType SYSV_MANAGED_SHM_ID = 2;
139 
140  using basic_managed_shm =
141  boost::interprocess::basic_managed_shared_memory<
142  char, my_mem_alloc_algo<boost::interprocess::mutex_family>,
143  index_type>;
144 
145 #if defined(OSVR_HAVE_WINDOWS_SHM)
146  using windows_managed_shm =
147  boost::interprocess::basic_managed_windows_shared_memory<
148  char, my_mem_alloc_algo<boost::interprocess::mutex_family>,
149  index_type>;
150  template <> struct device_type<windows_managed_shm> {
151  static void remove(const char *) {}
152  };
153 #endif
154 
155 #if defined(OSVR_HAVE_XSI_SHM)
156 
157  using sysv_managed_shm =
158  boost::interprocess::basic_managed_xsi_shared_memory<
159  char, my_mem_alloc_algo<boost::interprocess::mutex_family>,
160  index_type>;
161 #endif
162 
163 #ifdef OSVR_USE_PLATFORM_SPECIALIZED_SHM
164 #if defined(OSVR_HAVE_WINDOWS_SHM)
165  using default_managed_shm = windows_managed_shm;
166  static const SharedMemoryBackendType DEFAULT_MANAGED_SHM_ID =
167  WINDOWS_MANAGED_SHM_ID;
168 #elif defined(OSVR_HAVE_XSI_SHM)
169  using default_managed_shm = sysv_managed_shm;
170  static const SharedMemoryBackendType DEFAULT_MANAGED_SHM_ID =
171  SYSV_MANAGED_SHM_ID;
172 #else
173 #error \
174  "Unknown kind of platform-specialized shared memory - this set of conditionals must match the one with the includes above!"
175 #endif
176 #else // !OSVR_USE_PLATFORM_SPECIALIZED_SHM
177  using default_managed_shm = basic_managed_shm;
178  static const SharedMemoryBackendType DEFAULT_MANAGED_SHM_ID =
179  BASIC_MANAGED_SHM_ID;
180 #endif // OSVR_USE_PLATFORM_SPECIALIZED_SHM
181 
183  inline std::string make_name_safe(std::string const &input) {
184  static const char UNDERSCORE = '_';
185  std::string ret{input};
186 
188  if (ret.empty() || !std::isalpha(ret[0])) {
189  ret.insert(0u, "Z");
190  }
191 
192  for (auto &c : ret) {
193  if (std::isalpha(c) || std::isdigit(c) || UNDERSCORE == c) {
194  continue;
195  }
196  c = UNDERSCORE;
197  }
198  return ret;
199  }
200  } // namespace ipc
201  using ipc::ipc_offset_ptr;
202 } // namespace common
203 } // namespace osvr
204 
205 #endif // INCLUDED_SharedMemory_h_GUID_4EAFB440_5023_439B_9413_0E54BC240DBD
Handles spatial transformations.
Definition: SerializationTraitExample_Complicated.h:40
Helper function to be able to remove shared memory.
Definition: SharedMemory.h:128
Header wrapping the C99 standard stdint header.
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
Definition: newuoa.h:1888
std::string make_name_safe(std::string const &input)
Turns all "unsafe" characters in a name into underscores.
Definition: SharedMemory.h:183