ReactOS 0.4.16-dev-122-g325d74c
virtmem.c
Go to the documentation of this file.
1/*
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)
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 */
24 IN LPVOID lpAddress,
26 IN DWORD flAllocationType,
27 IN DWORD flProtect)
28{
30
31 /* Make sure the address is within the granularity of the system (64K) */
32 if ((lpAddress != NULL) &&
34 {
35 /* Fail the call */
37 return NULL;
38 }
39
40 /* Allocate the memory */
42 &lpAddress,
43 0,
44 &dwSize,
45 flAllocationType,
46 flProtect);
47
48 /* Check for status */
49 if (!NT_SUCCESS(Status))
50 {
51 /* We failed */
53 return NULL;
54 }
55
56 /* Return the allocated address */
57 return lpAddress;
58}
59
60/*
61 * @implemented
62 */
67 IN DWORD flAllocationType,
68 IN DWORD flProtect)
69{
70 /* Call the extended API */
72 lpAddress,
73 dwSize,
74 flAllocationType,
75 flProtect);
76}
77
78/*
79 * @implemented
80 */
81BOOL
84 IN LPVOID lpAddress,
86 IN DWORD dwFreeType)
87{
89
90 /* Validate size and flags */
91 if (!(dwSize) || !(dwFreeType & MEM_RELEASE))
92 {
93 /* Free the memory */
95 &lpAddress,
96 &dwSize,
97 dwFreeType);
98 if (!NT_SUCCESS(Status))
99 {
100 /* We failed */
102 return FALSE;
103 }
104
105 /* Return success */
106 return TRUE;
107 }
108
109 /* Invalid combo */
111 return FALSE;
112}
113
114/*
115 * @implemented
116 */
117BOOL
118NTAPI
121 IN DWORD dwFreeType)
122{
123 /* Call the extended API */
125 lpAddress,
126 dwSize,
127 dwFreeType);
128}
129
130/*
131 * @implemented
132 */
133BOOL
134NTAPI
137 IN DWORD flNewProtect,
138 OUT PDWORD lpflOldProtect)
139{
140 /* Call the extended API */
142 lpAddress,
143 dwSize,
144 flNewProtect,
145 lpflOldProtect);
146}
147
148/*
149 * @implemented
150 */
151BOOL
152NTAPI
154 IN LPVOID lpAddress,
156 IN DWORD flNewProtect,
157 OUT PDWORD lpflOldProtect)
158{
160
161 /* Change the protection */
163 &lpAddress,
164 &dwSize,
165 flNewProtect,
166 (PULONG)lpflOldProtect);
167 if (!NT_SUCCESS(Status))
168 {
169 /* We failed */
171 return FALSE;
172 }
173
174 /* Return success */
175 return TRUE;
176}
177
178/*
179 * @implemented
180 */
181BOOL
182NTAPI
185{
188 PVOID BaseAddress = lpAddress;
189
190 /* Lock the memory */
193 &RegionSize,
195 if (!NT_SUCCESS(Status))
196 {
197 /* We failed */
199 return FALSE;
200 }
201
202 /* Return success */
203 return TRUE;
204}
205
206/*
207 * @implemented
208 */
209SIZE_T
210NTAPI
214{
215 /* Call the extended API */
217 lpAddress,
218 lpBuffer,
219 dwLength);
220}
221
222/*
223 * @implemented
224 */
225SIZE_T
226NTAPI
228 IN LPCVOID lpAddress,
231{
234
235 /* Query basic information */
237 (LPVOID)lpAddress,
239 lpBuffer,
240 dwLength,
241 &ResultLength);
242 if (!NT_SUCCESS(Status))
243 {
244 /* We failed */
246 return 0;
247 }
248
249 /* Return the length returned */
250 return ResultLength;
251}
252
253/*
254 * @implemented
255 */
256BOOL
257NTAPI
260{
263 PVOID BaseAddress = lpAddress;
264
265 /* Lock the memory */
268 &RegionSize,
270 if (!NT_SUCCESS(Status))
271 {
272 /* We failed */
274 return FALSE;
275 }
276
277 /* Return success */
278 return TRUE;
279}
280
281/*
282 * @implemented
283 */
284UINT
285WINAPI
287 IN PVOID lpBaseAddress,
288 IN SIZE_T dwRegionSize,
289 IN PVOID *lpAddresses,
290 OUT PULONG_PTR lpdwCount,
291 OUT PULONG lpdwGranularity)
292{
294
296 dwFlags,
297 lpBaseAddress,
298 dwRegionSize,
299 lpAddresses,
300 lpdwCount,
301 lpdwGranularity);
302 if (!NT_SUCCESS(Status))
303 {
305 return -1;
306 }
307
308 return 0;
309}
310
311/*
312 * @implemented
313 */
314UINT
315WINAPI
317 IN SIZE_T dwRegionSize)
318{
320
322 lpBaseAddress,
323 dwRegionSize);
324 if (!NT_SUCCESS(Status))
325 {
327 return -1;
328 }
329
330 return 0;
331}
332
333/*
334 * @implemented
335 */
336BOOL
337WINAPI
339 IN PULONG_PTR NumberOfPages,
340 OUT PULONG_PTR UserPfnArray)
341{
343
344 Status = NtAllocateUserPhysicalPages(hProcess, NumberOfPages, UserPfnArray);
345 if (!NT_SUCCESS(Status))
346 {
348 return FALSE;
349 }
350
351 return TRUE;
352}
353
354/*
355 * @implemented
356 */
357BOOL
358WINAPI
360 IN PULONG_PTR NumberOfPages,
361 IN PULONG_PTR PageArray)
362{
364
365 Status = NtFreeUserPhysicalPages(hProcess, NumberOfPages, PageArray);
366 if (!NT_SUCCESS(Status))
367 {
369 return FALSE;
370 }
371
372 return TRUE;
373}
374
375/*
376 * @implemented
377 */
378BOOL
379WINAPI
381 IN ULONG_PTR NumberOfPages,
382 OUT PULONG_PTR PageArray OPTIONAL)
383{
385
386 Status = NtMapUserPhysicalPages(VirtualAddress, NumberOfPages, PageArray);
387 if (!NT_SUCCESS(Status))
388 {
390 return FALSE;
391 }
392
393 return TRUE;
394}
395
396/*
397 * @implemented
398 */
399BOOL
400WINAPI
402 IN ULONG_PTR NumberOfPages,
403 OUT PULONG_PTR PageArray OPTIONAL)
404{
406
407 Status = NtMapUserPhysicalPagesScatter(VirtualAddresses,
408 NumberOfPages,
409 PageArray);
410 if (!NT_SUCCESS(Status))
411 {
413 return FALSE;
414 }
415
416 return TRUE;
417}
418
419/* EOF */
LONG NTSTATUS
Definition: precomp.h:26
static TAGREF LPCWSTR LPDWORD LPVOID lpBuffer
Definition: db.cpp:175
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#define ERROR_INVALID_PARAMETER
Definition: compat.h:101
#define SetLastError(x)
Definition: compat.h:752
#define GetCurrentProcess()
Definition: compat.h:759
static DWORD DWORD * dwLength
Definition: fusion.c:86
PBASE_STATIC_SERVER_DATA BaseStaticServerData
Definition: dllmain.c:19
#define UlongToPtr(u)
Definition: config.h:106
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
Status
Definition: gdiplustypes.h:25
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:71
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:56
unsigned int UINT
Definition: ndis.h:50
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
__kernel_entry _Inout_ _Inout_ PSIZE_T RegionSize
Definition: mmfuncs.h:172
@ MemoryBasicInformation
Definition: mmtypes.h:183
#define MAP_PROCESS
Definition: mmtypes.h:67
#define NtCurrentProcess()
Definition: nt_native.h:1657
#define MEM_RELEASE
Definition: nt_native.h:1316
NTSTATUS NTAPI NtFreeUserPhysicalPages(IN HANDLE ProcessHandle, IN OUT PULONG_PTR NumberOfPages, IN OUT PULONG_PTR UserPfnArray)
Definition: procsup.c:1475
NTSTATUS NTAPI NtMapUserPhysicalPages(IN PVOID VirtualAddresses, IN ULONG_PTR NumberOfPages, IN OUT PULONG_PTR UserPfnArray)
Definition: procsup.c:1455
NTSTATUS NTAPI NtAllocateUserPhysicalPages(IN HANDLE ProcessHandle, IN OUT PULONG_PTR NumberOfPages, IN OUT PULONG_PTR UserPfnArray)
Definition: procsup.c:1445
NTSTATUS NTAPI NtMapUserPhysicalPagesScatter(IN PVOID *VirtualAddresses, IN ULONG_PTR NumberOfPages, IN OUT PULONG_PTR UserPfnArray)
Definition: procsup.c:1465
NTSTATUS NTAPI NtResetWriteWatch(IN HANDLE ProcessHandle, IN PVOID BaseAddress, IN SIZE_T RegionSize)
Definition: virtual.c:4328
NTSTATUS NTAPI NtGetWriteWatch(IN HANDLE ProcessHandle, IN ULONG Flags, IN PVOID BaseAddress, IN SIZE_T RegionSize, IN PVOID *UserAddressArray, OUT PULONG_PTR EntriesInUserAddressArray, OUT PULONG Granularity)
Definition: virtual.c:4157
NTSTATUS NTAPI NtFreeVirtualMemory(IN HANDLE ProcessHandle, IN PVOID *UBaseAddress, IN PSIZE_T URegionSize, IN ULONG FreeType)
Definition: virtual.c:5230
NTSTATUS NTAPI NtProtectVirtualMemory(IN HANDLE ProcessHandle, IN OUT PVOID *UnsafeBaseAddress, IN OUT SIZE_T *UnsafeNumberOfBytesToProtect, IN ULONG NewAccessProtection, OUT PULONG UnsafeOldAccessProtection)
Definition: virtual.c:3111
NTSTATUS NTAPI NtQueryVirtualMemory(IN HANDLE ProcessHandle, IN PVOID BaseAddress, IN MEMORY_INFORMATION_CLASS MemoryInformationClass, OUT PVOID MemoryInformation, IN SIZE_T MemoryInformationLength, OUT PSIZE_T ReturnLength)
Definition: virtual.c:4409
NTSTATUS NTAPI NtLockVirtualMemory(IN HANDLE ProcessHandle, IN OUT PVOID *BaseAddress, IN OUT PSIZE_T NumberOfBytesToLock, IN ULONG MapType)
Definition: virtual.c:3529
NTSTATUS NTAPI NtUnlockVirtualMemory(IN HANDLE ProcessHandle, IN OUT PVOID *BaseAddress, IN OUT PSIZE_T NumberOfBytesToUnlock, IN ULONG MapType)
Definition: virtual.c:3865
NTSTATUS NTAPI NtAllocateVirtualMemory(IN HANDLE ProcessHandle, IN OUT PVOID *UBaseAddress, IN ULONG_PTR ZeroBits, IN OUT PSIZE_T URegionSize, IN ULONG AllocationType, IN ULONG Protect)
Definition: virtual.c:4492
DWORD * PDWORD
Definition: pedump.c:68
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:167
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68
SYSTEM_BASIC_INFORMATION SysInfo
Definition: base.h:130
uint32_t * PULONG_PTR
Definition: typedefs.h:65
uint32_t * PULONG
Definition: typedefs.h:59
#define NTAPI
Definition: typedefs.h:36
ULONG_PTR SIZE_T
Definition: typedefs.h:80
uint32_t ULONG_PTR
Definition: typedefs.h:65
#define IN
Definition: typedefs.h:39
#define OUT
Definition: typedefs.h:40
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
_Must_inspect_result_ _In_ WDFDEVICE _In_ DEVICE_REGISTRY_PROPERTY _In_ ULONG _Out_ PULONG ResultLength
Definition: wdfdevice.h:3776
_Must_inspect_result_ _In_ WDFDMATRANSACTION _In_ PFN_WDF_PROGRAM_DMA _In_ WDF_DMA_DIRECTION _In_ PMDL _In_ PVOID VirtualAddress
UINT WINAPI GetWriteWatch(IN DWORD dwFlags, IN PVOID lpBaseAddress, IN SIZE_T dwRegionSize, IN PVOID *lpAddresses, OUT PULONG_PTR lpdwCount, OUT PULONG lpdwGranularity)
Definition: virtmem.c:286
LPVOID NTAPI VirtualAlloc(IN LPVOID lpAddress, IN SIZE_T dwSize, IN DWORD flAllocationType, IN DWORD flProtect)
Definition: virtmem.c:65
BOOL WINAPI FreeUserPhysicalPages(IN HANDLE hProcess, IN PULONG_PTR NumberOfPages, IN PULONG_PTR PageArray)
Definition: virtmem.c:359
BOOL NTAPI VirtualProtect(IN LPVOID lpAddress, IN SIZE_T dwSize, IN DWORD flNewProtect, OUT PDWORD lpflOldProtect)
Definition: virtmem.c:135
LPVOID NTAPI VirtualAllocEx(IN HANDLE hProcess, IN LPVOID lpAddress, IN SIZE_T dwSize, IN DWORD flAllocationType, IN DWORD flProtect)
Definition: virtmem.c:23
BOOL NTAPI VirtualProtectEx(IN HANDLE hProcess, IN LPVOID lpAddress, IN SIZE_T dwSize, IN DWORD flNewProtect, OUT PDWORD lpflOldProtect)
Definition: virtmem.c:153
BOOL WINAPI AllocateUserPhysicalPages(IN HANDLE hProcess, IN PULONG_PTR NumberOfPages, OUT PULONG_PTR UserPfnArray)
Definition: virtmem.c:338
BOOL WINAPI MapUserPhysicalPages(IN PVOID VirtualAddress, IN ULONG_PTR NumberOfPages, OUT PULONG_PTR PageArray OPTIONAL)
Definition: virtmem.c:380
BOOL WINAPI MapUserPhysicalPagesScatter(IN PVOID *VirtualAddresses, IN ULONG_PTR NumberOfPages, OUT PULONG_PTR PageArray OPTIONAL)
Definition: virtmem.c:401
BOOL NTAPI VirtualFree(IN LPVOID lpAddress, IN SIZE_T dwSize, IN DWORD dwFreeType)
Definition: virtmem.c:119
SIZE_T NTAPI VirtualQueryEx(IN HANDLE hProcess, IN LPCVOID lpAddress, OUT PMEMORY_BASIC_INFORMATION lpBuffer, IN SIZE_T dwLength)
Definition: virtmem.c:227
UINT WINAPI ResetWriteWatch(IN LPVOID lpBaseAddress, IN SIZE_T dwRegionSize)
Definition: virtmem.c:316
BOOL NTAPI VirtualUnlock(IN LPVOID lpAddress, IN SIZE_T dwSize)
Definition: virtmem.c:258
SIZE_T NTAPI VirtualQuery(IN LPCVOID lpAddress, OUT PMEMORY_BASIC_INFORMATION lpBuffer, IN SIZE_T dwLength)
Definition: virtmem.c:211
BOOL NTAPI VirtualFreeEx(IN HANDLE hProcess, IN LPVOID lpAddress, IN SIZE_T dwSize, IN DWORD dwFreeType)
Definition: virtmem.c:83
BOOL NTAPI VirtualLock(IN LPVOID lpAddress, IN SIZE_T dwSize)
Definition: virtmem.c:183
_In_ PCCERT_CONTEXT _In_ DWORD dwFlags
Definition: wincrypt.h:1176
CONST void * LPCVOID
Definition: windef.h:191
#define WINAPI
Definition: msvc.h:6