diff --git a/reactos/boot/freeldr/freeldr/CMakeLists.txt b/reactos/boot/freeldr/freeldr/CMakeLists.txt
index c538cf1e1c1..90e17f8d273 100644
--- a/reactos/boot/freeldr/freeldr/CMakeLists.txt
+++ b/reactos/boot/freeldr/freeldr/CMakeLists.txt
@@ -54,7 +54,7 @@ list(APPEND FREELDR_COMMON_SOURCE
inifile/parse.c
mm/meminit.c
mm/mm.c
- mm/heap.c
+ mm/heap_new.c
reactos/registry.c
reactos/arcname.c
reactos/archwsup.c
@@ -155,7 +155,6 @@ endif()
list(APPEND FREELDR_BASE_SOURCE
bootmgr.c # This file is compiled with custom definitions
freeldr.c
- rtl/bget.c
rtl/libsupp.c)
if (NOT MSVC)
diff --git a/reactos/boot/freeldr/freeldr/freeldr_base.rbuild b/reactos/boot/freeldr/freeldr/freeldr_base.rbuild
index 3be3b3408fc..c9e3f1c0fa5 100644
--- a/reactos/boot/freeldr/freeldr/freeldr_base.rbuild
+++ b/reactos/boot/freeldr/freeldr/freeldr_base.rbuild
@@ -40,7 +40,7 @@
parse.c
- heap.c
+ heap_new.c
meminit.c
mm.c
@@ -51,7 +51,6 @@
binhive.c
- bget.c
libsupp.c
diff --git a/reactos/boot/freeldr/freeldr/mm/heap_new.c b/reactos/boot/freeldr/freeldr/mm/heap_new.c
index 07a083bad69..716931ec466 100644
--- a/reactos/boot/freeldr/freeldr/mm/heap_new.c
+++ b/reactos/boot/freeldr/freeldr/mm/heap_new.c
@@ -168,7 +168,7 @@ HeapRelease(
if (Block->Size == 0)
{
/* Align the end address up to cover the end of the heap */
- EndAddress = ALIGN_DOWN_POINTER_BY(Block->Data, PAGE_SIZE);
+ EndAddress = ALIGN_UP_POINTER_BY(Block->Data, PAGE_SIZE);
}
else
{
@@ -176,14 +176,19 @@ HeapRelease(
EndAddress = ALIGN_DOWN_POINTER_BY(Block->Data, PAGE_SIZE);
}
- FreePages = (PFN_COUNT)((EndAddress - StartAddress) / MM_PAGE_SIZE);
- AllFreePages += FreePages;
+ /* Check if we have free pages */
+ if (EndAddress > StartAddress)
+ {
+ /* Calculate the size of the free region in pages */
+ FreePages = (PFN_COUNT)((EndAddress - StartAddress) / MM_PAGE_SIZE);
+ AllFreePages += FreePages;
- /* Now mark the pages free */
- MmMarkPagesInLookupTable(PageLookupTableAddress,
- (ULONG_PTR)StartAddress / MM_PAGE_SIZE,
- FreePages,
- LoaderFree);
+ /* Now mark the pages free */
+ MmMarkPagesInLookupTable(PageLookupTableAddress,
+ (ULONG_PTR)StartAddress / MM_PAGE_SIZE,
+ FreePages,
+ LoaderFree);
+ }
/* bail out, if it was the last block */
if (Block->Size == 0) break;
@@ -460,8 +465,6 @@ MmHeapFree(PVOID MemoryPointer)
HeapFree(FrLdrDefaultHeap, MemoryPointer, 'pHmM');
}
-
-#undef ExAllocatePoolWithTag
PVOID
NTAPI
ExAllocatePoolWithTag(
@@ -481,7 +484,6 @@ ExAllocatePool(
return HeapAllocate(FrLdrDefaultHeap, NumberOfBytes, 0);
}
-#undef ExFreePool
VOID
NTAPI
ExFreePool(
@@ -490,7 +492,6 @@ ExFreePool(
HeapFree(FrLdrDefaultHeap, P, 0);
}
-#undef ExFreePoolWithTag
VOID
NTAPI
ExFreePoolWithTag(
diff --git a/reactos/boot/freeldr/freeldr/windows/wlmemory.c b/reactos/boot/freeldr/freeldr/windows/wlmemory.c
index f26db80c0a6..1f04319e731 100644
--- a/reactos/boot/freeldr/freeldr/windows/wlmemory.c
+++ b/reactos/boot/freeldr/freeldr/windows/wlmemory.c
@@ -203,7 +203,7 @@ WinLdrSetupMemoryLayout(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock)
//BOOLEAN Status;
/* Cleanup heap */
- //HeapCleanupAll();
+ HeapCleanupAll();
//
// Creating a suitable memory map for Windows can be tricky, so let's