OVR_SharedMemory.h 7.8 KB

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