17cda07dca50855b4d2363efaa8e22ca4d32f382
[reactos.git] / freeldr / freeldr / mm / init.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2002 Brian Palmer <brianp@sginet.com>
4 *
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.
9 *
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.
14 *
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.
18 */
19
20 #include <freeldr.h>
21 #include <arch.h>
22 #include <mm.h>
23 #include "mem.h"
24 #include <rtl.h>
25 #include <debug.h>
26 #include <ui.h>
27
28
29 ULONG RealFreeLoaderModuleEnd = 0;
30
31 PVOID HeapBaseAddress = NULL;
32 ULONG HeapLengthInBytes = 0;
33 ULONG HeapMemBlockCount = 0;
34 PMEMBLOCK HeapMemBlockArray = NULL;
35
36 VOID InitMemoryManager(VOID)
37 {
38 ULONG MemBlocks;
39 ULONG BaseAddress;
40 ULONG Length;
41
42 // Round up to the next page of memory
43 RealFreeLoaderModuleEnd = ((FreeLoaderModuleEnd + 4095) / 4096) * 4096;
44 BaseAddress = RealFreeLoaderModuleEnd;
45 Length = (MAXLOWMEMADDR - RealFreeLoaderModuleEnd);
46
47 DbgPrint((DPRINT_MEMORY, "Initializing Memory Manager.\n"));
48 DbgPrint((DPRINT_MEMORY, "Conventional memory size: %d KB\n", GetConventionalMemorySize()));
49 DbgPrint((DPRINT_MEMORY, "Low memory map:\n"));
50 DbgPrint((DPRINT_MEMORY, "00000\t1000\tReserved\n"));
51 DbgPrint((DPRINT_MEMORY, "01000\t6000\t16-bit stack\n"));
52 DbgPrint((DPRINT_MEMORY, "07000\t1000\tUnused\n"));
53 DbgPrint((DPRINT_MEMORY, "08000\t%x\tFreeLoader program code\n", (RealFreeLoaderModuleEnd - 0x8000)));
54 DbgPrint((DPRINT_MEMORY, "%x\t%x\tLow memory heap\n", BaseAddress, Length));
55 DbgPrint((DPRINT_MEMORY, "78000\t8000\t32-bit stack\n"));
56 DbgPrint((DPRINT_MEMORY, "80000\t10000\tFile system read buffer\n"));
57 DbgPrint((DPRINT_MEMORY, "90000\t10000\tDisk read buffer\n"));
58 DbgPrint((DPRINT_MEMORY, "A0000\t60000\tReserved\n"));
59
60 // Calculate how many memory blocks we have
61 MemBlocks = (Length / MEM_BLOCK_SIZE);
62
63 // Adjust the heap length so we can reserve
64 // enough storage space for the MEMBLOCK array
65 Length -= (MemBlocks * sizeof(MEMBLOCK));
66
67 // Initialize our tracking variables
68 HeapBaseAddress = BaseAddress;
69 HeapLengthInBytes = Length;
70 HeapMemBlockCount = (HeapLengthInBytes / MEM_BLOCK_SIZE);
71 HeapMemBlockArray = (PMEMBLOCK)(HeapBaseAddress + HeapLengthInBytes);
72
73 // Clear the memory
74 RtlZeroMemory(HeapBaseAddress, HeapLengthInBytes);
75 RtlZeroMemory(HeapMemBlockArray, (HeapMemBlockCount * sizeof(MEMBLOCK)));
76
77 #ifdef DEBUG
78 DbgPrint((DPRINT_MEMORY, "Memory Manager initialized. BaseAddress = 0x%x Length = 0x%x. %d blocks in heap.\n", BaseAddress, Length, HeapMemBlockCount));
79 //MemAllocTest();
80 #endif
81 }