Files
eden/src/common/virtual_buffer.cpp
T

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

86 lines
2.2 KiB
C++
Raw Normal View History

// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
2025-10-22 04:53:40 +02:00
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/mman.h>
#endif
2025-12-11 05:01:48 +00:00
#ifdef __OPENORBIS__
#include <csignal>
#endif
#include "common/assert.h"
#include "common/virtual_buffer.h"
2025-12-01 21:29:18 +00:00
#include "common/logging/log.h"
// PlayStation 4
// Flag needs to be undef-ed on non PS4 since it has different semantics
// on some platforms.
#ifdef __OPENORBIS__
# ifndef MAP_SYSTEM
# define MAP_SYSTEM 0x2000
# endif
# ifndef MAP_VOID
# define MAP_VOID 0x100
# endif
#endif
namespace Common {
2020-11-17 19:58:41 -05:00
void* AllocateMemoryPages(std::size_t size) noexcept {
#ifdef _WIN32
void* base = VirtualAlloc(nullptr, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (base == nullptr) {
// Probably failing to reserve is less likely than failing to commit
base = VirtualAlloc(nullptr, size, MEM_COMMIT, PAGE_READWRITE);
}
2025-12-01 20:07:43 +00:00
#elif defined(__OPENORBIS__)
2025-12-06 23:02:09 +00:00
void* addr = mmap(nullptr, size, PROT_NONE, MAP_VOID | MAP_PRIVATE, -1, 0);
2025-12-01 21:29:18 +00:00
ASSERT(addr != MAP_FAILED);
#else
2025-12-01 20:07:43 +00:00
void* addr = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
ASSERT(addr != MAP_FAILED);
#endif
2025-12-01 20:07:43 +00:00
return addr;
}
2025-12-01 20:07:43 +00:00
void FreeMemoryPages(void* addr, [[maybe_unused]] std::size_t size) noexcept {
if (!addr)
return;
#ifdef _WIN32
2025-12-01 20:07:43 +00:00
VirtualFree(addr, 0, MEM_RELEASE)
#else
2025-12-01 20:07:43 +00:00
int rc = munmap(addr, size);
ASSERT(rc == 0);
#endif
}
2025-12-11 05:01:48 +00:00
#ifdef __OPENORBIS__
static struct sigaction old_sa_segv;
static void SwapHandler(int sig, siginfo_t* si, void* raw_context) {
2025-12-06 23:02:09 +00:00
void* aligned_addr = reinterpret_cast<void*>(uintptr_t(si->si_addr) & ~0xfff);
void* res = mmap(aligned_addr, 4096, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
ASSERT(res != MAP_FAILED);
2025-12-11 05:01:48 +00:00
}
bool InitSwap() noexcept {
struct sigaction sa;
sa.sa_handler = NULL;
2025-12-06 23:02:09 +00:00
sa.__sa_handler.__sa_sigaction = &SwapHandler;
2025-12-11 05:01:48 +00:00
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_SIGINFO | SA_RESTART;
return sigaction(SIGSEGV, &sa, &old_sa_segv) == 0;
}
#else
bool InitSwap() noexcept {
return true;
}
#endif
} // namespace Common