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.

61 lines
1.7 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>
2025-12-01 20:07:43 +00:00
#elif defined(__OPENORBIS__)
#include <orbis/libkernel.h>
#else
#include <sys/mman.h>
#endif
#include "common/assert.h"
#include "common/virtual_buffer.h"
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__)
u64 align = 16384;
void *addr = nullptr;
off_t direct_mem_off;
int32_t rc;
if ((rc = sceKernelAllocateDirectMemory(0, sceKernelGetDirectMemorySize(), size, align, 3, &direct_mem_off)) < 0) {
ASSERT(false && "sceKernelAllocateDirectMemory");
return nullptr;
}
if ((rc = sceKernelMapDirectMemory(&addr, size, 0x33, 0, direct_mem_off, align)) < 0) {
ASSERT(false && "sceKernelMapDirectMemory");
return nullptr;
}
ASSERT(addr != nullptr);
#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)
#elif defined(__OPENORBIS__)
#else
2025-12-01 20:07:43 +00:00
int rc = munmap(addr, size);
ASSERT(rc == 0);
#endif
}
} // namespace Common