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.

149 lines
2.4 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: N C M E M . C P P
  7. //
  8. // Contents: Common memory management routines.
  9. //
  10. // Notes:
  11. //
  12. // Author: shaunco 24 Mar 1997
  13. //
  14. //----------------------------------------------------------------------------
  15. #include <pch.h>
  16. #pragma hdrstop
  17. #include "ncdebug.h"
  18. #include "ncmem.h"
  19. // This global heap handle will be set to the process heap when the
  20. // first request to allocate memory through MemAlloc is made.
  21. //
  22. HANDLE g_hHeap;
  23. //+---------------------------------------------------------------------------
  24. //
  25. // Function: HrMalloc
  26. //
  27. // Purpose: HRESULT returning version of malloc.
  28. //
  29. // Arguments:
  30. // cb [in] Count of bytes to allocate.
  31. // ppv [out] Address of returned allocation.
  32. //
  33. // Returns: S_OK or E_OUTOFMEMORY;
  34. //
  35. // Author: shaunco 31 Mar 1998
  36. //
  37. // Notes: Free the returned buffer with free.
  38. //
  39. HRESULT
  40. HrMalloc (
  41. size_t cb,
  42. PVOID* ppv)
  43. {
  44. Assert (ppv);
  45. HRESULT hr = S_OK;
  46. *ppv = MemAlloc (cb);
  47. if (!*ppv)
  48. {
  49. hr = E_OUTOFMEMORY;
  50. }
  51. TraceHr (ttidError, FAL, hr, FALSE, "HrMalloc");
  52. return hr;
  53. }
  54. VOID*
  55. MemAlloc (
  56. size_t cb)
  57. {
  58. #ifdef USE_HEAP_ALLOC
  59. if (!g_hHeap)
  60. {
  61. g_hHeap = GetProcessHeap();
  62. }
  63. return HeapAlloc (g_hHeap, 0, cb);
  64. #else
  65. return malloc (cb);
  66. #endif
  67. }
  68. VOID*
  69. MemAllocRaiseException (
  70. size_t cb)
  71. {
  72. VOID* pv = MemAlloc (cb);
  73. if (!pv)
  74. {
  75. RaiseException (E_OUTOFMEMORY, 0, 0, NULL);
  76. }
  77. return pv;
  78. }
  79. VOID
  80. MemFree (
  81. VOID* pv)
  82. {
  83. #ifdef USE_HEAP_ALLOC
  84. if (!g_hHeap)
  85. {
  86. g_hHeap = GetProcessHeap();
  87. }
  88. HeapFree (g_hHeap, 0, pv);
  89. #else
  90. free (pv);
  91. #endif
  92. }
  93. //+---------------------------------------------------------------------------
  94. // CRT memory function overloads
  95. //
  96. const raiseexception_t raiseexception;
  97. VOID*
  98. __cdecl
  99. operator new (
  100. size_t cb,
  101. const raiseexception_t&)
  102. {
  103. Assert(FALSE);
  104. return MemAllocRaiseException (cb);
  105. }
  106. const extrabytes_t extrabytes;
  107. VOID*
  108. __cdecl
  109. operator new (
  110. size_t cb,
  111. const extrabytes_t&,
  112. size_t cbExtra)
  113. {
  114. return MemAlloc (cb + cbExtra);
  115. }
  116. VOID*
  117. __cdecl
  118. operator new (
  119. size_t cb)
  120. {
  121. return MemAlloc (cb);
  122. }
  123. VOID
  124. __cdecl
  125. operator delete (
  126. VOID* pv)
  127. {
  128. if (pv)
  129. {
  130. MemFree (pv);
  131. }
  132. }