dolphin/Source/Core/Common/MemArenaAndroid.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

128 lines
3.1 KiB
C++
Raw Normal View History

// Copyright 2008 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
2021-12-10 03:22:16 +01:00
#include "Common/MemArena.h"
#include <cerrno>
#include <cstddef>
#include <cstdlib>
#include <cstring>
2013-10-20 00:58:02 +02:00
#include <set>
#include <string>
#include <dlfcn.h>
#include <fcntl.h>
#include <linux/ashmem.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>
#include "Common/CommonFuncs.h"
#include "Common/CommonTypes.h"
#include "Common/Logging/Log.h"
#include "Common/MsgHandler.h"
#include "Common/StringUtil.h"
namespace Common
{
2013-02-26 20:49:00 +01:00
#define ASHMEM_DEVICE "/dev/ashmem"
static int AshmemCreateFileMapping(const char* name, size_t size)
2013-02-26 20:49:00 +01:00
{
// ASharedMemory path - works on API >= 26 and falls through on API < 26:
// We can't call ASharedMemory_create the normal way without increasing the
// minimum version requirement to API 26, so we use dlopen/dlsym instead
static void* libandroid = dlopen("libandroid.so", RTLD_LAZY | RTLD_LOCAL);
static auto shared_memory_create =
reinterpret_cast<int (*)(const char*, size_t)>(dlsym(libandroid, "ASharedMemory_create"));
if (shared_memory_create)
return shared_memory_create(name, size);
// /dev/ashmem path - works on API < 29:
2013-02-26 20:49:00 +01:00
int fd, ret;
fd = open(ASHMEM_DEVICE, O_RDWR);
if (fd < 0)
return fd;
// We don't really care if we can't set the name, it is optional
ioctl(fd, ASHMEM_SET_NAME, name);
2013-02-26 20:49:00 +01:00
ret = ioctl(fd, ASHMEM_SET_SIZE, size);
if (ret < 0)
{
close(fd);
NOTICE_LOG_FMT(MEMMAP, "Ashmem returned error: {:#010x}", ret);
2013-02-26 20:49:00 +01:00
return ret;
}
return fd;
}
MemArena::MemArena() = default;
MemArena::~MemArena() = default;
void MemArena::GrabSHMSegment(size_t size)
{
fd = AshmemCreateFileMapping(("dolphin-emu." + std::to_string(getpid())).c_str(), size);
2013-02-26 20:49:00 +01:00
if (fd < 0)
NOTICE_LOG_FMT(MEMMAP, "Ashmem allocation failed");
}
void MemArena::ReleaseSHMSegment()
{
close(fd);
}
void* MemArena::CreateView(s64 offset, size_t size)
{
return MapInMemoryRegion(offset, size, nullptr);
}
void MemArena::ReleaseView(void* view, size_t size)
{
UnmapFromMemoryRegion(view, size);
}
u8* MemArena::ReserveMemoryRegion(size_t memory_size)
{
2013-09-02 11:10:21 +02:00
// Android 4.3 changed how mmap works.
// if we map it private and then munmap it, we can't use the base returned.
// This may be due to changes in them to support a full SELinux implementation.
2013-09-30 03:53:32 +02:00
const int flags = MAP_ANON | MAP_SHARED;
void* base = mmap(nullptr, memory_size, PROT_NONE, flags, -1, 0);
2014-08-30 22:14:56 +02:00
if (base == MAP_FAILED)
{
PanicAlertFmt("Failed to map enough memory space: {}", LastStrerrorString());
return nullptr;
}
munmap(base, memory_size);
return static_cast<u8*>(base);
}
void MemArena::ReleaseMemoryRegion()
{
}
void* MemArena::MapInMemoryRegion(s64 offset, size_t size, void* base)
{
void* retval = mmap(base, size, PROT_READ | PROT_WRITE,
MAP_SHARED | ((base == nullptr) ? 0 : MAP_FIXED), fd, offset);
if (retval == MAP_FAILED)
{
NOTICE_LOG_FMT(MEMMAP, "mmap failed");
return nullptr;
}
else
{
return retval;
}
}
void MemArena::UnmapFromMemoryRegion(void* view, size_t size)
{
munmap(view, size);
}
} // namespace Common