2020-04-08 21:35:07 +00:00
|
|
|
// Copyright 2020 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-11-18 00:58:41 +00:00
|
|
|
#include <utility>
|
2020-04-08 21:35:07 +00:00
|
|
|
|
|
|
|
namespace Common {
|
|
|
|
|
2020-11-18 00:58:41 +00:00
|
|
|
void* AllocateMemoryPages(std::size_t size) noexcept;
|
|
|
|
void FreeMemoryPages(void* base, std::size_t size) noexcept;
|
2020-04-08 21:35:07 +00:00
|
|
|
|
|
|
|
template <typename T>
|
2020-11-18 00:58:41 +00:00
|
|
|
class VirtualBuffer final {
|
2020-04-08 21:35:07 +00:00
|
|
|
public:
|
|
|
|
constexpr VirtualBuffer() = default;
|
|
|
|
explicit VirtualBuffer(std::size_t count) : alloc_size{count * sizeof(T)} {
|
|
|
|
base_ptr = reinterpret_cast<T*>(AllocateMemoryPages(alloc_size));
|
|
|
|
}
|
|
|
|
|
2020-11-18 00:58:41 +00:00
|
|
|
~VirtualBuffer() noexcept {
|
2020-04-08 21:35:07 +00:00
|
|
|
FreeMemoryPages(base_ptr, alloc_size);
|
|
|
|
}
|
|
|
|
|
2020-11-18 00:58:41 +00:00
|
|
|
VirtualBuffer(const VirtualBuffer&) = delete;
|
|
|
|
VirtualBuffer& operator=(const VirtualBuffer&) = delete;
|
|
|
|
|
|
|
|
VirtualBuffer(VirtualBuffer&& other) noexcept
|
|
|
|
: alloc_size{std::exchange(other.alloc_size, 0)}, base_ptr{std::exchange(other.base_ptr),
|
|
|
|
nullptr} {}
|
|
|
|
|
|
|
|
VirtualBuffer& operator=(VirtualBuffer&& other) noexcept {
|
|
|
|
alloc_size = std::exchange(other.alloc_size, 0);
|
|
|
|
base_ptr = std::exchange(other.base_ptr, nullptr);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-04-08 21:35:07 +00:00
|
|
|
void resize(std::size_t count) {
|
|
|
|
FreeMemoryPages(base_ptr, alloc_size);
|
|
|
|
|
|
|
|
alloc_size = count * sizeof(T);
|
|
|
|
base_ptr = reinterpret_cast<T*>(AllocateMemoryPages(alloc_size));
|
|
|
|
}
|
|
|
|
|
2020-08-14 13:38:45 +00:00
|
|
|
[[nodiscard]] constexpr const T& operator[](std::size_t index) const {
|
2020-04-08 21:35:07 +00:00
|
|
|
return base_ptr[index];
|
|
|
|
}
|
|
|
|
|
2020-08-14 13:38:45 +00:00
|
|
|
[[nodiscard]] constexpr T& operator[](std::size_t index) {
|
2020-04-08 21:35:07 +00:00
|
|
|
return base_ptr[index];
|
|
|
|
}
|
|
|
|
|
2020-08-14 13:38:45 +00:00
|
|
|
[[nodiscard]] constexpr T* data() {
|
2020-04-08 21:35:07 +00:00
|
|
|
return base_ptr;
|
|
|
|
}
|
|
|
|
|
2020-08-14 13:38:45 +00:00
|
|
|
[[nodiscard]] constexpr const T* data() const {
|
2020-04-08 21:35:07 +00:00
|
|
|
return base_ptr;
|
|
|
|
}
|
|
|
|
|
2020-08-14 13:38:45 +00:00
|
|
|
[[nodiscard]] constexpr std::size_t size() const {
|
2020-04-08 21:35:07 +00:00
|
|
|
return alloc_size / sizeof(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::size_t alloc_size{};
|
|
|
|
T* base_ptr{};
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Common
|