comparison mem.c @ 784:c3e3a0d734e2

Better error handling in alloc_code
author Michael Pavone <pavone@retrodev.com>
date Tue, 21 Jul 2015 21:29:43 -0700
parents c47e1750c264
children 9f149f0e98b7
comparison
equal deleted inserted replaced
783:e64975fc5f98 784:c3e3a0d734e2
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>
11 13
12 #include "mem.h" 14 #include "mem.h"
13 #ifndef MAP_ANONYMOUS 15 #ifndef MAP_ANONYMOUS
14 #define MAP_ANONYMOUS MAP_ANON 16 #define MAP_ANONYMOUS MAP_ANON
15 #endif 17 #endif
23 //start at the 1GB mark to allow plenty of room for sbrk based malloc implementations 25 //start at the 1GB mark to allow plenty of room for sbrk based malloc implementations
24 //while still keeping well within 32-bit displacement range for calling code compiled into the executable 26 //while still keeping well within 32-bit displacement range for calling code compiled into the executable
25 static uint8_t *next = (uint8_t *)0x40000000; 27 static uint8_t *next = (uint8_t *)0x40000000;
26 *size += PAGE_SIZE - (*size & (PAGE_SIZE - 1)); 28 *size += PAGE_SIZE - (*size & (PAGE_SIZE - 1));
27 uint8_t *ret = mmap(NULL, *size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0); 29 uint8_t *ret = mmap(NULL, *size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0);
30 if (ret == MAP_FAILED) {
31 perror("alloc_code");
32 return NULL;
33 }
28 next = ret + *size; 34 next = ret + *size;
29 return ret; 35 return ret;
30 } 36 }
31 37