Branching for 0.3.15 release after two days of no response from a certain sphere...
[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 extern char __ImageBase;
23 #ifdef __GNUC__
24 #define FREELDR_SECTION_COUNT 3
25 #else
26 #define FREELDR_SECTION_COUNT 1
27 #endif
28
29 typedef struct _FREELDR_MEMORY_DESCRIPTOR
30 {
31 TYPE_OF_MEMORY MemoryType;
32 PFN_NUMBER BasePage;
33 PFN_NUMBER PageCount;
34 } FREELDR_MEMORY_DESCRIPTOR, *PFREELDR_MEMORY_DESCRIPTOR;
35
36
37 #if defined(__i386__) || defined(_PPC_) || defined(_MIPS_) || defined(_ARM_)
38
39 #define MM_PAGE_SIZE 4096
40 #define MM_PAGE_MASK 0xFFF
41 #define MM_PAGE_SHIFT 12
42 #define MM_MAX_PAGE 0xFFFFF
43
44 #define MM_SIZE_TO_PAGES(a) \
45 ( ((a) >> MM_PAGE_SHIFT) + ((a) & MM_PAGE_MASK ? 1 : 0) )
46
47 #endif // defined __i386__ or _PPC_ or _MIPS_
48
49 #if defined (_AMD64_)
50
51 #define MM_PAGE_SIZE 4096
52 #define MM_PAGE_MASK 0xFFF
53 #define MM_PAGE_SHIFT 12
54 // FIXME: freeldr implementation uses ULONG for page numbers
55 #define MM_MAX_PAGE 0xFFFFFFFFFFFFF
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 PFN_NUMBER 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 extern PVOID PageLookupTableAddress;
82 extern PFN_NUMBER TotalPagesInLookupTable;
83 extern PFN_NUMBER FreePagesInLookupTable;
84 extern PFN_NUMBER LastFreePageHint;
85
86 #if DBG
87 PCSTR MmGetSystemMemoryMapTypeString(TYPE_OF_MEMORY Type);
88 #endif
89
90 PFN_NUMBER MmGetPageNumberFromAddress(PVOID Address); // Returns the page number that contains a linear address
91 PFN_NUMBER MmGetAddressablePageCountIncludingHoles(VOID); // Returns the count of addressable pages from address zero including any memory holes and reserved memory regions
92 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)
93 VOID MmInitPageLookupTable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount); // Inits the page lookup table according to the memory types in the memory map
94 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
95 VOID MmAllocatePagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER StartPage, PFN_NUMBER PageCount, TYPE_OF_MEMORY MemoryType); // Allocates the specified pages in the lookup table
96 PFN_NUMBER MmCountFreePagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount); // Returns the number of free pages in the lookup table
97 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
98 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
99 VOID MmUpdateLastFreePageHint(PVOID PageLookupTable, PFN_NUMBER TotalPageCount); // Sets the LastFreePageHint to the last usable page of memory
100 BOOLEAN MmAreMemoryPagesAvailable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PVOID PageAddress, PFN_NUMBER PageCount); // Returns TRUE if the specified pages of memory are available, otherwise FALSE
101 VOID MmSetMemoryType(PVOID MemoryAddress, SIZE_T MemorySize, TYPE_OF_MEMORY NewType); // Use with EXTREME caution!
102
103 PPAGE_LOOKUP_TABLE_ITEM MmGetMemoryMap(PFN_NUMBER *NoEntries); // Returns a pointer to the memory mapping table and a number of entries in it
104
105
106 //BOOLEAN MmInitializeMemoryManager(ULONG LowMemoryStart, ULONG LowMemoryLength);
107 BOOLEAN MmInitializeMemoryManager(VOID);
108 VOID MmInitializeHeap(PVOID PageLookupTable);
109 PVOID MmAllocateMemory(SIZE_T MemorySize);
110 PVOID MmAllocateMemoryWithType(SIZE_T MemorySize, TYPE_OF_MEMORY MemoryType);
111 VOID MmFreeMemory(PVOID MemoryPointer);
112 PVOID MmAllocateMemoryAtAddress(SIZE_T MemorySize, PVOID DesiredAddress, TYPE_OF_MEMORY MemoryType);
113 PVOID MmAllocateHighestMemoryBelowAddress(SIZE_T MemorySize, PVOID DesiredAddress, TYPE_OF_MEMORY MemoryType);
114
115 PVOID MmHeapAlloc(SIZE_T MemorySize);
116 VOID MmHeapFree(PVOID MemoryPointer);
117
118 /* Heap */
119 extern PVOID FrLdrDefaultHeap;
120 extern PVOID FrLdrTempHeap;
121
122 PVOID
123 HeapCreate(
124 SIZE_T MaximumSize,
125 TYPE_OF_MEMORY MemoryType);
126
127 VOID
128 HeapDestroy(
129 PVOID HeapHandle);
130
131 VOID
132 HeapRelease(
133 PVOID HeapHandle);
134
135 VOID
136 HeapVerify(
137 PVOID HeapHandle);
138
139 VOID
140 HeapCleanupAll(VOID);
141
142 PVOID
143 HeapAllocate(
144 PVOID HeapHandle,
145 SIZE_T ByteSize,
146 ULONG Tag);
147
148 VOID
149 HeapFree(
150 PVOID HeapHandle,
151 PVOID Pointer,
152 ULONG Tag);
153