- Misc janitorial fixes by Brezenbak to fix some MSVC compile errors.
[reactos.git] / reactos / ntoskrnl / mm / cont.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/mm/cont.c
6 * PURPOSE: Manages continuous memory
7 *
8 * PROGRAMMERS: David Welch (welch@cwcom.net)
9 */
10
11 /* INCLUDES *****************************************************************/
12
13 #include <ntoskrnl.h>
14 #define NDEBUG
15 #include <internal/debug.h>
16
17 /* FUNCTIONS *****************************************************************/
18
19 static VOID
20 MmFreeContinuousPage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address,
21 PFN_TYPE Page, SWAPENTRY SwapEntry,
22 BOOLEAN Dirty)
23 {
24 ASSERT(SwapEntry == 0);
25 if (Page != 0)
26 {
27 MmReleasePageMemoryConsumer(MC_NPPOOL, Page);
28 }
29 }
30
31 /**********************************************************************
32 * NAME EXPORTED
33 * MmAllocateContiguousMemorySpecifyCache@32
34 *
35 * DESCRIPTION
36 * Allocates a range of physically contiguous memory
37 * with a cache parameter.
38 *
39 * ARGUMENTS
40 * NumberOfBytes
41 * Size of the memory block to allocate;
42 *
43 * LowestAcceptableAddress
44 * Lowest address valid for the caller.
45 *
46 * HighestAcceptableAddress
47 * Highest address valid for the caller.
48 *
49 * BoundaryAddressMultiple
50 * Address multiple not to be crossed by allocated buffer (optional).
51 *
52 * CacheType
53 * Type of caching to use.
54 *
55 * RETURN VALUE
56 * The virtual address of the memory block on success;
57 * NULL on error.
58 *
59 * REVISIONS
60 *
61 * @implemented
62 */
63 PVOID STDCALL
64 MmAllocateContiguousMemorySpecifyCache(IN SIZE_T NumberOfBytes,
65 IN PHYSICAL_ADDRESS LowestAcceptableAddress OPTIONAL,
66 IN PHYSICAL_ADDRESS HighestAcceptableAddress,
67 IN PHYSICAL_ADDRESS BoundaryAddressMultiple OPTIONAL,
68 IN MEMORY_CACHING_TYPE CacheType OPTIONAL)
69 {
70 PMEMORY_AREA MArea;
71 NTSTATUS Status;
72 PVOID BaseAddress = NULL;
73 PFN_TYPE PBase;
74 ULONG Protect;
75 ULONG i;
76
77 Protect = PAGE_EXECUTE_READWRITE | PAGE_SYSTEM;
78 if (CacheType == MmNonCached || CacheType == MmWriteCombined)
79 {
80 Protect |= PAGE_NOCACHE;
81 }
82 if (CacheType == MmWriteCombined)
83 {
84 Protect |= PAGE_WRITECOMBINE;
85 }
86
87 MmLockAddressSpace(MmGetKernelAddressSpace());
88 Status = MmCreateMemoryArea(MmGetKernelAddressSpace(),
89 MEMORY_AREA_CONTINUOUS_MEMORY,
90 &BaseAddress,
91 NumberOfBytes,
92 PAGE_READWRITE,
93 &MArea,
94 FALSE,
95 0,
96 RtlConvertLongToLargeInteger(0));
97 MmUnlockAddressSpace(MmGetKernelAddressSpace());
98
99 if (!NT_SUCCESS(Status))
100 {
101 return(NULL);
102 }
103 DPRINT( "Base = %x\n", BaseAddress );
104 PBase = MmGetContinuousPages(NumberOfBytes,
105 LowestAcceptableAddress,
106 HighestAcceptableAddress,
107 BoundaryAddressMultiple);
108 if (PBase == 0)
109 {
110 MmLockAddressSpace(MmGetKernelAddressSpace());
111 MmFreeMemoryArea(MmGetKernelAddressSpace(),
112 MArea,
113 NULL,
114 NULL);
115 MmUnlockAddressSpace(MmGetKernelAddressSpace());
116 return(NULL);
117 }
118 for (i = 0; i < (PAGE_ROUND_UP(NumberOfBytes) / PAGE_SIZE); i++, PBase++)
119 {
120 MmCreateVirtualMapping(NULL,
121 (char*)BaseAddress + (i * PAGE_SIZE),
122 Protect,
123 &PBase,
124 1);
125 }
126 return(BaseAddress);
127 }
128
129 /**********************************************************************
130 * NAME EXPORTED
131 * MmAllocateContiguousMemory@12
132 *
133 * DESCRIPTION
134 * Allocates a range of physically contiguous cache aligned
135 * memory from the non-paged pool.
136 *
137 * ARGUMENTS
138 * NumberOfBytes
139 * Size of the memory block to allocate;
140 *
141 * HighestAcceptableAddress
142 * Highest address valid for the caller.
143 *
144 * RETURN VALUE
145 * The virtual address of the memory block on success;
146 * NULL on error.
147 *
148 * NOTE
149 * Description taken from include/ddk/mmfuncs.h.
150 * Code taken from ntoskrnl/mm/special.c.
151 *
152 * REVISIONS
153 *
154 * @implemented
155 */
156 PVOID STDCALL
157 MmAllocateContiguousMemory (IN ULONG NumberOfBytes,
158 IN PHYSICAL_ADDRESS HighestAcceptableAddress)
159 {
160 return MmAllocateContiguousMemorySpecifyCache(NumberOfBytes,
161 RtlConvertLongToLargeInteger(0),
162 HighestAcceptableAddress,
163 RtlConvertLongToLargeInteger(0),
164 MmCached);
165 }
166
167
168 /**********************************************************************
169 * NAME EXPORTED
170 * MmFreeContiguousMemory@4
171 *
172 * DESCRIPTION
173 * Releases a range of physically contiguous memory allocated
174 * with MmAllocateContiguousMemory.
175 *
176 * ARGUMENTS
177 * BaseAddress
178 * Virtual address of the memory to be freed.
179 *
180 * RETURN VALUE
181 * None.
182 *
183 * NOTE
184 * Description taken from include/ddk/mmfuncs.h.
185 * Code taken from ntoskrnl/mm/special.c.
186 *
187 * REVISIONS
188 *
189 * @implemented
190 */
191 VOID STDCALL
192 MmFreeContiguousMemory(IN PVOID BaseAddress)
193 {
194 MmLockAddressSpace(MmGetKernelAddressSpace());
195 MmFreeMemoryAreaByPtr(MmGetKernelAddressSpace(),
196 BaseAddress,
197 MmFreeContinuousPage,
198 NULL);
199 MmUnlockAddressSpace(MmGetKernelAddressSpace());
200 }
201
202 /**********************************************************************
203 * NAME EXPORTED
204 * MmFreeContiguousMemorySpecifyCache@12
205 *
206 * DESCRIPTION
207 * Releases a range of physically contiguous memory allocated
208 * with MmAllocateContiguousMemorySpecifyCache.
209 *
210 * ARGUMENTS
211 * BaseAddress
212 * Virtual address of the memory to be freed.
213 *
214 * NumberOfBytes
215 * Size of the memory block to free.
216 *
217 * CacheType
218 * Type of caching used.
219 *
220 * RETURN VALUE
221 * None.
222 *
223 * REVISIONS
224 *
225 * @implemented
226 */
227 VOID STDCALL
228 MmFreeContiguousMemorySpecifyCache(IN PVOID BaseAddress,
229 IN ULONG NumberOfBytes,
230 IN MEMORY_CACHING_TYPE CacheType)
231 {
232 MmLockAddressSpace(MmGetKernelAddressSpace());
233 MmFreeMemoryAreaByPtr(MmGetKernelAddressSpace(),
234 BaseAddress,
235 MmFreeContinuousPage,
236 NULL);
237 MmUnlockAddressSpace(MmGetKernelAddressSpace());
238 }
239
240
241 /* EOF */