annotate mem.c @ 1021:4a92889e2889 v0.4.0

Fix OS X build
author Michael Pavone <pavone@retrodev.com>
date Wed, 04 May 2016 00:50:20 -0700
parents 6bafe1988e8c
children 101b5ce682fe
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
883
9f149f0e98b7 It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents: 784
diff changeset
3 This file is part of BlastEm.
467
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>
784
c3e3a0d734e2 Better error handling in alloc_code
Michael Pavone <pavone@retrodev.com>
parents: 759
diff changeset
11 #include <errno.h>
c3e3a0d734e2 Better error handling in alloc_code
Michael Pavone <pavone@retrodev.com>
parents: 759
diff changeset
12 #include <stdio.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
14
2bdad0f52f42 x86 code gen, initial work on translator
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
14 #include "mem.h"
883
9f149f0e98b7 It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents: 784
diff changeset
15 #include "arena.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
16 #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
17 #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
18 #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
19
759
c47e1750c264 Use MAP_32BIT on Linux since my hint seems to be ignored
Michael Pavone <pavone@retrodev.com>
parents: 758
diff changeset
20 #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
21 #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
22 #endif
c47e1750c264 Use MAP_32BIT on Linux since my hint seems to be ignored
Michael Pavone <pavone@retrodev.com>
parents: 758
diff changeset
23
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
24 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
25 {
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
26 //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
27 //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
28 static uint8_t *next = (uint8_t *)0x40000000;
883
9f149f0e98b7 It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents: 784
diff changeset
29 uint8_t *ret = try_alloc_arena();
9f149f0e98b7 It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents: 784
diff changeset
30 if (ret) {
9f149f0e98b7 It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents: 784
diff changeset
31 return ret;
9f149f0e98b7 It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents: 784
diff changeset
32 }
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
33 *size += PAGE_SIZE - (*size & (PAGE_SIZE - 1));
883
9f149f0e98b7 It is now possible to switch back and forth between the menu ROM and the game
Michael Pavone <pavone@retrodev.com>
parents: 784
diff changeset
34 ret = mmap(NULL, *size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0);
784
c3e3a0d734e2 Better error handling in alloc_code
Michael Pavone <pavone@retrodev.com>
parents: 759
diff changeset
35 if (ret == MAP_FAILED) {
c3e3a0d734e2 Better error handling in alloc_code
Michael Pavone <pavone@retrodev.com>
parents: 759
diff changeset
36 perror("alloc_code");
c3e3a0d734e2 Better error handling in alloc_code
Michael Pavone <pavone@retrodev.com>
parents: 759
diff changeset
37 return NULL;
c3e3a0d734e2 Better error handling in alloc_code
Michael Pavone <pavone@retrodev.com>
parents: 759
diff changeset
38 }
904
6bafe1988e8c Actually call track_block in alloc_code
Michael Pavone <pavone@retrodev.com>
parents: 883
diff changeset
39 track_block(ret);
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
40 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
41 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
42 }
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
43