From: Aleksey Demakov Date: Wed, 27 Jul 2011 17:07:32 +0000 (+0700) Subject: added virtual memory routines X-Git-Url: https://git.unchartedbackwaters.co.uk/w/?a=commitdiff_plain;h=e797d01e97199e7f8a263d47d35fb0103ea7574a;p=francis%2Flibjit.git added virtual memory routines --- diff --git a/ChangeLog b/ChangeLog index 783ec23..8b21823 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,28 @@ +2011-07-27 Aleksey Demakov + + * jit/jit-config.h: added new file as central location for all + platform config macros deduced from ./configure and cpp. + * jit/Makefile.am: add jit-config.h. + + * include/jit/jit-context.h, jit/jit-context.c + (jit_context_supports_threads): removed. + * include/jit/jit-init.h, jit/jit-init.c (jit_supports_threads): + added function to replace jit_context_supports_threads. + + * include/jit/jit-init.h, jit/jit-init.c + (jit_supports_virtual_memory): added new function to check if + the jit supports virtual memory routines. + + * include/jit/jit-vmem.h, jit/jit-vmem.c (jit_vmem_init) + (jit_vmem_page_size, jit_vmem_round_up, jit_vmem_round_down) + (jit_vmem_reserve, jit_vmem_reserve_committed, jit_vmem_release) + (jit_vmem_commit, jit_vmem_decommit, jit_vmem_protect): added new + files with virtual memory routines. + * include/jit/Makefile.am, include/jit/jit.h: add jit-vmem.h + * jit/Makefile.am: add jit-vmem.c + + * jit/jit-init.c (jit_init): call jit_vmem_init(). + 2011-07-10 Aleksey Demakov * jit/jit-reg-alloc.c (choose_output_register, _jit_regs_assign): diff --git a/include/jit/Makefile.am b/include/jit/Makefile.am index b7bf8bf..5ad4bf4 100644 --- a/include/jit/Makefile.am +++ b/include/jit/Makefile.am @@ -28,6 +28,7 @@ dist_libjitinclude_HEADERS = \ jit-unwind.h \ jit-util.h \ jit-value.h \ + jit-vmem.h \ jit-walk.h nodist_libjitinclude_HEADERS = \ diff --git a/include/jit/jit-context.h b/include/jit/jit-context.h index ccb89d9..cce58a6 100644 --- a/include/jit/jit-context.h +++ b/include/jit/jit-context.h @@ -29,7 +29,6 @@ extern "C" { jit_context_t jit_context_create(void) JIT_NOTHROW; void jit_context_destroy(jit_context_t context) JIT_NOTHROW; -int jit_context_supports_threads(jit_context_t context) JIT_NOTHROW; void jit_context_build_start(jit_context_t context) JIT_NOTHROW; void jit_context_build_end(jit_context_t context) JIT_NOTHROW; void jit_context_set_on_demand_driver( diff --git a/include/jit/jit-init.h b/include/jit/jit-init.h index 27e38af..e213bf2 100644 --- a/include/jit/jit-init.h +++ b/include/jit/jit-init.h @@ -28,8 +28,13 @@ extern "C" { #endif void jit_init(void) JIT_NOTHROW; + int jit_uses_interpreter(void) JIT_NOTHROW; +int jit_supports_threads(void) JIT_NOTHROW; + +int jit_supports_virtual_memory(void) JIT_NOTHROW; + #ifdef __cplusplus }; #endif diff --git a/include/jit/jit-vmem.h b/include/jit/jit-vmem.h new file mode 100644 index 0000000..f5f05f2 --- /dev/null +++ b/include/jit/jit-vmem.h @@ -0,0 +1,58 @@ +/* + * jit-vmem.h - Virtual memory routines. + * + * Copyright (C) 2011 Southern Storm Software, Pty Ltd. + * + * The libjit library is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 2.1 of + * the License, or (at your option) any later version. + * + * The libjit library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with the libjit library. If not, see + * . + */ + +#ifndef _JIT_VMEM_H +#define _JIT_VMEM_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum { + JIT_PROT_NONE, + JIT_PROT_READ, + JIT_PROT_READ_WRITE, + JIT_PROT_EXEC_READ, + JIT_PROT_EXEC_READ_WRITE, +} jit_prot_t; + + +void jit_vmem_init(void); + +jit_uint jit_vmem_page_size(void); +jit_nuint jit_vmem_round_up(jit_nuint value); +jit_nuint jit_vmem_round_down(jit_nuint value); + +void *jit_vmem_reserve(jit_uint size); +void *jit_vmem_reserve_committed(jit_uint size, jit_prot_t prot); +int jit_vmem_release(void *addr, jit_uint size); + +int jit_vmem_commit(void *addr, jit_uint size, jit_prot_t prot); +int jit_vmem_decommit(void *addr, jit_uint size); + +int jit_vmem_protect(void *addr, jit_uint size, jit_prot_t prot); + +#ifdef __cplusplus +} +#endif + +#endif /* _JIT_VMEM_H */ diff --git a/include/jit/jit.h b/include/jit/jit.h index 0cbdc75..c7dd475 100644 --- a/include/jit/jit.h +++ b/include/jit/jit.h @@ -44,6 +44,7 @@ extern "C" { #include #include #include +#include #include #ifdef __cplusplus diff --git a/jit/Makefile.am b/jit/Makefile.am index 965afe9..de7acb0 100644 --- a/jit/Makefile.am +++ b/jit/Makefile.am @@ -20,6 +20,7 @@ libjit_la_SOURCES = \ jit-cache.h \ jit-cache.c \ jit-compile.c \ + jit-config.h \ jit-context.c \ jit-cpuid-x86.h \ jit-cpuid-x86.c \ @@ -76,6 +77,7 @@ libjit_la_SOURCES = \ jit-type.c \ jit-unwind.c \ jit-value.c \ + jit-vmem.c \ jit-walk.c EXTRA_DIST = \ diff --git a/jit/jit-alloc.c b/jit/jit-alloc.c index 7700769..45d6a2c 100644 --- a/jit/jit-alloc.c +++ b/jit/jit-alloc.c @@ -20,8 +20,8 @@ * . */ -#include "jit-internal.h" -#include +#include "jit-config.h" + #ifdef HAVE_STDLIB_H #include #endif diff --git a/jit/jit-config.h b/jit/jit-config.h new file mode 100644 index 0000000..ad52a89 --- /dev/null +++ b/jit/jit-config.h @@ -0,0 +1,102 @@ +/* + * jit-config.h - Configuration macros for the JIT. + * + * Copyright (C) 2011 Southern Storm Software, Pty Ltd. + * + * This file is part of the libjit library. + * + * The libjit library is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 2.1 of + * the License, or (at your option) any later version. + * + * The libjit library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with the libjit library. If not, see + * . + */ + +#ifndef _JIT_CONFIG_H +#define _JIT_CONFIG_H + +#include + +/* + * Determine what kind of system we are running on. + */ +#if defined(__CYGWIN__) || defined(__CYGWIN32__) +# define JIT_WIN32_CYGWIN 1 +# define JIT_WIN32_PLATFORM 1 +#elif defined(_WIN32) || defined(WIN32) +# define JIT_WIN32_NATIVE 1 +# define JIT_WIN32_PLATFORM 1 +#elif defined(__APPLE__) && defined(__MACH__) +# define JIT_DARWIN_PLATFORM 1 +#elif defined(__linux__) +# define JIT_LINUX_PLATFORM 1 +#endif + +/* + * Determine the type of threading library that we are using. + */ +#if defined(HAVE_PTHREAD_H) && defined(HAVE_LIBPTHREAD) +# define JIT_THREADS_SUPPORTED 1 +# define JIT_THREADS_PTHREAD 1 +#elif defined(JIT_WIN32_PLATFORM) +# define JIT_THREADS_SUPPORTED 1 +# define JIT_THREADS_WIN32 1 +#else +# define JIT_THREADS_SUPPORTED 0 +#endif + +/* + * Determine the type of virtual memory API that we are using. + */ +#if defined(JIT_WIN32_PLATFORM) +# define JIT_VMEM_SUPPORTED 1 +# define JIT_VMEM_WIN32 1 +#elif defined(HAVE_SYS_MMAN_H) +# define JIT_VMEM_SUPPORTED 1 +# define JIT_VMEM_MMAP 1 +#else +# define JIT_VMEM_SUPPORTED 0 +#endif + +/* + * Determine which backend to use. + */ +#if defined(USE_LIBJIT_INTERPRETER) +# define JIT_BACKEND_INTERP 1 +# define JIT_HAVE_BACKEND 1 +#elif defined(__alpha) || defined(__alpha__) +# define JIT_BACKEND_ALPHA 1 +# define JIT_HAVE_BACKEND 1 +#elif defined(__arm) || defined(__arm__) +# define JIT_BACKEND_ARM 1 +# define JIT_HAVE_BACKEND 1 +#elif defined(__i386) || defined(__i386__) || defined(_M_IX86) +# define JIT_BACKEND_X86 1 +# define JIT_HAVE_BACKEND 1 +#elif defined(__amd64) || defined(__amd64__) || defined(_x86_64) || defined(_x86_64__) +# define JIT_BACKEND_X86_64 1 +# define JIT_HAVE_BACKEND 1 +#endif + +/* + * Fallback to interpreter if there is no appropriate native backend. + */ +#if !defined(JIT_HAVE_BACKEND) +# define JIT_BACKEND_INTERP 1 +#endif + +/* +#define _JIT_COMPILE_DEBUG 1 +#define _JIT_BLOCK_DEBUG 1 + */ + +#endif /* _JIT_CONFIG_H */ + diff --git a/jit/jit-context.c b/jit/jit-context.c index 3325960..c1ab3bf 100644 --- a/jit/jit-context.c +++ b/jit/jit-context.c @@ -125,16 +125,6 @@ void jit_context_destroy(jit_context_t context) } } -/*@ - * @deftypefun int jit_context_supports_threads (jit_context_t @var{context}) - * Determine if the JIT supports threads. - * @end deftypefun -@*/ -int jit_context_supports_threads(jit_context_t context) -{ - return JIT_THREADS_SUPPORTED; -} - /*@ * @deftypefun void jit_context_build_start (jit_context_t @var{context}) * This routine should be called before you start building a function diff --git a/jit/jit-dump.c b/jit/jit-dump.c index 8af455e..de6ac29 100644 --- a/jit/jit-dump.c +++ b/jit/jit-dump.c @@ -23,7 +23,6 @@ #include "jit-internal.h" #include "jit-rules.h" #include -#include #ifdef HAVE_STDLIB_H # include #endif diff --git a/jit/jit-elf-read.c b/jit/jit-elf-read.c index 3dedeee..cb1cf40 100644 --- a/jit/jit-elf-read.c +++ b/jit/jit-elf-read.c @@ -24,7 +24,6 @@ #include "jit-rules.h" #include "jit-elf-defs.h" #include "jit-memory.h" -#include #ifdef JIT_WIN32_PLATFORM #ifdef HAVE_SYS_TYPES_H #include diff --git a/jit/jit-except.c b/jit/jit-except.c index 39649f7..ae391f6 100644 --- a/jit/jit-except.c +++ b/jit/jit-except.c @@ -22,7 +22,6 @@ #include "jit-internal.h" #include "jit-rules.h" -#include #ifdef HAVE_STDLIB_H # include #endif diff --git a/jit/jit-init.c b/jit/jit-init.c index e85beef..5b08c8a 100644 --- a/jit/jit-init.c +++ b/jit/jit-init.c @@ -21,7 +21,6 @@ */ #include "jit-internal.h" -#include "jit-rules.h" /*@ * @deftypefun void jit_init (void) @@ -37,7 +36,8 @@ * initializations are quietly ignored. * @end deftypefun @*/ -void jit_init(void) +void +jit_init(void) { static int init_done = 0; @@ -58,6 +58,9 @@ void jit_init(void) _jit_signal_init(); #endif + /* Initialize the virtual memory system */ + jit_vmem_init(); + /* Initialize the backend */ _jit_init_backend(); @@ -72,7 +75,8 @@ done: * called prior to @code{jit_init}. * @end deftypefun @*/ -int jit_uses_interpreter(void) +int +jit_uses_interpreter(void) { #if defined(JIT_BACKEND_INTERP) return 1; @@ -80,3 +84,25 @@ int jit_uses_interpreter(void) return 0; #endif } + +/*@ + * @deftypefun int jit_supports_threads (void) + * Determine if the JIT supports threads. + * @end deftypefun +@*/ +int +jit_supports_threads(void) +{ + return JIT_THREADS_SUPPORTED; +} + +/*@ + * @deftypefun int jit_supports_virtual_memory (void) + * Determine if the JIT supports virtual memory. + * @end deftypefun +@*/ +int +jit_supports_virtual_memory(void) +{ + return JIT_VMEM_SUPPORTED; +} diff --git a/jit/jit-insn.c b/jit/jit-insn.c index 9c5b3fd..bbd60b6 100644 --- a/jit/jit-insn.c +++ b/jit/jit-insn.c @@ -23,7 +23,6 @@ #include "jit-internal.h" #include "jit-rules.h" #include "jit-setjmp.h" -#include #if HAVE_STDLIB_H # include #endif diff --git a/jit/jit-internal.h b/jit/jit-internal.h index 5d152ca..911348e 100644 --- a/jit/jit-internal.h +++ b/jit/jit-internal.h @@ -24,27 +24,12 @@ #define _JIT_INTERNAL_H #include +#include "jit-config.h" #ifdef __cplusplus extern "C" { #endif -/* -#define _JIT_COMPILE_DEBUG 1 -#define _JIT_BLOCK_DEBUG 1 -*/ - -/* - * Determine what kind of Win32 system we are running on. - */ -#if defined(__CYGWIN__) || defined(__CYGWIN32__) -#define JIT_WIN32_CYGWIN 1 -#define JIT_WIN32_PLATFORM 1 -#elif defined(_WIN32) || defined(WIN32) -#define JIT_WIN32_NATIVE 1 -#define JIT_WIN32_PLATFORM 1 -#endif - /* * We need the apply rules for "jit_redirector_size". */ diff --git a/jit/jit-interp.c b/jit/jit-interp.c index 15717c5..0e11df4 100644 --- a/jit/jit-interp.c +++ b/jit/jit-interp.c @@ -31,7 +31,6 @@ straight vanilla ANSI C. #include "jit-interp.h" #include "jit-rules.h" #include "jit-memory.h" -#include #if HAVE_STDLIB_H #include #endif diff --git a/jit/jit-intrinsic.c b/jit/jit-intrinsic.c index 904e1b8..cc0e11f 100644 --- a/jit/jit-intrinsic.c +++ b/jit/jit-intrinsic.c @@ -21,7 +21,6 @@ */ #include "jit-internal.h" -#include #if defined(HAVE_TGMATH_H) && !defined(JIT_NFLOAT_IS_DOUBLE) #include #elif defined(HAVE_MATH_H) diff --git a/jit/jit-rules.h b/jit/jit-rules.h index 3093cdd..67909ff 100644 --- a/jit/jit-rules.h +++ b/jit/jit-rules.h @@ -23,40 +23,13 @@ #ifndef _JIT_RULES_H #define _JIT_RULES_H +#include "jit-config.h" #include "jit-cache.h" -#include #ifdef __cplusplus extern "C" { #endif -/* - * Determine which backend to use. - */ -#if defined(USE_LIBJIT_INTERPRETER) -# define JIT_BACKEND_INTERP 1 -# define JIT_HAVE_BACKEND 1 -#elif defined(__alpha) || defined(__alpha__) -# define JIT_BACKEND_ALPHA 1 -# define JIT_HAVE_BACKEND 1 -#elif defined(__arm) || defined(__arm__) -# define JIT_BACKEND_ARM 1 -# define JIT_HAVE_BACKEND 1 -#elif defined(__i386) || defined(__i386__) || defined(_M_IX86) -# define JIT_BACKEND_X86 1 -# define JIT_HAVE_BACKEND 1 -#elif defined(__amd64) || defined(__amd64__) || defined(_x86_64) || defined(_x86_64__) -# define JIT_BACKEND_X86_64 1 -# define JIT_HAVE_BACKEND 1 -#endif - -/* - * Fallback to interpreter if there is no appropriate native backend. - */ -#if !defined(JIT_HAVE_BACKEND) -# define JIT_BACKEND_INTERP 1 -#endif - /* * Information about a register. */ diff --git a/jit/jit-signal.c b/jit/jit-signal.c index e755c54..75d8003 100644 --- a/jit/jit-signal.c +++ b/jit/jit-signal.c @@ -21,11 +21,10 @@ * . */ -#include +#include "jit-internal.h" #ifdef JIT_USE_SIGNALS -#include "jit-internal.h" #include #include #include diff --git a/jit/jit-string.c b/jit/jit-string.c index b37a946..7deefbf 100644 --- a/jit/jit-string.c +++ b/jit/jit-string.c @@ -20,8 +20,8 @@ * . */ -#include "jit-internal.h" -#include +#include +#include "jit-config.h" #ifdef HAVE_STRING_H #include #elif defined(HAVE_STRINGS_H) diff --git a/jit/jit-thread.h b/jit/jit-thread.h index bd64a93..e8b0046 100644 --- a/jit/jit-thread.h +++ b/jit/jit-thread.h @@ -23,30 +23,19 @@ #ifndef _JIT_THREAD_H #define _JIT_THREAD_H -#include -#if defined(HAVE_PTHREAD_H) && defined(HAVE_LIBPTHREAD) - #include -#elif defined(JIT_WIN32_PLATFORM) - #include +#include +#include "jit-config.h" + +#if defined(JIT_THREADS_PTHREAD) +# include +#elif defined(JIT_THREADS_WIN32) +# include #endif #ifdef __cplusplus extern "C" { #endif -/* - * Determine the type of threading library that we are using. - */ -#if defined(HAVE_PTHREAD_H) && defined(HAVE_LIBPTHREAD) - #define JIT_THREADS_SUPPORTED 1 - #define JIT_THREADS_PTHREAD 1 -#elif defined(JIT_WIN32_PLATFORM) - #define JIT_THREADS_SUPPORTED 1 - #define JIT_THREADS_WIN32 1 -#else - #define JIT_THREADS_SUPPORTED 0 -#endif - /* * Type that describes a thread's identifier, and the id comparison function. */ diff --git a/jit/jit-type.c b/jit/jit-type.c index 7c5fb80..8cec977 100644 --- a/jit/jit-type.c +++ b/jit/jit-type.c @@ -23,7 +23,6 @@ #include "jit-internal.h" #include "jit-apply-rules.h" #include "jit-rules.h" -#include /*@ diff --git a/jit/jit-vmem.c b/jit/jit-vmem.c new file mode 100644 index 0000000..dfdaf6c --- /dev/null +++ b/jit/jit-vmem.c @@ -0,0 +1,287 @@ +/* + * jit-vmem.c - Virtual memory routines. + * + * Copyright (C) 2011 Southern Storm Software, Pty Ltd. + * + * The libjit library is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 2.1 of + * the License, or (at your option) any later version. + * + * The libjit library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with the libjit library. If not, see + * . + */ + +#include +#include "jit-config.h" + +#if defined(JIT_VMEM_WIN32) +# include +#elif defined(JIT_VMEM_MMAP) +# include +#endif + +#if !defined(JIT_WIN32_PLATFORM) && defined(HAVE_UNISTD_H) +# include +#endif + +/* + * Define getpagesize() if not provided + */ +#if !defined(JIT_WIN32_PLATFORM) && !defined(HAVE_GETPAGESIZE) +# if defined(NBPG) +# define getpagesize() (NBPG) +# elif defined(PAGE_SIZE) +# define getpagesize() (PAGE_SIZE) +# else +# define getpagesize() (4096) +# endif +#endif + +/* + * Make sure that "MAP_ANONYMOUS" is correctly defined, because it + * may not exist on some variants of Unix. + */ +#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON) +# define MAP_ANONYMOUS MAP_ANON +#endif + +static jit_uint page_size; + +#if defined(JIT_VMEM_WIN32) +static DWORD +convert_prot(jit_prot_t prot) +{ + switch(prot) + { + case JIT_PROT_NONE: + return PAGE_NOACCESS; + case JIT_PROT_READ: + return PAGE_READONLY; + case JIT_PROT_READ_WRITE: + return PAGE_READWRITE; + case JIT_PROT_EXEC_READ: + return PAGE_EXECUTE_READ; + case JIT_PROT_EXEC_READ_WRITE: + return PAGE_EXECUTE_READWRITE; + } + return PAGE_NOACCESS; +} +#elif defined(JIT_VMEM_MMAP) +static int +convert_prot(jit_prot_t prot) +{ + switch(prot) + { + case JIT_PROT_NONE: + return PROT_NONE; + case JIT_PROT_READ: + return PROT_READ; + case JIT_PROT_READ_WRITE: + return PROT_READ | PROT_WRITE; + case JIT_PROT_EXEC_READ: + return PROT_EXEC | PROT_READ; + case JIT_PROT_EXEC_READ_WRITE: + return PROT_EXEC | PROT_READ | PROT_WRITE; + } + return PROT_NONE; +} +#endif + +void +jit_vmem_init(void) +{ +#if defined(JIT_VMEM_WIN32) + /* Get the page size from a Windows-specific API */ + SYSTEM_INFO sysInfo; + GetSystemInfo(&sysInfo); + page_size = (jit_uint) (sysInfo.dwPageSize); +#else + /* Get the page size using a Unix-like sequence */ + page_size = (jit_uint) getpagesize(); +#endif +} + +jit_uint +jit_vmem_page_size(void) +{ + return page_size; +} + +jit_nuint +jit_vmem_round_up(jit_nuint value) +{ + return (value + page_size - 1) & ~(page_size - 1); +} + +jit_nuint +jit_vmem_round_down(jit_nuint value) +{ + return ((jit_nuint) value) & ~(page_size - 1); +} + +void * +jit_vmem_reserve(jit_uint size) +{ +#if defined(JIT_VMEM_WIN32) + + return VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_NOACCESS); + +#elif defined(JIT_VMEM_MMAP) + + void *addr; + + addr = mmap(0, size, PROT_NONE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0); + if(addr == MAP_FAILED) + { + return (void *) 0; + } + return addr; + +#else + return (void *) 0; +#endif +} + +void * +jit_vmem_reserve_committed(jit_uint size, jit_prot_t prot) +{ +#if defined(JIT_VMEM_WIN32) + + DWORD nprot; + + nprot = convert_prot(prot); + return VirtualAlloc(NULL, size, MEM_RESERVE | MEM_COMMIT, nprot); + +#elif defined(JIT_VMEM_MMAP) + + void *addr; + int nprot; + + nprot = convert_prot(prot); + addr = mmap(0, size, nprot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if(addr == MAP_FAILED) + { + return (void *) 0; + } + return addr; + +#else + return (void *) 0; +#endif +} + +int +jit_vmem_release(void *addr, jit_uint size) +{ +#if defined(JIT_VMEM_WIN32) + + return VirtualFree(addr, 0, MEM_RELEASE) != 0; + +#elif defined(JIT_VMEM_MMAP) + + return munmap(addr, size) == 0; + +#else + return 0; +#endif +} + +int +jit_vmem_commit(void *addr, jit_uint size, jit_prot_t prot) +{ +#if defined(JIT_VMEM_WIN32) + + DWORD nprot; + + nprot = convert_prot(prot); + return VirtualAlloc(addr, size, MEM_COMMIT, nprot); + +#elif defined(JIT_VMEM_MMAP) + + int nprot; + + nprot = convert_prot(prot); + addr = mmap(0, size, nprot, MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); + if(addr == MAP_FAILED) + { + return (void *) 0; + } + return addr; + +#else + return 0; +#endif +} + +int +jit_vmem_decommit(void *addr, jit_uint size) +{ +#if defined(JIT_VMEM_WIN32) + + return VirtualFree(addr, size, MEM_DECOMMIT) != 0; + +#elif defined(JIT_VMEM_MMAP) + +#if defined(MADV_FREE) + int result = madvise(addr, size, MADV_FREE); + if(result < 0) + { + return 0; + } +#elif defined(MADV_DONTNEED) && defined(JIT_LINUX_PLATFORM) + int result = madvise(addr, size, MADV_DONTNEED); + if(result < 0) + { + return 0; + } +#endif + + addr = mmap(addr, size, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0); + if(addr == MAP_FAILED) + { + return 0; + } + return 1; + +#else + return 0; +#endif +} + +int +jit_vmem_protect(void *addr, jit_uint size, jit_prot_t prot) +{ +#if defined(JIT_VMEM_WIN32) + + DWORD nprot, oprot; + + nprot = convert_prot(prot); + if(VirtualProtect(addr, size, nprot, &oprot) == 0) + { + return 0; + } + return 1; + +#elif defined(JIT_VMEM_MMAP) + + int nprot; + + nprot = convert_prot(prot); + if(mprotect(addr, size, nprot) < 0) + { + return 0; + } + return 1; + +#else + return 0; +#endif +}