-
Notifications
You must be signed in to change notification settings - Fork 20
/
memoryUtil.cpp
33 lines (26 loc) · 897 Bytes
/
memoryUtil.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <memoryUtil.h>
namespace Memory
{
int ChangeMemoryProtection(void* function, unsigned int size, unsigned long newProtection)
{
#ifdef __linux__
void* alignedAddress = Align(function);
return !mprotect(alignedAddress, sysconf(_SC_PAGESIZE), newProtection);
#else
FlushInstructionCache(GetCurrentProcess(), function, size);
static DWORD oldProtection;
return VirtualProtect(function, size, newProtection, &oldProtection);
#endif
}
int ChangeMemoryProtection(void* address, unsigned int size, unsigned long newProtection, unsigned long & oldProtection)
{
#ifdef __linux__
void* alignedAddress = Align(address);
oldProtection = newProtection;
return !mprotect(alignedAddress, sysconf(_SC_PAGESIZE), newProtection);
#else
FlushInstructionCache(GetCurrentProcess(), address, size);
return VirtualProtect(address, size, newProtection, &oldProtection);
#endif
}
}