CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Common/MemArenaDarwin.cpp
Views: 1401
// Copyright (C) 2003 Dolphin Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official SVN repository and contact information can be found at15// http://code.google.com/p/dolphin-emu/1617#include "ppsspp_config.h"1819#if defined(__APPLE__)2021#include <cstdint>22#include <sys/stat.h>23#include <fcntl.h>24#include <unistd.h>25#include <cerrno>26#include <cstring>2728#include <mach/mach.h>29#include <mach/vm_map.h>3031#include "Common/Log.h"32#include "Common/File/FileUtil.h"33#include "Common/MemoryUtil.h"34#include "Common/MemArena.h"3536size_t MemArena::roundup(size_t x) {37return x;38}3940bool MemArena::GrabMemSpace(size_t size) {41vm_size = size;42kern_return_t retval = vm_allocate(mach_task_self(), &vm_mem, size, VM_FLAGS_ANYWHERE);43if (retval != KERN_SUCCESS) {44ERROR_LOG(Log::MemMap, "Failed to grab a block of virtual memory");45return false;46} else {47INFO_LOG(Log::MemMap, "Successfully allocated %d bytes at %p", (int)size, (void *)vm_mem);48return true;49}50}5152void MemArena::ReleaseSpace() {53vm_deallocate(mach_task_self(), vm_mem, vm_size);54vm_size = 0;55vm_mem = 0;56}5758void *MemArena::CreateView(s64 offset, size_t size, void *base) {59mach_port_t self = mach_task_self();60vm_address_t target = (vm_address_t)base;61uint64_t mask = 0;62bool anywhere = false;63vm_address_t source = vm_mem + offset;64vm_prot_t cur_protection = 0;65vm_prot_t max_protection = 0;66kern_return_t retval =67vm_remap(self, &target, size, mask, anywhere,68self, source, false,69&cur_protection, &max_protection, VM_INHERIT_DEFAULT);70if (retval != KERN_SUCCESS) {71// 1 == KERN_INVALID_ADDRESS72// 3 == KERN_NO_SPACE (race?)73// 4 == KERN_INVALID_ARGUMENT74ERROR_LOG(Log::MemMap, "vm_remap failed (%d) - could not remap from %llx (offset %llx) of size %llx to %p",75(int)retval, (uint64_t)source, (uint64_t)offset, (uint64_t)size, base);76return nullptr;77}78return (void *)target;79}8081void MemArena::ReleaseView(s64 offset, void* view, size_t size) {82vm_address_t addr = (vm_address_t)view;83vm_deallocate(mach_task_self(), addr, size);84}8586bool MemArena::NeedsProbing() {87#if PPSSPP_PLATFORM(IOS) && PPSSPP_ARCH(64BIT)88return true;89#else90return false;91#endif92}9394u8* MemArena::Find4GBBase() {95#if PPSSPP_PLATFORM(IOS) && PPSSPP_ARCH(64BIT)96// The caller will need to do probing, like on 32-bit Windows.97return nullptr;98#else99size_t size;100#if PPSSPP_ARCH(64BIT)101size = 0xE1000000;102#else103size = 0x10000000;104#endif105106vm_address_t addr = 0;107kern_return_t retval = vm_allocate(mach_task_self(), &addr, size, VM_FLAGS_ANYWHERE);108if (retval == KERN_SUCCESS) {109// Don't need the memory now, was just probing.110vm_deallocate(mach_task_self(), addr, size);111return (u8 *)addr;112}113#endif114return nullptr;115}116117#endif // __APPLE__118119120