annotate mem.c @ 759:c47e1750c264

Use MAP_32BIT on Linux since my hint seems to be ignored
author Michael Pavone <pavone@retrodev.com>
date Sun, 28 Jun 2015 10:21:51 -0700
parents b52cd6854c28
children c3e3a0d734e2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
467
140af5509ce7 Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents: 18
diff changeset
1 /*
140af5509ce7 Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents: 18
diff changeset
2 Copyright 2013 Michael Pavone
140af5509ce7 Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents: 18
diff changeset
3 This file is part of BlastEm.
140af5509ce7 Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents: 18
diff changeset
4 BlastEm is free software distributed under the terms of the GNU General Public License version 3 or greater. See COPYING for full license text.
140af5509ce7 Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents: 18
diff changeset
5 */
14
2bdad0f52f42 x86 code gen, initial work on translator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
6 #include <sys/mman.h>
2bdad0f52f42 x86 code gen, initial work on translator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
7 #include <stddef.h>
18
3e7bfde7606e M68K to x86 translation works for a limited subset of instructions and addressing modes
Mike Pavone <pavone@retrodev.com>
parents: 14
diff changeset
8 #include <stdint.h>
3e7bfde7606e M68K to x86 translation works for a limited subset of instructions and addressing modes
Mike Pavone <pavone@retrodev.com>
parents: 14
diff changeset
9 #include <stdlib.h>
3e7bfde7606e M68K to x86 translation works for a limited subset of instructions and addressing modes
Mike Pavone <pavone@retrodev.com>
parents: 14
diff changeset
10 #include <unistd.h>
758
b52cd6854c28 Use mmap with a hint rather than sbrk for allocating executable memory within 32-bit displacement range of compiled code
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
11
14
2bdad0f52f42 x86 code gen, initial work on translator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
12 #include "mem.h"
758
b52cd6854c28 Use mmap with a hint rather than sbrk for allocating executable memory within 32-bit displacement range of compiled code
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
13 #ifndef MAP_ANONYMOUS
b52cd6854c28 Use mmap with a hint rather than sbrk for allocating executable memory within 32-bit displacement range of compiled code
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
14 #define MAP_ANONYMOUS MAP_ANON
b52cd6854c28 Use mmap with a hint rather than sbrk for allocating executable memory within 32-bit displacement range of compiled code
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
15 #endif
18
3e7bfde7606e M68K to x86 translation works for a limited subset of instructions and addressing modes
Mike Pavone <pavone@retrodev.com>
parents: 14
diff changeset
16
759
c47e1750c264 Use MAP_32BIT on Linux since my hint seems to be ignored
Michael Pavone <pavone@retrodev.com>
parents: 758
diff changeset
17 #ifndef MAP_32BIT
c47e1750c264 Use MAP_32BIT on Linux since my hint seems to be ignored
Michael Pavone <pavone@retrodev.com>
parents: 758
diff changeset
18 #define MAP_32BIT 0
c47e1750c264 Use MAP_32BIT on Linux since my hint seems to be ignored
Michael Pavone <pavone@retrodev.com>
parents: 758
diff changeset
19 #endif
c47e1750c264 Use MAP_32BIT on Linux since my hint seems to be ignored
Michael Pavone <pavone@retrodev.com>
parents: 758
diff changeset
20
18
3e7bfde7606e M68K to x86 translation works for a limited subset of instructions and addressing modes
Mike Pavone <pavone@retrodev.com>
parents: 14
diff changeset
21 void * alloc_code(size_t *size)
3e7bfde7606e M68K to x86 translation works for a limited subset of instructions and addressing modes
Mike Pavone <pavone@retrodev.com>
parents: 14
diff changeset
22 {
758
b52cd6854c28 Use mmap with a hint rather than sbrk for allocating executable memory within 32-bit displacement range of compiled code
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
23 //start at the 1GB mark to allow plenty of room for sbrk based malloc implementations
b52cd6854c28 Use mmap with a hint rather than sbrk for allocating executable memory within 32-bit displacement range of compiled code
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
24 //while still keeping well within 32-bit displacement range for calling code compiled into the executable
b52cd6854c28 Use mmap with a hint rather than sbrk for allocating executable memory within 32-bit displacement range of compiled code
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
25 static uint8_t *next = (uint8_t *)0x40000000;
18
3e7bfde7606e M68K to x86 translation works for a limited subset of instructions and addressing modes
Mike Pavone <pavone@retrodev.com>
parents: 14
diff changeset
26 *size += PAGE_SIZE - (*size & (PAGE_SIZE - 1));
759
c47e1750c264 Use MAP_32BIT on Linux since my hint seems to be ignored
Michael Pavone <pavone@retrodev.com>
parents: 758
diff changeset
27 uint8_t *ret = mmap(NULL, *size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0);
758
b52cd6854c28 Use mmap with a hint rather than sbrk for allocating executable memory within 32-bit displacement range of compiled code
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
28 next = ret + *size;
18
3e7bfde7606e M68K to x86 translation works for a limited subset of instructions and addressing modes
Mike Pavone <pavone@retrodev.com>
parents: 14
diff changeset
29 return ret;
3e7bfde7606e M68K to x86 translation works for a limited subset of instructions and addressing modes
Mike Pavone <pavone@retrodev.com>
parents: 14
diff changeset
30 }
758
b52cd6854c28 Use mmap with a hint rather than sbrk for allocating executable memory within 32-bit displacement range of compiled code
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
31