ovr_sdk

view LibOVR/Src/Kernel/OVR_SharedMemory.h @ 0:1b39a1b46319

initial 0.4.4
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 14 Jan 2015 06:51:16 +0200
parents
children
line source
1 /************************************************************************************
3 PublicHeader: OVR
4 Filename : OVR_SharedMemory.h
5 Content : Inter-process shared memory subsystem
6 Created : June 1, 2014
7 Notes :
9 Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
11 Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
12 you may not use the Oculus VR Rift SDK except in compliance with the License,
13 which is provided at the time of installation or download, or which
14 otherwise accompanies this software in either electronic or hard copy form.
16 You may obtain a copy of the License at
18 http://www.oculusvr.com/licenses/LICENSE-3.2
20 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
21 distributed under the License is distributed on an "AS IS" BASIS,
22 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 See the License for the specific language governing permissions and
24 limitations under the License.
26 ************************************************************************************/
28 #ifndef OVR_SharedMemory_h
29 #define OVR_SharedMemory_h
31 #include "OVR_Types.h"
32 #include "OVR_RefCount.h"
33 #include "OVR_Allocator.h"
34 #include "OVR_System.h"
36 #ifdef OVR_SINGLE_PROCESS /* Everything running in one process usually for debugging */
37 #define OVR_FAKE_SHAREDMEMORY /* Single-process version to avoid admin privs */
38 #endif
40 namespace OVR {
42 class SharedMemoryInternal; // Opaque
45 // SharedMemory
46 // Note: Safe when used between 32-bit and 64-bit processes
47 class SharedMemory : public RefCountBase<SharedMemory>
48 {
49 friend class SharedMemoryFactory;
51 OVR_NON_COPYABLE(SharedMemory);
53 public:
54 // Only constructed by the SharedMemory Factory
55 SharedMemory(int size, void* data, SharedMemoryInternal* pInternal);
56 // Call close when it goes out of scope
57 ~SharedMemory();
59 // Modes for opening a new shared memory region
60 enum OpenMode
61 {
62 // Note: On Windows, Create* requires Administrator priviledges or running as a Service.
63 OpenMode_CreateOnly, // Must not already exist
64 OpenMode_OpenOnly, // Must already exist
65 OpenMode_CreateOrOpen // May exist or not
66 };
68 // Local access restrictions
69 enum AccessMode
70 {
71 AccessMode_ReadOnly, // Acquire read-only access
72 AccessMode_ReadWrite, // Acquire read or write access
73 };
75 // Remote access restrictions
76 enum RemoteMode
77 {
78 RemoteMode_ReadOnly, // Other processes will need to open in read-only mode
79 RemoteMode_ReadWrite // Other processes can open in read-write mode
80 };
82 // Modes for opening a new shared memory region
83 struct OpenParameters
84 {
85 OpenParameters() :
86 globalName(NULL),
87 minSizeBytes(0),
88 openMode(SharedMemory::OpenMode_CreateOrOpen),
89 remoteMode(SharedMemory::RemoteMode_ReadWrite),
90 accessMode(SharedMemory::AccessMode_ReadWrite)
91 {
92 }
94 // Creation parameters
95 const char* globalName; // Name of the shared memory region
96 int minSizeBytes; // Minimum number of bytes to request
97 SharedMemory::OpenMode openMode; // Creating the file or opening the file?
98 SharedMemory::RemoteMode remoteMode; // When creating, what access should other processes get?
99 SharedMemory::AccessMode accessMode; // When opening/creating, what access should this process get?
100 };
102 public:
103 // Returns the size of the shared memory region
104 int GetSizeI() const
105 {
106 return Size;
107 }
109 // Returns the process-local pointer to the shared memory region
110 // Note: This may be different on different processes
111 void* GetData() const
112 {
113 return Data;
114 }
116 protected:
117 int Size; // How many shared bytes are shared at the pointer address?
118 void* Data; // Pointer to the shared memory region.
120 // Hidden implementation class for OS-specific behavior
121 SharedMemoryInternal* Internal;
123 // Close and cleanup the shared memory region
124 // Note: This is called on destruction
125 void Close();
126 };
129 // SharedMemoryFactory
130 class SharedMemoryFactory : public NewOverrideBase, public SystemSingletonBase<SharedMemoryFactory>
131 {
132 OVR_DECLARE_SINGLETON(SharedMemoryFactory);
134 public:
135 // Construct a SharedMemory object.
136 // Note: The new object is reference-counted so it should be stored with Ptr<>. Initial reference count is 1.
137 Ptr<SharedMemory> Open(const SharedMemory::OpenParameters&);
138 };
141 // A shared object
142 // Its constructor will be called when creating a writer
143 // Its destructor will not be called
144 template<class SharedType>
145 class ISharedObject : public NewOverrideBase
146 {
147 public:
148 static const int RegionSize = (int)sizeof(SharedType);
150 protected:
151 Ptr<SharedMemory> pSharedMemory;
153 bool Open(const char* name, bool readOnly)
154 {
155 // Configure open parameters based on read-only mode
156 SharedMemory::OpenParameters params;
158 // FIXME: This is a hack. We currently need to allow clients to open this for read-write even
159 // though they only need read-only access. This is because in the first 0.4 release the
160 // LocklessUpdater class technically writes to it (increments by 0) to read from the space.
161 // This was quickly corrected in 0.4.1 and we are waiting for the right time to disallow write
162 // access when everyone upgrades to 0.4.1+.
163 //params.remoteMode = SharedMemory::RemoteMode_ReadOnly;
164 params.remoteMode = SharedMemory::RemoteMode_ReadWrite;
166 params.globalName = name;
167 params.accessMode = readOnly ? SharedMemory::AccessMode_ReadOnly : SharedMemory::AccessMode_ReadWrite;
168 params.minSizeBytes = RegionSize;
169 params.openMode = readOnly ? SharedMemory::OpenMode_OpenOnly : SharedMemory::OpenMode_CreateOrOpen;
171 // Attempt to open the shared memory file
172 pSharedMemory = SharedMemoryFactory::GetInstance()->Open(params);
174 // If it was not able to be opened,
175 if (pSharedMemory && pSharedMemory->GetSizeI() >= RegionSize && pSharedMemory->GetData())
176 {
177 // If writing,
178 if (!readOnly)
179 {
180 // Construct the object also
181 Construct<SharedType>(pSharedMemory->GetData());
182 }
184 return true;
185 }
187 return false;
188 }
190 SharedType* Get() const
191 {
192 if (!pSharedMemory)
193 {
194 return NULL;
195 }
197 void* data = pSharedMemory->GetData();
198 if (!data)
199 {
200 return NULL;
201 }
203 return reinterpret_cast<SharedType*>(data);
204 }
205 };
207 // Writer specialized shared object: Ctor will be called on Open()
208 template<class SharedType>
209 class SharedObjectWriter : public ISharedObject<SharedType>
210 {
211 public:
212 OVR_FORCE_INLINE bool Open(const char* name)
213 {
214 return ISharedObject<SharedType>::Open(name, false);
215 }
216 OVR_FORCE_INLINE SharedType* Get()
217 {
218 return ISharedObject<SharedType>::Get();
219 }
220 };
222 // Reader specialized shared object: Ctor will not be called
223 template<class SharedType>
224 class SharedObjectReader : public ISharedObject<SharedType>
225 {
226 public:
227 OVR_FORCE_INLINE bool Open(const char* name)
228 {
229 return ISharedObject<SharedType>::Open(name, true);
230 }
231 OVR_FORCE_INLINE const SharedType* Get() const
232 {
233 return ISharedObject<SharedType>::Get();
234 }
235 };
238 } // namespace OVR
240 #endif // OVR_SharedMemory_h