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.

135 lines
3.5 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows NT **/
  3. /** Copyright(c) Microsoft Corp., 1997 **/
  4. /**********************************************************************/
  5. /*
  6. madel.hxx
  7. This module contains the definitions for a memory allocation class that doesn't
  8. delete memory until the class goes away.
  9. FILE HISTORY:
  10. 1/12/98 michth created
  11. */
  12. /*
  13. To build this:
  14. The dll which builds this must declare in one of it's source files.
  15. DECLARE_DEBUG_VARIABLE();
  16. DECLARE_DEBUG_PRINTS_OBJECT();
  17. It also must include these lines in source files prior to including madel.hxx
  18. #define DLL_IMPLEMENTATION
  19. #define IMPLEMENTATION_EXPORT
  20. It also must execute this before calling the constructor.
  21. CREATE_DEBUG_PRINT_OBJECT("MemTest");
  22. There seems to be a build problem if irtlmisc.h is included twice.
  23. */
  24. #ifndef _MADEL_HXX_
  25. #define _MADEL_HXX_
  26. #include <manodel.hxx>
  27. #define MADEL_USE_PROCESS_HEAP USE_PROCESS_HEAP
  28. #define MADEL_DEFAULT_RESERVE_NUM 100
  29. #define MADEL_DEFAULT_MIN_BLOCK_MULTIPLE 5
  30. #define MADEL_DEFAULT_MAX_BLOCK_MULTIPLE 5
  31. #define MADEL_MAX_ALLOWED_BLOCK_MULTIPLE 128
  32. #define MADEL_MAX_ALLOWED_SIZE 0xffffff
  33. #define GREATER_OF(P1, P2) (((P1) >= (P2)) ? (P1) : (P2))
  34. #define LESSER_OF(P1, P2) (((P1) <= (P2)) ? (P1) : (P2))
  35. typedef struct _MADEL_BLOCK_HEADER {
  36. LIST_ENTRY m_leBlockList;
  37. PVOID pvFreeList;
  38. BYTE byteBlockMultiple;
  39. BYTE byteNumFree;
  40. BYTE byteAlignBytes;
  41. } MADEL_BLOCK_HEADER, *PMADEL_BLOCK_HEADER;
  42. typedef struct _MADEL_ALLOC_HEADER {
  43. DWORD dwSize : 24;
  44. DWORD bNumAlloc : 7 ;
  45. DWORD bMadelAlloc : 1;
  46. } MADEL_ALLOC_HEADER, *PMADEL_ALLOC_HEADER;
  47. class IRTL_DLLEXP MEMORY_ALLOC_DELETE;
  48. class IRTL_DLLEXP MEMORY_ALLOC_DELETE
  49. {
  50. public:
  51. MEMORY_ALLOC_DELETE( DWORD dwAllocSize,
  52. DWORD dwAlignment = 4,
  53. DWORD dwReserveNum = MADEL_DEFAULT_RESERVE_NUM,
  54. DWORD dwMinBlockMultiple = MADEL_DEFAULT_MIN_BLOCK_MULTIPLE,
  55. DWORD dwMaxBlockMultiple = MADEL_DEFAULT_MAX_BLOCK_MULTIPLE,
  56. HANDLE hHeap = MADEL_USE_PROCESS_HEAP);
  57. ~MEMORY_ALLOC_DELETE();
  58. PVOID Alloc();
  59. BOOL Free (PVOID pvMem);
  60. private:
  61. DWORD m_dwAllocSize;
  62. DWORD m_dwBlockMultiple;
  63. HANDLE m_hHeap;
  64. DWORD m_dwNumAlloced;
  65. DWORD m_dwNumFree;
  66. DWORD m_dwReserveNum;
  67. DWORD m_dwNumBlocks;
  68. DWORD m_dwBlockHeaderSpace;
  69. DWORD m_dwAllocHeaderSpace;
  70. DWORD m_dwMaxBlockMultiple;
  71. DWORD m_dwAlignment;
  72. DWORD m_dwAlignBytes;
  73. LIST_ENTRY m_leBlockList;
  74. LIST_ENTRY m_leFreeList;
  75. LIST_ENTRY m_leDeleteList;
  76. CRITICAL_SECTION m_csLock;
  77. BOOL m_bMaxBlockMultipleEqualsMin;
  78. BYTE m_byteLeastAllocsOnFreeList;
  79. VOID GetNewBlockMultiple();
  80. PVOID GetAllocFromList(PLIST_ENTRY pleListHead);
  81. BOOL AllocBlock();
  82. VOID CreateBlockFreeList(PMADEL_BLOCK_HEADER pmbhNewBlock);
  83. VOID LockThis( VOID ) { EnterCriticalSection(&m_csLock);}
  84. VOID UnlockThis( VOID ) { LeaveCriticalSection(&m_csLock);}
  85. inline VOID
  86. AlignAdjust(DWORD &rdwSize,
  87. DWORD dwAlignment);
  88. #ifdef _WIN64
  89. inline VOID
  90. AlignAdjust(ULONG_PTR &rSize,
  91. ULONG_PTR Alignment);
  92. #endif
  93. inline VOID
  94. InitAllocHead(PMADEL_ALLOC_HEADER pvAlloc,
  95. DWORD dwAllocIndex);
  96. inline PMADEL_BLOCK_HEADER
  97. GetBlockFromAlloc(PMADEL_ALLOC_HEADER pmahMem);
  98. };
  99. #endif // !_MADEL_