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.

184 lines
3.1 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. All rights reserved
  4. Module Name:
  5. memory.cxx
  6. Abstract:
  7. Allocator class. We use this class in the printer persist code.
  8. The TPrnStream class uses this class through inheritance.
  9. The reason why TPrnStream uses its own heap is that it is called
  10. by terminal services in the winlogon process. The heap protects
  11. our caller, cause in the worst case scenario we corrupt just our
  12. own heap. Also, if an exception is raised in the TPrnStream methods,
  13. we don't leak, because the destructor frees the heap.
  14. Author:
  15. Felix Maxa (amaxa) 31-Oct-2000
  16. --*/
  17. #include "precomp.h"
  18. #pragma hdrstop
  19. #include "memory.hxx"
  20. /*++
  21. Title:
  22. TAllocator::TAllocator
  23. Routine Description:
  24. Constructor
  25. Arguments:
  26. None.
  27. Return Value:
  28. None.
  29. --*/
  30. TAllocator::
  31. TAllocator(
  32. IN DWORD Options,
  33. IN SIZE_T InitialSize
  34. ) : m_hHeap(NULL),
  35. m_Options(Options),
  36. m_InitialSize(InitialSize)
  37. {
  38. }
  39. /*++
  40. Title:
  41. TAllocator::~TAllocator
  42. Routine Description:
  43. Destructor
  44. Arguments:
  45. None.
  46. Return Value:
  47. None.
  48. --*/
  49. TAllocator::
  50. ~TAllocator(
  51. VOID
  52. )
  53. {
  54. if (m_hHeap)
  55. {
  56. if (!HeapDestroy(m_hHeap))
  57. {
  58. DBGMSG(DBG_WARNING, ("TAllocator::~TAllocator Failed"
  59. "to destroy heap at 0x%x. Win32 error %u\n", m_hHeap, GetLastError()));
  60. }
  61. m_hHeap = NULL;
  62. }
  63. }
  64. /*++
  65. Title:
  66. TAllocator::AllocMem
  67. Routine Description:
  68. Allocates memory from a heap. The heap is created upon first
  69. call to this function.
  70. Arguments:
  71. cbSize - number of bytes to allocate
  72. Return Value:
  73. NULL - allocation failed
  74. valid pointer to memory allocated - allocation succeeded
  75. --*/
  76. PVOID
  77. TAllocator::
  78. AllocMem(
  79. IN SIZE_T CONST cbSize
  80. )
  81. {
  82. LPVOID pMem = NULL;
  83. if (!m_hHeap)
  84. {
  85. m_hHeap = HeapCreate(m_Options, m_InitialSize, 0);
  86. if (!m_hHeap)
  87. {
  88. DBGMSG(DBG_WARNING, ("TAllocator::AllocMem Failed to create heap. "
  89. "Win32 error %u\n", GetLastError()));
  90. }
  91. }
  92. if (m_hHeap)
  93. {
  94. pMem = HeapAlloc(m_hHeap, m_Options, cbSize);
  95. if (!pMem)
  96. {
  97. DBGMSG(DBG_WARNING, ("TAllocator::AllocMem Failed to allocated memory."
  98. " Win32 error %u\n", GetLastError()));
  99. }
  100. }
  101. return pMem;
  102. }
  103. /*++
  104. Title:
  105. TAllocator::FreeMem
  106. Routine Description:
  107. Frees a block of memory from the heap.
  108. Arguments:
  109. pMem - pointer to block to free
  110. Return Value:
  111. None.
  112. --*/
  113. VOID
  114. TAllocator::
  115. FreeMem(
  116. IN PVOID pMem
  117. )
  118. {
  119. if (pMem && m_hHeap)
  120. {
  121. if (!HeapFree(m_hHeap, m_Options, pMem))
  122. {
  123. DBGMSG(DBG_WARNING, ("TAllocator::FreeMem Failed to free block at 0x%x."
  124. " Win32 error %u\n", pMem, GetLastError()));
  125. }
  126. }
  127. }