diff --git a/include/QF/sys.h b/include/QF/sys.h index eb0f25e00..e1ed1a057 100644 --- a/include/QF/sys.h +++ b/include/QF/sys.h @@ -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); // diff --git a/libs/util/cmem.c b/libs/util/cmem.c index 651f94993..02f78bb0e 100644 --- a/libs/util/cmem.c +++ b/libs/util/cmem.c @@ -24,20 +24,20 @@ Boston, MA 02111-1307, USA */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + #include #include #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; } diff --git a/libs/util/sys.c b/libs/util/sys.c index f448a6f55..cada787f2 100644 --- a/libs/util/sys.c +++ b/libs/util/sys.c @@ -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