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.

148 lines
3.3 KiB

  1. //================================================================================
  2. // Copyright (C) 1997 Microsoft Corporation
  3. // Author: RameshV
  4. // Description: some memory handling stuff
  5. //================================================================================
  6. #include <mm.h>
  7. #include <align.h>
  8. #if DBG
  9. #define STATIC static
  10. #else
  11. #define STATIC
  12. #endif
  13. STATIC HANDLE MemHeapHandle = NULL;
  14. static DWORD Initialized = FALSE;
  15. static DWORD UseHeap = 0;
  16. ULONG MemNBytesAllocated = 0;
  17. //BeginExport(function)
  18. DWORD
  19. MemInit(
  20. VOID
  21. ) //EndExport(function)
  22. {
  23. Require(Initialized == FALSE);
  24. MemHeapHandle = HeapCreate(
  25. /* flOptions */ 0,
  26. /* dwInitialSize */ 64000,
  27. /* dwMaximumSize */ 0
  28. );
  29. if( MemHeapHandle == NULL ) return GetLastError();
  30. Initialized = TRUE;
  31. UseHeap = 1;
  32. return ERROR_SUCCESS;
  33. }
  34. //BeginExport(function)
  35. VOID
  36. MemCleanup(
  37. VOID
  38. ) //EndExport(function)
  39. {
  40. BOOL Status;
  41. if( 0 == Initialized ) return;
  42. Initialized --;
  43. Require(MemHeapHandle);
  44. Status = HeapDestroy(MemHeapHandle);
  45. MemHeapHandle = NULL;
  46. Require(FALSE != Status);
  47. Require(0 == Initialized);
  48. UseHeap = 0;
  49. }
  50. LPVOID _inline
  51. MemAllocInternal(
  52. IN DWORD nBytes
  53. )
  54. {
  55. LPVOID Ptr;
  56. if( 0 == UseHeap ) {
  57. Ptr = LocalAlloc(LMEM_FIXED, nBytes);
  58. // DbgPrint("MEM %08lx ALLOC\n", Ptr);
  59. return Ptr;
  60. }
  61. if( NULL == MemHeapHandle ) return NULL;
  62. return HeapAlloc(
  63. /* hHeap */ MemHeapHandle,
  64. /* dwFlags */ HEAP_ZERO_MEMORY,
  65. /* dwBytes */ nBytes
  66. );
  67. }
  68. DWORD _inline
  69. MemFreeInternal(
  70. IN LPVOID Mem
  71. )
  72. {
  73. BOOL Status;
  74. // DbgPrint("MEM %08lx FREE\n", Mem);
  75. if( 0 == UseHeap ) {
  76. if(NULL == LocalFree(Mem) )
  77. return ERROR_SUCCESS;
  78. return ERROR_INVALID_DATA;
  79. }
  80. if( NULL == MemHeapHandle ) {
  81. Require(FALSE);
  82. return ERROR_INVALID_DATA;
  83. }
  84. Status = HeapFree(
  85. /* hHeap */ MemHeapHandle,
  86. /* dwFlags */ 0,
  87. /* lpMem */ Mem
  88. );
  89. if( FALSE != Status ) return ERROR_SUCCESS;
  90. return GetLastError();
  91. }
  92. //BeginExport(function)
  93. LPVOID
  94. MemAlloc(
  95. IN DWORD nBytes
  96. ) //EndExport(function)
  97. {
  98. LPVOID Ptr;
  99. #if DBG
  100. Ptr = MemAllocInternal(ROUND_UP_COUNT(nBytes + sizeof(ULONG_PTR), ALIGN_WORST));
  101. if( NULL == Ptr ) return Ptr;
  102. *((ULONG_PTR *)Ptr) ++ = nBytes;
  103. InterlockedExchangeAdd(&MemNBytesAllocated, nBytes);
  104. return Ptr;
  105. #endif
  106. return MemAllocInternal(nBytes);
  107. }
  108. //BeginExport(function)
  109. DWORD
  110. MemFree(
  111. IN LPVOID Mem
  112. ) //EndExport(function)
  113. {
  114. LPVOID Ptr;
  115. #if DBG
  116. Ptr = -1 + (ULONG_PTR *)Mem;
  117. InterlockedExchangeAdd(&MemNBytesAllocated, - (LONG)(*(ULONG *)Ptr) );
  118. return MemFreeInternal(Ptr);
  119. #endif
  120. return MemFreeInternal(Mem);
  121. }
  122. //================================================================================
  123. // end of file
  124. //================================================================================