2021-06-20 17:17:07 -06:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
|
|
|
#include "Common/MemArena.h"
|
|
|
|
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
|
2023-05-10 17:51:43 -06:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2021-06-20 17:17:07 -06:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-09-01 20:01:32 -06:00
|
|
|
#include "Common/Assert.h"
|
2021-06-20 17:17:07 -06:00
|
|
|
#include "Common/CommonFuncs.h"
|
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
#include "Common/Logging/Log.h"
|
|
|
|
#include "Common/MsgHandler.h"
|
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
|
|
|
|
namespace Common
|
|
|
|
{
|
2021-06-20 18:13:00 -06:00
|
|
|
MemArena::MemArena() = default;
|
|
|
|
MemArena::~MemArena() = default;
|
|
|
|
|
2023-05-10 17:51:43 -06:00
|
|
|
void MemArena::GrabSHMSegment(size_t size, std::string_view base_name)
|
2021-06-20 17:17:07 -06:00
|
|
|
{
|
2023-05-10 17:51:43 -06:00
|
|
|
const std::string file_name = fmt::format("/{}.{}", base_name, getpid());
|
2021-06-21 16:15:03 -06:00
|
|
|
m_shm_fd = shm_open(file_name.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600);
|
|
|
|
if (m_shm_fd == -1)
|
2021-06-20 17:17:07 -06:00
|
|
|
{
|
|
|
|
ERROR_LOG_FMT(MEMMAP, "shm_open failed: {}", strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
shm_unlink(file_name.c_str());
|
2021-06-21 16:15:03 -06:00
|
|
|
if (ftruncate(m_shm_fd, size) < 0)
|
2021-06-20 17:17:07 -06:00
|
|
|
ERROR_LOG_FMT(MEMMAP, "Failed to allocate low memory space");
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemArena::ReleaseSHMSegment()
|
|
|
|
{
|
2021-06-21 16:15:03 -06:00
|
|
|
close(m_shm_fd);
|
2021-06-20 17:17:07 -06:00
|
|
|
}
|
|
|
|
|
2021-06-20 18:13:00 -06:00
|
|
|
void* MemArena::CreateView(s64 offset, size_t size)
|
2021-06-20 17:17:07 -06:00
|
|
|
{
|
2021-06-21 16:15:03 -06:00
|
|
|
void* retval = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, m_shm_fd, offset);
|
|
|
|
if (retval == MAP_FAILED)
|
|
|
|
{
|
|
|
|
NOTICE_LOG_FMT(MEMMAP, "mmap failed");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return retval;
|
|
|
|
}
|
2021-06-20 17:17:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void MemArena::ReleaseView(void* view, size_t size)
|
|
|
|
{
|
2021-06-21 16:15:03 -06:00
|
|
|
munmap(view, size);
|
2021-06-20 17:17:07 -06:00
|
|
|
}
|
|
|
|
|
2021-06-20 18:13:00 -06:00
|
|
|
u8* MemArena::ReserveMemoryRegion(size_t memory_size)
|
2021-06-20 17:17:07 -06:00
|
|
|
{
|
|
|
|
const int flags = MAP_ANON | MAP_PRIVATE;
|
|
|
|
void* base = mmap(nullptr, memory_size, PROT_NONE, flags, -1, 0);
|
|
|
|
if (base == MAP_FAILED)
|
|
|
|
{
|
|
|
|
PanicAlertFmt("Failed to map enough memory space: {}", LastStrerrorString());
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-06-21 16:15:03 -06:00
|
|
|
m_reserved_region = base;
|
|
|
|
m_reserved_region_size = memory_size;
|
2021-06-20 17:17:07 -06:00
|
|
|
return static_cast<u8*>(base);
|
|
|
|
}
|
2021-06-20 18:13:00 -06:00
|
|
|
|
|
|
|
void MemArena::ReleaseMemoryRegion()
|
|
|
|
{
|
2021-06-21 16:15:03 -06:00
|
|
|
if (m_reserved_region)
|
|
|
|
{
|
|
|
|
munmap(m_reserved_region, m_reserved_region_size);
|
|
|
|
m_reserved_region = nullptr;
|
|
|
|
}
|
2021-06-20 18:13:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void* MemArena::MapInMemoryRegion(s64 offset, size_t size, void* base)
|
|
|
|
{
|
2021-06-21 16:15:03 -06:00
|
|
|
void* retval = mmap(base, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, m_shm_fd, offset);
|
2021-06-20 18:13:00 -06:00
|
|
|
if (retval == MAP_FAILED)
|
|
|
|
{
|
|
|
|
NOTICE_LOG_FMT(MEMMAP, "mmap failed");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemArena::UnmapFromMemoryRegion(void* view, size_t size)
|
|
|
|
{
|
2021-06-21 16:15:03 -06:00
|
|
|
void* retval = mmap(view, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
|
|
|
|
if (retval == MAP_FAILED)
|
|
|
|
NOTICE_LOG_FMT(MEMMAP, "mmap failed");
|
2021-06-20 18:13:00 -06:00
|
|
|
}
|
2023-09-01 20:01:32 -06:00
|
|
|
|
|
|
|
LazyMemoryRegion::LazyMemoryRegion() = default;
|
|
|
|
|
|
|
|
LazyMemoryRegion::~LazyMemoryRegion()
|
|
|
|
{
|
|
|
|
Release();
|
|
|
|
}
|
|
|
|
|
2023-09-09 19:27:53 -06:00
|
|
|
#if !defined MAP_NORESERVE && (defined __FreeBSD__ || defined __OpenBSD__ || defined __NetBSD__)
|
|
|
|
// BSD does not implement MAP_NORESERVE, so define the flag to nothing.
|
|
|
|
// See https://reviews.freebsd.org/rS273250
|
|
|
|
#define MAP_NORESERVE 0
|
|
|
|
#endif
|
|
|
|
|
2023-09-01 20:01:32 -06:00
|
|
|
void* LazyMemoryRegion::Create(size_t size)
|
|
|
|
{
|
|
|
|
ASSERT(!m_memory);
|
|
|
|
|
2023-09-07 06:41:24 -06:00
|
|
|
if (size == 0)
|
|
|
|
return nullptr;
|
|
|
|
|
2023-09-09 19:27:53 -06:00
|
|
|
void* memory =
|
|
|
|
mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
|
2023-09-07 05:59:33 -06:00
|
|
|
if (memory == MAP_FAILED)
|
2023-09-01 20:01:32 -06:00
|
|
|
{
|
|
|
|
NOTICE_LOG_FMT(MEMMAP, "Memory allocation of {} bytes failed.", size);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_memory = memory;
|
|
|
|
m_size = size;
|
|
|
|
|
|
|
|
return memory;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LazyMemoryRegion::Clear()
|
|
|
|
{
|
|
|
|
ASSERT(m_memory);
|
|
|
|
|
2023-09-06 20:00:22 -06:00
|
|
|
void* new_memory = mmap(m_memory, m_size, PROT_READ | PROT_WRITE,
|
2023-09-09 19:27:53 -06:00
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_FIXED, -1, 0);
|
2023-09-06 20:00:22 -06:00
|
|
|
ASSERT(new_memory == m_memory);
|
2023-09-01 20:01:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void LazyMemoryRegion::Release()
|
|
|
|
{
|
|
|
|
if (m_memory)
|
|
|
|
{
|
|
|
|
munmap(m_memory, m_size);
|
|
|
|
m_memory = nullptr;
|
|
|
|
m_size = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-20 17:17:07 -06:00
|
|
|
} // namespace Common
|