OVR_SharedMemory.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /************************************************************************************
  2. Filename : OVR_SharedMemory.cpp
  3. Content : Inter-process shared memory subsystem
  4. Created : June 1, 2014
  5. Notes :
  6. Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
  7. Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
  8. you may not use the Oculus VR Rift SDK except in compliance with the License,
  9. which is provided at the time of installation or download, or which
  10. otherwise accompanies this software in either electronic or hard copy form.
  11. You may obtain a copy of the License at
  12. http://www.oculusvr.com/licenses/LICENSE-3.2
  13. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  14. distributed under the License is distributed on an "AS IS" BASIS,
  15. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. See the License for the specific language governing permissions and
  17. limitations under the License.
  18. ************************************************************************************/
  19. #include "OVR_SharedMemory.h"
  20. #include "OVR_Atomic.h"
  21. #include "OVR_Log.h"
  22. #include "OVR_String.h"
  23. #include "OVR_Array.h"
  24. #if defined(OVR_OS_WIN32)
  25. #include <Sddl.h> // ConvertStringSecurityDescriptorToSecurityDescriptor
  26. #endif // OVR_OS_WIN32
  27. #if defined(OVR_OS_LINUX) || defined(OVR_OS_MAC)
  28. #include <sys/mman.h> // shm_open(), mmap()
  29. #include <errno.h> // error results for mmap
  30. #include <sys/stat.h> // mode constants
  31. #include <fcntl.h> // O_ constants
  32. #include <unistd.h> // close()
  33. #endif // OVR_OS_LINUX
  34. OVR_DEFINE_SINGLETON(OVR::SharedMemoryFactory);
  35. namespace OVR {
  36. //-----------------------------------------------------------------------------
  37. // SharedMemoryInternalBase
  38. class SharedMemoryInternalBase : public NewOverrideBase
  39. {
  40. public:
  41. SharedMemoryInternalBase()
  42. {
  43. }
  44. virtual ~SharedMemoryInternalBase()
  45. {
  46. }
  47. virtual void* GetFileView() = 0;
  48. };
  49. //-----------------------------------------------------------------------------
  50. // FakeMemoryBlock
  51. class FakeMemoryBlock : public RefCountBase<FakeMemoryBlock>
  52. {
  53. String Name;
  54. char* Data;
  55. int SizeBytes;
  56. int References;
  57. public:
  58. FakeMemoryBlock(const String& name, int size) :
  59. Name(name),
  60. Data(NULL),
  61. SizeBytes(size),
  62. References(1)
  63. {
  64. Data = new char[SizeBytes];
  65. }
  66. ~FakeMemoryBlock()
  67. {
  68. delete[] Data;
  69. }
  70. bool IsNamed(const String& name)
  71. {
  72. return Name.CompareNoCase(name) == 0;
  73. }
  74. void* GetData()
  75. {
  76. return Data;
  77. }
  78. int GetSizeI()
  79. {
  80. return SizeBytes;
  81. }
  82. void IncrementReferences()
  83. {
  84. ++References;
  85. }
  86. bool DecrementReferences()
  87. {
  88. return --References <= 0;
  89. }
  90. };
  91. class FakeMemoryInternal : public SharedMemoryInternalBase
  92. {
  93. public:
  94. void* FileView;
  95. Ptr<FakeMemoryBlock> Block;
  96. FakeMemoryInternal(FakeMemoryBlock* block);
  97. ~FakeMemoryInternal();
  98. virtual void* GetFileView() OVR_OVERRIDE
  99. {
  100. return FileView;
  101. }
  102. };
  103. //-----------------------------------------------------------------------------
  104. // FakeMemoryManager
  105. class FakeMemoryManager : public NewOverrideBase, public SystemSingletonBase<FakeMemoryManager>
  106. {
  107. OVR_DECLARE_SINGLETON(FakeMemoryManager);
  108. Lock FakeLock;
  109. Array< Ptr<FakeMemoryBlock> > FakeArray;
  110. public:
  111. FakeMemoryInternal* Open(const char *name, int bytes, bool openOnly)
  112. {
  113. Lock::Locker locker(&FakeLock);
  114. const int count = FakeArray.GetSizeI();
  115. for (int ii = 0; ii < count; ++ii)
  116. {
  117. if (FakeArray[ii]->IsNamed(name))
  118. {
  119. FakeArray[ii]->IncrementReferences();
  120. return new FakeMemoryInternal(FakeArray[ii]);
  121. }
  122. }
  123. if (openOnly)
  124. {
  125. return NULL;
  126. }
  127. Ptr<FakeMemoryBlock> data = *new FakeMemoryBlock(name, bytes);
  128. FakeArray.PushBack(data);
  129. return new FakeMemoryInternal(data);
  130. }
  131. void Free(FakeMemoryBlock* block)
  132. {
  133. Lock::Locker locker(&FakeLock);
  134. const int count = FakeArray.GetSizeI();
  135. for (int ii = 0; ii < count; ++ii)
  136. {
  137. if (FakeArray[ii].GetPtr() == block)
  138. {
  139. // If the reference count hit zero,
  140. if (FakeArray[ii]->DecrementReferences())
  141. {
  142. // Toast
  143. FakeArray.RemoveAtUnordered(ii);
  144. }
  145. break;
  146. }
  147. }
  148. }
  149. };
  150. FakeMemoryManager::FakeMemoryManager()
  151. {
  152. PushDestroyCallbacks();
  153. }
  154. FakeMemoryManager::~FakeMemoryManager()
  155. {
  156. // If this assertion trips it is because we have not cleanly released shared memory resources.
  157. OVR_ASSERT(FakeArray.GetSizeI() == 0);
  158. }
  159. void FakeMemoryManager::OnSystemDestroy()
  160. {
  161. delete this;
  162. }
  163. FakeMemoryInternal::FakeMemoryInternal(FakeMemoryBlock* block) :
  164. Block(block)
  165. {
  166. FileView = Block->GetData();
  167. }
  168. FakeMemoryInternal::~FakeMemoryInternal()
  169. {
  170. FakeMemoryManager::GetInstance()->Free(Block);
  171. Block.Clear();
  172. }
  173. } // namespace OVR
  174. OVR_DEFINE_SINGLETON(FakeMemoryManager);
  175. namespace OVR {
  176. static SharedMemoryInternalBase* CreateFakeSharedMemory(const SharedMemory::OpenParameters& params)
  177. {
  178. return FakeMemoryManager::GetInstance()->Open(params.globalName, params.minSizeBytes, params.openMode == SharedMemory::OpenMode_OpenOnly);
  179. }
  180. //// Windows version
  181. #if defined(OVR_OS_WIN32)
  182. #pragma comment(lib, "advapi32.lib")
  183. // Hidden implementation class for OS-specific behavior
  184. class SharedMemoryInternal : public SharedMemoryInternalBase
  185. {
  186. public:
  187. HANDLE FileMapping;
  188. void* FileView;
  189. SharedMemoryInternal(HANDLE fileMapping, void* fileView) :
  190. FileMapping(fileMapping),
  191. FileView(fileView)
  192. {
  193. }
  194. ~SharedMemoryInternal()
  195. {
  196. // If file view is set,
  197. if (FileView)
  198. {
  199. UnmapViewOfFile(FileView);
  200. FileView = NULL;
  201. }
  202. // If file mapping is set,
  203. if (FileMapping != NULL)
  204. {
  205. CloseHandle(FileMapping);
  206. FileMapping = NULL;
  207. }
  208. }
  209. virtual void* GetFileView() OVR_OVERRIDE
  210. {
  211. return FileView;
  212. }
  213. };
  214. static SharedMemoryInternal* DoFileMap(HANDLE hFileMapping, const char* fileName, bool openReadOnly, int minSize)
  215. {
  216. // Interpret the access mode as a map desired access code
  217. DWORD mapDesiredAccess = openReadOnly ? FILE_MAP_READ : FILE_MAP_WRITE;
  218. // Map view of the file to this process
  219. void* pFileView = MapViewOfFile(hFileMapping, mapDesiredAccess, 0, 0, minSize);
  220. // If mapping could not be created,
  221. if (!pFileView)
  222. {
  223. CloseHandle(hFileMapping);
  224. OVR_DEBUG_LOG(("[SharedMemory] FAILURE: Unable to map view of file for %s error code = %d", fileName, GetLastError()));
  225. OVR_UNUSED(fileName);
  226. return NULL;
  227. }
  228. // Create internal representation
  229. SharedMemoryInternal* pimple = new SharedMemoryInternal(hFileMapping, pFileView);
  230. // If memory allocation fails,
  231. if (!pimple)
  232. {
  233. UnmapViewOfFile(pFileView);
  234. CloseHandle(hFileMapping);
  235. OVR_DEBUG_LOG(("[SharedMemory] FAILURE: Out of memory"));
  236. return NULL;
  237. }
  238. return pimple;
  239. }
  240. static SharedMemoryInternal* AttemptOpenSharedMemory(const char* fileName, int minSize, bool openReadOnly)
  241. {
  242. // Interpret the access mode as a map desired access code
  243. DWORD mapDesiredAccess = openReadOnly ? FILE_MAP_READ : FILE_MAP_WRITE;
  244. // Open file mapping
  245. std::wstring wFileName = UTF8StringToUCSString(fileName);
  246. HANDLE hFileMapping = OpenFileMappingW(mapDesiredAccess, TRUE, wFileName.c_str());
  247. // If file was mapped unsuccessfully,
  248. if (NULL == hFileMapping)
  249. {
  250. //OVR_DEBUG_LOG(("[SharedMemory] WARNING: Unable to open file mapping for %s error code = %d (not necessarily bad)", fileName, GetLastError()));
  251. return NULL;
  252. }
  253. // Map the file
  254. return DoFileMap(hFileMapping, fileName, openReadOnly, minSize);
  255. }
  256. static SharedMemoryInternal* AttemptCreateSharedMemory(const char* fileName, int minSize, bool openReadOnly, bool allowRemoteWrite)
  257. {
  258. // Prepare a SECURITY_ATTRIBUTES object
  259. SECURITY_ATTRIBUTES security;
  260. ZeroMemory(&security, sizeof(security));
  261. security.nLength = sizeof(security);
  262. // Security descriptor by DACL strings:
  263. // ACE strings grant Allow(A), Object/Contains Inheritance (OICI) of:
  264. // + Grant All (GA) to System (SY)
  265. // + Grant All (GA) to Built-in Administrators (BA)
  266. // + Grant Read-Only (GR) or Read-Write (GWGR) to Interactive Users (IU) - ie. games
  267. static const wchar_t* DACLString_ReadOnly = L"D:P(A;OICI;GA;;;SY)(A;OICI;GA;;;BA)(A;OICI;GR;;;IU)";
  268. static const wchar_t* DACLString_ReadWrite = L"D:P(A;OICI;GA;;;SY)(A;OICI;GA;;;BA)(A;OICI;GWGR;;;IU)";
  269. // Select the remote process access mode
  270. const wchar_t* remoteAccessString =
  271. allowRemoteWrite ? DACLString_ReadWrite : DACLString_ReadOnly;
  272. // Attempt to convert access string to security attributes
  273. // Note: This will allocate the security descriptor with LocalAlloc() and must be freed later
  274. BOOL bConvertOkay = ConvertStringSecurityDescriptorToSecurityDescriptorW(
  275. remoteAccessString, SDDL_REVISION_1, &security.lpSecurityDescriptor, NULL);
  276. // If conversion fails,
  277. if (!bConvertOkay)
  278. {
  279. OVR_DEBUG_LOG(("[SharedMemory] FAILURE: Unable to convert access string, error code = %d", GetLastError()));
  280. return NULL;
  281. }
  282. // Interpret the access mode as a page protection code
  283. int pageProtectCode = openReadOnly ? PAGE_READONLY : PAGE_READWRITE;
  284. std::wstring wFileName = UTF8StringToUCSString(fileName);
  285. // Attempt to create a file mapping
  286. HANDLE hFileMapping = CreateFileMappingW(INVALID_HANDLE_VALUE, // From page file
  287. &security, // Security attributes
  288. pageProtectCode, // Read-only?
  289. 0, // High word for size = 0
  290. minSize, // Low word for size
  291. wFileName.c_str()); // Name of global shared memory file
  292. // Free the security descriptor buffer
  293. LocalFree(security.lpSecurityDescriptor);
  294. // If mapping could not be created,
  295. if (NULL == hFileMapping)
  296. {
  297. OVR_DEBUG_LOG(("[SharedMemory] FAILURE: Unable to create file mapping for %s error code = %d", fileName, GetLastError()));
  298. return NULL;
  299. }
  300. #ifndef OVR_ALLOW_CREATE_FILE_MAPPING_IF_EXISTS
  301. // If the file mapping already exists,
  302. if (GetLastError() == ERROR_ALREADY_EXISTS)
  303. {
  304. CloseHandle(hFileMapping);
  305. OVR_DEBUG_LOG(("[SharedMemory] FAILURE: File mapping at %s already exists", fileName));
  306. return NULL;
  307. }
  308. #endif
  309. // Map the file
  310. return DoFileMap(hFileMapping, fileName, openReadOnly, minSize);
  311. }
  312. static SharedMemoryInternal* CreateSharedMemory(const SharedMemory::OpenParameters& params)
  313. {
  314. SharedMemoryInternal* retval = NULL;
  315. // Construct the file mapping name in a Windows-specific way
  316. OVR::String fileMappingName = params.globalName;
  317. const char *fileName = fileMappingName.ToCStr();
  318. // Is being opened read-only?
  319. const bool openReadOnly = (params.accessMode == SharedMemory::AccessMode_ReadOnly);
  320. // Try up to 3 times to reduce low-probability failures:
  321. static const int ATTEMPTS_MAX = 3;
  322. for (int attempts = 0; attempts < ATTEMPTS_MAX; ++attempts)
  323. {
  324. // If opening should be attempted first,
  325. if (params.openMode != SharedMemory::OpenMode_CreateOnly)
  326. {
  327. // Attempt to open a shared memory map
  328. retval = AttemptOpenSharedMemory(fileName, params.minSizeBytes, openReadOnly);
  329. // If successful,
  330. if (retval)
  331. {
  332. // Done!
  333. break;
  334. }
  335. }
  336. // If creating the shared memory is also acceptable,
  337. if (params.openMode != SharedMemory::OpenMode_OpenOnly)
  338. {
  339. // Interpret create mode
  340. const bool allowRemoteWrite = (params.remoteMode == SharedMemory::RemoteMode_ReadWrite);
  341. // Attempt to create a shared memory map
  342. retval = AttemptCreateSharedMemory(fileName, params.minSizeBytes, openReadOnly, allowRemoteWrite);
  343. // If successful,
  344. if (retval)
  345. {
  346. // Done!
  347. break;
  348. }
  349. }
  350. } // Re-attempt create/open
  351. // Note: On Windows the initial contents of the region are guaranteed to be zero.
  352. return retval;
  353. }
  354. #endif // OVR_OS_WIN32
  355. #if (defined(OVR_OS_LINUX) || defined(OVR_OS_MAC))
  356. // Hidden implementation class for OS-specific behavior
  357. class SharedMemoryInternal : public SharedMemoryInternalBase
  358. {
  359. public:
  360. int FileMapping;
  361. void* FileView;
  362. int FileSize;
  363. SharedMemoryInternal(int fileMapping, void* fileView, int fileSize) :
  364. FileMapping(fileMapping),
  365. FileView(fileView),
  366. FileSize(fileSize)
  367. {
  368. }
  369. virtual ~SharedMemoryInternal()
  370. {
  371. // If file view is set,
  372. if (FileView)
  373. {
  374. munmap(FileView, FileSize);
  375. FileView = MAP_FAILED;
  376. }
  377. // If file mapping is set,
  378. if (FileMapping >= 0)
  379. {
  380. close(FileMapping);
  381. FileMapping = -1;
  382. }
  383. }
  384. virtual void* GetFileView() OVR_OVERRIDE
  385. {
  386. return FileView;
  387. }
  388. };
  389. static SharedMemoryInternal* DoFileMap(int hFileMapping, const char* fileName, bool openReadOnly, int minSize)
  390. {
  391. // Calculate the required flags based on read/write mode
  392. int prot = openReadOnly ? PROT_READ : (PROT_READ|PROT_WRITE);
  393. // Map the file view
  394. void* pFileView = mmap(NULL, minSize, prot, MAP_SHARED, hFileMapping, 0);
  395. if (pFileView == MAP_FAILED)
  396. {
  397. close(hFileMapping);
  398. OVR_DEBUG_LOG(("[SharedMemory] FAILURE: Unable to map view of file for %s error code = %d", fileName, errno));
  399. OVR_UNUSED(fileName);
  400. return NULL;
  401. }
  402. // Create internal representation
  403. SharedMemoryInternal* pimple = new SharedMemoryInternal(hFileMapping, pFileView, minSize);
  404. // If memory allocation fails,
  405. if (!pimple)
  406. {
  407. munmap(pFileView, minSize);
  408. close(hFileMapping);
  409. OVR_DEBUG_LOG(("[SharedMemory] FAILURE: Out of memory"));
  410. return NULL;
  411. }
  412. return pimple;
  413. }
  414. static SharedMemoryInternal* AttemptOpenSharedMemory(const char* fileName, int minSize, bool openReadOnly)
  415. {
  416. // Calculate permissions and flags based on read/write mode
  417. int flags = openReadOnly ? O_RDONLY : O_RDWR;
  418. int perms = openReadOnly ? S_IRUSR : (S_IRUSR | S_IWUSR);
  419. // Attempt to open the shared memory file
  420. int hFileMapping = shm_open(fileName, flags, perms);
  421. // If file was not opened successfully,
  422. if (hFileMapping < 0)
  423. {
  424. OVR_DEBUG_LOG(("[SharedMemory] WARNING: Unable to open file mapping for %s error code = %d (not necessarily bad)", fileName, errno));
  425. return NULL;
  426. }
  427. // Map the file
  428. return DoFileMap(hFileMapping, fileName, openReadOnly, minSize);
  429. }
  430. static SharedMemoryInternal* AttemptCreateSharedMemory(const char* fileName, int minSize, bool openReadOnly, bool allowRemoteWrite)
  431. {
  432. // Create mode
  433. // Note: Cannot create the shared memory file read-only because then ftruncate() will fail.
  434. int flags = O_CREAT | O_RDWR;
  435. #ifndef OVR_ALLOW_CREATE_FILE_MAPPING_IF_EXISTS
  436. // Require exclusive access when creating (seems like a good idea without trying it yet..)
  437. if (shm_unlink(fileName) < 0)
  438. {
  439. OVR_DEBUG_LOG(("[SharedMemory] WARNING: Unable to unlink shared memory file %s error code = %d", fileName, errno));
  440. }
  441. flags |= O_EXCL;
  442. #endif
  443. // Set own read/write permissions
  444. int perms = openReadOnly ? S_IRUSR : (S_IRUSR|S_IWUSR);
  445. // Allow other users to read/write the shared memory file
  446. perms |= allowRemoteWrite ? (S_IWGRP|S_IWOTH|S_IRGRP|S_IROTH) : (S_IRGRP|S_IROTH);
  447. // Attempt to open the shared memory file
  448. int hFileMapping = shm_open(fileName, flags, perms);
  449. // If file was not opened successfully,
  450. if (hFileMapping < 0)
  451. {
  452. OVR_DEBUG_LOG(("[SharedMemory] FAILURE: Unable to create file mapping for %s error code = %d", fileName, errno));
  453. return NULL;
  454. }
  455. int truncRes = ftruncate(hFileMapping, minSize);
  456. // If file was not opened successfully,
  457. if (truncRes < 0)
  458. {
  459. close(hFileMapping);
  460. OVR_DEBUG_LOG(("[SharedMemory] FAILURE: Unable to truncate file for %s to %d error code = %d", fileName, minSize, errno));
  461. return NULL;
  462. }
  463. // Map the file
  464. return DoFileMap(hFileMapping, fileName, openReadOnly, minSize);
  465. }
  466. static SharedMemoryInternal* CreateSharedMemory(const SharedMemory::OpenParameters& params)
  467. {
  468. SharedMemoryInternal* retval = NULL;
  469. // Construct the file mapping name in a Linux-specific way
  470. OVR::String fileMappingName = "/";
  471. fileMappingName += params.globalName;
  472. const char *fileName = fileMappingName.ToCStr();
  473. // Is being opened read-only?
  474. const bool openReadOnly = (params.accessMode == SharedMemory::AccessMode_ReadOnly);
  475. // Try up to 3 times to reduce low-probability failures:
  476. static const int ATTEMPTS_MAX = 3;
  477. for (int attempts = 0; attempts < ATTEMPTS_MAX; ++attempts)
  478. {
  479. // If opening should be attempted first,
  480. if (params.openMode != SharedMemory::OpenMode_CreateOnly)
  481. {
  482. // Attempt to open a shared memory map
  483. retval = AttemptOpenSharedMemory(fileName, params.minSizeBytes, openReadOnly);
  484. // If successful,
  485. if (retval)
  486. {
  487. // Done!
  488. break;
  489. }
  490. }
  491. // If creating the shared memory is also acceptable,
  492. if (params.openMode != SharedMemory::OpenMode_OpenOnly)
  493. {
  494. // Interpret create mode
  495. const bool allowRemoteWrite = (params.remoteMode == SharedMemory::RemoteMode_ReadWrite);
  496. // Attempt to create a shared memory map
  497. retval = AttemptCreateSharedMemory(fileName, params.minSizeBytes, openReadOnly, allowRemoteWrite);
  498. // If successful,
  499. if (retval)
  500. {
  501. // Done!
  502. break;
  503. }
  504. }
  505. } // Re-attempt create/open
  506. // Note: On Windows the initial contents of the region are guaranteed to be zero.
  507. return retval;
  508. }
  509. #endif // OVR_OS_LINUX
  510. //-----------------------------------------------------------------------------
  511. // SharedMemory
  512. static bool FakingSharedMemory = false;
  513. void SharedMemory::SetFakeSharedMemory(bool enabled)
  514. {
  515. FakingSharedMemory = enabled;
  516. }
  517. bool SharedMemory::IsFakingSharedMemory()
  518. {
  519. return FakingSharedMemory;
  520. }
  521. SharedMemory::SharedMemory(int size, void* data, const String& name, SharedMemoryInternalBase* pInternal) :
  522. Size(size),
  523. Data(data),
  524. Name(name),
  525. Internal(pInternal)
  526. {
  527. }
  528. SharedMemory::~SharedMemory()
  529. {
  530. // Call close when it goes out of scope
  531. Close();
  532. delete Internal;
  533. }
  534. void SharedMemory::Close()
  535. {
  536. if (Internal)
  537. {
  538. delete Internal;
  539. Internal = NULL;
  540. }
  541. }
  542. //-----------------------------------------------------------------------------
  543. // SharedMemoryFactory
  544. Ptr<SharedMemory> SharedMemoryFactory::Open(const SharedMemory::OpenParameters& params)
  545. {
  546. Ptr<SharedMemory> retval;
  547. // If no name specified or no size requested,
  548. if (!params.globalName || (params.minSizeBytes <= 0))
  549. {
  550. OVR_DEBUG_LOG(("[SharedMemory] FAILURE: Invalid parameters to Create()"));
  551. return NULL;
  552. }
  553. #ifdef OVR_BUILD_DEBUG
  554. const char* OpType = "{Unknown}";
  555. switch (params.openMode)
  556. {
  557. case SharedMemory::OpenMode_CreateOnly: OpType = "Creating"; break;
  558. case SharedMemory::OpenMode_CreateOrOpen: OpType = "Creating/Opening"; break;
  559. case SharedMemory::OpenMode_OpenOnly: OpType = "Opening"; break;
  560. default: OVR_ASSERT(false); break;
  561. }
  562. OVR_DEBUG_LOG(("[SharedMemory] %s shared memory region: %s > %d bytes",
  563. OpType, params.globalName, params.minSizeBytes));
  564. #endif
  565. // Attempt to create a shared memory region from the parameters
  566. SharedMemoryInternalBase* pInternal;
  567. if (SharedMemory::IsFakingSharedMemory())
  568. {
  569. pInternal = CreateFakeSharedMemory(params);
  570. }
  571. else
  572. {
  573. pInternal = CreateSharedMemory(params);
  574. }
  575. if (pInternal)
  576. {
  577. // Create the wrapper object
  578. retval = *new SharedMemory(params.minSizeBytes, pInternal->GetFileView(), params.globalName, pInternal);
  579. }
  580. return retval;
  581. }
  582. SharedMemoryFactory::SharedMemoryFactory()
  583. {
  584. OVR_DEBUG_LOG(("[SharedMemory] Creating factory"));
  585. PushDestroyCallbacks();
  586. }
  587. SharedMemoryFactory::~SharedMemoryFactory()
  588. {
  589. OVR_DEBUG_LOG(("[SharedMemory] Destroying factory"));
  590. }
  591. void SharedMemoryFactory::OnSystemDestroy()
  592. {
  593. delete this;
  594. }
  595. } // namespace OVR