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