Improve code for making executable memory

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@26727 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2008-06-29 09:18:33 +00:00
parent 721a6881f8
commit 070482eebc

View file

@ -76,16 +76,28 @@
- (id) initWithSize: (unsigned)_size - (id) initWithSize: (unsigned)_size
{ {
#if defined(HAVE_MMAP) #if defined(HAVE_MMAP)
#ifndef MAP_ANON #ifndef MAP_ANONYMOUS
#define MAP_ANON MAP_ANONYMOUS #define MAP_ANONYMOUS MAP_ANON
#endif #endif
#if defined(HAVE_MPROTECT)
/* We have mprotect, so we create memory as writable and change it to
* executable later (writable and executable may not be possible at
* the same time).
*/
buffer = mmap (NULL, _size, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
#else
/* We do not have mprotect, so we have to try to create writable and
* executable memory.
*/
buffer = mmap (NULL, _size, PROT_READ|PROT_WRITE|PROT_EXEC, buffer = mmap (NULL, _size, PROT_READ|PROT_WRITE|PROT_EXEC,
MAP_PRIVATE|MAP_ANON, -1, 0); MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
#endif /* HAVE_MPROTECT */
if (buffer == (void*)-1) if (buffer == (void*)-1)
#else #else
buffer = malloc(_size); buffer = malloc(_size);
if (buffer == (void*)0) if (buffer == (void*)0)
#endif #endif /* HAVE_MMAP */
{ {
NSLog(@"Failed to map %u bytes for FFI: %@", _size, [NSError _last]); NSLog(@"Failed to map %u bytes for FFI: %@", _size, [NSError _last]);
buffer = 0; buffer = 0;
@ -105,7 +117,7 @@
- (void) protect - (void) protect
{ {
#if defined(HAVE_MPROTECT) #if defined(HAVE_MPROTECT)
if (mprotect(buffer, size, PROT_READ | PROT_EXEC) == -1) if (mprotect(buffer, size, PROT_READ|PROT_EXEC) == -1)
{ {
NSLog(@"Failed to protect closure for FFI: %@", [NSError _last]); NSLog(@"Failed to protect closure for FFI: %@", [NSError _last]);
} }