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.

229 lines
4.3 KiB

  1. /*++
  2. Copyright (c) 1990, 1991 Microsoft Corporation
  3. Module Name:
  4. hwheap.c
  5. Abstract:
  6. This is a very simple Heap Manager for NT OS Hardware recognizer.
  7. This module provides functions to allocate memory in byte-unit
  8. from a permanent heap.
  9. Author:
  10. Shie-Lin Tzong (shielint) 18-Oct-91
  11. Environment:
  12. Kernel Mode
  13. Revision History:
  14. --*/
  15. #include "hwdetect.h"
  16. #include "string.h"
  17. VOID
  18. GrowHeapSpace(
  19. ULONG
  20. );
  21. VOID
  22. HeapCheck(
  23. PVOID
  24. );
  25. //
  26. // Heap management variables.
  27. //
  28. ULONG HwHeapBase = 0; // Current virtual address of base of heap
  29. ULONG HwHeapPointer = 0; // Pointer to the end of available heap
  30. ULONG HwHeapSize = 0; // Size of Heap
  31. ULONG HwAvailableHeap = 0; // Currently available heap space
  32. #if DBG
  33. ULONG HwPreviousAllocSize = 0;
  34. #endif
  35. BOOLEAN
  36. HwResizeHeap (
  37. ULONG NewHeapSize
  38. )
  39. /*++
  40. Routine Description:
  41. The routine grows current heap to the specified size.
  42. It reallocates the heap, copies the data in current heap to
  43. the new heap, updates heap variables, updates heap pointers
  44. in hardware data structures and finally frees the old heap.
  45. Arguments:
  46. NewHeapSize - Specifies the size of the new heap.
  47. Returns:
  48. TRUE - if operation is done sucessfully. Else it returns FALSE.
  49. --*/
  50. {
  51. //
  52. // Not implemented yet.
  53. //
  54. return(FALSE);
  55. }
  56. BOOLEAN
  57. HwInitializeHeap(
  58. ULONG HeapStart,
  59. ULONG HeapSize
  60. )
  61. /*++
  62. Routine Description:
  63. The routine allocates heap and initializes some vital heap
  64. variables.
  65. Arguments:
  66. None
  67. Returns:
  68. FALSE - if unable to allocate initial heap. Else it returns TRUE.
  69. --*/
  70. {
  71. HwHeapBase = HeapStart;
  72. HwHeapPointer = HwHeapBase;
  73. HwHeapSize = HeapSize;
  74. HwAvailableHeap = HwHeapSize;
  75. return(TRUE);
  76. }
  77. FPVOID
  78. HwAllocateHeap(
  79. ULONG RequestSize,
  80. BOOLEAN ZeroInitialized
  81. )
  82. /**
  83. Routine Description:
  84. Allocates memory from the hardware recognizer's heap.
  85. The heap begins with a default size. If a request exhausts heap space,
  86. the heap will be grown to accomodate the request. The heap can grow
  87. up to any size limited by NTLDR. If we run out of heap space and are
  88. unable to allocate more memory, a value of NULL will be returned.
  89. Arguments:
  90. RequestSize - Size of block to allocate.
  91. ZeroInitialized - Specifies if the heap should be zero initialized.
  92. Returns:
  93. Returns a pointer to the allocated block of memory. A NULL pointer
  94. will be returned if we run out of heap and are unable to resize
  95. current heap.
  96. --*/
  97. {
  98. FPVOID ReturnPointer;
  99. if (RequestSize > HwAvailableHeap) {
  100. //
  101. // We're out of heap. Try to grow current heap to satisfy the
  102. // request.
  103. //
  104. if (!HwResizeHeap(HwHeapSize + RequestSize)) {
  105. #if DBG
  106. BlPrint("Unable to grow heap\n");
  107. #endif
  108. return(NULL);
  109. }
  110. }
  111. //
  112. // Set our return value to the new Heap pointer then
  113. // update the remaining space and heap pointer.
  114. //
  115. MAKE_FP(ReturnPointer, HwHeapPointer);
  116. HwHeapPointer += RequestSize;
  117. #if DBG
  118. HwPreviousAllocSize = RequestSize;
  119. #endif
  120. HwAvailableHeap -= RequestSize;
  121. if (ZeroInitialized) {
  122. _fmemset(ReturnPointer, 0, (USHORT)RequestSize);
  123. }
  124. return (ReturnPointer);
  125. }
  126. VOID
  127. HwFreeHeap(
  128. ULONG Size
  129. )
  130. /**
  131. Routine Description:
  132. Unallocates memory from the hardware recognizer's heap.
  133. The unallocation is very basic. It simply moves heap pointer
  134. back by the size specified and increases the heap size by the
  135. specified size. The routine should be used only when previous
  136. allocateHeap allocated too much memory.
  137. Arguments:
  138. RequestSize - Size of block to allocate.
  139. Returns:
  140. Returns a pointer to the allocated block of memory. A NULL pointer
  141. will be returned if we run out of heap and are unable to resize
  142. current heap.
  143. --*/
  144. {
  145. #if DBG
  146. if (Size > HwPreviousAllocSize) {
  147. BlPrint("Invalid heap deallocation ...\n");
  148. } else {
  149. HwPreviousAllocSize -= Size;
  150. }
  151. #endif
  152. HwHeapPointer -= Size;
  153. HwAvailableHeap += Size;
  154. }
  155.