1 /* ======== SourceMod ========
2 * Copyright (C) 2004-2005 SourceMod Development Team
3 * No warranties of any kind
8 * License: See LICENSE.txt
10 * Author(s): PM, Damaged Soul
11 * Contributors: lancevorgin, XAD, theqizmo, Damaged-Soul
12 * ============================
15 #ifndef __SHINT_MEMORY_H__
16 #define __SHINT_MEMORY_H__
19 // Unprotect now sets to readwrite
20 // The vtable doesn't need to be executable anyway
22 # if /********/ defined _WIN32
24 # define SH_MEM_READ 1
25 # define SH_MEM_WRITE 2
26 # define SH_MEM_EXEC 4
27 # elif /******/ defined __linux__
28 # include <sys/mman.h>
29 // http://www.die.net/doc/linux/man/man2/mprotect.2.html
32 # define PAGESIZE 4096
34 # define SH_MEM_READ PROT_READ
35 # define SH_MEM_WRITE PROT_WRITE
36 # define SH_MEM_EXEC PROT_EXEC
38 // We need to align addr down to pagesize on linux
39 // We assume PAGESIZE is a power of two
40 # define SH_LALIGN(x) (void*)((int)(x) & ~(PAGESIZE-1))
43 # error Unsupported OS/Compiler
49 inline bool SetMemAccess(void *addr, size_t len, int access)
52 return mprotect(SH_LALIGN(addr), len, access)==0 ? true : false;
59 prot = PAGE_READONLY; break;
60 case SH_MEM_READ | SH_MEM_WRITE:
61 prot = PAGE_READWRITE; break;
62 case SH_MEM_READ | SH_MEM_EXEC:
63 prot = PAGE_EXECUTE_READ; break;
65 case SH_MEM_READ | SH_MEM_WRITE | SH_MEM_EXEC:
66 prot = PAGE_EXECUTE_READWRITE; break;
68 return VirtualProtect(addr, len, prot, &tmp) ? true : false;