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.

276 lines
6.8 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. suballoc.c
  5. Abstract:
  6. This module contains code for managing a paritially commited address
  7. space. It handles allocation of chunks of memory smaller than the
  8. commit granularity. It commits and decommits memory as needed using
  9. the supplied function for committing and decommitting memory. The
  10. structures used for tracking the address space are allocated outside
  11. of the specified addresss space.
  12. Author:
  13. Dave Hastings (daveh) creation-date 21-Jan-1994
  14. Notes:
  15. Since this package does not actually access memory in the address space
  16. it is managing, it will work as well with real linear addresses or
  17. "Intel Addresses" such as would be encountered with the Insignia Emulator
  18. on risc.
  19. Revision History:
  20. --*/
  21. //
  22. // Constants
  23. //
  24. //
  25. // Smallest chunk that will be sub allocated
  26. // 1024 was chosen currently, because that is the
  27. // smallest chunk XMS will allocate.
  28. //
  29. #define SUBALLOC_GRANULARITY 1024
  30. //
  31. // The size of the character array require to hold the
  32. // bits defining whether the individual allocation entities
  33. // in the commited chunk are allocated or free
  34. //
  35. // BUGBUG Assert that COMMIT_GRANULARITY is a multiple of SUBALLOC_GRANULARITY
  36. //
  37. #define SUBALLOC_BITFIELD_SIZE COMMIT_GRANULARITY / SUBALLOC_GRANULARITY
  38. //
  39. // Types
  40. //
  41. //
  42. // Routine for committing a specific region of of the address
  43. // space. Although the return type is NTSTATUS, the only value
  44. // that is checked is 0 (for STATUS_SUCCESS). If STATUS_SUCCESS
  45. // is returned, it is assumed that the function worked. If not,
  46. // it is assumed that it failed. No special meaning is attached to
  47. // particular non-zero values.
  48. //
  49. typedef
  50. NTSTATUS
  51. (*PSACOMMITROUTINE)(
  52. ULONG BaseAddress,
  53. ULONG Size
  54. );
  55. //
  56. // Structure for tracking the address space. Each chunk of
  57. // memory of SUBALLOC_GRANULARITY in size is represented by
  58. // a bit. Each chunk of memory of COMMIT_GRANULARITY is
  59. // represented by one element of the array of bitfields.
  60. // The FreeCheck array half of the union allows us to check
  61. // whether the entire committed chunk is free more quickly.
  62. //
  63. // ?? Should we add a field to indicate whether the chunk is
  64. // committed? We can always check for all allocated bits
  65. // zero, and use that as an indication that the chunk is
  66. // not committed.
  67. //
  68. // BUGBUG Assert that the bits fit into memory the way we think
  69. // they do.
  70. //
  71. typedef struct _SubAllocation {
  72. PSACOMMITROUTINE CommitRoutine;
  73. ULONG BaseAddress;
  74. ULONG Size;
  75. union {
  76. //
  77. // bitfield with one bit per chunk. Bit set indicates
  78. // allocated. Bit clear indicates free. All bits
  79. // clear indicates un committed
  80. //
  81. UINT Allocated[SUBALLOC_BITFIELD_SIZE] : 1;
  82. //
  83. // Hopefully a faster way to check all bits.
  84. //
  85. UINT FreeCheck[SUBALLOC_BITFIELD_SIZE / sizeof(UINT)];
  86. } CommitedChunk;
  87. } SUBALLOCATIONDATA, *PSUBALLOCATIONDATA
  88. PVOID
  89. SAInitialize(
  90. ULONG BaseAddress,
  91. ULONG Size,
  92. PSACOMMITROUTINE CommitRoutine
  93. )
  94. /*++
  95. Routine Description:
  96. This function performs initialization of the sub allocation package
  97. for the specified addresss range. It allocates the data structures
  98. necessary to track the allocations
  99. Arguments:
  100. BaseAddress -- Supplies the base address of the address space to
  101. sub allocate.
  102. Size -- Supplies the size in bytes of the address space to sub allocate.
  103. CommitRoutine -- Supplies a pointer to the routine used to commit regions
  104. of the address space.
  105. Return Value:
  106. If the function was successful it returns a pointer to the sub-allocation
  107. data structures. Otherwise it returns NULL.
  108. --*/
  109. {
  110. }
  111. BOOL
  112. SAQueryFree(
  113. PVOID SubAllocation,
  114. PULONG FreeBytes
  115. )
  116. /*++
  117. Routine Description:
  118. This routine returns the number of free bytes in the
  119. sub allocated address space.
  120. Arguments:
  121. SubAllocation -- Supplies the pointer returned by SAInitialize
  122. FreeBytes -- Returns the number of free bytes
  123. Return Value:
  124. TRUE -- if successful, and FreeBytes contains the number of free bytes.
  125. FALSE otherwise
  126. --*/
  127. {
  128. }
  129. BOOL
  130. SAAllocate(
  131. PVOID SubAllocation,
  132. ULONG Size,
  133. PULONG Address
  134. )
  135. /*++
  136. Routine Description:
  137. This function allocates a portion of the address space described by
  138. SubAllocation. If necessary, it will commit additional blocks. Address is
  139. rounded down to the next lower SUBALLOC_GRANULARITY boundary.
  140. size is rounded up to the next higher multiple of SUBALLOC_GRANULARITY.
  141. Arguments:
  142. SubAllocation -- Supplies the pointer returned by SAInitialize.
  143. Size -- Supplies the size in bytes of the region to allocate.
  144. Address -- Returns the address of the region allocated.
  145. Return Value:
  146. TRUE if successful. If false is returned, no address is returned.
  147. Notes:
  148. Zero is a valid value for the returned address.
  149. --*/
  150. {
  151. }
  152. BOOL
  153. SAFree(
  154. PVOID SubAllocation,
  155. ULONG Size,
  156. ULONG Address
  157. )
  158. /*++
  159. Routine Description:
  160. This routine frees a sub-allocated chunk of memory. If the
  161. entire commited block (or blocks) that the specified chunk
  162. belongs to are free, the chunks are decommitted. Address is
  163. rounded down to the next lower SUBALLOC_GRANULARITY boundary.
  164. size is rounded up to the next higher multiple of SUBALLOC_GRANULARITY.
  165. Arguments:
  166. SubAllocation -- Supplies the pointer returned by SAInitialize.
  167. Size -- Supplies the size in bytes of the region to free.
  168. Address -- Supplies the address of the region to free.
  169. Return Value:
  170. TRUE if successful.
  171. Notes:
  172. It is possible to free a different size at a particular
  173. address than was allocated. This will not cause the
  174. SubAllocation package any problems.
  175. --*/
  176. {
  177. }
  178. BOOL
  179. SAReallocate(
  180. PVOID SubAllocation,
  181. ULONG OriginalSize,
  182. ULONG OriginalAddress,
  183. ULONG NewSize,
  184. PULONG NewAddress
  185. )
  186. /*++
  187. Routine Description:
  188. This routine reallocates a sub allocated block of memory.
  189. The sizes are rounded up to the next SUBALLOC_GRANULARITY.
  190. The Original address is rounded down to the next SUBALLOC_GRANULARITY
  191. boundary. Only min(OriginalSize, NewSize) bytes of data are copied to
  192. the new block. The block changed in place if possible.
  193. Arguments:
  194. SubAllocation -- Supplies the pointer returned by SAInitialize.
  195. OriginalSize -- Supplies the old size in bytes of the block.
  196. OriginalAddress -- Supplies the old address of the block.
  197. NewSize -- Supplies the new size in bytes of the block.
  198. NewAddress -- Returns the new address of the block.
  199. Return Value:
  200. True if successful. If unsucessful, no allocation is changed.
  201. Notes:
  202. If the caller does not supply the correct original size for the block,
  203. some memory may be lost, and the block may be moved unnecessarily.
  204. --*/
  205. {
  206. }