From 3396f352cb4c16aa4732df25425ff02574b8dc01 Mon Sep 17 00:00:00 2001 From: Yuri Kunde Schlesner Date: Thu, 7 May 2015 19:01:09 -0300 Subject: [PATCH] Common: Remove mem_arena.cpp/h It is superfluous for Citra. (It's only really necessary if you're doing JIT. We were using it but not taking any advantage from it.) This should make 32-bit builds work again. --- src/common/CMakeLists.txt | 2 - src/common/mem_arena.cpp | 394 -------------------------------------- src/common/mem_arena.h | 70 ------- src/core/mem_map.cpp | 113 +++-------- src/core/mem_map.h | 12 -- 5 files changed, 31 insertions(+), 560 deletions(-) delete mode 100644 src/common/mem_arena.cpp delete mode 100644 src/common/mem_arena.h diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index b5b129a09..11659c3c5 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -10,7 +10,6 @@ set(SRCS logging/text_formatter.cpp logging/backend.cpp math_util.cpp - mem_arena.cpp memory_util.cpp misc.cpp profiler.cpp @@ -43,7 +42,6 @@ set(HEADERS logging/backend.h make_unique.h math_util.h - mem_arena.h memory_util.h platform.h profiler.h diff --git a/src/common/mem_arena.cpp b/src/common/mem_arena.cpp deleted file mode 100644 index 689fdb92b..000000000 --- a/src/common/mem_arena.cpp +++ /dev/null @@ -1,394 +0,0 @@ -// Copyright (C) 2003 Dolphin Project. - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, version 2.0 or later versions. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License 2.0 for more details. - -// A copy of the GPL 2.0 should have been included with the program. -// If not, see http://www.gnu.org/licenses/ - -// Official SVN repository and contact information can be found at -// http://code.google.com/p/dolphin-emu/ - -#include - -#include "common/logging/log.h" -#include "common/mem_arena.h" -#include "common/memory_util.h" -#include "common/platform.h" -#include "common/string_util.h" - -#ifndef _WIN32 -#include -#include -#include - -#ifdef ANDROID -#include -#include -#endif -#endif - -#ifdef ANDROID - -// Hopefully this ABI will never change... - - -#define ASHMEM_DEVICE "/dev/ashmem" - -/* -* ashmem_create_region - creates a new ashmem region and returns the file -* descriptor, or <0 on error -* -* `name' is an optional label to give the region (visible in /proc/pid/maps) -* `size' is the size of the region, in page-aligned bytes -*/ -int ashmem_create_region(const char *name, size_t size) -{ - int fd, ret; - - fd = open(ASHMEM_DEVICE, O_RDWR); - if (fd < 0) - return fd; - - if (name) { - char buf[ASHMEM_NAME_LEN]; - - strncpy(buf, name, sizeof(buf)); - ret = ioctl(fd, ASHMEM_SET_NAME, buf); - if (ret < 0) - goto error; - } - - ret = ioctl(fd, ASHMEM_SET_SIZE, size); - if (ret < 0) - goto error; - - return fd; - -error: - LOG_ERROR(Common_Memory, "NASTY ASHMEM ERROR: ret = %08x", ret); - close(fd); - return ret; -} - -int ashmem_set_prot_region(int fd, int prot) -{ - return ioctl(fd, ASHMEM_SET_PROT_MASK, prot); -} - -int ashmem_pin_region(int fd, size_t offset, size_t len) -{ - struct ashmem_pin pin = { offset, len }; - return ioctl(fd, ASHMEM_PIN, &pin); -} - -int ashmem_unpin_region(int fd, size_t offset, size_t len) -{ - struct ashmem_pin pin = { offset, len }; - return ioctl(fd, ASHMEM_UNPIN, &pin); -} -#endif // Android - - -#if defined(_WIN32) -SYSTEM_INFO sysInfo; -#endif - - -// Windows mappings need to be on 64K boundaries, due to Alpha legacy. -#ifdef _WIN32 -size_t roundup(size_t x) { - int gran = sysInfo.dwAllocationGranularity ? sysInfo.dwAllocationGranularity : 0x10000; - return (x + gran - 1) & ~(gran - 1); -} -#else -size_t roundup(size_t x) { - return x; -} -#endif - - -void MemArena::GrabLowMemSpace(size_t size) -{ -#ifdef _WIN32 - hMemoryMapping = CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0, (DWORD)(size), nullptr); - GetSystemInfo(&sysInfo); -#elif defined(ANDROID) - // Use ashmem so we don't have to allocate a file on disk! - fd = ashmem_create_region("Citra_RAM", size); - // Note that it appears that ashmem is pinned by default, so no need to pin. - if (fd < 0) - { - LOG_ERROR(Common_Memory, "Failed to grab ashmem space of size: %08x errno: %d", (int)size, (int)(errno)); - return; - } -#else - // Try to find a non-existing filename for our shared memory. - // In most cases the first one will be available, but it's nicer to search - // a bit more. - for (int i = 0; i < 10000; i++) - { - std::string file_name = Common::StringFromFormat("/citramem.%d", i); - fd = shm_open(file_name.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600); - if (fd != -1) - { - shm_unlink(file_name.c_str()); - break; - } - else if (errno != EEXIST) - { - LOG_ERROR(Common_Memory, "shm_open failed: %s", strerror(errno)); - return; - } - } - if (ftruncate(fd, size) < 0) - LOG_ERROR(Common_Memory, "Failed to allocate low memory space"); -#endif -} - - -void MemArena::ReleaseSpace() -{ -#ifdef _WIN32 - CloseHandle(hMemoryMapping); - hMemoryMapping = 0; -#else - close(fd); -#endif -} - - -void *MemArena::CreateView(s64 offset, size_t size, void *base) -{ -#ifdef _WIN32 - size = roundup(size); - void *ptr = MapViewOfFileEx(hMemoryMapping, FILE_MAP_ALL_ACCESS, 0, (DWORD)((u64)offset), size, base); - return ptr; -#else - void *retval = mmap(base, size, PROT_READ | PROT_WRITE, MAP_SHARED | - // Do not sync memory to underlying file. Linux has this by default. -#ifdef __FreeBSD__ - MAP_NOSYNC | -#endif - ((base == nullptr) ? 0 : MAP_FIXED), fd, offset); - - if (retval == MAP_FAILED) - { - LOG_ERROR(Common_Memory, "mmap failed"); - return nullptr; - } - return retval; -#endif -} - - -void MemArena::ReleaseView(void* view, size_t size) -{ -#ifdef _WIN32 - UnmapViewOfFile(view); -#else - munmap(view, size); -#endif -} - -u8* MemArena::Find4GBBase() -{ -#if EMU_ARCH_BITS == 64 -#ifdef _WIN32 - // 64 bit - u8* base = (u8*)VirtualAlloc(0, 0xE1000000, MEM_RESERVE, PAGE_READWRITE); - VirtualFree(base, 0, MEM_RELEASE); - return base; -#else - // Very precarious - mmap cannot return an error when trying to map already used pages. - // This makes the Windows approach above unusable on Linux, so we will simply pray... - return reinterpret_cast(0x2300000000ULL); -#endif - -#else // 32 bit - -#ifdef _WIN32 - u8* base = (u8*)VirtualAlloc(0, 0x10000000, MEM_RESERVE, PAGE_READWRITE); - if (base) { - VirtualFree(base, 0, MEM_RELEASE); - } - return base; -#else - void* base = mmap(0, 0x10000000, PROT_READ | PROT_WRITE, - MAP_ANON | MAP_SHARED, -1, 0); - if (base == MAP_FAILED) { - LOG_ERROR(Common_Memory, "Failed to map 256 MB of memory space: %s", strerror(errno)); - return 0; - } - munmap(base, 0x10000000); - return static_cast(base); -#endif -#endif -} - - -// yeah, this could also be done in like two bitwise ops... -#define SKIP(a_flags, b_flags) -//if (!(a_flags & MV_WII_ONLY) && (b_flags & MV_WII_ONLY)) -// continue; -//if (!(a_flags & MV_FAKE_VMEM) && (b_flags & MV_FAKE_VMEM)) -// continue; - -static bool Memory_TryBase(u8 *base, const MemoryView *views, int num_views, u32 flags, MemArena *arena) { - // OK, we know where to find free space. Now grab it! - // We just mimic the popular BAT setup. - size_t position = 0; - size_t last_position = 0; - - // Zero all the pointers to be sure. - for (int i = 0; i < num_views; i++) - { - if (views[i].out_ptr_low) - *views[i].out_ptr_low = 0; - if (views[i].out_ptr) - *views[i].out_ptr = 0; - } - - int i; - for (i = 0; i < num_views; i++) - { - const MemoryView &view = views[i]; - if (view.size == 0) - continue; - SKIP(flags, view.flags); - if (view.flags & MV_MIRROR_PREVIOUS) { - position = last_position; - } - else { - *(view.out_ptr_low) = (u8*)arena->CreateView(position, view.size); - if (!*view.out_ptr_low) - goto bail; - } -#if EMU_ARCH_BITS == 64 - *view.out_ptr = (u8*)arena->CreateView( - position, view.size, base + view.virtual_address); -#else - if (view.flags & MV_MIRROR_PREVIOUS) { // TODO: should check if the two & 0x3FFFFFFF are identical. - // No need to create multiple identical views. - *view.out_ptr = *views[i - 1].out_ptr; - } - else { - *view.out_ptr = (u8*)arena->CreateView( - position, view.size, base + (view.virtual_address & 0x3FFFFFFF)); - if (!*view.out_ptr) - goto bail; - } -#endif - - last_position = position; - position += roundup(view.size); - } - - return true; - -bail: - // Argh! ERROR! Free what we grabbed so far so we can try again. - for (int j = 0; j <= i; j++) - { - if (views[i].size == 0) - continue; - SKIP(flags, views[i].flags); - if (views[j].out_ptr_low && *views[j].out_ptr_low) - { - arena->ReleaseView(*views[j].out_ptr_low, views[j].size); - *views[j].out_ptr_low = nullptr; - } - if (*views[j].out_ptr) - { -#if EMU_ARCH_BITS == 64 - arena->ReleaseView(*views[j].out_ptr, views[j].size); -#else - if (!(views[j].flags & MV_MIRROR_PREVIOUS)) - { - arena->ReleaseView(*views[j].out_ptr, views[j].size); - } -#endif - *views[j].out_ptr = nullptr; - } - } - return false; -} - -u8 *MemoryMap_Setup(const MemoryView *views, int num_views, u32 flags, MemArena *arena) -{ - size_t total_mem = 0; - int base_attempts = 0; - - for (int i = 0; i < num_views; i++) - { - if (views[i].size == 0) - continue; - SKIP(flags, views[i].flags); - if ((views[i].flags & MV_MIRROR_PREVIOUS) == 0) - total_mem += roundup(views[i].size); - } - // Grab some pagefile backed memory out of the void ... - arena->GrabLowMemSpace(total_mem); - - // Now, create views in high memory where there's plenty of space. -#if EMU_ARCH_BITS == 64 - u8 *base = MemArena::Find4GBBase(); - // This really shouldn't fail - in 64-bit, there will always be enough - // address space. - if (!Memory_TryBase(base, views, num_views, flags, arena)) - { - LOG_ERROR(Common_Memory, "MemoryMap_Setup: Failed finding a memory base."); - return 0; - } -#elif defined(_WIN32) - // Try a whole range of possible bases. Return once we got a valid one. - u32 max_base_addr = 0x7FFF0000 - 0x10000000; - u8 *base = nullptr; - - for (u32 base_addr = 0x01000000; base_addr < max_base_addr; base_addr += 0x400000) - { - base_attempts++; - base = (u8 *)base_addr; - if (Memory_TryBase(base, views, num_views, flags, arena)) - { - LOG_DEBUG(Common_Memory, "Found valid memory base at %p after %i tries.", base, base_attempts); - base_attempts = 0; - break; - } - } -#else - // Linux32 is fine with the x64 method, although limited to 32-bit with no automirrors. - u8 *base = MemArena::Find4GBBase(); - if (!Memory_TryBase(base, views, num_views, flags, arena)) - { - LOG_ERROR(Common_Memory, "MemoryMap_Setup: Failed finding a memory base."); - return 0; - } -#endif - if (base_attempts) - LOG_ERROR(Common_Memory, "No possible memory base pointer found!"); - return base; -} - -void MemoryMap_Shutdown(const MemoryView *views, int num_views, u32 flags, MemArena *arena) -{ - for (int i = 0; i < num_views; i++) - { - if (views[i].size == 0) - continue; - SKIP(flags, views[i].flags); - if (views[i].out_ptr_low && *views[i].out_ptr_low) - arena->ReleaseView(*views[i].out_ptr_low, views[i].size); - if (*views[i].out_ptr && (views[i].out_ptr_low && *views[i].out_ptr != *views[i].out_ptr_low)) - arena->ReleaseView(*views[i].out_ptr, views[i].size); - *views[i].out_ptr = nullptr; - if (views[i].out_ptr_low) - *views[i].out_ptr_low = nullptr; - } -} diff --git a/src/common/mem_arena.h b/src/common/mem_arena.h deleted file mode 100644 index d514fe58c..000000000 --- a/src/common/mem_arena.h +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright (C) 2003 Dolphin Project. - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, version 2.0 or later versions. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License 2.0 for more details. - -// A copy of the GPL 2.0 should have been included with the program. -// If not, see http://www.gnu.org/licenses/ - -// Official SVN repository and contact information can be found at -// http://code.google.com/p/dolphin-emu/ - -#pragma once - -#ifdef _WIN32 -#include -#endif - -#include "common/common_types.h" - -// This class lets you create a block of anonymous RAM, and then arbitrarily map views into it. -// Multiple views can mirror the same section of the block, which makes it very convient for emulating -// memory mirrors. - -class MemArena -{ -public: - void GrabLowMemSpace(size_t size); - void ReleaseSpace(); - void *CreateView(s64 offset, size_t size, void *base = 0); - void ReleaseView(void *view, size_t size); - - // This only finds 1 GB in 32-bit - static u8 *Find4GBBase(); -private: - -#ifdef _WIN32 - HANDLE hMemoryMapping; -#else - int fd; -#endif -}; - -enum { - MV_MIRROR_PREVIOUS = 1, - // MV_FAKE_VMEM = 2, - // MV_WII_ONLY = 4, - MV_IS_PRIMARY_RAM = 0x100, - MV_IS_EXTRA1_RAM = 0x200, - MV_IS_EXTRA2_RAM = 0x400, -}; - -struct MemoryView -{ - u8 **out_ptr_low; - u8 **out_ptr; - u32 virtual_address; - u32 size; - u32 flags; -}; - -// Uses a memory arena to set up an emulator-friendly memory map according to -// a passed-in list of MemoryView structures. -u8 *MemoryMap_Setup(const MemoryView *views, int num_views, u32 flags, MemArena *arena); -void MemoryMap_Shutdown(const MemoryView *views, int num_views, u32 flags, MemArena *arena); diff --git a/src/core/mem_map.cpp b/src/core/mem_map.cpp index ae88cfb11..46e271c80 100644 --- a/src/core/mem_map.cpp +++ b/src/core/mem_map.cpp @@ -2,10 +2,8 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. -#include "common/common_funcs.h" #include "common/common_types.h" #include "common/logging/log.h" -#include "common/mem_arena.h" #include "core/mem_map.h" @@ -13,100 +11,51 @@ namespace Memory { -u8* g_base; ///< The base pointer to the auto-mirrored arena. +u8* g_exefs_code; ///< ExeFS:/.code is loaded here +u8* g_system_mem; ///< System memory +u8* g_heap; ///< Application heap (main memory) +u8* g_heap_linear; ///< Linear heap +u8* g_vram; ///< Video memory (VRAM) pointer +u8* g_shared_mem; ///< Shared memory +u8* g_dsp_mem; ///< DSP memory +u8* g_kernel_mem; ///< Kernel memory -static MemArena arena; ///< The MemArena class +namespace { -u8* g_exefs_code; ///< ExeFS:/.code is loaded here -u8* g_system_mem; ///< System memory -u8* g_heap; ///< Application heap (main memory) -u8* g_heap_linear; ///< Linear heap -u8* g_vram; ///< Video memory (VRAM) pointer -u8* g_shared_mem; ///< Shared memory -u8* g_dsp_mem; ///< DSP memory -u8* g_kernel_mem; ///< Kernel memory - -static u8* physical_bootrom; ///< Bootrom physical memory -static u8* uncached_bootrom; - -static u8* physical_exefs_code; ///< Phsical ExeFS:/.code is loaded here -static u8* physical_system_mem; ///< System physical memory -static u8* physical_fcram; ///< Main physical memory (FCRAM) -static u8* physical_heap_gsp; ///< GSP heap physical memory -static u8* physical_vram; ///< Video physical memory (VRAM) -static u8* physical_shared_mem; ///< Physical shared memory -static u8* physical_dsp_mem; ///< Physical DSP memory -static u8* physical_kernel_mem; ///< Kernel memory - -// We don't declare the IO region in here since its handled by other means. -static MemoryView g_views[] = { - {&g_exefs_code, &physical_exefs_code, EXEFS_CODE_VADDR, EXEFS_CODE_SIZE, 0}, - {&g_vram, &physical_vram, VRAM_VADDR, VRAM_SIZE, 0}, - {&g_heap, &physical_fcram, HEAP_VADDR, HEAP_SIZE, MV_IS_PRIMARY_RAM}, - {&g_shared_mem, &physical_shared_mem, SHARED_MEMORY_VADDR, SHARED_MEMORY_SIZE, 0}, - {&g_system_mem, &physical_system_mem, SYSTEM_MEMORY_VADDR, SYSTEM_MEMORY_SIZE, 0}, - {&g_dsp_mem, &physical_dsp_mem, DSP_MEMORY_VADDR, DSP_MEMORY_SIZE, 0}, - {&g_kernel_mem, &physical_kernel_mem, KERNEL_MEMORY_VADDR, KERNEL_MEMORY_SIZE, 0}, - {&g_heap_linear, &physical_heap_gsp, HEAP_LINEAR_VADDR, HEAP_LINEAR_SIZE, 0}, +struct MemoryArea { + u8** ptr; + size_t size; }; -/*static MemoryView views[] = -{ - {&m_pScratchPad, &m_pPhysicalScratchPad, 0x00010000, SCRATCHPAD_SIZE, 0}, - {NULL, &m_pUncachedScratchPad, 0x40010000, SCRATCHPAD_SIZE, MV_MIRROR_PREVIOUS}, - {&m_pVRAM, &m_pPhysicalVRAM, 0x04000000, 0x00800000, 0}, - {NULL, &m_pUncachedVRAM, 0x44000000, 0x00800000, MV_MIRROR_PREVIOUS}, - {&m_pRAM, &m_pPhysicalRAM, 0x08000000, g_MemorySize, MV_IS_PRIMARY_RAM}, // only from 0x08800000 is it usable (last 24 megs) - {NULL, &m_pUncachedRAM, 0x48000000, g_MemorySize, MV_MIRROR_PREVIOUS | MV_IS_PRIMARY_RAM}, - {NULL, &m_pKernelRAM, 0x88000000, g_MemorySize, MV_MIRROR_PREVIOUS | MV_IS_PRIMARY_RAM}, +// We don't declare the IO regions in here since its handled by other means. +static MemoryArea memory_areas[] = { + {&g_exefs_code, EXEFS_CODE_SIZE }, + {&g_vram, VRAM_SIZE }, + {&g_heap, HEAP_SIZE }, + {&g_shared_mem, SHARED_MEMORY_SIZE}, + {&g_system_mem, SYSTEM_MEMORY_SIZE}, + {&g_dsp_mem, DSP_MEMORY_SIZE }, + {&g_kernel_mem, KERNEL_MEMORY_SIZE}, + {&g_heap_linear, HEAP_LINEAR_SIZE }, +}; - // TODO: There are a few swizzled mirrors of VRAM, not sure about the best way to - // implement those. -};*/ - -static const int kNumMemViews = sizeof(g_views) / sizeof(MemoryView); ///< Number of mem views +} void Init() { - int flags = 0; - - for (size_t i = 0; i < ARRAY_SIZE(g_views); i++) { - if (g_views[i].flags & MV_IS_PRIMARY_RAM) - g_views[i].size = FCRAM_SIZE; + for (MemoryArea& area : memory_areas) { + *area.ptr = new u8[area.size]; } - - g_base = MemoryMap_Setup(g_views, kNumMemViews, flags, &arena); MemBlock_Init(); - LOG_DEBUG(HW_Memory, "initialized OK, RAM at %p (mirror at 0 @ %p)", g_heap, - physical_fcram); + LOG_DEBUG(HW_Memory, "initialized OK, RAM at %p", g_heap); } void Shutdown() { - u32 flags = 0; - MemoryMap_Shutdown(g_views, kNumMemViews, flags, &arena); - arena.ReleaseSpace(); MemBlock_Shutdown(); - - g_base = nullptr; - g_exefs_code = nullptr; - g_system_mem = nullptr; - g_heap = nullptr; - g_heap_linear = nullptr; - g_vram = nullptr; - g_shared_mem = nullptr; - g_dsp_mem = nullptr; - g_kernel_mem = nullptr; - - physical_bootrom = nullptr; - uncached_bootrom = nullptr; - physical_exefs_code = nullptr; - physical_system_mem = nullptr; - physical_fcram = nullptr; - physical_heap_gsp = nullptr; - physical_vram = nullptr; - physical_shared_mem = nullptr; - physical_dsp_mem = nullptr; - physical_kernel_mem = nullptr; + for (MemoryArea& area : memory_areas) { + delete[] *area.ptr; + *area.ptr = nullptr; + } LOG_DEBUG(HW_Memory, "shutdown OK"); } diff --git a/src/core/mem_map.h b/src/core/mem_map.h index b11f2ea68..1fb77c94a 100644 --- a/src/core/mem_map.h +++ b/src/core/mem_map.h @@ -105,18 +105,6 @@ struct MemoryBlock { //////////////////////////////////////////////////////////////////////////////////////////////////// -// Base is a pointer to the base of the memory map. Yes, some MMU tricks -// are used to set up a full GC or Wii memory map in process memory. on -// 32-bit, you have to mask your offsets with 0x3FFFFFFF. This means that -// some things are mirrored too many times, but eh... it works. - -// In 64-bit, this might point to "high memory" (above the 32-bit limit), -// so be sure to load it into a 64-bit register. -extern u8 *g_base; - -// These are guaranteed to point to "low memory" addresses (sub-32-bit). -// 64-bit: Pointers to low-mem (sub-0x10000000) mirror -// 32-bit: Same as the corresponding physical/virtual pointers. extern u8* g_heap_linear; ///< Linear heap (main memory) extern u8* g_heap; ///< Application heap (main memory) extern u8* g_vram; ///< Video memory (VRAM)