Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenvirtmem.c
Go to the documentation of this file.
00001 /* 00002 * PROJECT: ReactOS Win32 Base API 00003 * LICENSE: GPL - See COPYING in the top level directory 00004 * FILE: dll/win32/kernel32/client/virtmem.c 00005 * PURPOSE: Handles virtual memory APIs 00006 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org) 00007 */ 00008 00009 /* INCLUDES *******************************************************************/ 00010 00011 #include <k32.h> 00012 00013 #define NDEBUG 00014 #include <debug.h> 00015 00016 /* FUNCTIONS ******************************************************************/ 00017 00018 /* 00019 * @implemented 00020 */ 00021 LPVOID 00022 NTAPI 00023 VirtualAllocEx(IN HANDLE hProcess, 00024 IN LPVOID lpAddress, 00025 IN SIZE_T dwSize, 00026 IN DWORD flAllocationType, 00027 IN DWORD flProtect) 00028 { 00029 NTSTATUS Status; 00030 00031 /* Make sure the address is within the granularity of the system (64K) */ 00032 if ((lpAddress) && 00033 (lpAddress < (PVOID)BaseStaticServerData->SysInfo.AllocationGranularity)) 00034 { 00035 /* Fail the call */ 00036 SetLastError(ERROR_INVALID_PARAMETER); 00037 return NULL; 00038 } 00039 00040 /* Handle any possible exceptions */ 00041 _SEH2_TRY 00042 { 00043 /* Allocate the memory */ 00044 Status = NtAllocateVirtualMemory(hProcess, 00045 &lpAddress, 00046 0, 00047 &dwSize, 00048 flAllocationType, 00049 flProtect); 00050 } 00051 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) 00052 { 00053 Status = _SEH2_GetExceptionCode(); 00054 } 00055 _SEH2_END; 00056 00057 /* Check for status */ 00058 if (!NT_SUCCESS(Status)) 00059 { 00060 /* We failed */ 00061 BaseSetLastNTError(Status); 00062 return NULL; 00063 } 00064 00065 /* Return the allocated address */ 00066 return lpAddress; 00067 } 00068 00069 /* 00070 * @implemented 00071 */ 00072 LPVOID 00073 NTAPI 00074 VirtualAlloc(IN LPVOID lpAddress, 00075 IN SIZE_T dwSize, 00076 IN DWORD flAllocationType, 00077 IN DWORD flProtect) 00078 { 00079 /* Call the extended API */ 00080 return VirtualAllocEx(GetCurrentProcess(), 00081 lpAddress, 00082 dwSize, 00083 flAllocationType, 00084 flProtect); 00085 } 00086 00087 /* 00088 * @implemented 00089 */ 00090 BOOL 00091 NTAPI 00092 VirtualFreeEx(IN HANDLE hProcess, 00093 IN LPVOID lpAddress, 00094 IN SIZE_T dwSize, 00095 IN DWORD dwFreeType) 00096 { 00097 NTSTATUS Status; 00098 00099 /* Validate size and flags */ 00100 if (!(dwSize) || !(dwFreeType & MEM_RELEASE)) 00101 { 00102 /* Free the memory */ 00103 Status = NtFreeVirtualMemory(hProcess, 00104 &lpAddress, 00105 &dwSize, 00106 dwFreeType); 00107 if (!NT_SUCCESS(Status)) 00108 { 00109 /* We failed */ 00110 BaseSetLastNTError(Status); 00111 return FALSE; 00112 } 00113 00114 /* Return success */ 00115 return TRUE; 00116 } 00117 00118 /* Invalid combo */ 00119 BaseSetLastNTError(STATUS_INVALID_PARAMETER); 00120 return FALSE; 00121 } 00122 00123 /* 00124 * @implemented 00125 */ 00126 BOOL 00127 NTAPI 00128 VirtualFree(IN LPVOID lpAddress, 00129 IN SIZE_T dwSize, 00130 IN DWORD dwFreeType) 00131 { 00132 /* Call the extended API */ 00133 return VirtualFreeEx(GetCurrentProcess(), 00134 lpAddress, 00135 dwSize, 00136 dwFreeType); 00137 } 00138 00139 /* 00140 * @implemented 00141 */ 00142 BOOL 00143 NTAPI 00144 VirtualProtect(IN LPVOID lpAddress, 00145 IN SIZE_T dwSize, 00146 IN DWORD flNewProtect, 00147 OUT PDWORD lpflOldProtect) 00148 { 00149 /* Call the extended API */ 00150 return VirtualProtectEx(GetCurrentProcess(), 00151 lpAddress, 00152 dwSize, 00153 flNewProtect, 00154 lpflOldProtect); 00155 } 00156 00157 /* 00158 * @implemented 00159 */ 00160 BOOL 00161 NTAPI 00162 VirtualProtectEx(IN HANDLE hProcess, 00163 IN LPVOID lpAddress, 00164 IN SIZE_T dwSize, 00165 IN DWORD flNewProtect, 00166 OUT PDWORD lpflOldProtect) 00167 { 00168 NTSTATUS Status; 00169 00170 /* Change the protection */ 00171 Status = NtProtectVirtualMemory(hProcess, 00172 &lpAddress, 00173 &dwSize, 00174 flNewProtect, 00175 (PULONG)lpflOldProtect); 00176 if (!NT_SUCCESS(Status)) 00177 { 00178 /* We failed */ 00179 BaseSetLastNTError(Status); 00180 return FALSE; 00181 } 00182 00183 /* Return success */ 00184 return TRUE; 00185 } 00186 00187 /* 00188 * @implemented 00189 */ 00190 BOOL 00191 NTAPI 00192 VirtualLock(IN LPVOID lpAddress, 00193 IN SIZE_T dwSize) 00194 { 00195 NTSTATUS Status; 00196 SIZE_T RegionSize = dwSize; 00197 PVOID BaseAddress = lpAddress; 00198 00199 /* Lock the memory */ 00200 Status = NtLockVirtualMemory(NtCurrentProcess(), 00201 &BaseAddress, 00202 &RegionSize, 00203 MAP_PROCESS); 00204 if (!NT_SUCCESS(Status)) 00205 { 00206 /* We failed */ 00207 BaseSetLastNTError(Status); 00208 return FALSE; 00209 } 00210 00211 /* Return success */ 00212 return TRUE; 00213 } 00214 00215 /* 00216 * @implemented 00217 */ 00218 SIZE_T 00219 NTAPI 00220 VirtualQuery(IN LPCVOID lpAddress, 00221 OUT PMEMORY_BASIC_INFORMATION lpBuffer, 00222 IN SIZE_T dwLength) 00223 { 00224 /* Call the extended API */ 00225 return VirtualQueryEx(NtCurrentProcess(), 00226 lpAddress, 00227 lpBuffer, 00228 dwLength); 00229 } 00230 00231 /* 00232 * @implemented 00233 */ 00234 SIZE_T 00235 NTAPI 00236 VirtualQueryEx(IN HANDLE hProcess, 00237 IN LPCVOID lpAddress, 00238 OUT PMEMORY_BASIC_INFORMATION lpBuffer, 00239 IN SIZE_T dwLength) 00240 { 00241 NTSTATUS Status; 00242 SIZE_T ResultLength; 00243 00244 /* Query basic information */ 00245 Status = NtQueryVirtualMemory(hProcess, 00246 (LPVOID)lpAddress, 00247 MemoryBasicInformation, 00248 lpBuffer, 00249 dwLength, 00250 &ResultLength); 00251 if (!NT_SUCCESS(Status)) 00252 { 00253 /* We failed */ 00254 BaseSetLastNTError(Status); 00255 return 0; 00256 } 00257 00258 /* Return the length returned */ 00259 return ResultLength; 00260 } 00261 00262 /* 00263 * @implemented 00264 */ 00265 BOOL 00266 NTAPI 00267 VirtualUnlock(IN LPVOID lpAddress, 00268 IN SIZE_T dwSize) 00269 { 00270 NTSTATUS Status; 00271 SIZE_T RegionSize = dwSize; 00272 PVOID BaseAddress = lpAddress; 00273 00274 /* Lock the memory */ 00275 Status = NtUnlockVirtualMemory(NtCurrentProcess(), 00276 &BaseAddress, 00277 &RegionSize, 00278 MAP_PROCESS); 00279 if (!NT_SUCCESS(Status)) 00280 { 00281 /* We failed */ 00282 BaseSetLastNTError(Status); 00283 return FALSE; 00284 } 00285 00286 /* Return success */ 00287 return TRUE; 00288 } 00289 00290 /* 00291 * @implemented 00292 */ 00293 UINT 00294 WINAPI 00295 GetWriteWatch(IN DWORD dwFlags, 00296 IN PVOID lpBaseAddress, 00297 IN SIZE_T dwRegionSize, 00298 IN PVOID *lpAddresses, 00299 OUT PULONG_PTR lpdwCount, 00300 OUT PULONG lpdwGranularity) 00301 { 00302 NTSTATUS Status; 00303 00304 Status = NtGetWriteWatch(GetCurrentProcess(), 00305 dwFlags, 00306 lpBaseAddress, 00307 dwRegionSize, 00308 lpAddresses, 00309 lpdwCount, 00310 lpdwGranularity); 00311 if (!NT_SUCCESS(Status)) 00312 { 00313 BaseSetLastNTError(Status); 00314 return -1; 00315 } 00316 00317 return 0; 00318 } 00319 00320 /* 00321 * @implemented 00322 */ 00323 UINT 00324 WINAPI 00325 ResetWriteWatch(IN LPVOID lpBaseAddress, 00326 IN SIZE_T dwRegionSize) 00327 { 00328 NTSTATUS Status; 00329 00330 Status = NtResetWriteWatch(NtCurrentProcess(), 00331 lpBaseAddress, 00332 dwRegionSize); 00333 if (!NT_SUCCESS(Status)) 00334 { 00335 BaseSetLastNTError(Status); 00336 return -1; 00337 } 00338 00339 return 0; 00340 } 00341 00342 /* 00343 * @implemented 00344 */ 00345 BOOL 00346 WINAPI 00347 AllocateUserPhysicalPages(IN HANDLE hProcess, 00348 IN PULONG_PTR NumberOfPages, 00349 OUT PULONG_PTR UserPfnArray) 00350 { 00351 NTSTATUS Status; 00352 00353 Status = NtAllocateUserPhysicalPages(hProcess, NumberOfPages, UserPfnArray); 00354 if (!NT_SUCCESS(Status)) 00355 { 00356 BaseSetLastNTError(Status); 00357 return FALSE; 00358 } 00359 00360 return TRUE; 00361 } 00362 00363 /* 00364 * @implemented 00365 */ 00366 BOOL 00367 WINAPI 00368 FreeUserPhysicalPages(IN HANDLE hProcess, 00369 IN PULONG_PTR NumberOfPages, 00370 IN PULONG_PTR PageArray) 00371 { 00372 NTSTATUS Status; 00373 00374 Status = NtFreeUserPhysicalPages(hProcess, NumberOfPages, PageArray); 00375 if (!NT_SUCCESS(Status)) 00376 { 00377 BaseSetLastNTError(Status); 00378 return FALSE; 00379 } 00380 00381 return TRUE; 00382 } 00383 00384 /* 00385 * @implemented 00386 */ 00387 BOOL 00388 WINAPI 00389 MapUserPhysicalPages(IN PVOID VirtualAddress, 00390 IN ULONG_PTR NumberOfPages, 00391 OUT PULONG_PTR PageArray OPTIONAL) 00392 { 00393 NTSTATUS Status; 00394 00395 Status = NtMapUserPhysicalPages(VirtualAddress, NumberOfPages, PageArray); 00396 if (!NT_SUCCESS(Status)) 00397 { 00398 BaseSetLastNTError(Status); 00399 return FALSE; 00400 } 00401 00402 return TRUE; 00403 } 00404 00405 /* 00406 * @implemented 00407 */ 00408 BOOL 00409 WINAPI 00410 MapUserPhysicalPagesScatter(IN PVOID *VirtualAddresses, 00411 IN ULONG_PTR NumberOfPages, 00412 OUT PULONG_PTR PageArray OPTIONAL) 00413 { 00414 NTSTATUS Status; 00415 00416 Status = NtMapUserPhysicalPagesScatter(VirtualAddresses, 00417 NumberOfPages, 00418 PageArray); 00419 if (!NT_SUCCESS(Status)) 00420 { 00421 BaseSetLastNTError(Status); 00422 return FALSE; 00423 } 00424 00425 return TRUE; 00426 } 00427 00428 /* EOF */ Generated on Sun May 27 2012 04:21:01 for ReactOS by
1.7.6.1
|