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.

189 lines
5.0 KiB

  1. #pragma once
  2. #include "lhport.h"
  3. #include "pshpack8.h"
  4. class CPositionIndependentBlob;
  5. template <typename T>
  6. class CFunction
  7. {
  8. public:
  9. union
  10. {
  11. T * pfn;
  12. ULONGLONG ull;
  13. };
  14. };
  15. typedef void (__stdcall * PFN_GROW_FUNCTION)(CPositionIndependentBlob * This);
  16. class CPositionIndependentOperatorNew
  17. {
  18. public:
  19. void * operator new(size_t n, CPositionIndependentBlob * Container);
  20. };
  21. //
  22. // aka position independent heap, with support for persistance to disk
  23. //
  24. class CPositionIndependentBlob : public CPositionIndependentOperatorNew
  25. {
  26. public:
  27. //protected:
  28. void OutOfMemory();
  29. public:
  30. static void ThrowWin32Error(DWORD);
  31. BOOL IsPagefileMapping();
  32. union
  33. {
  34. ULONGLONG ll;
  35. CPositionIndependentBlob * m_Container;
  36. };
  37. CPositionIndependentBlob();
  38. ~CPositionIndependentBlob();
  39. BOOL Write(HANDLE FileHandle, ULONGLONG Offset);
  40. BOOL Read(HANDLE FileHandle, ULONGLONG Offset);
  41. void GetBounds(const BYTE * Bounds[2]);
  42. void IsInBlob(const BYTE * Pointer, ULONG Size, BOOL * IsInBlob);
  43. class CBlock
  44. {
  45. public:
  46. CBlock() : m_Offset(0), m_Size(0) { }
  47. ULONG m_Offset;
  48. ULONG m_Size;
  49. bool operator==(const CBlock & x) const
  50. {
  51. if (this == &x)
  52. return true;
  53. if (m_Offset != x.m_Offset)
  54. return false;
  55. if (m_Size != x.m_Size)
  56. return false;
  57. return true;
  58. }
  59. typedef std::pair<ULONG, ULONG> CPair;
  60. class CLessThanByOffset
  61. {
  62. public:
  63. bool operator()(const CBlock & x, const CBlock & y) const
  64. {
  65. return CPair(x.m_Offset, x.m_Size) < CPair(y.m_Offset, y.m_Size);
  66. }
  67. };
  68. class CLessThanBySize
  69. {
  70. public:
  71. bool operator()(const CBlock & x, const CBlock & y) const
  72. {
  73. return CPair(x.m_Size, x.m_Offset) < CPair(y.m_Size, y.m_Offset);
  74. }
  75. };
  76. static bool Neighbors(const CBlock & x, const CBlock & y)
  77. {
  78. if (y.m_Offset + y.m_Size == x.m_Offset)
  79. return true;
  80. if (x.m_Offset + x.m_Size == y.m_Offset)
  81. return true;
  82. return false;
  83. }
  84. bool Neighbors(const CBlock & x) const
  85. {
  86. return Neighbors(*this, x);
  87. }
  88. };
  89. union
  90. {
  91. struct
  92. {
  93. ULONG m_HeaderSize;
  94. ULONG m_HeaderVersion;
  95. ULONG m_BitsInPointer;
  96. BYTE m_IsBigEndian;
  97. BYTE m_Spare1[3];
  98. union
  99. {
  100. ULONGLONG ll;
  101. HANDLE m_FileHandle;
  102. };
  103. union
  104. {
  105. ULONGLONG ll;
  106. HANDLE m_FileMapping;
  107. };
  108. ULONGLONG m_FileOffset;
  109. union
  110. {
  111. ULONGLONG m_Base;
  112. PBYTE m_BasePointer;
  113. };
  114. ULONG m_AllocatedSize;
  115. ULONG m_UsedSize;
  116. ULONG m_FreeSize;
  117. ULONG m_OffsetToFreeBlocks;
  118. ULONG m_NumberOfFreeBlocks;
  119. CBlock m_MostRecentlyFreedBlock;
  120. };
  121. BYTE Page[0x2000];
  122. };
  123. typedef std::set<CBlock, CBlock::CLessThanByOffset> CBlockByOffsetSet;
  124. typedef std::set<CBlock, CBlock::CLessThanBySize> CBlockBySizeSet;
  125. ULONG m_PendingFreeBySize;
  126. CBlockByOffsetSet m_FreeBlocksSortedByOffset;
  127. CBlockBySizeSet m_FreeBlocksSortedBySize;
  128. CBlockByOffsetSet m_InUseBlocksSortedByOffset;
  129. PBYTE GetBasePointer();
  130. void Alloc(ULONG NumberOfBytes, ULONG * Offset);
  131. void Alloc(CPositionIndependentBlob * Container, ULONG NumberOfBytes, ULONG * Offset);
  132. void Free(const BYTE * Pointer);
  133. void Reserve(ULONG Bytes, ULONG Blocks) { /* UNDONE */ }
  134. void Grow(ULONG);
  135. void RecalculateBlocksBySize(); // protected
  136. void (CPositionIndependentBlob::*m_pmfCompact)();
  137. void * OperatorNew(SIZE_T NumberOfBytes)
  138. {
  139. void * Pointer = 0;
  140. ULONG Offset = 0;
  141. Alloc(static_cast<ULONG>(NumberOfBytes), &Offset);
  142. OffsetToPointer(Offset, &Pointer);
  143. return Pointer;
  144. }
  145. //PBYTE OffsetToPointer(ULONG Offset) { return m_BasePointer + Offset; }
  146. //template <typename T> void OffsetToPointer(ULONG Offset, T * & Pointer) { Pointer = reinterpret_cast<T*>(m_Base + static_cast<SIZE_T>(Offset)); }
  147. template <typename T> void OffsetToPointer(ULONG Offset, T * * Pointer) { *Pointer = reinterpret_cast<T*>(m_Base + static_cast<SIZE_T>(Offset)); }
  148. template <typename T>
  149. ULONG PointerToOffset(const T * Pointer)
  150. {
  151. return static_cast<ULONG>(reinterpret_cast<PBYTE>(Pointer) - m_BasePointer);
  152. }
  153. };
  154. #include "poppack.h"