2022-04-23 01:59:50 -07:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2020-04-08 14:35:07 -07:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/virtual_buffer.h"
|
|
|
|
|
|
|
|
namespace Common {
|
|
|
|
|
2020-11-17 16:58:41 -08:00
|
|
|
void* AllocateMemoryPages(std::size_t size) noexcept {
|
2020-04-08 14:35:07 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
void* base{VirtualAlloc(nullptr, size, MEM_COMMIT, PAGE_READWRITE)};
|
|
|
|
#else
|
|
|
|
void* base{mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0)};
|
|
|
|
|
|
|
|
if (base == MAP_FAILED) {
|
|
|
|
base = nullptr;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ASSERT(base);
|
|
|
|
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
2020-11-17 16:58:41 -08:00
|
|
|
void FreeMemoryPages(void* base, [[maybe_unused]] std::size_t size) noexcept {
|
2020-04-08 14:35:07 -07:00
|
|
|
if (!base) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#ifdef _WIN32
|
|
|
|
ASSERT(VirtualFree(base, 0, MEM_RELEASE));
|
|
|
|
#else
|
|
|
|
ASSERT(munmap(base, size) == 0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Common
|