3 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 ULONG AllocationCount
= 0;
28 VOID
VerifyHeap(VOID
);
29 VOID
DumpMemoryAllocMap(VOID
);
30 VOID
IncrementAllocationCount(VOID
);
31 VOID
DecrementAllocationCount(VOID
);
32 VOID
MemAllocTest(VOID
);
37 * Normally, we allocate whole pages. This is ofcourse wastefull for small
38 * allocations (a few bytes). So, for small allocations (smaller than a page)
39 * we sub-allocate. When the first small allocation is done, a page is
40 * requested. We keep a pointer to that page in SubAllocationPage. The alloc
41 * is satisfied by returning a pointer to the beginning of the page. We also
42 * keep track of how many bytes are still available in the page in SubAllocationRest.
43 * When the next small request comes in, we try to allocate it just after the
44 * memory previously allocated. If it won't fit, we allocate a new page and
45 * the whole process starts again.
46 * Note that suballocations are done back-to-back, there's no bookkeeping at all.
47 * That also means that we cannot really free suballocations. So, when a free is
48 * done and it is determined that this might be a free of a sub-allocation, we
49 * just no-op the free.
50 * Perhaps we should use the heap routines from ntdll here.
52 static PVOID SubAllocationPage
= NULL
;
53 static unsigned SubAllocationRest
= 0;
55 BOOLEAN AllocateFromEnd
= TRUE
;
57 VOID
MmChangeAllocationPolicy(BOOLEAN PolicyAllocatePagesFromEnd
)
59 AllocateFromEnd
= PolicyAllocatePagesFromEnd
;
62 PVOID
MmAllocateMemory(ULONG MemorySize
)
65 ULONG FirstFreePageFromEnd
;
70 DbgPrint((DPRINT_MEMORY
, "MmAllocateMemory() called for 0 bytes. Returning NULL.\n"));
71 UiMessageBoxCritical("Memory allocation failed: MmAllocateMemory() called for 0 bytes.");
75 MemorySize
= ROUND_UP(MemorySize
, 4);
76 if (MemorySize
<= SubAllocationRest
)
78 MemPointer
= (PVOID
)((ULONG_PTR
)SubAllocationPage
+ MM_PAGE_SIZE
- SubAllocationRest
);
79 SubAllocationRest
-= MemorySize
;
83 // Find out how many blocks it will take to
84 // satisfy this allocation
85 PagesNeeded
= ROUND_UP(MemorySize
, MM_PAGE_SIZE
) / MM_PAGE_SIZE
;
87 // If we don't have enough available mem
89 if (FreePagesInLookupTable
< PagesNeeded
)
91 DbgPrint((DPRINT_MEMORY
, "Memory allocation failed in MmAllocateMemory(). Not enough free memory to allocate %d bytes. AllocationCount: %d\n", MemorySize
, AllocationCount
));
92 UiMessageBoxCritical("Memory allocation failed: out of memory.");
96 FirstFreePageFromEnd
= MmFindAvailablePages(PageLookupTableAddress
, TotalPagesInLookupTable
, PagesNeeded
, AllocateFromEnd
);
98 if (FirstFreePageFromEnd
== (ULONG
)-1)
100 DbgPrint((DPRINT_MEMORY
, "Memory allocation failed in MmAllocateMemory(). Not enough free memory to allocate %d bytes. AllocationCount: %d\n", MemorySize
, AllocationCount
));
101 UiMessageBoxCritical("Memory allocation failed: out of memory.");
105 MmAllocatePagesInLookupTable(PageLookupTableAddress
, FirstFreePageFromEnd
, PagesNeeded
);
107 FreePagesInLookupTable
-= PagesNeeded
;
108 MemPointer
= (PVOID
)(FirstFreePageFromEnd
* MM_PAGE_SIZE
);
110 if (MemorySize
< MM_PAGE_SIZE
)
112 SubAllocationPage
= MemPointer
;
113 SubAllocationRest
= MM_PAGE_SIZE
- MemorySize
;
118 IncrementAllocationCount();
119 DbgPrint((DPRINT_MEMORY
, "Allocated %d bytes (%d pages) of memory starting at page %d. AllocCount: %d\n", MemorySize
, PagesNeeded
, FirstFreePageFromEnd
, AllocationCount
));
120 DbgPrint((DPRINT_MEMORY
, "Memory allocation pointer: 0x%x\n", MemPointer
));
124 // Now return the pointer
128 PVOID
MmAllocateMemoryAtAddress(ULONG MemorySize
, PVOID DesiredAddress
)
131 ULONG StartPageNumber
;
136 DbgPrint((DPRINT_MEMORY
, "MmAllocateMemoryAtAddress() called for 0 bytes. Returning NULL.\n"));
137 UiMessageBoxCritical("Memory allocation failed: MmAllocateMemoryAtAddress() called for 0 bytes.");
141 // Find out how many blocks it will take to
142 // satisfy this allocation
143 PagesNeeded
= ROUND_UP(MemorySize
, MM_PAGE_SIZE
) / MM_PAGE_SIZE
;
145 // Get the starting page number
146 StartPageNumber
= MmGetPageNumberFromAddress(DesiredAddress
);
148 // If we don't have enough available mem
150 if (FreePagesInLookupTable
< PagesNeeded
)
152 DbgPrint((DPRINT_MEMORY
, "Memory allocation failed in MmAllocateMemoryAtAddress(). "
153 "Not enough free memory to allocate %d bytes (requesting %d pages but have only %d). "
154 "AllocationCount: %d\n", MemorySize
, PagesNeeded
, FreePagesInLookupTable
, AllocationCount
));
155 UiMessageBoxCritical("Memory allocation failed: out of memory.");
159 if (MmAreMemoryPagesAvailable(PageLookupTableAddress
, TotalPagesInLookupTable
, DesiredAddress
, PagesNeeded
) == FALSE
)
161 DbgPrint((DPRINT_MEMORY
, "Memory allocation failed in MmAllocateMemoryAtAddress(). "
162 "Not enough free memory to allocate %d bytes at address %p. AllocationCount: %d\n",
163 MemorySize
, DesiredAddress
, AllocationCount
));
165 // Don't tell this to user since caller should try to alloc this memory
166 // at a different address
167 //UiMessageBoxCritical("Memory allocation failed: out of memory.");
171 MmAllocatePagesInLookupTable(PageLookupTableAddress
, StartPageNumber
, PagesNeeded
);
173 FreePagesInLookupTable
-= PagesNeeded
;
174 MemPointer
= (PVOID
)(StartPageNumber
* MM_PAGE_SIZE
);
177 IncrementAllocationCount();
178 DbgPrint((DPRINT_MEMORY
, "Allocated %d bytes (%d pages) of memory starting at page %d. AllocCount: %d\n", MemorySize
, PagesNeeded
, StartPageNumber
, AllocationCount
));
179 DbgPrint((DPRINT_MEMORY
, "Memory allocation pointer: 0x%x\n", MemPointer
));
183 // Now return the pointer
187 PVOID
MmAllocateHighestMemoryBelowAddress(ULONG MemorySize
, PVOID DesiredAddress
)
190 ULONG FirstFreePageFromEnd
;
191 ULONG DesiredAddressPageNumber
;
196 DbgPrint((DPRINT_MEMORY
, "MmAllocateHighestMemoryBelowAddress() called for 0 bytes. Returning NULL.\n"));
197 UiMessageBoxCritical("Memory allocation failed: MmAllocateHighestMemoryBelowAddress() called for 0 bytes.");
201 // Find out how many blocks it will take to
202 // satisfy this allocation
203 PagesNeeded
= ROUND_UP(MemorySize
, MM_PAGE_SIZE
) / MM_PAGE_SIZE
;
205 // Get the page number for their desired address
206 DesiredAddressPageNumber
= (ULONG
)DesiredAddress
/ MM_PAGE_SIZE
;
208 // If we don't have enough available mem
210 if (FreePagesInLookupTable
< PagesNeeded
)
212 DbgPrint((DPRINT_MEMORY
, "Memory allocation failed in MmAllocateHighestMemoryBelowAddress(). Not enough free memory to allocate %d bytes. AllocationCount: %d\n", MemorySize
, AllocationCount
));
213 UiMessageBoxCritical("Memory allocation failed: out of memory.");
217 FirstFreePageFromEnd
= MmFindAvailablePagesBeforePage(PageLookupTableAddress
, TotalPagesInLookupTable
, PagesNeeded
, DesiredAddressPageNumber
);
219 if (FirstFreePageFromEnd
== 0)
221 DbgPrint((DPRINT_MEMORY
, "Memory allocation failed in MmAllocateHighestMemoryBelowAddress(). Not enough free memory to allocate %d bytes. AllocationCount: %d\n", MemorySize
, AllocationCount
));
222 UiMessageBoxCritical("Memory allocation failed: out of memory.");
226 MmAllocatePagesInLookupTable(PageLookupTableAddress
, FirstFreePageFromEnd
, PagesNeeded
);
228 FreePagesInLookupTable
-= PagesNeeded
;
229 MemPointer
= (PVOID
)(FirstFreePageFromEnd
* MM_PAGE_SIZE
);
232 IncrementAllocationCount();
233 DbgPrint((DPRINT_MEMORY
, "Allocated %d bytes (%d pages) of memory starting at page %d. AllocCount: %d\n", MemorySize
, PagesNeeded
, FirstFreePageFromEnd
, AllocationCount
));
234 DbgPrint((DPRINT_MEMORY
, "Memory allocation pointer: 0x%x\n", MemPointer
));
238 // Now return the pointer
242 VOID
MmFreeMemory(PVOID MemoryPointer
)
247 PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable
= (PPAGE_LOOKUP_TABLE_ITEM
)PageLookupTableAddress
;
251 // Make sure we didn't get a bogus pointer
252 if (MemoryPointer
>= (PVOID
)(TotalPagesInLookupTable
* MM_PAGE_SIZE
))
254 BugCheck((DPRINT_MEMORY
, "Bogus memory pointer (0x%x) passed to MmFreeMemory()\n", MemoryPointer
));
258 // Find out the page number of the first
259 // page of memory they allocated
260 PageNumber
= MmGetPageNumberFromAddress(MemoryPointer
);
261 PageCount
= RealPageLookupTable
[PageNumber
].PageAllocationLength
;
264 // Make sure we didn't get a bogus pointer
265 if ((PageCount
< 1) || (PageCount
> (TotalPagesInLookupTable
- PageNumber
)))
267 BugCheck((DPRINT_MEMORY
, "Invalid page count in lookup table. PageLookupTable[%d].PageAllocationLength = %d\n", PageNumber
, RealPageLookupTable
[PageNumber
].PageAllocationLength
));
270 // Loop through our array check all the pages
271 // to make sure they are allocated with a length of 0
272 for (Idx
=PageNumber
+1; Idx
<(PageNumber
+ PageCount
); Idx
++)
274 if ((RealPageLookupTable
[Idx
].PageAllocated
== LoaderFree
) ||
275 (RealPageLookupTable
[Idx
].PageAllocationLength
!= 0))
277 BugCheck((DPRINT_MEMORY
, "Invalid page entry in lookup table, PageAllocated should = 1 and PageAllocationLength should = 0 because this is not the first block in the run. PageLookupTable[%d].PageAllocated = %d PageLookupTable[%d].PageAllocationLength = %d\n", PageNumber
, RealPageLookupTable
[PageNumber
].PageAllocated
, PageNumber
, RealPageLookupTable
[PageNumber
].PageAllocationLength
));
283 /* If this allocation is only a single page, it could be a sub-allocated page.
284 * Just don't free it */
290 // Loop through our array and mark all the
292 for (Idx
=PageNumber
; Idx
<(PageNumber
+ PageCount
); Idx
++)
294 RealPageLookupTable
[Idx
].PageAllocated
= LoaderFree
;
295 RealPageLookupTable
[Idx
].PageAllocationLength
= 0;
298 FreePagesInLookupTable
+= PageCount
;
301 DecrementAllocationCount();
302 DbgPrint((DPRINT_MEMORY
, "Freed %d pages of memory starting at page %d. AllocationCount: %d\n", PageCount
, PageNumber
, AllocationCount
));
308 VOID
VerifyHeap(VOID
)
313 PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable
= (PPAGE_LOOKUP_TABLE_ITEM
)PageLookupTableAddress
;
315 if (DUMP_MEM_MAP_ON_VERIFY
)
317 DumpMemoryAllocMap();
320 // Loop through the array and verify that
321 // everything is kosher
322 for (Idx
=0; Idx
<TotalPagesInLookupTable
; Idx
++)
324 // Check if this block is allocated
325 if (RealPageLookupTable
[Idx
].PageAllocated
!= LoaderFree
)
327 // This is the first block in the run so it
328 // had better have a length that is within range
329 if ((RealPageLookupTable
[Idx
].PageAllocationLength
< 1) || (RealPageLookupTable
[Idx
].PageAllocationLength
> (TotalPagesInLookupTable
- Idx
)))
331 BugCheck((DPRINT_MEMORY
, "Allocation length out of range in heap table. PageLookupTable[Idx].PageAllocationLength = %d\n", RealPageLookupTable
[Idx
].PageAllocationLength
));
334 // Now go through and verify that the rest of
335 // this run has the blocks marked allocated
336 // with a length of zero but don't check the
337 // first one because we already did
338 Count
= RealPageLookupTable
[Idx
].PageAllocationLength
;
339 for (Idx2
=1; Idx2
<Count
; Idx2
++)
341 // Make sure it's allocated
342 if (RealPageLookupTable
[Idx
+ Idx2
].PageAllocated
== LoaderFree
)
344 BugCheck((DPRINT_MEMORY
, "Lookup table indicates hole in memory allocation. RealPageLookupTable[Idx + Idx2].PageAllocated == 0\n"));
347 // Make sure the length is zero
348 if (RealPageLookupTable
[Idx
+ Idx2
].PageAllocationLength
!= 0)
350 BugCheck((DPRINT_MEMORY
, "Allocation chain has non-zero value in non-first block in lookup table. RealPageLookupTable[Idx + Idx2].PageAllocationLength != 0\n"));
354 // Move on to the next run
359 // Nope, not allocated so make sure the length is zero
360 if (RealPageLookupTable
[Idx
].PageAllocationLength
!= 0)
362 BugCheck((DPRINT_MEMORY
, "Free block is start of memory allocation. RealPageLookupTable[Idx].PageAllocationLength != 0\n"));
368 VOID
DumpMemoryAllocMap(VOID
)
371 PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable
= (PPAGE_LOOKUP_TABLE_ITEM
)PageLookupTableAddress
;
373 DbgPrint((DPRINT_MEMORY
, "----------- Memory Allocation Bitmap -----------\n"));
375 for (Idx
=0; Idx
<TotalPagesInLookupTable
; Idx
++)
379 DbgPrint((DPRINT_MEMORY
, "\n"));
380 DbgPrint((DPRINT_MEMORY
, "%x:\t", (Idx
* MM_PAGE_SIZE
)));
382 else if ((Idx
% 4) == 0)
384 DbgPrint((DPRINT_MEMORY
, " "));
387 switch (RealPageLookupTable
[Idx
].PageAllocated
)
390 DbgPrint((DPRINT_MEMORY
, "*"));
393 DbgPrint((DPRINT_MEMORY
, "-"));
395 case LoaderLoadedProgram
:
396 DbgPrint((DPRINT_MEMORY
, "O"));
398 case LoaderFirmwareTemporary
:
399 DbgPrint((DPRINT_MEMORY
, "T"));
401 case LoaderFirmwarePermanent
:
402 DbgPrint((DPRINT_MEMORY
, "P"));
404 case LoaderOsloaderHeap
:
405 DbgPrint((DPRINT_MEMORY
, "H"));
407 case LoaderOsloaderStack
:
408 DbgPrint((DPRINT_MEMORY
, "S"));
410 case LoaderSystemCode
:
411 DbgPrint((DPRINT_MEMORY
, "K"));
414 DbgPrint((DPRINT_MEMORY
, "L"));
416 case LoaderBootDriver
:
417 DbgPrint((DPRINT_MEMORY
, "B"));
419 case LoaderStartupPcrPage
:
420 DbgPrint((DPRINT_MEMORY
, "G"));
422 case LoaderRegistryData
:
423 DbgPrint((DPRINT_MEMORY
, "R"));
425 case LoaderMemoryData
:
426 DbgPrint((DPRINT_MEMORY
, "M"));
429 DbgPrint((DPRINT_MEMORY
, "N"));
431 case LoaderSpecialMemory
:
432 DbgPrint((DPRINT_MEMORY
, "C"));
435 DbgPrint((DPRINT_MEMORY
, "?"));
440 DbgPrint((DPRINT_MEMORY
, "\n"));
443 VOID
IncrementAllocationCount(VOID
)
448 VOID
DecrementAllocationCount(VOID
)
453 VOID
MemAllocTest(VOID
)
461 MemPtr1
= MmAllocateMemory(4096);
462 printf("MemPtr1: 0x%x\n", (int)MemPtr1
);
464 MemPtr2
= MmAllocateMemory(4096);
465 printf("MemPtr2: 0x%x\n", (int)MemPtr2
);
467 MemPtr3
= MmAllocateMemory(4096);
468 printf("MemPtr3: 0x%x\n", (int)MemPtr3
);
469 DumpMemoryAllocMap();
473 MmFreeMemory(MemPtr2
);
476 MemPtr4
= MmAllocateMemory(2048);
477 printf("MemPtr4: 0x%x\n", (int)MemPtr4
);
479 MemPtr5
= MmAllocateMemory(4096);
480 printf("MemPtr5: 0x%x\n", (int)MemPtr5
);
485 ULONG
GetSystemMemorySize(VOID
)
487 return (TotalPagesInLookupTable
* MM_PAGE_SIZE
);
490 PPAGE_LOOKUP_TABLE_ITEM
MmGetMemoryMap(ULONG
*NoEntries
)
492 PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable
= (PPAGE_LOOKUP_TABLE_ITEM
)PageLookupTableAddress
;
494 *NoEntries
= TotalPagesInLookupTable
;
496 return RealPageLookupTable
;