Skip to content
Permalink
Browse files

Merge pull request #12250 from hrydgard/android-base-pointer-64-bit

Android: Get our base pointer in a saner way than YOLO, if possible.
  • Loading branch information...
hrydgard committed Aug 17, 2019
2 parents 3356f94 + 06facfc commit 1561f55255ff3cb194a3bd25077acbbc3cb8e89c
Showing with 21 additions and 4 deletions.
  1. +21 −4 Common/MemArenaAndroid.cpp
@@ -19,6 +19,7 @@

#ifdef __ANDROID__

#include "base/logging.h"
#include "MemoryUtil.h"
#include "MemArena.h"
#include "StringUtils.h"
@@ -102,12 +103,28 @@ void MemArena::ReleaseView(void* view, size_t size) {

u8* MemArena::Find4GBBase() {
#if PPSSPP_ARCH(64BIT)
// Just grab some random 4GB...
return reinterpret_cast<u8*>(0x2300000000ULL);
// We should probably just go look in /proc/self/maps for some free space.
// But let's try the anonymous mmap trick, just like on 32-bit, but bigger and
// aligned to 4GB for the movk trick. We can ensure that we get an aligned 4GB
// address by grabbing 8GB and aligning the pointer.
const uint64_t EIGHT_GIGS = 0x200000000ULL;
void *base = mmap(0, EIGHT_GIGS, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);
if (base) {
ILOG("base: %p", base);
uint64_t aligned_base = ((uint64_t)base + 0xFFFFFFFF) & ~0xFFFFFFFFULL;
ILOG("aligned_base: %p", (void *)aligned_base);
munmap(base, EIGHT_GIGS);
return reinterpret_cast<u8 *>(aligned_base);
} else {
u8 *hardcoded_ptr = reinterpret_cast<u8*>(0x2300000000ULL);
ILOG("Failed to anonymously map 8GB. Fall back to the hardcoded pointer %p.", hardcoded_ptr);
// Just grab some random 4GB...
// This has been known to fail lately though, see issue #12249.
return hardcoded_ptr;
}
#else
// Address masking is used in 32-bit mode, so we can get away with less memory.
void* base = mmap(0, 0x10000000, PROT_READ | PROT_WRITE,
MAP_ANON | MAP_SHARED, -1, 0);
void* base = mmap(0, 0x10000000, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);
if (base == MAP_FAILED) {
PanicAlert("Failed to map 256 MB of memory space: %s", strerror(errno));
return 0;

0 comments on commit 1561f55

Please sign in to comment.
You can’t perform that action at this time.