diff options
author | Michael Pavone <pavone@retrodev.com> | 2015-07-26 16:51:03 -0700 |
---|---|---|
committer | Michael Pavone <pavone@retrodev.com> | 2015-07-26 16:51:03 -0700 |
commit | 38c7278727cb3dd65d9e5dc98e69dd306b12c476 (patch) | |
tree | 616c527669fcab6828bcb71949e3ccd3f054c75d /mem.c | |
parent | 2c702a302754c4eb4a4c2aeabd9e533b0efd2bde (diff) | |
parent | 6cd1ab5ac4004b9e9433c5d16a6ed7fa049cf767 (diff) |
Merge
Diffstat (limited to 'mem.c')
-rw-r--r-- | mem.c | 37 |
1 files changed, 17 insertions, 20 deletions
@@ -8,33 +8,30 @@ #include <stdint.h> #include <stdlib.h> #include <unistd.h> +#include <errno.h> +#include <stdio.h> + #include "mem.h" +#ifndef MAP_ANONYMOUS +#define MAP_ANONYMOUS MAP_ANON +#endif -/* -void * alloc_code(size_t *size) -{ - *size += PAGE_SIZE - (*size & (PAGE_SIZE - 1)); - return mmap(NULL, *size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); -} -*/ - -/* -void * alloc_code(size_t *size) -{ - char * ret = malloc(*size); - char * base = (char *)(((intptr_t)ret) & (~(PAGE_SIZE-1))); - mprotect(base, (ret + *size) - base, PROT_EXEC | PROT_READ | PROT_WRITE); - return ret; -} -*/ +#ifndef MAP_32BIT +#define MAP_32BIT 0 +#endif void * alloc_code(size_t *size) { + //start at the 1GB mark to allow plenty of room for sbrk based malloc implementations + //while still keeping well within 32-bit displacement range for calling code compiled into the executable + static uint8_t *next = (uint8_t *)0x40000000; *size += PAGE_SIZE - (*size & (PAGE_SIZE - 1)); - void * ret = sbrk(*size); - if (ret == ((void *)-1)) { + uint8_t *ret = mmap(NULL, *size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0); + if (ret == MAP_FAILED) { + perror("alloc_code"); return NULL; } - mprotect(ret, *size, PROT_EXEC | PROT_READ | PROT_WRITE); + next = ret + *size; return ret; } + |