Skip to content

Commit

Permalink
Use virtual memory to allocate g_mem_base (if possible).
Browse files Browse the repository at this point in the history
  • Loading branch information
bsmiles32 committed Nov 5, 2017
1 parent 8964997 commit 3d7f6c4
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
3 changes: 3 additions & 0 deletions projects/unix/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ endif
ifeq ($(OS), FREEBSD)
TARGET = libmupen64plus$(POSTFIX).so.2.0.0
SONAME = libmupen64plus$(POSTFIX).so.2
CFLAGS += -DHAVE_MMAP
LDFLAGS += -Wl,-Bsymbolic -shared -Wl,-export-dynamic -Wl,-soname,$(SONAME)
LDLIBS += -L${LOCALBASE}/lib -lc
ifeq ($(ARCH_DETECTED), 64BITS)
Expand All @@ -171,6 +172,7 @@ endif
ifeq ($(OS), LINUX)
TARGET = libmupen64plus$(POSTFIX).so.2.0.0
SONAME = libmupen64plus$(POSTFIX).so.2
CFLAGS += -DHAVE_MMAP
LDFLAGS += -Wl,-Bsymbolic -shared -Wl,-export-dynamic -Wl,-soname,$(SONAME)
LDLIBS += -ldl
# only export api symbols
Expand All @@ -187,6 +189,7 @@ ifeq ($(OS), OSX)
OSX_SDK_PATH = $(OSX_SDK_ROOT)/$(shell ls $(OSX_SDK_ROOT) | tail -1)

TARGET = libmupen64plus$(POSTFIX).dylib
CFLAGS += -DHAVE_MMAP
LDFLAGS += -framework CoreFoundation -dynamiclib
LDLIBS += -ldl
ifeq ($(ARCH_DETECTED), 64BITS)
Expand Down
5 changes: 3 additions & 2 deletions src/api/frontend.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "m64p_config.h"
#include "m64p_frontend.h"
#include "m64p_types.h"
#include "device/memory/memory.h"
#include "main/cheat.h"
#include "main/eventloop.h"
#include "main/main.h"
Expand Down Expand Up @@ -93,7 +94,7 @@ EXPORT m64p_error CALL CoreStartup(int APIVersion, const char *ConfigPath, const
return M64ERR_INTERNAL;

/* allocate base memory */
g_mem_base = malloc(0x20000000);
g_mem_base = init_mem_base();
if (g_mem_base == NULL) {
return M64ERR_NO_MEMORY;
}
Expand Down Expand Up @@ -122,7 +123,7 @@ EXPORT m64p_error CALL CoreShutdown(void)
SDL_Quit();

/* deallocate base memory */
free(g_mem_base);
release_mem_base(g_mem_base);
g_mem_base = NULL;

l_CoreInit = 0;
Expand Down
79 changes: 79 additions & 0 deletions src/device/memory/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,82 @@ void map_region(struct memory* mem,
mem->handlers[region] = *handler;
}
}



#include "device/device.h"

#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#elif defined(HAVE_MMAP)
#include <sys/mman.h>
#else
#include <stdlib.h>
#endif

#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))

enum { MEM_BASE_SIZE = 0x20000000 };
enum { RAM_MAX_SIZE = 0x00800000 };
enum { ROM_MAX_SIZE = 0x04000000 };

void* init_mem_base(void)
{
void* mem;
size_t m;

struct {
uint32_t begin;
size_t size;
} mappings[] = {
{ MM_RDRAM_DRAM, RAM_MAX_SIZE }, // FIXME: use real dram_size
{ MM_RSP_MEM, 0x00002000 },
{ MM_DD_ROM, 0x00800000 },
{ MM_CART_ROM, ROM_MAX_SIZE }, // FIXME: use real rom_size
{ MM_PIF_MEM, 0x00000800 },
};

#if defined(_WIN32)
mem = VirtualAlloc(NULL, MEM_BASE_SIZE, MEM_RESERVE, PAGE_READWRITE);

for (m = 0; m < ARRAY_SIZE(mappings); ++m) {
if (VirtualAlloc((unsigned char*)mem + mappings[m].begin, mappings[m].size, MEM_COMMIT, PAGE_READWRITE) != 0) {
DebugMessage(M64MSG_ERROR, "Failed to change memory protection.");
VirtualFree(mem, 0, MEM_RELEASE);
return NULL;
}
}
#elif defined(HAVE_MMAP)
mem = mmap(NULL, MEM_BASE_SIZE, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (mem == MAP_FAILED) {
return NULL;
}

for (m = 0; m < ARRAY_SIZE(mappings); ++m) {
if (mprotect((unsigned char*)mem + mappings[m].begin, mappings[m].size, PROT_READ | PROT_WRITE) != 0) {
DebugMessage(M64MSG_ERROR, "Failed to change memory protection.");
munmap(mem, MEM_BASE_SIZE);
return NULL;
}
}
#else
mem = malloc(MEM_BASE_SIZE);
if (mem == NULL) {
return NULL;
}
#endif

return mem;
}

void release_mem_base(void* mem)
{
#if defined(_WIN32)
VirtualFree(mem, 0, MEM_RELEASE);
#elif defined(HAVE_MMAP)
munmap(mem, MEM_BASE_SIZE);
#else
free(mem);
#endif
}
3 changes: 3 additions & 0 deletions src/device/memory/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,8 @@ void deactivate_memory_break_write(struct memory* mem, uint32_t address);
int get_memory_type(struct memory* mem, uint32_t address);
#endif

void* init_mem_base(void);
void release_mem_base(void* mem);

#endif

0 comments on commit 3d7f6c4

Please sign in to comment.