Merge trunk (r43561)
[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 &lpAddress,
84 &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 NTSTATUS Status;
174 ULONG RegionSize = dwSize;
175 PVOID BaseAddress = lpAddress;
176
177 /* Lock the memory */
178 Status = NtLockVirtualMemory(NtCurrentProcess(),
179 &BaseAddress,
180 &RegionSize,
181 MAP_PROCESS);
182 if (!NT_SUCCESS(Status))
183 {
184 /* We failed */
185 SetLastErrorByStatus(Status);
186 return FALSE;
187 }
188
189 /* Return success */
190 return TRUE;
191 }
192
193 /*
194 * @implemented
195 */
196 DWORD
197 NTAPI
198 VirtualQuery(IN LPCVOID lpAddress,
199 OUT PMEMORY_BASIC_INFORMATION lpBuffer,
200 IN SIZE_T dwLength)
201 {
202 /* Call the extended API */
203 return VirtualQueryEx(NtCurrentProcess(),
204 lpAddress,
205 lpBuffer,
206 dwLength);
207 }
208
209 /*
210 * @implemented
211 */
212 DWORD
213 NTAPI
214 VirtualQueryEx(IN HANDLE hProcess,
215 IN LPCVOID lpAddress,
216 OUT PMEMORY_BASIC_INFORMATION lpBuffer,
217 IN SIZE_T dwLength)
218 {
219 NTSTATUS Status;
220 ULONG ResultLength;
221
222 /* Query basic information */
223 Status = NtQueryVirtualMemory(hProcess,
224 (LPVOID)lpAddress,
225 MemoryBasicInformation,
226 lpBuffer,
227 dwLength,
228 &ResultLength);
229 if (!NT_SUCCESS(Status))
230 {
231 /* We failed */
232 SetLastErrorByStatus(Status);
233 return 0;
234 }
235
236 /* Return the length returned */
237 return ResultLength;
238 }
239
240 /*
241 * @implemented
242 */
243 BOOL
244 NTAPI
245 VirtualUnlock(IN LPVOID lpAddress,
246 IN SIZE_T dwSize)
247 {
248 NTSTATUS Status;
249 ULONG RegionSize = dwSize;
250 PVOID BaseAddress = lpAddress;
251
252 /* Lock the memory */
253 Status = NtUnlockVirtualMemory(NtCurrentProcess(),
254 &BaseAddress,
255 &RegionSize,
256 MAP_PROCESS);
257 if (!NT_SUCCESS(Status))
258 {
259 /* We failed */
260 SetLastErrorByStatus(Status);
261 return FALSE;
262 }
263
264 /* Return success */
265 return TRUE;
266 }
267
268 /*
269 * @implemented
270 */
271 UINT
272 WINAPI
273 GetWriteWatch(
274 DWORD dwFlags,
275 PVOID lpBaseAddress,
276 SIZE_T dwRegionSize,
277 PVOID *lpAddresses,
278 PULONG_PTR lpdwCount,
279 PULONG lpdwGranularity
280 )
281 {
282 NTSTATUS Status;
283
284 Status = NtGetWriteWatch(GetCurrentProcess(),
285 dwFlags,
286 lpBaseAddress,
287 dwRegionSize,
288 lpAddresses,
289 lpdwCount,
290 lpdwGranularity);
291
292 if (!NT_SUCCESS(Status))
293 {
294 SetLastErrorByStatus(Status);
295 return -1;
296 }
297
298 return 0;
299 }
300
301 /*
302 * @implemented
303 */
304 UINT
305 WINAPI
306 ResetWriteWatch(
307 LPVOID lpBaseAddress,
308 SIZE_T dwRegionSize
309 )
310 {
311 NTSTATUS Status;
312
313 Status = NtResetWriteWatch(NtCurrentProcess(),
314 lpBaseAddress,
315 dwRegionSize);
316
317 if (!NT_SUCCESS(Status))
318 {
319 SetLastErrorByStatus(Status);
320 return -1;
321 }
322
323 return 0;
324 }
325
326 /*
327 * @implemented
328 */
329 BOOL
330 WINAPI
331 AllocateUserPhysicalPages(
332 HANDLE hProcess,
333 PULONG_PTR NumberOfPages,
334 PULONG_PTR UserPfnArray
335 )
336 {
337 NTSTATUS Status;
338
339 Status = NtAllocateUserPhysicalPages(hProcess,
340 NumberOfPages,
341 UserPfnArray);
342
343 if (!NT_SUCCESS(Status))
344 {
345 SetLastErrorByStatus(Status);
346 return FALSE;
347 }
348
349 return TRUE;
350 }
351
352 /*
353 * @implemented
354 */
355 BOOL
356 WINAPI
357 FreeUserPhysicalPages(
358 HANDLE hProcess,
359 PULONG_PTR NumberOfPages,
360 PULONG_PTR PageArray
361 )
362 {
363 NTSTATUS Status;
364
365 Status = NtFreeUserPhysicalPages(hProcess,
366 NumberOfPages,
367 PageArray);
368
369 if (!NT_SUCCESS(Status))
370 {
371 SetLastErrorByStatus(Status);
372 return FALSE;
373 }
374
375 return TRUE;
376 }
377
378 /*
379 * @implemented
380 */
381 BOOL
382 WINAPI
383 MapUserPhysicalPages(
384 PVOID VirtualAddress,
385 ULONG_PTR NumberOfPages,
386 PULONG_PTR PageArray OPTIONAL
387 )
388 {
389 NTSTATUS Status;
390
391 Status = NtMapUserPhysicalPages(VirtualAddress,
392 NumberOfPages,
393 PageArray);
394
395 if (!NT_SUCCESS(Status))
396 {
397 SetLastErrorByStatus(Status);
398 return FALSE;
399 }
400
401 return TRUE;
402 }
403
404 /*
405 * @implemented
406 */
407 BOOL
408 WINAPI
409 MapUserPhysicalPagesScatter(
410 PVOID *VirtualAddresses,
411 ULONG_PTR NumberOfPages,
412 PULONG_PTR PageArray OPTIONAL
413 )
414 {
415 NTSTATUS Status;
416
417 Status = NtMapUserPhysicalPagesScatter(VirtualAddresses,
418 NumberOfPages,
419 PageArray);
420
421 if (!NT_SUCCESS(Status))
422 {
423 SetLastErrorByStatus(Status);
424 return FALSE;
425 }
426
427 return TRUE;
428 }
429
430 /* EOF */