Skip to content

Commit

Permalink
Merge pull request #12250 from hrydgard/android-base-pointer-64-bit
Browse files Browse the repository at this point in the history
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 1561f55
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions Common/MemArenaAndroid.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@


#ifdef __ANDROID__ #ifdef __ANDROID__


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


u8* MemArena::Find4GBBase() { u8* MemArena::Find4GBBase() {
#if PPSSPP_ARCH(64BIT) #if PPSSPP_ARCH(64BIT)
// Just grab some random 4GB... // We should probably just go look in /proc/self/maps for some free space.
return reinterpret_cast<u8*>(0x2300000000ULL); // 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 #else
// Address masking is used in 32-bit mode, so we can get away with less memory. // 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, void* base = mmap(0, 0x10000000, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);
MAP_ANON | MAP_SHARED, -1, 0);
if (base == MAP_FAILED) { if (base == MAP_FAILED) {
PanicAlert("Failed to map 256 MB of memory space: %s", strerror(errno)); PanicAlert("Failed to map 256 MB of memory space: %s", strerror(errno));
return 0; return 0;
Expand Down

0 comments on commit 1561f55

Please sign in to comment.