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.

649 lines
17 KiB

  1. //+--------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1992
  5. //
  6. // File: dfbasis.hxx
  7. //
  8. // Contents: DocFile basis block class
  9. //
  10. // Classes: CDFBasis
  11. //
  12. // History: 26-Feb-92 DrewB Created
  13. //
  14. //---------------------------------------------------------------
  15. #ifndef __DFBASIS_HXX__
  16. #define __DFBASIS_HXX__
  17. #include <dfmsp.hxx>
  18. #include <msf.hxx>
  19. #include <context.hxx>
  20. #include <freelist.hxx>
  21. interface ILockBytes;
  22. class CPubDocFile;
  23. class CDocFile;
  24. SAFE_DFBASED_PTR(CBasedPubDocFilePtr, CPubDocFile);
  25. //+--------------------------------------------------------------
  26. //
  27. // Class: CDFBasis (dfb)
  28. //
  29. // Purpose: Defines base variables for sharing between exposed
  30. // DocFiles
  31. //
  32. // History: 26-Feb-92 DrewB Created
  33. // 18-May-93 AlexT Added CMallocBased
  34. //
  35. //---------------------------------------------------------------
  36. typedef enum
  37. {
  38. CDFB_DIRECTDOCFILE = 0,
  39. CDFB_DIRECTSTREAM = 1,
  40. CDFB_WRAPPEDDOCFILE = 2,
  41. CDFB_TRANSACTEDSTREAM = 3
  42. } CDFB_CLASSTYPE;
  43. #define CDFB_CLASSCOUNT 4
  44. class CDFBasis : public CMallocBased
  45. {
  46. public:
  47. inline CDFBasis(IMalloc * const pMalloc, DFLAGS df,
  48. DWORD dwLockFlags, CGlobalContext *pgc);
  49. inline ~CDFBasis(void);
  50. void vRelease(void);
  51. inline void vAddRef(void);
  52. inline void SetAccess(CPerContext *ppc);
  53. inline void ClearAccess(void);
  54. inline ULONG CountContexts(void);
  55. inline ILockBytes **GetPBase(void);
  56. inline ILockBytes *GetBase(void);
  57. inline CFileStream **GetPDirty(void);
  58. inline CFileStream *GetDirty(void);
  59. inline ILockBytes *GetOriginal(void);
  60. inline void SetBase(ILockBytes *plkb);
  61. inline void SetDirty(CFileStream *pfst);
  62. inline void SetOriginal(ILockBytes *plkb);
  63. inline void SetContext(CPerContext *ppc);
  64. inline DWORD GetOrigLockFlags(void);
  65. inline ULONG GetNewTemp(void);
  66. inline DFSIGNATURE GetNewSig(void);
  67. inline CMStream *GetScratch(void);
  68. inline void SetScratch(CMStream *pms);
  69. inline CMStream *GetBaseMultiStream(void);
  70. inline void SetBaseMultiStream(CMStream *pms);
  71. inline CPubDocFile *GetCopyBase(void);
  72. inline void SetCopyBase(CPubDocFile *pdf);
  73. inline CFreeList *GetFreeList(CDFB_CLASSTYPE classtype);
  74. inline SCODE Reserve(UINT cItems, CDFB_CLASSTYPE classtype);
  75. inline void CDFBasis::Unreserve(UINT cItems, CDFB_CLASSTYPE classtype);
  76. inline DFLAGS GetOpenFlags(void);
  77. inline IMalloc *GetMalloc(void);
  78. #ifdef DIRECTWRITERLOCK
  79. HRESULT WaitForWriteAccess (DWORD dwTimeout, CGlobalContext *pgc);
  80. HRESULT ReleaseWriteAccess ();
  81. inline BOOL DirectWriterMode ();
  82. inline BOOL HaveWriteAccess ();
  83. #endif
  84. private:
  85. CBasedMStreamPtr _pms;
  86. CBasedMStreamPtr _pmsScratch;
  87. DFLAGS const _dfOpen;
  88. ILockBytes *_plkbBase;
  89. CFileStream *_pfstDirty;
  90. ILockBytes *_plkbOriginal;
  91. LONG _cReferences;
  92. ULONG _cTemps;
  93. DFSIGNATURE _cSigs;
  94. CBasedPubDocFilePtr _pdfCopyBase;
  95. CBasedGlobalContextPtr _pgcGlobalContext;
  96. DWORD _dwOrigLocks;
  97. CFreeList _afrl[CDFB_CLASSCOUNT];
  98. static size_t _aReserveSize[CDFB_CLASSCOUNT];
  99. #ifdef DIRECTWRITERLOCK
  100. HRESULT TryReadLocks (CGlobalContext *pgc, ULONG ulMask);
  101. BOOL _fDirectWriterMode;
  102. BOOL _fWriteLocked;
  103. #endif
  104. IMalloc * const _pMalloc;
  105. };
  106. SAFE_DFBASED_PTR(CBasedDFBasisPtr, CDFBasis);
  107. //+--------------------------------------------------------------
  108. //
  109. // Member: CDFBasis::CDFBasis, public
  110. //
  111. // Synopsis: Constructor
  112. //
  113. // History: 02-Mar-92 DrewB Created
  114. //
  115. //---------------------------------------------------------------
  116. inline CDFBasis::CDFBasis(IMalloc * const pMalloc, DFLAGS df,
  117. DWORD dwLockFlags, CGlobalContext *pgc)
  118. : _pMalloc(pMalloc), _dfOpen(df)
  119. {
  120. _cReferences = 1;
  121. _cTemps = 0;
  122. _cSigs = 0;
  123. _pdfCopyBase = NULL;
  124. _plkbBase = NULL;
  125. _pfstDirty = NULL;
  126. _plkbOriginal = NULL;
  127. _dwOrigLocks = dwLockFlags;
  128. _pms = NULL;
  129. _pmsScratch = NULL;
  130. #ifdef DIRECTWRITERLOCK
  131. _fDirectWriterMode = P_READWRITE(df) && !P_TRANSACTED(df) &&
  132. !P_DENYREAD(df) && P_DENYWRITE(df);
  133. _fWriteLocked = FALSE;
  134. #endif
  135. _pgcGlobalContext = pgc;
  136. _pgcGlobalContext->AddRef();
  137. }
  138. //+--------------------------------------------------------------
  139. //
  140. // Member: CDFBasis::~CDFBasis, public
  141. //
  142. // Synopsis: Destructor
  143. //
  144. // History: 02-Mar-92 DrewB Created
  145. //
  146. //---------------------------------------------------------------
  147. inline CDFBasis::~CDFBasis(void)
  148. {
  149. delete BP_TO_P(CMStream *, _pmsScratch);
  150. _pgcGlobalContext->Release();
  151. msfAssert(_cReferences == 0);
  152. }
  153. //+--------------------------------------------------------------
  154. //
  155. // Member: CDFBasis::AddRef, public
  156. //
  157. // Synopsis: Increase reference count
  158. //
  159. // History: 02-Mar-92 DrewB Created
  160. //
  161. //---------------------------------------------------------------
  162. inline void CDFBasis::vAddRef(void)
  163. {
  164. InterlockedIncrement(&_cReferences);
  165. }
  166. //+--------------------------------------------------------------
  167. //
  168. // Member: CDFBasis::SetAccess, public
  169. //
  170. // Synopsis: Takes semaphore and sets pointers
  171. //
  172. // Arguments: [ppc] - Context
  173. //
  174. // History: 02-Mar-92 DrewB Created
  175. //
  176. //---------------------------------------------------------------
  177. inline void CDFBasis::SetAccess(CPerContext *ppc)
  178. {
  179. SetContext(ppc);
  180. }
  181. //+--------------------------------------------------------------
  182. //
  183. // Member: CDFBasis::ClearAccess, public
  184. //
  185. // Synopsis: Releases semaphore and resets pointers
  186. //
  187. // History: 02-Mar-92 DrewB Created
  188. //
  189. //---------------------------------------------------------------
  190. inline void CDFBasis::ClearAccess(void)
  191. {
  192. #if DBG == 1
  193. _plkbBase = NULL;
  194. _pfstDirty = NULL;
  195. #endif
  196. }
  197. //+--------------------------------------------------------------
  198. //
  199. // Member: CDFBasis::GetPBase, public
  200. //
  201. // Synopsis: Returns a pointer to _plstBase
  202. //
  203. // History: 02-Mar-92 DrewB Created
  204. //
  205. //---------------------------------------------------------------
  206. inline ILockBytes **CDFBasis::GetPBase(void)
  207. {
  208. return &_plkbBase;
  209. }
  210. //+--------------------------------------------------------------
  211. //
  212. // Member: CDFBasis::GetPDirty, public
  213. //
  214. // Synopsis: Returns a pointer to _plstDirty
  215. //
  216. // History: 02-Mar-92 DrewB Created
  217. //
  218. //---------------------------------------------------------------
  219. inline CFileStream **CDFBasis::GetPDirty(void)
  220. {
  221. return &_pfstDirty;
  222. }
  223. //+--------------------------------------------------------------
  224. //
  225. // Member: CDFBasis::GetBase, public
  226. //
  227. // Synopsis: Returns _plstBase
  228. //
  229. // History: 02-Mar-92 DrewB Created
  230. //
  231. //---------------------------------------------------------------
  232. inline ILockBytes *CDFBasis::GetBase(void)
  233. {
  234. return _plkbBase;
  235. }
  236. //+--------------------------------------------------------------
  237. //
  238. // Member: CDFBasis::SetBase, public
  239. //
  240. // Synopsis: Sets the base
  241. //
  242. // History: 02-Mar-92 DrewB Created
  243. //
  244. //---------------------------------------------------------------
  245. inline void CDFBasis::SetBase(ILockBytes *plkb)
  246. {
  247. _plkbBase = plkb;
  248. }
  249. //+--------------------------------------------------------------
  250. //
  251. // Member: CDFBasis::GetDirty, public
  252. //
  253. // Synopsis: Returns _plstDirty
  254. //
  255. // History: 02-Mar-92 DrewB Created
  256. //
  257. //---------------------------------------------------------------
  258. inline CFileStream *CDFBasis::GetDirty(void)
  259. {
  260. return _pfstDirty;
  261. }
  262. //+--------------------------------------------------------------
  263. //
  264. // Member: CDFBasis::SetDirty, public
  265. //
  266. // Synopsis: Sets the Dirty
  267. //
  268. // History: 02-Mar-92 DrewB Created
  269. //
  270. //---------------------------------------------------------------
  271. inline void CDFBasis::SetDirty(CFileStream *pfst)
  272. {
  273. _pfstDirty = pfst;
  274. }
  275. //+--------------------------------------------------------------
  276. //
  277. // Member: CDFBasis::GetOriginal, public
  278. //
  279. // Synopsis: Returns _plstOriginal
  280. //
  281. // History: 02-Mar-92 DrewB Created
  282. //
  283. //---------------------------------------------------------------
  284. inline ILockBytes *CDFBasis::GetOriginal(void)
  285. {
  286. return _plkbOriginal;
  287. }
  288. //+--------------------------------------------------------------
  289. //
  290. // Member: CDFBasis::SetOriginal, public
  291. //
  292. // Synopsis: Sets the Original
  293. //
  294. // History: 02-Mar-92 DrewB Created
  295. //
  296. //---------------------------------------------------------------
  297. inline void CDFBasis::SetOriginal(ILockBytes *plkb)
  298. {
  299. _plkbOriginal = plkb;
  300. }
  301. //+--------------------------------------------------------------
  302. //
  303. // Member: CDFBasis::SetContext, public
  304. //
  305. // Synopsis: Sets the context
  306. //
  307. // History: 02-Mar-92 DrewB Created
  308. //
  309. //---------------------------------------------------------------
  310. inline void CDFBasis::SetContext(CPerContext *ppc)
  311. {
  312. _plkbBase = ppc->GetBase();
  313. _pfstDirty = ppc->GetDirty();
  314. _plkbOriginal = ppc->GetOriginal();
  315. }
  316. //+--------------------------------------------------------------
  317. //
  318. // Member: CDFBasis::GetContextCount, public
  319. //
  320. // Synopsis: Returns the number of contexts. This can be compared against
  321. // The number of locks on the file to determine if there are any
  322. // seperate opens.
  323. //
  324. // History: 17-Mar-97 BChapman Created
  325. //
  326. //---------------------------------------------------------------
  327. inline ULONG CDFBasis::CountContexts(void)
  328. {
  329. return _pgcGlobalContext->CountContexts();
  330. }
  331. //+--------------------------------------------------------------
  332. //
  333. // Member: CDFBasis::GetNewTemp, public
  334. //
  335. // Synopsis: Return _cTemps++
  336. //
  337. // History: 03-Mar-92 DrewB Created
  338. //
  339. //---------------------------------------------------------------
  340. inline ULONG CDFBasis::GetNewTemp(void)
  341. {
  342. return _cTemps++;
  343. }
  344. //+--------------------------------------------------------------
  345. //
  346. // Member: CDFBasis::GetNewSig, public
  347. //
  348. // Synopsis: Return _cSigs++
  349. //
  350. // History: 03-Mar-92 DrewB Created
  351. //
  352. //---------------------------------------------------------------
  353. inline DFSIGNATURE CDFBasis::GetNewSig(void)
  354. {
  355. return _cSigs++;
  356. }
  357. //+--------------------------------------------------------------
  358. //
  359. // Member: CDFBasis::GetCopyBase, public
  360. //
  361. // Synopsis: Returns _pdfCopyBase
  362. //
  363. // History: 02-Mar-92 DrewB Created
  364. //
  365. //---------------------------------------------------------------
  366. inline CPubDocFile *CDFBasis::GetCopyBase(void)
  367. {
  368. return BP_TO_P(CPubDocFile *, _pdfCopyBase);
  369. }
  370. //+--------------------------------------------------------------
  371. //
  372. // Member: CDFBasis::SetCopyBase, public
  373. //
  374. // Synopsis: Sets _pdfCopyBase
  375. //
  376. // History: 02-Mar-92 DrewB Created
  377. //
  378. //---------------------------------------------------------------
  379. inline void CDFBasis::SetCopyBase(CPubDocFile *pdf)
  380. {
  381. _pdfCopyBase = P_TO_BP(CBasedPubDocFilePtr, pdf);
  382. }
  383. //+--------------------------------------------------------------
  384. //
  385. // Member: CDFBasis::GetScratch, public
  386. //
  387. // Synopsis: Returns handle to scratch multistream
  388. //
  389. // History: 02-Mar-92 DrewB Created
  390. //
  391. //---------------------------------------------------------------
  392. inline CMStream * CDFBasis::GetScratch(void)
  393. {
  394. return BP_TO_P(CMStream *, _pmsScratch);
  395. }
  396. //+--------------------------------------------------------------
  397. //
  398. // Member: CDFBasis::SetScratch, public
  399. //
  400. // Synopsis: Sets scratch MS
  401. //
  402. // History: 02-Mar-92 DrewB Created
  403. //
  404. //---------------------------------------------------------------
  405. inline void CDFBasis::SetScratch(CMStream *pms)
  406. {
  407. _pmsScratch = P_TO_BP(CBasedMStreamPtr, pms);
  408. }
  409. //+--------------------------------------------------------------
  410. //
  411. // Member: CDFBasis::GetBaseMultiStream, public
  412. //
  413. // Synopsis: Returns pointer to base multistream
  414. //
  415. // History: 25-Feb-95 PhilipLa Created
  416. //
  417. //---------------------------------------------------------------
  418. inline CMStream * CDFBasis::GetBaseMultiStream(void)
  419. {
  420. return BP_TO_P(CMStream *, _pms);
  421. }
  422. //+--------------------------------------------------------------
  423. //
  424. // Member: CDFBasis::SetBaseMultiStream, public
  425. //
  426. // Synopsis: Sets base MS
  427. //
  428. // History: 25-Feb-95 PhilipLa Created
  429. //
  430. //---------------------------------------------------------------
  431. inline void CDFBasis::SetBaseMultiStream(CMStream *pms)
  432. {
  433. _pms = P_TO_BP(CBasedMStreamPtr, pms);
  434. }
  435. //+--------------------------------------------------------------
  436. //
  437. // Member: CDFBasis::GetOrigLockFlags, public
  438. //
  439. // Synopsis: Returns the original's lock flags
  440. //
  441. // History: 11-Sep-92 DrewB Created
  442. //
  443. //---------------------------------------------------------------
  444. inline DWORD CDFBasis::GetOrigLockFlags(void)
  445. {
  446. return _dwOrigLocks;
  447. }
  448. //+-------------------------------------------------------------------------
  449. //
  450. // Member: CDFBasis::GetOpenFlags
  451. //
  452. // Synopsis: Returns access/share flags
  453. //
  454. // History: 21-Dec-92 AlexT Created
  455. //
  456. //--------------------------------------------------------------------------
  457. inline DFLAGS CDFBasis::GetOpenFlags(void)
  458. {
  459. return(_dfOpen);
  460. }
  461. //+---------------------------------------------------------------------------
  462. //
  463. // Member: CDFBasis::GetFreeList, public
  464. //
  465. // Synopsis: Gets the free list for the indicated reserved type
  466. //
  467. // History: 05-Nov-92 DrewB Created
  468. // 18-May-93 AlexT Switch to array
  469. //
  470. //----------------------------------------------------------------------------
  471. inline CFreeList *CDFBasis::GetFreeList(CDFB_CLASSTYPE classtype)
  472. {
  473. msfAssert(classtype >= 0 && classtype <= CDFB_CLASSCOUNT);
  474. return &_afrl[classtype];
  475. }
  476. //+---------------------------------------------------------------------------
  477. //
  478. // Member: CDFBasis::Reserve, public
  479. //
  480. // Synopsis: Reserve memory for instances
  481. //
  482. // Arguments: [cItems] -- count of items to reserve
  483. // [classtype] -- class of items to reserve
  484. //
  485. // History: 21-May-93 AlexT Created
  486. //
  487. //----------------------------------------------------------------------------
  488. inline SCODE CDFBasis::Reserve(UINT cItems, CDFB_CLASSTYPE classtype)
  489. {
  490. msfAssert(classtype >= 0 && classtype <= CDFB_CLASSCOUNT);
  491. return _afrl[classtype].Reserve(_pMalloc,
  492. cItems,
  493. _aReserveSize[classtype]);
  494. }
  495. //+---------------------------------------------------------------------------
  496. //
  497. // Member: CDFBasis::Unreserve, public
  498. //
  499. // Synopsis: Unreserve memory for instances
  500. //
  501. // Arguments: [cItems] -- count of items to unreserve
  502. // [classtype] -- class of items to unreserve
  503. //
  504. // History: 21-May-93 AlexT Created
  505. //
  506. //----------------------------------------------------------------------------
  507. inline void CDFBasis::Unreserve(UINT cItems, CDFB_CLASSTYPE classtype)
  508. {
  509. msfAssert(classtype >= 0 && classtype <= CDFB_CLASSCOUNT);
  510. _afrl[classtype].Unreserve(cItems);
  511. }
  512. //+---------------------------------------------------------------------------
  513. //
  514. // Member: CDFBasis::GetMalloc, public
  515. //
  516. // Synopsis: Return allocator associated with basis
  517. //
  518. // History: 21-May-93 AlexT Created
  519. //
  520. //----------------------------------------------------------------------------
  521. inline IMalloc *CDFBasis::GetMalloc(void)
  522. {
  523. #ifdef MULTIHEAP
  524. //We can't return the _pMalloc pointer here, since it's only valid
  525. // in the creating process. What we really want to return in the
  526. // pointer to the current allocator, which is always &g_smAllocator.
  527. //return (IMalloc *)_pMalloc;
  528. return (IMalloc *)&g_smAllocator;
  529. #else
  530. return (IMalloc *)_pMalloc;
  531. #endif
  532. }
  533. #ifdef DIRECTWRITERLOCK
  534. //+---------------------------------------------------------------------------
  535. //
  536. // Member: CDFBasis::DirectWriterMode, public
  537. //
  538. // Synopsis: indicates if this is readwrite deny write
  539. //
  540. // History: 11-Aug-1996 HenryLee created
  541. //
  542. //----------------------------------------------------------------------------
  543. inline BOOL CDFBasis::DirectWriterMode ()
  544. {
  545. return _fDirectWriterMode;
  546. }
  547. //+---------------------------------------------------------------------------
  548. //
  549. // Member: CDFBasis::HaveWriteLock, public
  550. //
  551. // Synopsis: indicates if this if someone is holding the exclusive writelock
  552. //
  553. // History: 11-Aug-1996 HenryLee created
  554. //
  555. //----------------------------------------------------------------------------
  556. inline BOOL CDFBasis::HaveWriteAccess ()
  557. {
  558. return _fWriteLocked;
  559. }
  560. #endif
  561. #endif