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.

140 lines
3.3 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1994.
  5. //
  6. // File: shardbuf.hxx
  7. //
  8. // Contents: A shared buffer used by the bigtable code to create temporary
  9. // variants. Rather than allocate memory each time, this will
  10. // facilitate usage of a single buffer.
  11. //
  12. // Classes: CSharedBuffer and XUseSharedBuffer
  13. //
  14. // Functions:
  15. //
  16. // History: 5-22-95 srikants Created
  17. //
  18. //----------------------------------------------------------------------------
  19. #pragma once
  20. #include <tableseg.hxx>
  21. //+---------------------------------------------------------------------------
  22. //
  23. // Class: CSharedBuffer
  24. //
  25. // Purpose: A Shared buffer used by the table to do temporary alloocations
  26. // for variant creation.
  27. //
  28. // History: 5-22-95 srikants Created
  29. //
  30. // Notes:
  31. //
  32. //----------------------------------------------------------------------------
  33. class CSharedBuffer
  34. {
  35. friend class XUseSharedBuffer;
  36. public:
  37. CSharedBuffer() : _fInUse(FALSE) {}
  38. BOOL IsInUse() const { return _fInUse; }
  39. private:
  40. BOOL _fInUse;
  41. // this is an array of LONGLONGs to force 8-byte alignment
  42. LONGLONG _abBuffer[ ( TBL_MAX_DATA + sizeof PROPVARIANT ) /
  43. sizeof LONGLONG ];
  44. BYTE * _GetBuffer()
  45. {
  46. Win4Assert( 0 == ( sizeof _abBuffer & ( sizeof(LONGLONG) - 1 ) ) );
  47. return (BYTE *) &_abBuffer;
  48. }
  49. void _SetInUse()
  50. {
  51. Win4Assert( !_fInUse );
  52. _fInUse = TRUE;
  53. }
  54. void _Release()
  55. {
  56. Win4Assert( _fInUse );
  57. _fInUse = FALSE;
  58. }
  59. };
  60. //+---------------------------------------------------------------------------
  61. //
  62. // Class: XUseSharedBuffer
  63. //
  64. // Purpose: Used to access the shared buffer and make sure that there
  65. // are no two simultaneous users of the shared buffer.
  66. //
  67. // History: 5-22-95 srikants Created
  68. //
  69. // Notes: It is assumed that the shared buffer is being accessed under
  70. // a larger lock like the bigtable lock.
  71. //
  72. //----------------------------------------------------------------------------
  73. class XUseSharedBuffer
  74. {
  75. public:
  76. XUseSharedBuffer( CSharedBuffer & sharedBuf,
  77. BOOL fAcquire = TRUE ) : _sharedBuf(sharedBuf),
  78. _fAcquired(FALSE)
  79. {
  80. if ( fAcquire )
  81. {
  82. Win4Assert( !_sharedBuf.IsInUse() );
  83. if ( _sharedBuf.IsInUse() )
  84. {
  85. THROW( CException( STATUS_CONFLICTING_ADDRESSES ) );
  86. }
  87. _sharedBuf._SetInUse();
  88. _fAcquired = TRUE;
  89. }
  90. }
  91. ~XUseSharedBuffer()
  92. {
  93. if ( _fAcquired )
  94. {
  95. _sharedBuf._Release();
  96. }
  97. }
  98. void LokAcquire()
  99. {
  100. Win4Assert( !_fAcquired );
  101. _sharedBuf._SetInUse();
  102. _fAcquired = TRUE;
  103. }
  104. BYTE * LokGetBuffer()
  105. {
  106. Win4Assert( _fAcquired );
  107. return _sharedBuf._GetBuffer();
  108. }
  109. unsigned LokGetSize() const { return sizeof(_sharedBuf._abBuffer); }
  110. BOOL IsAcquired() const { return _fAcquired; }
  111. private:
  112. CSharedBuffer & _sharedBuf;
  113. BOOL _fAcquired;
  114. };