Mercurial > repos > blastem
comparison mem.c @ 2406:8e86cd581620
Better implementation of alloc_code for ASLR/libretro cases that also hopefully works on modern Mac OS
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Tue, 02 Jan 2024 21:07:09 -0800 |
parents | 101b5ce682fe |
children | 7d851046f035 |
comparison
equal
deleted
inserted
replaced
2405:b50fa7602e39 | 2406:8e86cd581620 |
---|---|
15 #include "arena.h" | 15 #include "arena.h" |
16 #ifndef MAP_ANONYMOUS | 16 #ifndef MAP_ANONYMOUS |
17 #define MAP_ANONYMOUS MAP_ANON | 17 #define MAP_ANONYMOUS MAP_ANON |
18 #endif | 18 #endif |
19 | 19 |
20 #ifndef MAP_32BIT | |
21 #define MAP_32BIT 0 | |
22 #endif | |
23 | |
24 void * alloc_code(size_t *size) | 20 void * alloc_code(size_t *size) |
25 { | 21 { |
26 //start at the 1GB mark to allow plenty of room for sbrk based malloc implementations | 22 //start at 1GB above compiled code to allow plenty of room for sbrk based malloc implementations |
27 //while still keeping well within 32-bit displacement range for calling code compiled into the executable | 23 //while still keeping well within 32-bit displacement range for calling code compiled into the executable |
28 static uint8_t *next = (uint8_t *)0x40000000; | 24 static uint8_t *next = ((uint8_t *)alloc_code) + 0x40000000; |
29 uint8_t *ret = try_alloc_arena(); | 25 uint8_t *ret = try_alloc_arena(); |
30 if (ret) { | 26 if (ret) { |
31 return ret; | 27 return ret; |
32 } | 28 } |
33 if (*size & (PAGE_SIZE -1)) { | 29 if (*size & (PAGE_SIZE -1)) { |
34 *size += PAGE_SIZE - (*size & (PAGE_SIZE - 1)); | 30 *size += PAGE_SIZE - (*size & (PAGE_SIZE - 1)); |
35 } | 31 } |
36 ret = mmap(next, *size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0); | 32 ret = mmap(next, *size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
37 if (ret == MAP_FAILED) { | 33 if (ret == MAP_FAILED) { |
38 perror("alloc_code"); | 34 perror("alloc_code"); |
39 return NULL; | 35 return NULL; |
40 } | 36 } |
37 printf("alloc_code next was %p, ret is %p, alloc_code address %p\n", next, ret, alloc_code); | |
41 track_block(ret); | 38 track_block(ret); |
42 next = ret + *size; | 39 next = ret + *size; |
43 return ret; | 40 return ret; |
44 } | 41 } |
45 | 42 |