2 * PROJECT: ReactOS Win32 Base API
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/win32/kernel32/client/virtmem.c
5 * PURPOSE: Handles virtual memory APIs
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
9 /* INCLUDES *******************************************************************/
16 /* FUNCTIONS ******************************************************************/
23 VirtualAllocEx(IN HANDLE hProcess
,
26 IN DWORD flAllocationType
,
31 /* Make sure the address is within the granularity of the system (64K) */
33 (lpAddress
< (PVOID
)BaseStaticServerData
->SysInfo
.AllocationGranularity
))
36 SetLastError(ERROR_INVALID_PARAMETER
);
40 /* Handle any possible exceptions */
43 /* Allocate the memory */
44 Status
= NtAllocateVirtualMemory(hProcess
,
51 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
53 Status
= _SEH2_GetExceptionCode();
57 /* Check for status */
58 if (!NT_SUCCESS(Status
))
61 BaseSetLastNTError(Status
);
65 /* Return the allocated address */
74 VirtualAlloc(IN LPVOID lpAddress
,
76 IN DWORD flAllocationType
,
79 /* Call the extended API */
80 return VirtualAllocEx(GetCurrentProcess(),
92 VirtualFreeEx(IN HANDLE hProcess
,
99 /* Validate size and flags */
100 if (!(dwSize
) || !(dwFreeType
& MEM_RELEASE
))
102 /* Free the memory */
103 Status
= NtFreeVirtualMemory(hProcess
,
107 if (!NT_SUCCESS(Status
))
110 BaseSetLastNTError(Status
);
119 BaseSetLastNTError(STATUS_INVALID_PARAMETER
);
128 VirtualFree(IN LPVOID lpAddress
,
132 /* Call the extended API */
133 return VirtualFreeEx(GetCurrentProcess(),
144 VirtualProtect(IN LPVOID lpAddress
,
146 IN DWORD flNewProtect
,
147 OUT PDWORD lpflOldProtect
)
149 /* Call the extended API */
150 return VirtualProtectEx(GetCurrentProcess(),
162 VirtualProtectEx(IN HANDLE hProcess
,
165 IN DWORD flNewProtect
,
166 OUT PDWORD lpflOldProtect
)
170 /* Change the protection */
171 Status
= NtProtectVirtualMemory(hProcess
,
175 (PULONG
)lpflOldProtect
);
176 if (!NT_SUCCESS(Status
))
179 BaseSetLastNTError(Status
);
192 VirtualLock(IN LPVOID lpAddress
,
196 SIZE_T RegionSize
= dwSize
;
197 PVOID BaseAddress
= lpAddress
;
199 /* Lock the memory */
200 Status
= NtLockVirtualMemory(NtCurrentProcess(),
204 if (!NT_SUCCESS(Status
))
207 BaseSetLastNTError(Status
);
220 VirtualQuery(IN LPCVOID lpAddress
,
221 OUT PMEMORY_BASIC_INFORMATION lpBuffer
,
224 /* Call the extended API */
225 return VirtualQueryEx(NtCurrentProcess(),
236 VirtualQueryEx(IN HANDLE hProcess
,
237 IN LPCVOID lpAddress
,
238 OUT PMEMORY_BASIC_INFORMATION lpBuffer
,
244 /* Query basic information */
245 Status
= NtQueryVirtualMemory(hProcess
,
247 MemoryBasicInformation
,
251 if (!NT_SUCCESS(Status
))
254 BaseSetLastNTError(Status
);
258 /* Return the length returned */
267 VirtualUnlock(IN LPVOID lpAddress
,
271 SIZE_T RegionSize
= dwSize
;
272 PVOID BaseAddress
= lpAddress
;
274 /* Lock the memory */
275 Status
= NtUnlockVirtualMemory(NtCurrentProcess(),
279 if (!NT_SUCCESS(Status
))
282 BaseSetLastNTError(Status
);
295 GetWriteWatch(IN DWORD dwFlags
,
296 IN PVOID lpBaseAddress
,
297 IN SIZE_T dwRegionSize
,
298 IN PVOID
*lpAddresses
,
299 OUT PULONG_PTR lpdwCount
,
300 OUT PULONG lpdwGranularity
)
304 Status
= NtGetWriteWatch(GetCurrentProcess(),
311 if (!NT_SUCCESS(Status
))
313 BaseSetLastNTError(Status
);
325 ResetWriteWatch(IN LPVOID lpBaseAddress
,
326 IN SIZE_T dwRegionSize
)
330 Status
= NtResetWriteWatch(NtCurrentProcess(),
333 if (!NT_SUCCESS(Status
))
335 BaseSetLastNTError(Status
);
347 AllocateUserPhysicalPages(IN HANDLE hProcess
,
348 IN PULONG_PTR NumberOfPages
,
349 OUT PULONG_PTR UserPfnArray
)
353 Status
= NtAllocateUserPhysicalPages(hProcess
, NumberOfPages
, UserPfnArray
);
354 if (!NT_SUCCESS(Status
))
356 BaseSetLastNTError(Status
);
368 FreeUserPhysicalPages(IN HANDLE hProcess
,
369 IN PULONG_PTR NumberOfPages
,
370 IN PULONG_PTR PageArray
)
374 Status
= NtFreeUserPhysicalPages(hProcess
, NumberOfPages
, PageArray
);
375 if (!NT_SUCCESS(Status
))
377 BaseSetLastNTError(Status
);
389 MapUserPhysicalPages(IN PVOID VirtualAddress
,
390 IN ULONG_PTR NumberOfPages
,
391 OUT PULONG_PTR PageArray OPTIONAL
)
395 Status
= NtMapUserPhysicalPages(VirtualAddress
, NumberOfPages
, PageArray
);
396 if (!NT_SUCCESS(Status
))
398 BaseSetLastNTError(Status
);
410 MapUserPhysicalPagesScatter(IN PVOID
*VirtualAddresses
,
411 IN ULONG_PTR NumberOfPages
,
412 OUT PULONG_PTR PageArray OPTIONAL
)
416 Status
= NtMapUserPhysicalPagesScatter(VirtualAddresses
,
419 if (!NT_SUCCESS(Status
))
421 BaseSetLastNTError(Status
);