Mercurial > repos > blastem
annotate mem.c @ 758:b52cd6854c28
Use mmap with a hint rather than sbrk for allocating executable memory within 32-bit displacement range of compiled code
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Sun, 28 Jun 2015 10:12:37 -0700 |
parents | 140af5509ce7 |
children | c47e1750c264 |
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 |
3e7bfde7606e
M68K to x86 translation works for a limited subset of instructions and addressing modes
Mike Pavone <pavone@retrodev.com>
parents:
14
diff
changeset
|
17 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
|
18 { |
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
|
19 //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
|
20 //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
|
21 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
|
22 *size += PAGE_SIZE - (*size & (PAGE_SIZE - 1)); |
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 uint8_t *ret = mmap(NULL, *size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
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 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
|
25 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
|
26 } |
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
|
27 |