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 PVOID
MmAllocateMemory(ULONG MemorySize
)
58 ULONG FirstFreePageFromEnd
;
63 DbgPrint((DPRINT_MEMORY
, "MmAllocateMemory() called for 0 bytes. Returning NULL.\n"));
64 UiMessageBoxCritical("Memory allocation failed: MmAllocateMemory() called for 0 bytes.");
68 MemorySize
= ROUND_UP(MemorySize
, 4);
69 if (MemorySize
<= SubAllocationRest
)
71 MemPointer
= (PVOID
)((ULONG_PTR
)SubAllocationPage
+ MM_PAGE_SIZE
- SubAllocationRest
);
72 SubAllocationRest
-= MemorySize
;
76 // Find out how many blocks it will take to
77 // satisfy this allocation
78 PagesNeeded
= ROUND_UP(MemorySize
, MM_PAGE_SIZE
) / MM_PAGE_SIZE
;
80 // If we don't have enough available mem
82 if (FreePagesInLookupTable
< PagesNeeded
)
84 DbgPrint((DPRINT_MEMORY
, "Memory allocation failed. Not enough free memory to allocate %d bytes. AllocationCount: %d\n", MemorySize
, AllocationCount
));
85 UiMessageBoxCritical("Memory allocation failed: out of memory.");
89 FirstFreePageFromEnd
= MmFindAvailablePagesFromEnd(PageLookupTableAddress
, TotalPagesInLookupTable
, PagesNeeded
);
91 if (FirstFreePageFromEnd
== 0)
93 DbgPrint((DPRINT_MEMORY
, "Memory allocation failed. Not enough free memory to allocate %d bytes. AllocationCount: %d\n", MemorySize
, AllocationCount
));
94 UiMessageBoxCritical("Memory allocation failed: out of memory.");
98 MmAllocatePagesInLookupTable(PageLookupTableAddress
, FirstFreePageFromEnd
, PagesNeeded
);
100 FreePagesInLookupTable
-= PagesNeeded
;
101 MemPointer
= (PVOID
)(FirstFreePageFromEnd
* MM_PAGE_SIZE
);
103 if (MemorySize
< MM_PAGE_SIZE
)
105 SubAllocationPage
= MemPointer
;
106 SubAllocationRest
= MM_PAGE_SIZE
- MemorySize
;
111 IncrementAllocationCount();
112 DbgPrint((DPRINT_MEMORY
, "Allocated %d bytes (%d pages) of memory starting at page %d. AllocCount: %d\n", MemorySize
, PagesNeeded
, FirstFreePageFromEnd
, AllocationCount
));
113 DbgPrint((DPRINT_MEMORY
, "Memory allocation pointer: 0x%x\n", MemPointer
));
117 // Now return the pointer
121 PVOID
MmAllocateMemoryAtAddress(ULONG MemorySize
, PVOID DesiredAddress
)
124 ULONG StartPageNumber
;
129 DbgPrint((DPRINT_MEMORY
, "MmAllocateMemoryAtAddress() called for 0 bytes. Returning NULL.\n"));
130 UiMessageBoxCritical("Memory allocation failed: MmAllocateMemoryAtAddress() called for 0 bytes.");
134 // Find out how many blocks it will take to
135 // satisfy this allocation
136 PagesNeeded
= ROUND_UP(MemorySize
, MM_PAGE_SIZE
) / MM_PAGE_SIZE
;
138 // Get the starting page number
139 StartPageNumber
= MmGetPageNumberFromAddress(DesiredAddress
);
141 // If we don't have enough available mem
143 if (FreePagesInLookupTable
< PagesNeeded
)
145 DbgPrint((DPRINT_MEMORY
, "Memory allocation failed. Not enough free memory to allocate %d bytes. AllocationCount: %d\n", MemorySize
, AllocationCount
));
146 UiMessageBoxCritical("Memory allocation failed: out of memory.");
150 if (MmAreMemoryPagesAvailable(PageLookupTableAddress
, TotalPagesInLookupTable
, DesiredAddress
, PagesNeeded
) == FALSE
)
152 DbgPrint((DPRINT_MEMORY
, "Memory allocation failed. Not enough free memory to allocate %d bytes. AllocationCount: %d\n", MemorySize
, AllocationCount
));
153 UiMessageBoxCritical("Memory allocation failed: out of memory.");
157 MmAllocatePagesInLookupTable(PageLookupTableAddress
, StartPageNumber
, PagesNeeded
);
159 FreePagesInLookupTable
-= PagesNeeded
;
160 MemPointer
= (PVOID
)(StartPageNumber
* MM_PAGE_SIZE
);
163 IncrementAllocationCount();
164 DbgPrint((DPRINT_MEMORY
, "Allocated %d bytes (%d pages) of memory starting at page %d. AllocCount: %d\n", MemorySize
, PagesNeeded
, StartPageNumber
, AllocationCount
));
165 DbgPrint((DPRINT_MEMORY
, "Memory allocation pointer: 0x%x\n", MemPointer
));
169 // Now return the pointer
173 PVOID
MmAllocateHighestMemoryBelowAddress(ULONG MemorySize
, PVOID DesiredAddress
)
176 ULONG FirstFreePageFromEnd
;
177 ULONG DesiredAddressPageNumber
;
182 DbgPrint((DPRINT_MEMORY
, "MmAllocateHighestMemoryBelowAddress() called for 0 bytes. Returning NULL.\n"));
183 UiMessageBoxCritical("Memory allocation failed: MmAllocateHighestMemoryBelowAddress() called for 0 bytes.");
187 // Find out how many blocks it will take to
188 // satisfy this allocation
189 PagesNeeded
= ROUND_UP(MemorySize
, MM_PAGE_SIZE
) / MM_PAGE_SIZE
;
191 // Get the page number for their desired address
192 DesiredAddressPageNumber
= (ULONG
)DesiredAddress
/ MM_PAGE_SIZE
;
194 // If we don't have enough available mem
196 if (FreePagesInLookupTable
< PagesNeeded
)
198 DbgPrint((DPRINT_MEMORY
, "Memory allocation failed. Not enough free memory to allocate %d bytes. AllocationCount: %d\n", MemorySize
, AllocationCount
));
199 UiMessageBoxCritical("Memory allocation failed: out of memory.");
203 FirstFreePageFromEnd
= MmFindAvailablePagesBeforePage(PageLookupTableAddress
, TotalPagesInLookupTable
, PagesNeeded
, DesiredAddressPageNumber
);
205 if (FirstFreePageFromEnd
== 0)
207 DbgPrint((DPRINT_MEMORY
, "Memory allocation failed. Not enough free memory to allocate %d bytes. AllocationCount: %d\n", MemorySize
, AllocationCount
));
208 UiMessageBoxCritical("Memory allocation failed: out of memory.");
212 MmAllocatePagesInLookupTable(PageLookupTableAddress
, FirstFreePageFromEnd
, PagesNeeded
);
214 FreePagesInLookupTable
-= PagesNeeded
;
215 MemPointer
= (PVOID
)(FirstFreePageFromEnd
* MM_PAGE_SIZE
);
218 IncrementAllocationCount();
219 DbgPrint((DPRINT_MEMORY
, "Allocated %d bytes (%d pages) of memory starting at page %d. AllocCount: %d\n", MemorySize
, PagesNeeded
, FirstFreePageFromEnd
, AllocationCount
));
220 DbgPrint((DPRINT_MEMORY
, "Memory allocation pointer: 0x%x\n", MemPointer
));
224 // Now return the pointer
228 VOID
MmFreeMemory(PVOID MemoryPointer
)
233 PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable
= (PPAGE_LOOKUP_TABLE_ITEM
)PageLookupTableAddress
;
237 // Make sure we didn't get a bogus pointer
238 if (MemoryPointer
>= (PVOID
)(TotalPagesInLookupTable
* MM_PAGE_SIZE
))
240 BugCheck((DPRINT_MEMORY
, "Bogus memory pointer (0x%x) passed to MmFreeMemory()\n", MemoryPointer
));
244 // Find out the page number of the first
245 // page of memory they allocated
246 PageNumber
= MmGetPageNumberFromAddress(MemoryPointer
);
247 PageCount
= RealPageLookupTable
[PageNumber
].PageAllocationLength
;
250 // Make sure we didn't get a bogus pointer
251 if ((PageCount
< 1) || (PageCount
> (TotalPagesInLookupTable
- PageNumber
)))
253 BugCheck((DPRINT_MEMORY
, "Invalid page count in lookup table. PageLookupTable[%d].PageAllocationLength = %d\n", PageNumber
, RealPageLookupTable
[PageNumber
].PageAllocationLength
));
256 // Loop through our array check all the pages
257 // to make sure they are allocated with a length of 0
258 for (Idx
=PageNumber
+1; Idx
<(PageNumber
+ PageCount
); Idx
++)
260 if ((RealPageLookupTable
[Idx
].PageAllocated
!= 1) ||
261 (RealPageLookupTable
[Idx
].PageAllocationLength
!= 0))
263 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
));
269 /* If this allocation is only a single page, it could be a sub-allocated page.
270 * Just don't free it */
276 // Loop through our array and mark all the
278 for (Idx
=PageNumber
; Idx
<(PageNumber
+ PageCount
); Idx
++)
280 RealPageLookupTable
[Idx
].PageAllocated
= 0;
281 RealPageLookupTable
[Idx
].PageAllocationLength
= 0;
284 FreePagesInLookupTable
+= PageCount
;
287 DecrementAllocationCount();
288 DbgPrint((DPRINT_MEMORY
, "Freed %d pages of memory starting at page %d. AllocationCount: %d\n", PageCount
, PageNumber
, AllocationCount
));
294 VOID
VerifyHeap(VOID
)
299 PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable
= (PPAGE_LOOKUP_TABLE_ITEM
)PageLookupTableAddress
;
301 if (DUMP_MEM_MAP_ON_VERIFY
)
303 DumpMemoryAllocMap();
306 // Loop through the array and verify that
307 // everything is kosher
308 for (Idx
=0; Idx
<TotalPagesInLookupTable
; Idx
++)
310 // Check if this block is allocated
311 if (RealPageLookupTable
[Idx
].PageAllocated
!= 0)
313 // This is the first block in the run so it
314 // had better have a length that is within range
315 if ((RealPageLookupTable
[Idx
].PageAllocationLength
< 1) || (RealPageLookupTable
[Idx
].PageAllocationLength
> (TotalPagesInLookupTable
- Idx
)))
317 BugCheck((DPRINT_MEMORY
, "Allocation length out of range in heap table. PageLookupTable[Idx].PageAllocationLength = %d\n", RealPageLookupTable
[Idx
].PageAllocationLength
));
320 // Now go through and verify that the rest of
321 // this run has the blocks marked allocated
322 // with a length of zero but don't check the
323 // first one because we already did
324 Count
= RealPageLookupTable
[Idx
].PageAllocationLength
;
325 for (Idx2
=1; Idx2
<Count
; Idx2
++)
327 // Make sure it's allocated
328 if (RealPageLookupTable
[Idx
+ Idx2
].PageAllocated
== 0)
330 BugCheck((DPRINT_MEMORY
, "Lookup table indicates hole in memory allocation. RealPageLookupTable[Idx + Idx2].PageAllocated == 0\n"));
333 // Make sure the length is zero
334 if (RealPageLookupTable
[Idx
+ Idx2
].PageAllocationLength
!= 0)
336 BugCheck((DPRINT_MEMORY
, "Allocation chain has non-zero value in non-first block in lookup table. RealPageLookupTable[Idx + Idx2].PageAllocationLength != 0\n"));
340 // Move on to the next run
345 // Nope, not allocated so make sure the length is zero
346 if (RealPageLookupTable
[Idx
].PageAllocationLength
!= 0)
348 BugCheck((DPRINT_MEMORY
, "Free block is start of memory allocation. RealPageLookupTable[Idx].PageAllocationLength != 0\n"));
354 VOID
DumpMemoryAllocMap(VOID
)
357 PPAGE_LOOKUP_TABLE_ITEM RealPageLookupTable
= (PPAGE_LOOKUP_TABLE_ITEM
)PageLookupTableAddress
;
359 DbgPrint((DPRINT_MEMORY
, "----------- Memory Allocation Bitmap -----------\n"));
361 for (Idx
=0; Idx
<TotalPagesInLookupTable
; Idx
++)
365 DbgPrint((DPRINT_MEMORY
, "\n"));
366 DbgPrint((DPRINT_MEMORY
, "%x:\t", (Idx
* MM_PAGE_SIZE
)));
368 else if ((Idx
% 4) == 0)
370 DbgPrint((DPRINT_MEMORY
, " "));
373 switch (RealPageLookupTable
[Idx
].PageAllocated
)
376 DbgPrint((DPRINT_MEMORY
, "*"));
379 DbgPrint((DPRINT_MEMORY
, "A"));
381 case MEMTYPE_RESERVED
:
382 DbgPrint((DPRINT_MEMORY
, "R"));
384 case MEMTYPE_ACPI_RECLAIM
:
385 DbgPrint((DPRINT_MEMORY
, "M"));
387 case MEMTYPE_ACPI_NVS
:
388 DbgPrint((DPRINT_MEMORY
, "N"));
391 DbgPrint((DPRINT_MEMORY
, "X"));
396 DbgPrint((DPRINT_MEMORY
, "\n"));
399 VOID
IncrementAllocationCount(VOID
)
404 VOID
DecrementAllocationCount(VOID
)
409 VOID
MemAllocTest(VOID
)
417 MemPtr1
= MmAllocateMemory(4096);
418 printf("MemPtr1: 0x%x\n", (int)MemPtr1
);
420 MemPtr2
= MmAllocateMemory(4096);
421 printf("MemPtr2: 0x%x\n", (int)MemPtr2
);
423 MemPtr3
= MmAllocateMemory(4096);
424 printf("MemPtr3: 0x%x\n", (int)MemPtr3
);
425 DumpMemoryAllocMap();
429 MmFreeMemory(MemPtr2
);
432 MemPtr4
= MmAllocateMemory(2048);
433 printf("MemPtr4: 0x%x\n", (int)MemPtr4
);
435 MemPtr5
= MmAllocateMemory(4096);
436 printf("MemPtr5: 0x%x\n", (int)MemPtr5
);
441 ULONG
GetSystemMemorySize(VOID
)
443 return (TotalPagesInLookupTable
* MM_PAGE_SIZE
);