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.

489 lines
12 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. poolmem.c
  5. Abstract:
  6. poolmem provides a managed allocation scheme in which large blocks of memory are
  7. allocated (pools) and then divided up by request into low overhead memory chunks
  8. upon request. poolmem provides for easy creation/clean-up of memory, freeing the
  9. developer for more important tasks.
  10. Author:
  11. Marc R. Whitten (marcw) 13-Feb-1997
  12. Revision History:
  13. jimschm 28-Sep-1998 Debug message fixes
  14. --*/
  15. #include "pch.h"
  16. typedef struct _POOLMEMORYBLOCK POOLMEMORYBLOCK, *PPOOLMEMORYBLOCK;
  17. struct _POOLMEMORYBLOCK {
  18. DWORD Index; // Tracks into RawMemory.
  19. DWORD Size; // the size in bytes of RawMemory.
  20. PPOOLMEMORYBLOCK NextBlock; // A pointer to the next block in the pool chain.
  21. PPOOLMEMORYBLOCK PrevBlock; // A pointer to the prev block in the pool chain.
  22. DWORD UseCount; // The number of allocations currently referring
  23. // to this block.
  24. PBYTE RawMemory; // The actual bytes of allocable memory in this block.
  25. };
  26. typedef struct _ALLOCATION ALLOCATION, * PALLOCATION;
  27. struct _ALLOCATION {
  28. PPOOLMEMORYBLOCK ParentBlock; // A reference to the block from which this allocation
  29. // was created.
  30. };
  31. typedef struct _POOLHEADER {
  32. PPOOLMEMORYBLOCK PoolHead; // The active memory block in this pool.
  33. DWORD MinimumBlockSize; // minimum size to allocate when a new block is needed.
  34. } POOLHEADER, *PPOOLHEADER;
  35. BOOL
  36. pPoolMemAddMemory (
  37. IN POOLHANDLE Handle,
  38. IN DWORD Size
  39. )
  40. /*++
  41. Routine Description:
  42. pPoolMemAddMemory is the function responsible for actually growing the size of
  43. the pool by adding a new block of memory. This function is used by
  44. PoolMemInitPool and PoolMemGetMemory.
  45. when called, this function attempts to allocate at least poolHeader ->
  46. MinimumBlockSize bytes of memory. If the requested size is actually larger
  47. than the minimum, the requested size is allocated instead. This is consistent
  48. with PoolMem's main purpose: An efficient allocator for larger numbers of small
  49. objects. If PoolMem is being used to allocate very large objects, the benefits
  50. are lost and poolmem becomes a very inefficient allocator.
  51. Arguments:
  52. Handle - A Handle to a Pool of Memory.
  53. Size - Size to allocate.
  54. Return Value:
  55. returns TRUE if memory was successfully added, FALSE otherwise.
  56. --*/
  57. {
  58. PBYTE allocedMemory;
  59. PPOOLMEMORYBLOCK newBlock;
  60. PPOOLHEADER poolHeader = (PPOOLHEADER) Handle;
  61. DWORD sizeNeeded;
  62. //
  63. // Determine size needed and attempt to allocate memory.
  64. //
  65. if (Size + sizeof(POOLMEMORYBLOCK) > poolHeader -> MinimumBlockSize) {
  66. sizeNeeded = Size + sizeof(POOLMEMORYBLOCK);
  67. }
  68. else {
  69. sizeNeeded = poolHeader -> MinimumBlockSize;
  70. }
  71. allocedMemory = MemAlloc(sizeNeeded);
  72. if (allocedMemory) {
  73. //
  74. // Use the beginning of the alloc'ed block as the poolblock structure.
  75. //
  76. newBlock = (PPOOLMEMORYBLOCK) allocedMemory;
  77. newBlock -> Size = sizeNeeded - sizeof(POOLMEMORYBLOCK);
  78. newBlock -> RawMemory = allocedMemory + sizeof(POOLMEMORYBLOCK);
  79. newBlock -> Index = 0;
  80. newBlock -> UseCount = 0;
  81. //
  82. // Link the block into the list.
  83. //
  84. if (poolHeader -> PoolHead) {
  85. poolHeader -> PoolHead -> PrevBlock = newBlock;
  86. }
  87. newBlock -> NextBlock = poolHeader -> PoolHead;
  88. newBlock -> PrevBlock = NULL;
  89. poolHeader -> PoolHead = newBlock;
  90. }
  91. //
  92. // Assuming allocedMemory is non-NULL, we have succeeded.
  93. //
  94. return allocedMemory != NULL;
  95. }
  96. POOLHANDLE
  97. PoolMemInitPool (
  98. VOID
  99. )
  100. /*++
  101. Routine Description:
  102. Initializes a new memory pool and returns a handle to it.
  103. Arguments:
  104. None.
  105. Return Value:
  106. If the function completes succssessfully, it returns a valid POOLHANDLE, otherwise,
  107. it returns NULL.
  108. --*/
  109. {
  110. BOOL ableToAddMemory;
  111. PPOOLHEADER header = NULL;
  112. //
  113. // Allocate the header of this pool.
  114. //
  115. header = MemAlloc(sizeof(POOLHEADER));
  116. if (header) {
  117. //
  118. // Allocation was successful. Now, initialize the pool.
  119. //
  120. header -> MinimumBlockSize = POOLMEMORYBLOCKSIZE;
  121. header -> PoolHead = NULL;
  122. //
  123. // Actually add some memory to the pool.
  124. //
  125. ableToAddMemory = pPoolMemAddMemory(header,0);
  126. if (!ableToAddMemory) {
  127. //
  128. // Unable to add memory to the pool.
  129. //
  130. MemFree(header);
  131. header = NULL;
  132. }
  133. }
  134. return (POOLHANDLE) header;
  135. }
  136. VOID
  137. PoolMemEmptyPool (
  138. IN POOLHANDLE Handle
  139. )
  140. /*++
  141. Routine Description:
  142. PoolMemEmptyPool resets the index pointer of the index block back
  143. to zero, so the next allocation will come from the already allocated
  144. active block.
  145. Calling this function invalidates all pointers previously allocated from
  146. the active block.
  147. Arguments:
  148. Handle - Specifies the pool to reset
  149. Return Value:
  150. None.
  151. --*/
  152. {
  153. PPOOLHEADER poolHeader = (PPOOLHEADER) Handle;
  154. poolHeader -> PoolHead -> UseCount = 0;
  155. poolHeader -> PoolHead -> Index = 0;
  156. }
  157. VOID
  158. PoolMemSetMinimumGrowthSize (
  159. IN POOLHANDLE Handle,
  160. IN DWORD Size
  161. )
  162. /*++
  163. Routine Description:
  164. Sets the minimum growth size for a memory pool. This value is used when new blocks
  165. are actually added to the pool. The PoolMem allocator will attempt to allocate at
  166. least this minimum size.
  167. Arguments:
  168. Handle - A valid POOLHANDLE.
  169. Size - The minimum size in bytes to grow the pool by on each allocation.
  170. Return Value:
  171. None.
  172. --*/
  173. {
  174. PPOOLHEADER poolHeader = (PPOOLHEADER) Handle;
  175. poolHeader -> MinimumBlockSize = max(Size,0);
  176. }
  177. VOID
  178. PoolMemDestroyPool (
  179. POOLHANDLE Handle
  180. )
  181. /*++
  182. Routine Description:
  183. PoolMemDestroyPool completely cleans up the memory pool identified by Handle. It
  184. simply walks the list of memory blocks associated with the memory pool, freeing each of them.
  185. Arguments:
  186. Handle - A valid POOLHANDLE.
  187. Return Value:
  188. None.
  189. --*/
  190. {
  191. PPOOLMEMORYBLOCK nextBlock;
  192. PPOOLMEMORYBLOCK blockToFree;
  193. PPOOLHEADER poolHeader;
  194. poolHeader = (PPOOLHEADER) Handle;
  195. //
  196. // Walk the list, freeing as we go.
  197. //
  198. blockToFree = poolHeader -> PoolHead;
  199. while (blockToFree != NULL) {
  200. nextBlock = blockToFree->NextBlock;
  201. MemFree(blockToFree);
  202. blockToFree = nextBlock;
  203. }
  204. //
  205. // Also, deallocate the poolheader itself.
  206. //
  207. MemFree(poolHeader);
  208. }
  209. PVOID
  210. PoolMemRealGetMemory (
  211. IN POOLHANDLE Handle,
  212. IN DWORD Size,
  213. IN DWORD AlignSize
  214. )
  215. /*++
  216. Routine Description:
  217. PoolMemRealGetMemory is the worker routine that processes all requests to retrieve memory
  218. from a pool. Other calls eventually decay into a call to this common routine. This routine
  219. attempts to service the request out of the current memory block, or, if it cannot, out of
  220. a newly allocated block.
  221. Arguments:
  222. (File) - The File from whence the call orignated. This is used for memory tracking and checking
  223. in the debug version.
  224. (Line) - The Line from whence the call orignated.
  225. Handle - A valid POOLHANDLE.
  226. Size - Contains the size in bytes that the caller needs from the pool.
  227. AlignSize - Provides an alignment value. The returned memory will be aligned on <alignsize> byte
  228. boundaries.
  229. Return Value:
  230. The allocated memory, or, NULL if no memory could be allocated.
  231. --*/
  232. {
  233. BOOL haveEnoughMemory = TRUE;
  234. PVOID rMemory = NULL;
  235. PPOOLHEADER poolHeader = (PPOOLHEADER) Handle;
  236. PPOOLMEMORYBLOCK currentBlock;
  237. PALLOCATION allocation;
  238. DWORD sizeNeeded;
  239. DWORD_PTR padLength;
  240. //
  241. // Assume that the current block of memory will be sufficient.
  242. //
  243. currentBlock = poolHeader -> PoolHead;
  244. //
  245. // Determine if more memory is needed, attempt to add if needed. Note that the size
  246. // must include the size of an ALLOCATION struct in addition to the size required
  247. // by the callee. Note the references to AlignSize in the test below. This is to ensure
  248. // that there is enough memory to allocate after taking into acount data alignment.
  249. //
  250. sizeNeeded = Size + sizeof(ALLOCATION);
  251. if (currentBlock -> Size - currentBlock -> Index < sizeNeeded + AlignSize) {
  252. haveEnoughMemory = pPoolMemAddMemory(poolHeader,sizeNeeded + AlignSize);
  253. //
  254. // Make sure that the currentBlock is correctly set
  255. //
  256. currentBlock = poolHeader -> PoolHead;
  257. }
  258. //
  259. // If there is enough memory available, return it.
  260. //
  261. if (haveEnoughMemory) {
  262. if (AlignSize) {
  263. padLength = (DWORD_PTR) currentBlock + sizeof(POOLMEMORYBLOCK)
  264. + currentBlock -> Index + sizeof(ALLOCATION);
  265. currentBlock -> Index += (DWORD)(AlignSize - (padLength % AlignSize)) % AlignSize;
  266. }
  267. //
  268. // Save a reference to this block in the memorys ALLOCATION structure.
  269. // This will be used to decrease the use count on a block when releasing
  270. // memory.
  271. //
  272. (PBYTE) allocation = &(currentBlock -> RawMemory[currentBlock -> Index]);
  273. allocation -> ParentBlock = currentBlock;
  274. //
  275. // Ok, get a reference to the actual memory to return to the user.
  276. //
  277. rMemory = (PVOID)
  278. &(currentBlock->RawMemory[currentBlock -> Index + sizeof(ALLOCATION)]);
  279. //
  280. // Update memory block data fields.
  281. //
  282. currentBlock->Index += sizeNeeded;
  283. currentBlock->UseCount++;
  284. }
  285. return rMemory;
  286. }
  287. VOID
  288. PoolMemReleaseMemory (
  289. IN POOLHANDLE Handle,
  290. IN LPVOID Memory
  291. )
  292. /*++
  293. Routine Description:
  294. PoolMemReleaseMemory notifies the Pool that a piece of memory is no longer needed.
  295. if all memory within a non-active block (i.e. not the first block) is released,
  296. that block will be freed. If all memory is released within an active block, that blocks
  297. stats are simply cleared, effectively reclaiming its space.
  298. Arguments:
  299. Handle - A Handle to a Pool of Memory.
  300. Memory - Contains the address of the memory that is no longer needed.
  301. Return Value:
  302. None.
  303. --*/
  304. {
  305. PALLOCATION allocation;
  306. PPOOLHEADER poolHeader = (PPOOLHEADER) Handle;
  307. //
  308. // Get a reference to the ALLOCATION struct that precedes the actual memory.
  309. //
  310. allocation = (PALLOCATION) Memory - 1;
  311. //
  312. // Check to make sure this memory has not previously been freed.
  313. //
  314. if (allocation -> ParentBlock == NULL) {
  315. return;
  316. }
  317. //
  318. // Update the use count on this allocations parent block.
  319. //
  320. allocation -> ParentBlock -> UseCount--;
  321. if (allocation -> ParentBlock -> UseCount == 0) {
  322. //
  323. // This was the last allocation still referring to the parent block.
  324. //
  325. if (allocation -> ParentBlock != poolHeader -> PoolHead) {
  326. //
  327. // Since the parent block isn't the active block, simply delete it.
  328. //
  329. if (allocation -> ParentBlock -> NextBlock) {
  330. allocation -> ParentBlock -> NextBlock -> PrevBlock =
  331. allocation -> ParentBlock -> PrevBlock;
  332. }
  333. allocation -> ParentBlock -> PrevBlock -> NextBlock =
  334. allocation -> ParentBlock -> NextBlock;
  335. MemFree(allocation -> ParentBlock);
  336. }
  337. else {
  338. //
  339. // Since this is the active block, reset it.
  340. //
  341. allocation -> ParentBlock -> Index = 0;
  342. allocation -> ParentBlock = NULL;
  343. }
  344. }
  345. else {
  346. allocation -> ParentBlock = NULL;
  347. }
  348. }
  349.