summaryrefslogtreecommitdiff
path: root/source/sw_protect.c
blob: 6b9ce403c7a6ee77dabde01572127765eb12bd25 (plain)
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
34
35
#include "config.h"
#include "q_shared.h"

#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include <unistd.h>
#include <sys/mman.h>
#endif

/*
================
Sys_MakeCodeWriteable
================
*/
void Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length) {
#ifdef _WIN32
    DWORD  flOldProtect;

    if (!VirtualProtect((LPVOID)startaddr, length, PAGE_READWRITE, &flOldProtect))
        Com_Error(ERR_FATAL, "Protection change failed\n");
#else
    int r;
    unsigned long addr;
    int psize = getpagesize();

    addr = (startaddr & ~(psize-1)) - psize;

    r = mprotect((char*)addr, length + startaddr - addr + psize, 7);
    if (r < 0)
            Com_Error( ERR_FATAL, "Protection change failed\n");
#endif
}