Mercurial > repos > blastem
comparison mem.c @ 803:236a184bf6f0
Merge
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Sun, 26 Jul 2015 16:51:03 -0700 |
parents | c3e3a0d734e2 |
children | 9f149f0e98b7 |
comparison
equal
deleted
inserted
replaced
802:6811f601008f | 803:236a184bf6f0 |
---|---|
6 #include <sys/mman.h> | 6 #include <sys/mman.h> |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include <unistd.h> | 10 #include <unistd.h> |
11 #include <errno.h> | |
12 #include <stdio.h> | |
13 | |
11 #include "mem.h" | 14 #include "mem.h" |
15 #ifndef MAP_ANONYMOUS | |
16 #define MAP_ANONYMOUS MAP_ANON | |
17 #endif | |
12 | 18 |
13 /* | 19 #ifndef MAP_32BIT |
14 void * alloc_code(size_t *size) | 20 #define MAP_32BIT 0 |
15 { | 21 #endif |
16 *size += PAGE_SIZE - (*size & (PAGE_SIZE - 1)); | |
17 return mmap(NULL, *size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
18 } | |
19 */ | |
20 | |
21 /* | |
22 void * alloc_code(size_t *size) | |
23 { | |
24 char * ret = malloc(*size); | |
25 char * base = (char *)(((intptr_t)ret) & (~(PAGE_SIZE-1))); | |
26 mprotect(base, (ret + *size) - base, PROT_EXEC | PROT_READ | PROT_WRITE); | |
27 return ret; | |
28 } | |
29 */ | |
30 | 22 |
31 void * alloc_code(size_t *size) | 23 void * alloc_code(size_t *size) |
32 { | 24 { |
25 //start at the 1GB mark to allow plenty of room for sbrk based malloc implementations | |
26 //while still keeping well within 32-bit displacement range for calling code compiled into the executable | |
27 static uint8_t *next = (uint8_t *)0x40000000; | |
33 *size += PAGE_SIZE - (*size & (PAGE_SIZE - 1)); | 28 *size += PAGE_SIZE - (*size & (PAGE_SIZE - 1)); |
34 void * ret = sbrk(*size); | 29 uint8_t *ret = mmap(NULL, *size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0); |
35 if (ret == ((void *)-1)) { | 30 if (ret == MAP_FAILED) { |
31 perror("alloc_code"); | |
36 return NULL; | 32 return NULL; |
37 } | 33 } |
38 mprotect(ret, *size, PROT_EXEC | PROT_READ | PROT_WRITE); | 34 next = ret + *size; |
39 return ret; | 35 return ret; |
40 } | 36 } |
37 |