1
0
mirror of https://github.com/alliedmodders/metamod-source.git synced 2024-11-29 11:24:19 +01:00
HLMetaModOfficial/sourcehook/sh_memory.h
David Anderson 38c0cb36b4 Added license/author tags to files
--HG--
extra : convert_revision : svn%3Ac2935e3e-5518-0410-8daf-afa5dab7d4e3/trunk%4011
2005-04-16 23:33:57 +00:00

71 lines
1.6 KiB
C++

/* ======== SourceHook ========
* Copyright (C) 2004-2005 Metamod:Source Development Team
* No warranties of any kind
*
* License: zlib/libpng
*
* Author(s): Pavol "PM OnoTo" Marko, Scott "Damaged Soul" Ehlert
* Contributors: lancevorgin, XAD, theqizmo
* ============================
*/
#ifndef __SHINT_MEMORY_H__
#define __SHINT_MEMORY_H__
// Feb 17 / 2005:
// Unprotect now sets to readwrite
// The vtable doesn't need to be executable anyway
# if /********/ defined _WIN32
# include <windows.h>
# define SH_MEM_READ 1
# define SH_MEM_WRITE 2
# define SH_MEM_EXEC 4
# elif /******/ defined __linux__
# include <sys/mman.h>
// http://www.die.net/doc/linux/man/man2/mprotect.2.html
# include <limits.h>
# ifndef PAGESIZE
# define PAGESIZE 4096
# endif
# define SH_MEM_READ PROT_READ
# define SH_MEM_WRITE PROT_WRITE
# define SH_MEM_EXEC PROT_EXEC
// We need to align addr down to pagesize on linux
// We assume PAGESIZE is a power of two
# define SH_LALIGN(x) (void*)((int)(x) & ~(PAGESIZE-1))
# else
# error Unsupported OS/Compiler
# endif
namespace SourceHook
{
inline bool SetMemAccess(void *addr, size_t len, int access)
{
# ifdef __linux__
return mprotect(SH_LALIGN(addr), len, access)==0 ? true : false;
# else
DWORD tmp;
DWORD prot;
switch (access)
{
case SH_MEM_READ:
prot = PAGE_READONLY; break;
case SH_MEM_READ | SH_MEM_WRITE:
prot = PAGE_READWRITE; break;
case SH_MEM_READ | SH_MEM_EXEC:
prot = PAGE_EXECUTE_READ; break;
default:
case SH_MEM_READ | SH_MEM_WRITE | SH_MEM_EXEC:
prot = PAGE_EXECUTE_READWRITE; break;
}
return VirtualProtect(addr, len, prot, &tmp) ? true : false;
# endif
}
}
#endif