Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
2.8 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. All rights reserved.
  4. Module Name:
  5. sharemem.hxx
  6. Abstract:
  7. Simple shared memory object.
  8. Author:
  9. Albert Ting (AlbertT) 17-Dec-1996
  10. Revision History:
  11. --*/
  12. #ifndef SHAREMEM_HXX
  13. #define SHAREMEM_HXX
  14. /********************************************************************
  15. TShareMemLock
  16. Lock a TShareMem for access. If this call succeeds, pGet() will
  17. return the pointer to the object.
  18. TShareMem ShareMem( sizeof( TMyObject ),
  19. TEXT( "MyObject" ),
  20. TShareMem::kCreate | TShareMem::kReadWrite,
  21. NULL,
  22. NULL );
  23. if( ShareMem.bValid( )){
  24. TMyObject *pMyObject;
  25. TShareMemLock<TMyObject> SML( ShareMem, &pMyObject );
  26. //
  27. // Use pMyObject--note it can't have any virtual functions!
  28. //
  29. }
  30. //
  31. // Closing scope of TShareMemLock removes lock.
  32. //
  33. ********************************************************************/
  34. class TShareMem;
  35. template<class T>
  36. class TShareMemLock {
  37. public:
  38. TShareMemLock(
  39. TShareMem &ShareMem,
  40. T **ppMem
  41. );
  42. ~TShareMemLock(
  43. VOID
  44. );
  45. private:
  46. TShareMem &m_ShareMem;
  47. };
  48. template<class T>
  49. inline
  50. TShareMemLock<T>::
  51. TShareMemLock(
  52. IN TShareMem &ShareMem,
  53. OUT T **ppMem
  54. ) : m_ShareMem( ShareMem )
  55. {
  56. m_ShareMem.vEnter();
  57. *ppMem = static_cast<T*>( ShareMem.pGetData() );
  58. }
  59. template<class T>
  60. inline
  61. TShareMemLock<T>::
  62. ~TShareMemLock(
  63. VOID
  64. )
  65. {
  66. m_ShareMem.vLeave();
  67. }
  68. class TShareMem {
  69. public:
  70. enum {
  71. kNull = 0,
  72. kCreate = 1,
  73. kReadWrite = 2
  74. };
  75. TShareMem(
  76. UINT uSize,
  77. LPCTSTR pszName,
  78. UINT uFlags,
  79. PSECURITY_ATTRIBUTES psa,
  80. PUINT puSizeDisposition
  81. );
  82. ~TShareMem(
  83. VOID
  84. );
  85. BOOL
  86. bValid(
  87. VOID
  88. );
  89. VOID
  90. vEnter(
  91. VOID
  92. );
  93. VOID
  94. vLeave(
  95. VOID
  96. );
  97. PVOID
  98. pGetData(
  99. VOID
  100. );
  101. private:
  102. typedef struct HEADER {
  103. UINT uHeaderSize;
  104. UINT uSize;
  105. } *PHEADER;
  106. HEADER&
  107. GetHeader(
  108. VOID
  109. );
  110. HANDLE m_hFile;
  111. PVOID m_pvBase;
  112. PVOID m_pvUserData;
  113. HANDLE m_hMutex;
  114. };
  115. inline
  116. BOOL
  117. TShareMem::
  118. bValid(
  119. VOID
  120. )
  121. {
  122. return m_pvUserData != NULL;
  123. }
  124. inline
  125. TShareMem::HEADER&
  126. TShareMem::
  127. GetHeader(
  128. VOID
  129. )
  130. {
  131. return *static_cast<PHEADER>( m_pvBase );
  132. }
  133. inline
  134. VOID
  135. TShareMem::
  136. vEnter(
  137. VOID
  138. )
  139. {
  140. WaitForSingleObject( m_hMutex, INFINITE );
  141. }
  142. inline
  143. VOID
  144. TShareMem::
  145. vLeave(
  146. VOID
  147. )
  148. {
  149. ReleaseMutex( m_hMutex );
  150. }
  151. inline
  152. PVOID
  153. TShareMem::
  154. pGetData(
  155. VOID
  156. )
  157. {
  158. return m_pvUserData;
  159. }
  160. #endif // ifdef SHAREMEM_HXX