Synchronize with trunk's revision r57599.
[reactos.git] / 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 struct _FREELDR_MEMORY_DESCRIPTOR
23 {
24 TYPE_OF_MEMORY MemoryType;
25 PFN_NUMBER BasePage;
26 PFN_NUMBER PageCount;
27 } FREELDR_MEMORY_DESCRIPTOR, *PFREELDR_MEMORY_DESCRIPTOR;
28
29
30 #if defined(__i386__) || defined(_PPC_) || defined(_MIPS_) || defined(_ARM_)
31
32 #define MM_PAGE_SIZE 4096
33 #define MM_PAGE_MASK 0xFFF
34 #define MM_PAGE_SHIFT 12
35 #define MM_MAX_PAGE 0xFFFFF
36
37 #define MM_SIZE_TO_PAGES(a) \
38 ( ((a) >> MM_PAGE_SHIFT) + ((a) & MM_PAGE_MASK ? 1 : 0) )
39
40 #endif // defined __i386__ or _PPC_ or _MIPS_
41
42 #if defined (_AMD64_)
43
44 #define MM_PAGE_SIZE 4096
45 #define MM_PAGE_MASK 0xFFF
46 #define MM_PAGE_SHIFT 12
47 // FIXME: freeldr implementation uses ULONG for page numbers
48 #define MM_MAX_PAGE 0xFFFFFFFFFFFFF
49
50 #define MM_SIZE_TO_PAGES(a) \
51 ( ((a) >> MM_PAGE_SHIFT) + ((a) & MM_PAGE_MASK ? 1 : 0) )
52
53 #endif
54
55 // HEAP and STACK size
56 #define HEAP_PAGES 0x400
57 #define STACK_PAGES 0x00
58
59 #include <pshpack1.h>
60 typedef struct
61 {
62 TYPE_OF_MEMORY PageAllocated; // Type of allocated memory (LoaderFree if this memory is free)
63 PFN_NUMBER PageAllocationLength; // Number of pages allocated (or zero if this isn't the first page in the chain)
64 } PAGE_LOOKUP_TABLE_ITEM, *PPAGE_LOOKUP_TABLE_ITEM;
65 #include <poppack.h>
66
67 //
68 // Define this to 1 if you want the entire contents
69 // of the memory allocation bitmap displayed
70 // when a chunk is allocated or freed
71 //
72 #define DUMP_MEM_MAP_ON_VERIFY 0
73
74 extern PVOID PageLookupTableAddress;
75 extern PFN_NUMBER TotalPagesInLookupTable;
76 extern PFN_NUMBER FreePagesInLookupTable;
77 extern PFN_NUMBER LastFreePageHint;
78
79 #if DBG
80 PCSTR MmGetSystemMemoryMapTypeString(TYPE_OF_MEMORY Type);
81 #endif
82
83 PFN_NUMBER MmGetPageNumberFromAddress(PVOID Address); // Returns the page number that contains a linear address
84 PFN_NUMBER MmGetAddressablePageCountIncludingHoles(VOID); // Returns the count of addressable pages from address zero including any memory holes and reserved memory regions
85 PVOID MmFindLocationForPageLookupTable(PFN_NUMBER TotalPageCount); // Returns the address for a memory chunk big enough to hold the page lookup table (starts search from end of memory)
86 VOID MmInitPageLookupTable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount); // Inits the page lookup table according to the memory types in the memory map
87 VOID MmMarkPagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER StartPage, PFN_NUMBER PageCount, TYPE_OF_MEMORY PageAllocated); // Marks the specified pages as allocated or free in the lookup table
88 VOID MmAllocatePagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER StartPage, PFN_NUMBER PageCount, TYPE_OF_MEMORY MemoryType); // Allocates the specified pages in the lookup table
89 PFN_NUMBER MmCountFreePagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount); // Returns the number of free pages in the lookup table
90 PFN_NUMBER MmFindAvailablePages(PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PFN_NUMBER PagesNeeded, BOOLEAN FromEnd); // Returns the page number of the first available page range from the beginning or end of memory
91 PFN_NUMBER MmFindAvailablePagesBeforePage(PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PFN_NUMBER PagesNeeded, PFN_NUMBER LastPage); // Returns the page number of the first available page range before the specified page
92 VOID MmUpdateLastFreePageHint(PVOID PageLookupTable, PFN_NUMBER TotalPageCount); // Sets the LastFreePageHint to the last usable page of memory
93 BOOLEAN MmAreMemoryPagesAvailable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PVOID PageAddress, PFN_NUMBER PageCount); // Returns TRUE if the specified pages of memory are available, otherwise FALSE
94 VOID MmSetMemoryType(PVOID MemoryAddress, SIZE_T MemorySize, TYPE_OF_MEMORY NewType); // Use with EXTREME caution!
95
96 PPAGE_LOOKUP_TABLE_ITEM MmGetMemoryMap(PFN_NUMBER *NoEntries); // Returns a pointer to the memory mapping table and a number of entries in it
97
98
99 //BOOLEAN MmInitializeMemoryManager(ULONG LowMemoryStart, ULONG LowMemoryLength);
100 BOOLEAN MmInitializeMemoryManager(VOID);
101 VOID MmInitializeHeap(PVOID PageLookupTable);
102 PVOID MmAllocateMemory(SIZE_T MemorySize);
103 PVOID MmAllocateMemoryWithType(SIZE_T MemorySize, TYPE_OF_MEMORY MemoryType);
104 VOID MmFreeMemory(PVOID MemoryPointer);
105 PVOID MmAllocateMemoryAtAddress(SIZE_T MemorySize, PVOID DesiredAddress, TYPE_OF_MEMORY MemoryType);
106 PVOID MmAllocateHighestMemoryBelowAddress(SIZE_T MemorySize, PVOID DesiredAddress, TYPE_OF_MEMORY MemoryType);
107
108 PVOID MmHeapAlloc(SIZE_T MemorySize);
109 VOID MmHeapFree(PVOID MemoryPointer);
110
111 /* Heap */
112 extern PVOID FrLdrDefaultHeap;
113 extern PVOID FrLdrTempHeap;
114
115 PVOID
116 HeapCreate(
117 SIZE_T MaximumSize,
118 TYPE_OF_MEMORY MemoryType);
119
120 VOID
121 HeapDestroy(
122 PVOID HeapHandle);
123
124 VOID
125 HeapRelease(
126 PVOID HeapHandle);
127
128 VOID
129 HeapCleanupAll(VOID);
130
131 PVOID
132 HeapAllocate(
133 PVOID HeapHandle,
134 SIZE_T ByteSize,
135 ULONG Tag);
136
137 VOID
138 HeapFree(
139 PVOID HeapHandle,
140 PVOID Pointer,
141 ULONG Tag);
142