Something went wrong. Try again.
Reactos
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420/* * PROJECT: ReactOS Win32 Base API * LICENSE: GPL - See COPYING in the top level directory * FILE: dll/win32/kernel32/client/virtmem.c * PURPOSE: Handles virtual memory APIs * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org) */
/* INCLUDES *******************************************************************/
#include <k32.h>
#define NDEBUG#include <debug.h>
/* FUNCTIONS ******************************************************************/
/* * @implemented */LPVOIDNTAPIVirtualAllocEx(IN HANDLE hProcess, IN LPVOID lpAddress, IN SIZE_T dwSize, IN DWORD flAllocationType, IN DWORD flProtect){ NTSTATUS Status;
/* Make sure the address is within the granularity of the system (64K) */ if ((lpAddress != NULL) && (lpAddress < UlongToPtr(BaseStaticServerData->SysInfo.AllocationGranularity))) { /* Fail the call */ SetLastError(ERROR_INVALID_PARAMETER); return NULL; }
/* Allocate the memory */ Status = NtAllocateVirtualMemory(hProcess, &lpAddress, 0, &dwSize, flAllocationType, flProtect);
/* Check for status */ if (!NT_SUCCESS(Status)) { /* We failed */ BaseSetLastNTError(Status); return NULL; }
/* Return the allocated address */ return lpAddress;}
/* * @implemented */LPVOIDNTAPIVirtualAlloc(IN LPVOID lpAddress, IN SIZE_T dwSize, IN DWORD flAllocationType, IN DWORD flProtect){ /* Call the extended API */ return VirtualAllocEx(GetCurrentProcess(), lpAddress, dwSize, flAllocationType, flProtect);}
/* * @implemented */BOOLNTAPIVirtualFreeEx(IN HANDLE hProcess, IN LPVOID lpAddress, IN SIZE_T dwSize, IN DWORD dwFreeType){ NTSTATUS Status;
/* Validate size and flags */ if (!(dwSize) || !(dwFreeType & MEM_RELEASE)) { /* Free the memory */ Status = NtFreeVirtualMemory(hProcess, &lpAddress, &dwSize, dwFreeType); if (!NT_SUCCESS(Status)) { /* We failed */ BaseSetLastNTError(Status); return FALSE; }
/* Return success */ return TRUE; }
/* Invalid combo */ BaseSetLastNTError(STATUS_INVALID_PARAMETER); return FALSE;}
/* * @implemented */BOOLNTAPIVirtualFree(IN LPVOID lpAddress, IN SIZE_T dwSize, IN DWORD dwFreeType){ /* Call the extended API */ return VirtualFreeEx(GetCurrentProcess(), lpAddress, dwSize, dwFreeType);}
/* * @implemented */BOOLNTAPIVirtualProtect(IN LPVOID lpAddress, IN SIZE_T dwSize, IN DWORD flNewProtect, OUT PDWORD lpflOldProtect){ /* Call the extended API */ return VirtualProtectEx(GetCurrentProcess(), lpAddress, dwSize, flNewProtect, lpflOldProtect);}
/* * @implemented */BOOLNTAPIVirtualProtectEx(IN HANDLE hProcess, IN LPVOID lpAddress, IN SIZE_T dwSize, IN DWORD flNewProtect, OUT PDWORD lpflOldProtect){ NTSTATUS Status;
/* Change the protection */ Status = NtProtectVirtualMemory(hProcess, &lpAddress, &dwSize, flNewProtect, (PULONG)lpflOldProtect); if (!NT_SUCCESS(Status)) { /* We failed */ BaseSetLastNTError(Status); return FALSE; }
/* Return success */ return TRUE;}
/* * @implemented */BOOLNTAPIVirtualLock(IN LPVOID lpAddress, IN SIZE_T dwSize){ NTSTATUS Status; SIZE_T RegionSize = dwSize; PVOID BaseAddress = lpAddress;
/* Lock the memory */ Status = NtLockVirtualMemory(NtCurrentProcess(), &BaseAddress, &RegionSize, MAP_PROCESS); if (!NT_SUCCESS(Status)) { /* We failed */ BaseSetLastNTError(Status); return FALSE; }
/* Return success */ return TRUE;}
/* * @implemented */SIZE_TNTAPIVirtualQuery(IN LPCVOID lpAddress, OUT PMEMORY_BASIC_INFORMATION lpBuffer, IN SIZE_T dwLength){ /* Call the extended API */ return VirtualQueryEx(NtCurrentProcess(), lpAddress, lpBuffer, dwLength);}
/* * @implemented */SIZE_TNTAPIVirtualQueryEx(IN HANDLE hProcess, IN LPCVOID lpAddress, OUT PMEMORY_BASIC_INFORMATION lpBuffer, IN SIZE_T dwLength){ NTSTATUS Status; SIZE_T ResultLength;
/* Query basic information */ Status = NtQueryVirtualMemory(hProcess, (LPVOID)lpAddress, MemoryBasicInformation, lpBuffer, dwLength, &ResultLength); if (!NT_SUCCESS(Status)) { /* We failed */ BaseSetLastNTError(Status); return 0; }
/* Return the length returned */ return ResultLength;}
/* * @implemented */BOOLNTAPIVirtualUnlock(IN LPVOID lpAddress, IN SIZE_T dwSize){ NTSTATUS Status; SIZE_T RegionSize = dwSize; PVOID BaseAddress = lpAddress;
/* Lock the memory */ Status = NtUnlockVirtualMemory(NtCurrentProcess(), &BaseAddress, &RegionSize, MAP_PROCESS); if (!NT_SUCCESS(Status)) { /* We failed */ BaseSetLastNTError(Status); return FALSE; }
/* Return success */ return TRUE;}
/* * @implemented */UINTWINAPIGetWriteWatch(IN DWORD dwFlags, IN PVOID lpBaseAddress, IN SIZE_T dwRegionSize, IN PVOID *lpAddresses, OUT PULONG_PTR lpdwCount, OUT PULONG lpdwGranularity){ NTSTATUS Status;
Status = NtGetWriteWatch(GetCurrentProcess(), dwFlags, lpBaseAddress, dwRegionSize, lpAddresses, lpdwCount, lpdwGranularity); if (!NT_SUCCESS(Status)) { BaseSetLastNTError(Status); return -1; }
return 0;}
/* * @implemented */UINTWINAPIResetWriteWatch(IN LPVOID lpBaseAddress, IN SIZE_T dwRegionSize){ NTSTATUS Status;
Status = NtResetWriteWatch(NtCurrentProcess(), lpBaseAddress, dwRegionSize); if (!NT_SUCCESS(Status)) { BaseSetLastNTError(Status); return -1; }
return 0;}
/* * @implemented */BOOLWINAPIAllocateUserPhysicalPages(IN HANDLE hProcess, IN PULONG_PTR NumberOfPages, OUT PULONG_PTR UserPfnArray){ NTSTATUS Status;
Status = NtAllocateUserPhysicalPages(hProcess, NumberOfPages, UserPfnArray); if (!NT_SUCCESS(Status)) { BaseSetLastNTError(Status); return FALSE; }
return TRUE;}
/* * @implemented */BOOLWINAPIFreeUserPhysicalPages(IN HANDLE hProcess, IN PULONG_PTR NumberOfPages, IN PULONG_PTR PageArray){ NTSTATUS Status;
Status = NtFreeUserPhysicalPages(hProcess, NumberOfPages, PageArray); if (!NT_SUCCESS(Status)) { BaseSetLastNTError(Status); return FALSE; }
return TRUE;}
/* * @implemented */BOOLWINAPIMapUserPhysicalPages(IN PVOID VirtualAddress, IN ULONG_PTR NumberOfPages, OUT PULONG_PTR PageArray OPTIONAL){ NTSTATUS Status;
Status = NtMapUserPhysicalPages(VirtualAddress, NumberOfPages, PageArray); if (!NT_SUCCESS(Status)) { BaseSetLastNTError(Status); return FALSE; }
return TRUE;}
/* * @implemented */BOOLWINAPIMapUserPhysicalPagesScatter(IN PVOID *VirtualAddresses, IN ULONG_PTR NumberOfPages, OUT PULONG_PTR PageArray OPTIONAL){ NTSTATUS Status;
Status = NtMapUserPhysicalPagesScatter(VirtualAddresses, NumberOfPages, PageArray); if (!NT_SUCCESS(Status)) { BaseSetLastNTError(Status); return FALSE; }
return TRUE;}
/* EOF */