Leaked source code of windows server 2003
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.

276 lines
5.4 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Abstract:
  4. Implementation of helper classes.
  5. Author:
  6. Souren Aghajanyan (sourenag) 24-Sep-2001
  7. Revision History:
  8. <alias> <date> <comments>
  9. --*/
  10. #include "pch.h"
  11. #include "log.h"
  12. #include "log_man.h"
  13. #include "functions.h"
  14. BOOL
  15. CMutualExclusionObject::Open(
  16. IN PCWSTR pNameOfObject,
  17. IN BOOL bInitialOwnership,
  18. IN UINT SpinCount
  19. )
  20. {
  21. m_hHandle = g_OpenMutex(pNameOfObject);
  22. if(m_hHandle){
  23. if(bInitialOwnership){
  24. Acquiry();
  25. }
  26. return TRUE;
  27. }
  28. m_hHandle = g_CreateMutex(pNameOfObject, bInitialOwnership);
  29. if(!m_hHandle){
  30. return FALSE;
  31. }
  32. m_SpinCount = (g_GetProcessorsNumber() > 1)? SpinCount: 0;
  33. return TRUE;
  34. }
  35. VOID
  36. CMutualExclusionObject::Close(
  37. VOID
  38. )
  39. {
  40. if(!m_hHandle){
  41. return;
  42. }
  43. g_CloseHandle(m_hHandle);
  44. m_hHandle = NULL;
  45. m_SpinCount = 0;
  46. }
  47. VOID
  48. CMutualExclusionObject::Acquiry(
  49. VOID
  50. )
  51. {
  52. if(!m_hHandle){
  53. ASSERT(m_hHandle);
  54. return;
  55. }
  56. for(UINT i = 0; i < m_SpinCount; i++){
  57. if(WAIT_TIMEOUT != g_WaitForSingleObject(m_hHandle, 0)){
  58. return;
  59. }
  60. }
  61. g_WaitForSingleObject(m_hHandle, INFINITE);
  62. }
  63. VOID
  64. CMutualExclusionObject::Release(
  65. VOID
  66. )
  67. {
  68. if(!m_hHandle){
  69. ASSERT(m_hHandle);
  70. return;
  71. }
  72. g_ReleaseMutex(m_hHandle);
  73. }
  74. BOOL
  75. CSharedMemory::Open(
  76. IN PCWSTR pNameOfObject,
  77. IN UINT uiInitialSizeOfMapView,
  78. IN BOOL * pAlreadyExist OPTIONAL
  79. )
  80. {
  81. m_hHandle = g_OpenSharedMemory(pNameOfObject);
  82. if(m_hHandle){
  83. if(pAlreadyExist){
  84. *pAlreadyExist = TRUE;
  85. }
  86. }
  87. else{
  88. m_hHandle = g_CreateSharedMemory(uiInitialSizeOfMapView, pNameOfObject);
  89. if(!m_hHandle){
  90. return FALSE;
  91. }
  92. if(pAlreadyExist){
  93. *pAlreadyExist = FALSE;
  94. }
  95. }
  96. m_ViewOfSection = g_MapSharedMemory(m_hHandle);
  97. if(!m_ViewOfSection){
  98. Close();
  99. return FALSE;
  100. }
  101. return TRUE;
  102. }
  103. VOID CSharedMemory::Close(
  104. VOID
  105. )
  106. {
  107. if(m_hHandle){
  108. if(m_ViewOfSection){
  109. g_UnMapSharedMemory(m_ViewOfSection);
  110. m_ViewOfSection = NULL;
  111. }
  112. g_CloseHandle(m_hHandle);
  113. m_hHandle = NULL;
  114. }
  115. }
  116. PVOID
  117. CBuffer::Allocate(
  118. IN UINT uiSize
  119. )
  120. {
  121. #define DEFAULT_SIZE (1<<12)
  122. if(!m_pvBuffer){
  123. UINT uiTempSize = uiSize? uiSize: DEFAULT_SIZE;
  124. m_pvBuffer = MALLOC(uiSize);
  125. if(!m_pvBuffer){
  126. return FALSE;
  127. }
  128. m_uiSize = uiSize;
  129. m_uiUsedSize = uiTempSize;
  130. }
  131. else
  132. {
  133. if(m_uiSize < uiSize){
  134. FREE(m_pvBuffer);
  135. m_pvBuffer = MALLOC(uiSize);
  136. if(!m_pvBuffer){
  137. m_uiUsedSize = m_uiSize = 0;
  138. return FALSE;
  139. }
  140. m_uiUsedSize = m_uiSize = uiSize;
  141. }
  142. else{
  143. m_uiUsedSize = uiSize;
  144. }
  145. }
  146. return m_pvBuffer;
  147. }
  148. BOOL
  149. CBuffer::PreAllocate(
  150. IN UINT uiSize
  151. )
  152. {
  153. if(Allocate(uiSize)){
  154. Allocate(0);
  155. return TRUE;
  156. }
  157. return FALSE;
  158. }
  159. PVOID
  160. CBuffer::ReAllocate(
  161. IN UINT uiSize
  162. )
  163. {
  164. ASSERT(uiSize);
  165. if(!m_pvBuffer){
  166. return Allocate(uiSize);
  167. }
  168. if(m_uiSize < uiSize){
  169. PVOID pvBuffer = REALLOC(m_pvBuffer, uiSize);
  170. if(!pvBuffer){
  171. return NULL;
  172. }
  173. m_pvBuffer = pvBuffer;
  174. m_uiUsedSize = m_uiSize = uiSize;
  175. }
  176. else{
  177. m_uiUsedSize = uiSize;
  178. }
  179. return m_pvBuffer;
  180. }
  181. BOOL
  182. CSharedAccessFile::Open(
  183. IN PCWSTR pFilePath,
  184. IN BOOL SharedWriteAccess,
  185. IN BOOL CreateAlwaysNewIfPossible,
  186. IN BOOL bWriteThrough,
  187. IN BOOL * pbAlreadyOpened
  188. )
  189. {
  190. BOOL bAlreadyOpened = FALSE;
  191. m_hHandle = g_CreateSharedFile(pFilePath, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
  192. if(INVALID_HANDLE_VALUE != m_hHandle){
  193. g_CloseHandle(m_hHandle);
  194. bAlreadyOpened = FALSE;
  195. }
  196. else{
  197. bAlreadyOpened = (ERROR_SHARING_VIOLATION == GetLastError());
  198. }
  199. if(pbAlreadyOpened){
  200. *pbAlreadyOpened = bAlreadyOpened;
  201. }
  202. m_hHandle = g_CreateSharedFile(pFilePath,
  203. SharedWriteAccess? FILE_SHARE_WRITE | FILE_SHARE_READ: 0,
  204. (CreateAlwaysNewIfPossible && !bAlreadyOpened)? CREATE_ALWAYS: OPEN_ALWAYS,
  205. FILE_ATTRIBUTE_NORMAL | (bWriteThrough? FILE_FLAG_WRITE_THROUGH: 0));
  206. if(INVALID_HANDLE_VALUE == m_hHandle){
  207. return FALSE;
  208. }
  209. return TRUE;
  210. }
  211. VOID
  212. CSharedAccessFile::Close()
  213. {
  214. if(INVALID_HANDLE_VALUE != m_hHandle){
  215. g_CloseHandle(m_hHandle);
  216. m_hHandle = INVALID_HANDLE_VALUE;
  217. }
  218. }
  219. BOOL
  220. CSharedAccessFile::Append(
  221. IN PVOID pBuffer,
  222. IN UINT Size
  223. )
  224. {
  225. DWORD dwNumberOfBytesWritten;
  226. ASSERT(pBuffer && Size);
  227. g_SetFilePointer(m_hHandle, 0, FILE_END);
  228. BOOL bResult = g_WriteFile(m_hHandle, pBuffer, Size, &dwNumberOfBytesWritten);
  229. return bResult;
  230. }