[freeldr] Add some more memory management functions
[reactos.git] / reactos / boot / freeldr / freeldr / include / mm.h
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2003 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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #pragma once
21
22 typedef enum
23 {
24 BiosMemoryUsable=1,
25 BiosMemoryReserved,
26 BiosMemoryAcpiReclaim,
27 BiosMemoryAcpiNvs
28 } BIOS_MEMORY_TYPE;
29
30 #include <pshpack1.h>
31 typedef struct
32 {
33 ULONGLONG BaseAddress;
34 ULONGLONG Length;
35 ULONG Type;
36 ULONG Reserved;
37 } BIOS_MEMORY_MAP, *PBIOS_MEMORY_MAP;
38 #include <poppack.h>
39
40 #if defined(__i386__) || defined(_PPC_) || defined(_MIPS_) || defined(_ARM_)
41
42 #define MM_PAGE_SIZE 4096
43 #define MM_PAGE_MASK 0xFFF
44 #define MM_PAGE_SHIFT 12
45
46 #define MM_SIZE_TO_PAGES(a) \
47 ( ((a) >> MM_PAGE_SHIFT) + ((a) & MM_PAGE_MASK ? 1 : 0) )
48
49 #endif // defined __i386__ or _PPC_ or _MIPS_
50
51 #if defined (_AMD64_)
52
53 #define MM_PAGE_SIZE 4096
54 #define MM_PAGE_MASK 0xFFF
55 #define MM_PAGE_SHIFT 12
56
57 #define MM_SIZE_TO_PAGES(a) \
58 ( ((a) >> MM_PAGE_SHIFT) + ((a) & MM_PAGE_MASK ? 1 : 0) )
59
60 #endif
61
62 // HEAP and STACK size
63 #define HEAP_PAGES 0x400
64 #define STACK_PAGES 0x00
65
66 #include <pshpack1.h>
67 typedef struct
68 {
69 TYPE_OF_MEMORY PageAllocated; // Type of allocated memory (LoaderFree if this memory is free)
70 ULONG PageAllocationLength; // Number of pages allocated (or zero if this isn't the first page in the chain)
71 } PAGE_LOOKUP_TABLE_ITEM, *PPAGE_LOOKUP_TABLE_ITEM;
72 #include <poppack.h>
73
74 //
75 // Define this to 1 if you want the entire contents
76 // of the memory allocation bitmap displayed
77 // when a chunk is allocated or freed
78 //
79 #define DUMP_MEM_MAP_ON_VERIFY 0
80
81
82
83 extern PVOID PageLookupTableAddress;
84 extern ULONG TotalPagesInLookupTable;
85 extern ULONG FreePagesInLookupTable;
86 extern ULONG LastFreePageHint;
87
88 #if DBG
89 PCSTR MmGetSystemMemoryMapTypeString(MEMORY_TYPE Type);
90 #endif
91
92 ULONG MmGetPageNumberFromAddress(PVOID Address); // Returns the page number that contains a linear address
93 ULONG MmGetAddressablePageCountIncludingHoles(VOID); // Returns the count of addressable pages from address zero including any memory holes and reserved memory regions
94 PVOID MmFindLocationForPageLookupTable(ULONG TotalPageCount); // Returns the address for a memory chunk big enough to hold the page lookup table (starts search from end of memory)
95 VOID MmInitPageLookupTable(PVOID PageLookupTable, ULONG TotalPageCount); // Inits the page lookup table according to the memory types in the memory map
96 VOID MmMarkPagesInLookupTable(PVOID PageLookupTable, ULONG StartPage, ULONG PageCount, TYPE_OF_MEMORY PageAllocated); // Marks the specified pages as allocated or free in the lookup table
97 VOID MmAllocatePagesInLookupTable(PVOID PageLookupTable, ULONG StartPage, ULONG PageCount, TYPE_OF_MEMORY MemoryType); // Allocates the specified pages in the lookup table
98 ULONG MmCountFreePagesInLookupTable(PVOID PageLookupTable, ULONG TotalPageCount); // Returns the number of free pages in the lookup table
99 ULONG MmFindAvailablePages(PVOID PageLookupTable, ULONG TotalPageCount, ULONG PagesNeeded, BOOLEAN FromEnd); // Returns the page number of the first available page range from the beginning or end of memory
100 ULONG MmFindAvailablePagesBeforePage(PVOID PageLookupTable, ULONG TotalPageCount, ULONG PagesNeeded, ULONG LastPage); // Returns the page number of the first available page range before the specified page
101 VOID MmUpdateLastFreePageHint(PVOID PageLookupTable, ULONG TotalPageCount); // Sets the LastFreePageHint to the last usable page of memory
102 BOOLEAN MmAreMemoryPagesAvailable(PVOID PageLookupTable, ULONG TotalPageCount, PVOID PageAddress, ULONG PageCount); // Returns TRUE if the specified pages of memory are available, otherwise FALSE
103 VOID MmSetMemoryType(PVOID MemoryAddress, ULONG MemorySize, TYPE_OF_MEMORY NewType); // Use with EXTREME caution!
104
105 PPAGE_LOOKUP_TABLE_ITEM MmGetMemoryMap(ULONG *NoEntries); // Returns a pointer to the memory mapping table and a number of entries in it
106
107
108 //BOOLEAN MmInitializeMemoryManager(ULONG LowMemoryStart, ULONG LowMemoryLength);
109 BOOLEAN MmInitializeMemoryManager(VOID);
110 VOID MmInitializeHeap(PVOID PageLookupTable);
111 PVOID MmAllocateMemory(ULONG MemorySize);
112 PVOID MmAllocateMemoryWithType(ULONG MemorySize, TYPE_OF_MEMORY MemoryType);
113 VOID MmFreeMemory(PVOID MemoryPointer);
114 PVOID MmAllocateMemoryAtAddress(ULONG MemorySize, PVOID DesiredAddress, TYPE_OF_MEMORY MemoryType);
115 PVOID MmAllocateHighestMemoryBelowAddress(ULONG MemorySize, PVOID DesiredAddress, TYPE_OF_MEMORY MemoryType);
116
117 PVOID MmHeapAlloc(ULONG MemorySize);
118 VOID MmHeapFree(PVOID MemoryPointer);
119
120 #define ExAllocatePool(pool, size) MmHeapAlloc(size)
121 #define ExAllocatePoolWithTag(pool, size, tag) MmHeapAlloc(size)
122 #define ExFreePool(p) MmHeapFree(p)