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.

298 lines
7.0 KiB

  1. //+--------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1992.
  5. //
  6. // File: docfilep.hxx
  7. //
  8. // Contents: Private DocFile definitions
  9. //
  10. // History: 15-Mar-92 DrewB Created
  11. //
  12. //---------------------------------------------------------------
  13. #ifndef __DOCFILEP_HXX__
  14. #define __DOCFILEP_HXX__
  15. #include <dfbasis.hxx>
  16. #include <context.hxx>
  17. // Set/clear basis access
  18. #define SetBasisAccess(pdfb, ppc) ((pdfb)->SetAccess(ppc))
  19. #define ClearBasisAccess(pdfb) ((pdfb)->ClearAccess())
  20. // Set access for the basis with optimization if semaphoring
  21. // is not important
  22. #define SetDifferentBasisAccess(pdfb, ppc) SetBasisAccess(pdfb, ppc)
  23. // Optimal set/clear access for targets
  24. // Read and Write cases could be combined but this would limit
  25. // future flexibility if they ever need to be treated differently
  26. // Assumes _pdfb and _ppc exist
  27. #define SetReadAccess() SetDifferentBasisAccess(_pdfb, _ppc)
  28. #define SetWriteAccess() SetDifferentBasisAccess(_pdfb, _ppc)
  29. #define ClearReadAccess() ClearBasisAccess(_pdfb)
  30. #define ClearWriteAccess() ClearBasisAccess(_pdfb)
  31. //+---------------------------------------------------------------------------
  32. //
  33. // Class: CSafeAccess (sa)
  34. //
  35. // Purpose: Safe class for Set/ClearAccess
  36. //
  37. // History: 19-Oct-93 DrewB Created
  38. //
  39. // Notes: Only one class since read/write access are currently the
  40. // same. Because of the macros, this can easily be changed
  41. // if necessary
  42. //
  43. //----------------------------------------------------------------------------
  44. #if WIN32 == 300
  45. class CSafeAccess INHERIT_UNWIND_IF_CAIRO
  46. {
  47. DECLARE_UNWIND
  48. #else
  49. class CSafeAccess
  50. {
  51. #endif
  52. public:
  53. CSafeAccess(CDFBasis *pdfb, CPerContext *ppc)
  54. {
  55. _pdfb = pdfb;
  56. _ppc = ppc;
  57. _fAccess = FALSE;
  58. #if WIN32 == 300
  59. END_CONSTRUCTION(CSafeAccess);
  60. #endif
  61. }
  62. #ifdef MULTIHEAP
  63. // When the constructor for CSafeAccess is invoked,
  64. // the base pointer may be invalid, so we set the
  65. // CDFBasis pointer when reading or writing
  66. void Read(CDFBasis *pdfb)
  67. {
  68. _pdfb = pdfb;
  69. SetDifferentBasisAccess(_pdfb, _ppc);
  70. _fAccess = TRUE;
  71. }
  72. void Write(CDFBasis *pdfb)
  73. {
  74. _pdfb = pdfb;
  75. SetDifferentBasisAccess(_pdfb, _ppc);
  76. _fAccess = TRUE;
  77. }
  78. #else
  79. void Read(void)
  80. {
  81. SetDifferentBasisAccess(_pdfb, _ppc);
  82. _fAccess = TRUE;
  83. }
  84. void Write(void)
  85. {
  86. SetDifferentBasisAccess(_pdfb, _ppc);
  87. _fAccess = TRUE;
  88. }
  89. #endif
  90. ~CSafeAccess(void)
  91. {
  92. if (_fAccess)
  93. ClearBasisAccess(_pdfb);
  94. }
  95. private:
  96. CDFBasis *_pdfb;
  97. CPerContext *_ppc;
  98. BOOL _fAccess;
  99. };
  100. #ifdef MULTIHEAP
  101. #define SAFE_ACCESS CSafeAccess _sa(NULL, _ppc)
  102. #define SafeReadAccess() _sa.Read(BP_TO_P(CDFBasis *, _pdfb))
  103. #define SafeWriteAccess() _sa.Write(BP_TO_P(CDFBasis *, _pdfb))
  104. #else
  105. #define SAFE_ACCESS CSafeAccess _sa(BP_TO_P(CDFBasis *, _pdfb), _ppc)
  106. #define SafeReadAccess() _sa.Read()
  107. #define SafeWriteAccess() _sa.Write()
  108. #endif
  109. #define TakeSem() _ppc->TakeSem(DFM_TIMEOUT)
  110. #define ReleaseSem(sc) if (SUCCEEDED(sc)) _ppc->ReleaseSem(); else 1
  111. //+---------------------------------------------------------------------------
  112. //
  113. // Class: CSafeSem (ss)
  114. //
  115. // Purpose: Safe class for holding the access semaphore
  116. //
  117. // History: 19-Oct-93 DrewB Created
  118. //
  119. //----------------------------------------------------------------------------
  120. #if WIN32 == 300
  121. class CSafeSem INHERIT_UNWIND_IF_CAIRO
  122. {
  123. DECLARE_UNWIND
  124. #else
  125. class CSafeSem
  126. {
  127. #endif
  128. public:
  129. CSafeSem(CPerContext *ppc)
  130. {
  131. _sc = STG_E_INUSE;
  132. _ppc = ppc;
  133. #ifdef MULTIHEAP
  134. _ppcPrev = NULL;
  135. _pSmAllocator = NULL;
  136. #endif
  137. #if WIN32 == 300
  138. END_CONSTRUCTION(CSafeSem);
  139. #endif
  140. }
  141. SCODE Take(void)
  142. {
  143. #ifdef MULTIHEAP
  144. _sc = TakeSem();
  145. _pSmAllocator = &g_smAllocator;
  146. _ppc->SetAllocatorState (&_ppcPrev, _pSmAllocator);
  147. return _sc;
  148. #else
  149. return _sc = TakeSem();
  150. #endif
  151. }
  152. void Release(void)
  153. {
  154. #ifdef MULTIHEAP
  155. if (_pSmAllocator != NULL)
  156. {
  157. if (_ppcPrev != NULL)
  158. _ppcPrev->SetAllocatorState(NULL, _pSmAllocator);
  159. else
  160. _pSmAllocator->SetState(NULL, NULL, 0, NULL, NULL);
  161. _pSmAllocator = NULL;
  162. }
  163. #endif
  164. ReleaseSem(_sc);
  165. _sc = STG_E_INUSE;
  166. }
  167. ~CSafeSem(void)
  168. {
  169. Release();
  170. }
  171. private:
  172. SCODE _sc;
  173. CPerContext *_ppc;
  174. #ifdef MULTIHEAP
  175. CPerContext *_ppcPrev;
  176. CSmAllocator *_pSmAllocator;
  177. #endif
  178. };
  179. #define SAFE_SEM CSafeSem _ss(_ppc)
  180. #define TakeSafeSem() _ss.Take()
  181. #define ReleaseSafeSem() _ss.Release()
  182. #define CWCMAXPATHCOMPLEN CWCSTREAMNAME
  183. #define CBMAXPATHCOMPLEN (CWCMAXPATHCOMPLEN*sizeof(WCHAR))
  184. // Common bundles of STGM flags
  185. #define STGM_RDWR (STGM_READ | STGM_WRITE | STGM_READWRITE)
  186. #define STGM_DENY (STGM_SHARE_DENY_NONE | STGM_SHARE_DENY_READ | \
  187. STGM_SHARE_DENY_WRITE | STGM_SHARE_EXCLUSIVE)
  188. #define FLUSH_CACHE(cf) \
  189. ((cf & STGC_DANGEROUSLYCOMMITMERELYTODISKCACHE) == 0)
  190. #define NOT_SINGLE(md) (((md) & STGM_DENY) != STGM_SHARE_EXCLUSIVE)
  191. #define EnforceSingle(mode) (NOT_SINGLE(mode) ? STG_E_INVALIDFUNCTION : S_OK)
  192. #ifndef _VerifyCommitFlags_DEFINED
  193. #define _VerifyCommitFlags_DEFINED
  194. inline SCODE VerifyCommitFlags(DWORD cf)
  195. {
  196. cf &= ~( STGC_OVERWRITE
  197. | STGC_ONLYIFCURRENT
  198. | STGC_DANGEROUSLYCOMMITMERELYTODISKCACHE
  199. | STGC_CONSOLIDATE );
  200. //
  201. // If there are any extra unknown flags left, then error.
  202. //
  203. if(0 == cf)
  204. return S_OK;
  205. else
  206. return STG_E_INVALIDFLAG;
  207. }
  208. #endif // _VerifyCommitFlags_DEFINED
  209. inline SCODE VerifyLockType(DWORD lt)
  210. {
  211. switch(lt)
  212. {
  213. case LOCK_WRITE:
  214. case LOCK_EXCLUSIVE:
  215. case LOCK_ONLYONCE:
  216. return S_OK;
  217. default:
  218. return STG_E_INVALIDFLAG;
  219. }
  220. }
  221. //+-------------------------------------------------------------------------
  222. //
  223. // Function: VerifyStatFlag
  224. //
  225. // Synopsis: verify Stat flag
  226. //
  227. // Arguments: [grfStatFlag] - stat flag
  228. //
  229. // Returns: S_OK or STG_E_INVALIDFLAG
  230. //
  231. // History: 10-Nov-92 AlexT Created
  232. //
  233. //--------------------------------------------------------------------------
  234. inline SCODE VerifyStatFlag(DWORD grfStatFlag)
  235. {
  236. SCODE sc = S_OK;
  237. if ((grfStatFlag & ~STATFLAG_NONAME) != 0)
  238. sc = STG_E_INVALIDFLAG;
  239. return(sc);
  240. }
  241. //+-------------------------------------------------------------------------
  242. //
  243. // Function: VerifyMoveFlags
  244. //
  245. // Synopsis: verify Move flag
  246. //
  247. // Arguments: [grfMoveFlag] - stat flag
  248. //
  249. // Returns: S_OK or STG_E_INVALIDFLAG
  250. //
  251. // History: 10-Nov-92 AlexT Created
  252. //
  253. //--------------------------------------------------------------------------
  254. inline SCODE VerifyMoveFlags(DWORD grfMoveFlag)
  255. {
  256. SCODE sc = S_OK;
  257. if ((grfMoveFlag & ~STGMOVE_COPY) != 0)
  258. sc = STG_E_INVALIDFLAG;
  259. return(sc);
  260. }
  261. #define OL_VALIDATE(x) if (FAILED(sc = CExpParameterValidate::x)) {olDebugOut((DEB_ERROR, "Error %lX at %s:%d\n", sc, __FILE__, __LINE__)); return sc;}
  262. #endif