- Only reserve APIC memory on x86
[reactos.git] / reactos / dll / win32 / kernel32 / mem / virtual.c
1 /*
2 * PROJECT: ReactOS Win32 Base API
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/win32/kernel32/mem/virtual.c
5 * PURPOSE: Handles virtual memory APIs
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7 */
8
9 /* INCLUDES ******************************************************************/
10
11 #include <k32.h>
12
13 #define NDEBUG
14 #include <debug.h>
15
16 /* FUNCTIONS *****************************************************************/
17
18 /*
19 * @implemented
20 */
21 LPVOID
22 NTAPI
23 VirtualAllocEx(IN HANDLE hProcess,
24 IN LPVOID lpAddress,
25 IN SIZE_T dwSize,
26 IN DWORD flAllocationType,
27 IN DWORD flProtect)
28 {
29 NTSTATUS Status;
30
31 /* Allocate the memory */
32 Status = NtAllocateVirtualMemory(hProcess,
33 (PVOID *)&lpAddress,
34 0,
35 &dwSize,
36 flAllocationType,
37 flProtect);
38 if (!NT_SUCCESS(Status))
39 {
40 /* We failed */
41 SetLastErrorByStatus(Status);
42 return NULL;
43 }
44
45 /* Return the allocated address */
46 return lpAddress;
47 }
48
49 /*
50 * @implemented
51 */
52 LPVOID
53 NTAPI
54 VirtualAlloc(IN LPVOID lpAddress,
55 IN SIZE_T dwSize,
56 IN DWORD flAllocationType,
57 IN DWORD flProtect)
58 {
59 /* Call the extended API */
60 return VirtualAllocEx(GetCurrentProcess(),
61 lpAddress,
62 dwSize,
63 flAllocationType,
64 flProtect);
65 }
66
67 /*
68 * @implemented
69 */
70 BOOL
71 NTAPI
72 VirtualFreeEx(IN HANDLE hProcess,
73 IN LPVOID lpAddress,
74 IN SIZE_T dwSize,
75 IN DWORD dwFreeType)
76 {
77 NTSTATUS Status;
78
79 if (dwSize == 0 || !(dwFreeType & MEM_RELEASE))
80 {
81 /* Free the memory */
82 Status = NtFreeVirtualMemory(hProcess,
83 (PVOID *)&lpAddress,
84 (PULONG)&dwSize,
85 dwFreeType);
86 if (!NT_SUCCESS(Status))
87 {
88 /* We failed */
89 SetLastErrorByStatus(Status);
90 return FALSE;
91 }
92
93 /* Return success */
94 return TRUE;
95 }
96
97 SetLastErrorByStatus(STATUS_INVALID_PARAMETER);
98 return FALSE;
99 }
100
101 /*
102 * @implemented
103 */
104 BOOL
105 NTAPI
106 VirtualFree(IN LPVOID lpAddress,
107 IN SIZE_T dwSize,
108 IN DWORD dwFreeType)
109 {
110 /* Call the extended API */
111 return VirtualFreeEx(GetCurrentProcess(),
112 lpAddress,
113 dwSize,
114 dwFreeType);
115 }
116
117 /*
118 * @implemented
119 */
120 BOOL
121 NTAPI
122 VirtualProtect(IN LPVOID lpAddress,
123 IN SIZE_T dwSize,
124 IN DWORD flNewProtect,
125 OUT PDWORD lpflOldProtect)
126 {
127 /* Call the extended API */
128 return VirtualProtectEx(GetCurrentProcess(),
129 lpAddress,
130 dwSize,
131 flNewProtect,
132 lpflOldProtect);
133 }
134
135 /*
136 * @implemented
137 */
138 BOOL
139 NTAPI
140 VirtualProtectEx(IN HANDLE hProcess,
141 IN LPVOID lpAddress,
142 IN SIZE_T dwSize,
143 IN DWORD flNewProtect,
144 OUT PDWORD lpflOldProtect)
145 {
146 NTSTATUS Status;
147
148 /* Change the protection */
149 Status = NtProtectVirtualMemory(hProcess,
150 &lpAddress,
151 &dwSize,
152 flNewProtect,
153 (PULONG)lpflOldProtect);
154 if (!NT_SUCCESS(Status))
155 {
156 /* We failed */
157 SetLastErrorByStatus(Status);
158 return FALSE;
159 }
160
161 /* Return success */
162 return TRUE;
163 }
164
165 /*
166 * @implemented
167 */
168 BOOL
169 NTAPI
170 VirtualLock(IN LPVOID lpAddress,
171 IN SIZE_T dwSize)
172 {
173 ULONG BytesLocked;
174 NTSTATUS Status;
175
176 /* Lock the memory */
177 Status = NtLockVirtualMemory(NtCurrentProcess(),
178 lpAddress,
179 dwSize,
180 &BytesLocked);
181 if (!NT_SUCCESS(Status))
182 {
183 /* We failed */
184 SetLastErrorByStatus(Status);
185 return FALSE;
186 }
187
188 /* Return success */
189 return TRUE;
190 }
191
192 /*
193 * @implemented
194 */
195 DWORD
196 NTAPI
197 VirtualQuery(IN LPCVOID lpAddress,
198 OUT PMEMORY_BASIC_INFORMATION lpBuffer,
199 IN SIZE_T dwLength)
200 {
201 /* Call the extended API */
202 return VirtualQueryEx(NtCurrentProcess(),
203 lpAddress,
204 lpBuffer,
205 dwLength);
206 }
207
208 /*
209 * @implemented
210 */
211 DWORD
212 NTAPI
213 VirtualQueryEx(IN HANDLE hProcess,
214 IN LPCVOID lpAddress,
215 OUT PMEMORY_BASIC_INFORMATION lpBuffer,
216 IN SIZE_T dwLength)
217 {
218 NTSTATUS Status;
219 ULONG ResultLength;
220
221 /* Query basic information */
222 Status = NtQueryVirtualMemory(hProcess,
223 (LPVOID)lpAddress,
224 MemoryBasicInformation,
225 lpBuffer,
226 dwLength,
227 &ResultLength);
228 if (!NT_SUCCESS(Status))
229 {
230 /* We failed */
231 SetLastErrorByStatus(Status);
232 return 0;
233 }
234
235 /* Return the length returned */
236 return ResultLength;
237 }
238
239 /*
240 * @implemented
241 */
242 BOOL
243 NTAPI
244 VirtualUnlock(IN LPVOID lpAddress,
245 IN SIZE_T dwSize)
246 {
247 ULONG BytesLocked;
248 NTSTATUS Status;
249
250 /* Unlock the memory */
251 Status = NtUnlockVirtualMemory(NtCurrentProcess(),
252 lpAddress,
253 dwSize,
254 &BytesLocked);
255 if (!NT_SUCCESS(Status))
256 {
257 /* We failed */
258 SetLastErrorByStatus(Status);
259 return FALSE;
260 }
261
262 /* Return success */
263 return TRUE;
264 }
265
266 /*
267 * @implemented
268 */
269 UINT
270 WINAPI
271 GetWriteWatch(
272 DWORD dwFlags,
273 PVOID lpBaseAddress,
274 SIZE_T dwRegionSize,
275 PVOID *lpAddresses,
276 PULONG_PTR lpdwCount,
277 PULONG lpdwGranularity
278 )
279 {
280 NTSTATUS Status;
281
282 Status = NtGetWriteWatch(GetCurrentProcess(),
283 dwFlags,
284 lpBaseAddress,
285 dwRegionSize,
286 lpAddresses,
287 lpdwCount,
288 lpdwGranularity);
289
290 if (!NT_SUCCESS(Status))
291 {
292 SetLastError(RtlNtStatusToDosError(Status));
293 return -1;
294 }
295
296 return 0;
297 }
298
299 /* EOF */