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/MemArenaAndroid.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#ifdef __ANDROID__2021#include <sys/stat.h>22#include <fcntl.h>23#include <unistd.h>24#include <cerrno>25#include <sys/ioctl.h>26#include <linux/ashmem.h>27#include <dlfcn.h>2829#include "Common/Log.h"30#include "Common/MemoryUtil.h"31#include "Common/MemArena.h"32#include "Common/StringUtils.h"33#include "Common/System/System.h"3435// Hopefully this ABI will never change...3637#define ASHMEM_DEVICE "/dev/ashmem"3839bool MemArena::NeedsProbing() {40return false;41}4243// ashmem_create_region - creates a new ashmem region and returns the file44// descriptor, or <0 on error45// This function is defined in much later version of the ndk, so we can only access it via dlopen().46// `name' is an optional label to give the region (visible in /proc/pid/maps)47// `size' is the size of the region, in page-aligned bytes48static int ashmem_create_region(const char *name, size_t size) {49static void* handle = dlopen("libandroid.so", RTLD_LAZY | RTLD_LOCAL);50using type_ASharedMemory_create = int(*)(const char *name, size_t size);51static type_ASharedMemory_create function_create = nullptr;5253if (handle != nullptr) {54function_create =55reinterpret_cast<type_ASharedMemory_create>(dlsym(handle, "ASharedMemory_create"));56}5758if (function_create != nullptr) {59return function_create(name, size);60} else {61return -1;62}63}6465// legacy_ashmem_create_region - creates a new ashmem region and returns the file66// descriptor, or <0 on error67// `name' is an optional label to give the region (visible in /proc/pid/maps)68// `size' is the size of the region, in page-aligned bytes69static int legacy_ashmem_create_region(const char *name, size_t size) {70int fd = open(ASHMEM_DEVICE, O_RDWR);71if (fd < 0)72return fd;7374int ret;75if (name) {76char buf[ASHMEM_NAME_LEN];77truncate_cpy(buf, name);78ret = ioctl(fd, ASHMEM_SET_NAME, buf);79if (ret < 0)80goto error;81}8283ret = ioctl(fd, ASHMEM_SET_SIZE, size);84if (ret < 0)85goto error;8687return fd;8889error:90ERROR_LOG(Log::MemMap, "NASTY ASHMEM ERROR: ret = %08x", ret);91close(fd);92return ret;93}9495// Windows mappings need to be on 64K boundaries, due to Alpha legacy.96size_t MemArena::roundup(size_t x) {97return x;98}99100bool MemArena::GrabMemSpace(size_t size) {101// Use ashmem so we don't have to allocate a file on disk!102const char* name = "PPSSPP_RAM";103104// Since version 26 Android provides a new api for accessing SharedMemory.105if (System_GetPropertyInt(SYSPROP_SYSTEMVERSION) >= 26) {106fd = ashmem_create_region(name, size);107} else {108fd = legacy_ashmem_create_region(name, size);109}110// Note that it appears that ashmem is pinned by default, so no need to pin.111if (fd < 0) {112ERROR_LOG(Log::MemMap, "Failed to grab ashmem space of size: %08x errno: %d", (int)size, (int)(errno));113return false;114}115return true;116}117118void MemArena::ReleaseSpace() {119close(fd);120}121122void *MemArena::CreateView(s64 offset, size_t size, void *base) {123void *retval = mmap(base, size, PROT_READ | PROT_WRITE, MAP_SHARED | ((base == 0) ? 0 : MAP_FIXED), fd, offset);124if (retval == MAP_FAILED) {125NOTICE_LOG(Log::MemMap, "mmap on ashmem (fd: %d) failed", (int)fd);126return nullptr;127}128return retval;129}130131void MemArena::ReleaseView(s64 offset, void* view, size_t size) {132munmap(view, size);133}134135u8* MemArena::Find4GBBase() {136#if PPSSPP_ARCH(64BIT)137// We should probably just go look in /proc/self/maps for some free space.138// But let's try the anonymous mmap trick, just like on 32-bit, but bigger and139// aligned to 4GB for the movk trick. We can ensure that we get an aligned 4GB140// address by grabbing 8GB and aligning the pointer.141const uint64_t EIGHT_GIGS = 0x200000000ULL;142void *base = mmap(0, EIGHT_GIGS, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);143if (base && base != MAP_FAILED) {144INFO_LOG(Log::System, "base: %p", base);145uint64_t aligned_base = ((uint64_t)base + 0xFFFFFFFF) & ~0xFFFFFFFFULL;146INFO_LOG(Log::System, "aligned_base: %p", (void *)aligned_base);147munmap(base, EIGHT_GIGS);148return reinterpret_cast<u8 *>(aligned_base);149} else {150u8 *hardcoded_ptr = reinterpret_cast<u8*>(0x2300000000ULL);151INFO_LOG(Log::System, "Failed to anonymously map 8GB. Fall back to the hardcoded pointer %p.", hardcoded_ptr);152// Just grab some random 4GB...153// This has been known to fail lately though, see issue #12249.154return hardcoded_ptr;155}156#else157// Address masking is used in 32-bit mode, so we can get away with less memory.158void *base = mmap(0, 0x10000000, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);159160if (base == MAP_FAILED) {161ERROR_LOG(Log::System, "Failed to map 256 MB of memory space: %s", strerror(errno));162return nullptr;163}164165munmap(base, 0x10000000);166return static_cast<u8*>(base);167#endif168}169170#endif // __ANDROID__171172173