[util] Add Sys_PageSize

Eliminate that particular problem once and for all.
This commit is contained in:
Bill Currie 2021-04-01 18:20:56 +09:00
parent 73a610508c
commit 9ded490806
3 changed files with 28 additions and 22 deletions

View file

@ -120,6 +120,7 @@ void Sys_Init_Cvars (void);
//
void Sys_MakeCodeWriteable (uintptr_t startaddr, size_t length);
void Sys_PageIn (void *ptr, size_t size);
long Sys_PageSize (void);
void *Sys_Alloc (size_t size);
//

View file

@ -24,20 +24,20 @@
Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <unistd.h>
#include <malloc.h>
#include "QF/alloc.h"
#include "QF/cmem.h"
#include "QF/sys.h"
#ifdef _WIN32
#define cmem_alloc(align, size) _aligned_malloc(size, align)
#define cmem_free(mem) _aligned_free(mem)
#define _SC_PAGESIZE 1
static size_t sysconf (int key)
{
return 4096;
}
#else
#define cmem_alloc(align, size) aligned_alloc(align, size)
#define cmem_free(mem) free(mem)
@ -58,7 +58,7 @@ new_memsuper (void)
{
memsuper_t *super = cmem_alloc (MEM_LINE_SIZE, sizeof (*super));
memset (super, 0, sizeof (*super));
super->page_size = sysconf (_SC_PAGESIZE);
super->page_size = Sys_PageSize ();
super->page_mask = (super->page_size - 1);
return super;
}

View file

@ -442,20 +442,10 @@ Sys_MakeCodeWriteable (uintptr_t startaddr, size_t length)
#else
# ifdef HAVE_MPROTECT
int r;
long psize = Sys_PageSize ();
unsigned long endaddr = startaddr + length;
# ifdef HAVE__SC_PAGESIZE
long psize = sysconf (_SC_PAGESIZE);
startaddr &= ~(psize - 1);
endaddr = (endaddr + psize - 1) & ~(psize -1);
# else
# ifdef HAVE_GETPAGESIZE
int psize = getpagesize ();
startaddr &= ~(psize - 1);
endaddr = (endaddr + psize - 1) & ~(psize - 1);
# endif
# endif
// systems with mprotect but not getpagesize (or similar) probably don't
// need to page align the arguments to mprotect (eg, QNX)
r = mprotect ((char *) startaddr, endaddr - startaddr,
@ -619,18 +609,33 @@ Sys_PageIn (void *ptr, size_t size)
//#endif
}
VISIBLE long
Sys_PageSize (void)
{
#ifdef _WIN32
SYSTEM_INFO si;
GetSystemInfo (&si);
return si.dwPageSize;
#else
# ifdef HAVE__SC_PAGESIZE
return sysconf (_SC_PAGESIZE);
# else
# ifdef HAVE_GETPAGESIZE
return getpagesize ();
# endif
# endif
#endif
}
VISIBLE void *
Sys_Alloc (size_t size)
{
#ifdef _WIN32
size_t page_size = 4096;
size_t page_size = Sys_PageSize ();
size_t page_mask = page_size - 1;
size = (size + page_mask) & ~page_mask;
#ifdef _WIN32
return VirtualAlloc (0, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
#else
size_t page_size = sysconf (_SC_PAGESIZE);
size_t page_mask = page_size - 1;
size = (size + page_mask) & ~page_mask;
return mmap (0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,
-1, 0);
#endif