]> git.unchartedbackwaters.co.uk Git - francis/libjit.git/commitdiff
2006-08-22 Klaus treichel <ktreichel@web.de>
authorKlaus Treichel <ktreichel@web.de>
Tue, 22 Aug 2006 18:02:55 +0000 (18:02 +0000)
committerKlaus Treichel <ktreichel@web.de>
Tue, 22 Aug 2006 18:02:55 +0000 (18:02 +0000)
* jit/jit-alloc.c: Use mmap and munmap to allocate executable memory
where available because memory allocated with malloc is not executable
on some archs/distros.

ChangeLog
jit/jit-alloc.c

index a50d4d57ce3a04f5de69e41a41efe1ff731692ff..44d417f701833e3aa8317d3100a4ef96ca15c1d3 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2006-08-22  Klaus treichel  <ktreichel@web.de>
+
+       * jit/jit-alloc.c: Use mmap and munmap to allocate executable memory
+       where available because memory allocated with malloc is not executable
+       on some archs/distros.
+
 2006-08-23  Aleksey Demakov  <ademakov@gmail.com>
 
        * jit/jit-reg-alloc.c: improve handling of three-address op codes.
index cd3ea47388172de98e332b21f664e8aa61f3286d..e2793823854c3a163ef5fbfd429f3e9f8c28388f 100644 (file)
        #include <windows.h>
        #include <io.h>
 #endif
+#if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MMAP) && defined(HAVE_MUNMAP)
+/*
+ * Make sure that "MAP_ANONYMOUS" is correctly defined, because it
+ * may not exist on some variants of Unix.
+ */
+#ifndef MAP_ANONYMOUS
+    #ifdef MAP_ANON
+        #define MAP_ANONYMOUS        MAP_ANON
+    #endif
+#endif
+#ifdef MAP_ANONYMOUS
+#define JIT_USE_MMAP
+#endif
+#endif
+
 
 /*@
  * @section Memory allocation
@@ -126,7 +141,18 @@ void jit_free(void *ptr)
 @*/
 void *jit_malloc_exec(unsigned int size)
 {
+#ifdef JIT_USE_MMAP
+       void *ptr = mmap(0, size, PROT_READ | PROT_WRITE | PROT_EXEC,
+                                                         MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+
+       if(ptr == (void *)-1)
+       {
+               return (void *)0;
+       }
+       return ptr;
+#else
        return malloc(size);
+#endif
 }
 
 /*@
@@ -141,7 +167,11 @@ void jit_free_exec(void *ptr, unsigned int size)
 {
        if(ptr)
        {
+       #ifdef JIT_USE_MMAP
+               munmap(ptr, size);
+       #else
                free(ptr);
+       #endif
        }
 }