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.

178 lines
4.9 KiB

  1. /*++
  2. Copyright (C) 1997-2000 Microsoft Corporation
  3. Module Name:
  4. MMFARENA.H
  5. Abstract:
  6. CArena derivative based on memory mapped files.
  7. History:
  8. a-raymcc 23-Apr-96 Created
  9. paulall 23-Mar-98 Re-worked
  10. --*/
  11. #ifndef _MMFARENA_H_
  12. #define _MMFARENA_H_
  13. #include "corepol.h"
  14. #include "FlexArry.h"
  15. #if defined _WIN32
  16. #define MMF_DELETED_MASK 0x80000000
  17. #define MMF_REMOVE_DELETED_MASK 0x7FFFFFFF
  18. #define MMF_DEBUG_DELETED_TAG 0xFFFFFFFF
  19. #define MMF_DEBUG_INUSE_TAG 0xFEFEFEFE
  20. #elif defined _WIN64
  21. #define MMF_DELETED_MASK 0x8000000000000000
  22. #define MMF_REMOVE_DELETED_MASK 0x7FFFFFFFFFFFFFFF
  23. #define MMF_DEBUG_DELETED_TAG 0xFFFFFFFFFFFFFFFF
  24. #define MMF_DEBUG_INUSE_TAG 0xFEFEFEFEFEFEFEFE
  25. #endif
  26. struct MMFOffsetItem;
  27. #include "corex.h"
  28. class DATABASE_FULL_EXCEPTION : public CX_Exception
  29. {
  30. };
  31. //***************************************************************************
  32. //
  33. // struct MMF_ARENA_HEADER
  34. //
  35. // Root structure for MMF Arena. This is recorded on the disk
  36. // image at the very beginning of the file.
  37. //
  38. //***************************************************************************
  39. #pragma pack(4) // Require fixed aligment.
  40. typedef struct
  41. {
  42. // Version used to create file
  43. // vvvvvv MUST BE FIRST VALUE vvvvvvv
  44. DWORD m_dwVersion; // <<<<<< MUST BE FIRST VALUE!
  45. // ^^^^^^ MUST BE FIRST VALUE ^^^^^^^
  46. DWORD m_dwGranularity; // Granularity of allocation
  47. DWORD m_dwCurrentSize; // Current size of heap
  48. DWORD m_dwMaxSize; // Max heap size, -1= no limit
  49. DWORD m_dwGrowBy; // Bytes to grow by during out-of-heap
  50. DWORD_PTR m_dwHeapExtent; // First unused address
  51. DWORD_PTR m_dwEndOfHeap; // Last valid address + 1
  52. DWORD_PTR m_dwFreeList; // NULL if empty
  53. DWORD_PTR m_dwRootBlock; // Root block
  54. DWORD m_dwSizeOfFirstPage; //Size of the first block
  55. } MMF_ARENA_HEADER;
  56. typedef struct
  57. {
  58. DWORD m_dwSize; //Size of block. Highest bit set when deleted.
  59. } MMF_BLOCK_HEADER;
  60. typedef struct
  61. {
  62. DWORD_PTR m_dwFLback; //previous free block in the chain, NULL if not deleted
  63. DWORD m_dwCheckBlock[2];
  64. } MMF_BLOCK_TRAILER;
  65. typedef struct
  66. {
  67. DWORD_PTR m_dwFLnext; //Next free block in the chain
  68. } MMF_BLOCK_DELETED;
  69. typedef struct
  70. {
  71. DWORD m_dwSize; //Size of page
  72. } MMF_PAGE_HEADER; //Page header... not there for first page.
  73. #pragma pack() // Restore previous aligment.
  74. //***************************************************************************
  75. //
  76. // class CMMFArena2
  77. //
  78. // Implements an offset-based heap over a memory-mapped file.
  79. //
  80. //***************************************************************************
  81. class CMMFArena2
  82. {
  83. DWORD m_dwStatus;
  84. HANDLE m_hFile;
  85. MMF_ARENA_HEADER *m_pHeapDescriptor;
  86. DWORD m_dwCursor;
  87. DWORD m_dwMappingGranularity;
  88. DWORD m_dwMaxPageSize;
  89. CFlexArray m_OffsetManager;
  90. //Retrieves the size of the block from the header and removes the deleted bit
  91. DWORD GetSize(MMF_BLOCK_HEADER *pBlock) { return pBlock->m_dwSize & MMF_REMOVE_DELETED_MASK; }
  92. //Validates a pointer
  93. BOOL ValidateBlock(DWORD_PTR dwBlock)
  94. #if (defined DEBUG || defined _DEBUG)
  95. ;
  96. #pragma message("MMF heap validation enabled.")
  97. #else
  98. { return TRUE;}
  99. #endif
  100. MMFOffsetItem *OpenBasePage(DWORD &dwSizeOfRepository);
  101. MMFOffsetItem *OpenExistingPage(DWORD_PTR dwBaseOffset);
  102. void ClosePage(MMFOffsetItem *pOffsetItem);
  103. void CloseAllPages();
  104. public:
  105. enum { create_new, use_existing };
  106. // Constructor.
  107. CMMFArena2();
  108. // Destructor.
  109. ~CMMFArena2();
  110. //Methods to open an MMF
  111. bool LoadMMF(const TCHAR *pszFile);
  112. DWORD Size(DWORD_PTR dwBlock);
  113. DWORD GetVersion() { return (m_pHeapDescriptor? m_pHeapDescriptor->m_dwVersion : 0); }
  114. DWORD GetStatus() { return m_dwStatus; }
  115. //Given an offset, returns a fixed up pointer
  116. LPBYTE OffsetToPtr(DWORD_PTR dwOffset);
  117. //Given a pointer, returns an offset from the start of the MMF
  118. DWORD_PTR PtrToOffset(LPBYTE pBlock);
  119. MMF_ARENA_HEADER *GetMMFHeader() { return m_pHeapDescriptor; }
  120. };
  121. //***************************************************************************
  122. //
  123. // Fixup helpers.
  124. //
  125. // These are all strongly type variations of the same thing: they fix
  126. // up the based ptr to a dereferenceable pointer or take the ptr and
  127. // fix it back down to an offset.
  128. //
  129. //***************************************************************************
  130. extern CMMFArena2* g_pDbArena;
  131. template <class T> T Fixup(T ptr)
  132. { return T(g_pDbArena->OffsetToPtr(DWORD_PTR(ptr))); }
  133. template <class T> T Fixdown(T ptr)
  134. { return T(g_pDbArena->PtrToOffset(LPBYTE(ptr))); }
  135. #endif