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) */
32 if ((lpAddress
!= NULL
) &&
33 (lpAddress
< UlongToPtr(BaseStaticServerData
->SysInfo
.AllocationGranularity
)))
36 SetLastError(ERROR_INVALID_PARAMETER
);
40 /* Allocate the memory */
41 Status
= NtAllocateVirtualMemory(hProcess
,
48 /* Check for status */
49 if (!NT_SUCCESS(Status
))
52 BaseSetLastNTError(Status
);
56 /* Return the allocated address */
65 VirtualAlloc(IN LPVOID lpAddress
,
67 IN DWORD flAllocationType
,
70 /* Call the extended API */
71 return VirtualAllocEx(GetCurrentProcess(),
83 VirtualFreeEx(IN HANDLE hProcess
,
90 /* Validate size and flags */
91 if (!(dwSize
) || !(dwFreeType
& MEM_RELEASE
))
94 Status
= NtFreeVirtualMemory(hProcess
,
98 if (!NT_SUCCESS(Status
))
101 BaseSetLastNTError(Status
);
110 BaseSetLastNTError(STATUS_INVALID_PARAMETER
);
119 VirtualFree(IN LPVOID lpAddress
,
123 /* Call the extended API */
124 return VirtualFreeEx(GetCurrentProcess(),
135 VirtualProtect(IN LPVOID lpAddress
,
137 IN DWORD flNewProtect
,
138 OUT PDWORD lpflOldProtect
)
140 /* Call the extended API */
141 return VirtualProtectEx(GetCurrentProcess(),
153 VirtualProtectEx(IN HANDLE hProcess
,
156 IN DWORD flNewProtect
,
157 OUT PDWORD lpflOldProtect
)
161 /* Change the protection */
162 Status
= NtProtectVirtualMemory(hProcess
,
166 (PULONG
)lpflOldProtect
);
167 if (!NT_SUCCESS(Status
))
170 BaseSetLastNTError(Status
);
183 VirtualLock(IN LPVOID lpAddress
,
187 SIZE_T RegionSize
= dwSize
;
188 PVOID BaseAddress
= lpAddress
;
190 /* Lock the memory */
191 Status
= NtLockVirtualMemory(NtCurrentProcess(),
195 if (!NT_SUCCESS(Status
))
198 BaseSetLastNTError(Status
);
211 VirtualQuery(IN LPCVOID lpAddress
,
212 OUT PMEMORY_BASIC_INFORMATION lpBuffer
,
215 /* Call the extended API */
216 return VirtualQueryEx(NtCurrentProcess(),
227 VirtualQueryEx(IN HANDLE hProcess
,
228 IN LPCVOID lpAddress
,
229 OUT PMEMORY_BASIC_INFORMATION lpBuffer
,
235 /* Query basic information */
236 Status
= NtQueryVirtualMemory(hProcess
,
238 MemoryBasicInformation
,
242 if (!NT_SUCCESS(Status
))
245 BaseSetLastNTError(Status
);
249 /* Return the length returned */
258 VirtualUnlock(IN LPVOID lpAddress
,
262 SIZE_T RegionSize
= dwSize
;
263 PVOID BaseAddress
= lpAddress
;
265 /* Lock the memory */
266 Status
= NtUnlockVirtualMemory(NtCurrentProcess(),
270 if (!NT_SUCCESS(Status
))
273 BaseSetLastNTError(Status
);
286 GetWriteWatch(IN DWORD dwFlags
,
287 IN PVOID lpBaseAddress
,
288 IN SIZE_T dwRegionSize
,
289 IN PVOID
*lpAddresses
,
290 OUT PULONG_PTR lpdwCount
,
291 OUT PULONG lpdwGranularity
)
295 Status
= NtGetWriteWatch(GetCurrentProcess(),
302 if (!NT_SUCCESS(Status
))
304 BaseSetLastNTError(Status
);
316 ResetWriteWatch(IN LPVOID lpBaseAddress
,
317 IN SIZE_T dwRegionSize
)
321 Status
= NtResetWriteWatch(NtCurrentProcess(),
324 if (!NT_SUCCESS(Status
))
326 BaseSetLastNTError(Status
);
338 AllocateUserPhysicalPages(IN HANDLE hProcess
,
339 IN PULONG_PTR NumberOfPages
,
340 OUT PULONG_PTR UserPfnArray
)
344 Status
= NtAllocateUserPhysicalPages(hProcess
, NumberOfPages
, UserPfnArray
);
345 if (!NT_SUCCESS(Status
))
347 BaseSetLastNTError(Status
);
359 FreeUserPhysicalPages(IN HANDLE hProcess
,
360 IN PULONG_PTR NumberOfPages
,
361 IN PULONG_PTR PageArray
)
365 Status
= NtFreeUserPhysicalPages(hProcess
, NumberOfPages
, PageArray
);
366 if (!NT_SUCCESS(Status
))
368 BaseSetLastNTError(Status
);
380 MapUserPhysicalPages(IN PVOID VirtualAddress
,
381 IN ULONG_PTR NumberOfPages
,
382 OUT PULONG_PTR PageArray OPTIONAL
)
386 Status
= NtMapUserPhysicalPages(VirtualAddress
, NumberOfPages
, PageArray
);
387 if (!NT_SUCCESS(Status
))
389 BaseSetLastNTError(Status
);
401 MapUserPhysicalPagesScatter(IN PVOID
*VirtualAddresses
,
402 IN ULONG_PTR NumberOfPages
,
403 OUT PULONG_PTR PageArray OPTIONAL
)
407 Status
= NtMapUserPhysicalPagesScatter(VirtualAddresses
,
410 if (!NT_SUCCESS(Status
))
412 BaseSetLastNTError(Status
);