summaryrefslogtreecommitdiff
path: root/src/common/virtual_buffer.h
diff options
context:
space:
mode:
authorGravatar Lioncash2020-11-17 20:09:55 -0500
committerGravatar Lioncash2020-11-17 20:09:58 -0500
commit0ca91ced2dab3654e78e4c465506d7baad3cbeeb (patch)
tree4165380bb5e6bc44d901e9c93651bc2bb620ef0f /src/common/virtual_buffer.h
parentpage_table: Allow page tables to be moved (diff)
downloadyuzu-0ca91ced2dab3654e78e4c465506d7baad3cbeeb.tar.gz
yuzu-0ca91ced2dab3654e78e4c465506d7baad3cbeeb.tar.xz
yuzu-0ca91ced2dab3654e78e4c465506d7baad3cbeeb.zip
virtual_buffer: Add compile-time type-safety guarantees with VirtualBuffer
VirtualBuffer makes use of VirtualAlloc (on Windows) and mmap() (on other platforms). Neither of these ensure that non-trivial objects are properly constructed in the allocated memory. To prevent potential undefined behavior occurring due to that, we can add a static assert to loudly complain about cases where that is done.
Diffstat (limited to 'src/common/virtual_buffer.h')
-rw-r--r--src/common/virtual_buffer.h6
1 files changed, 6 insertions, 0 deletions
diff --git a/src/common/virtual_buffer.h b/src/common/virtual_buffer.h
index 363913d45..078e61c77 100644
--- a/src/common/virtual_buffer.h
+++ b/src/common/virtual_buffer.h
@@ -4,6 +4,7 @@
4 4
5#pragma once 5#pragma once
6 6
7#include <type_traits>
7#include <utility> 8#include <utility>
8 9
9namespace Common { 10namespace Common {
@@ -14,6 +15,11 @@ void FreeMemoryPages(void* base, std::size_t size) noexcept;
14template <typename T> 15template <typename T>
15class VirtualBuffer final { 16class VirtualBuffer final {
16public: 17public:
18 static_assert(
19 std::is_trivially_constructible_v<T>,
20 "T must be trivially constructible, as non-trivial constructors will not be executed "
21 "with the current allocator");
22
17 constexpr VirtualBuffer() = default; 23 constexpr VirtualBuffer() = default;
18 explicit VirtualBuffer(std::size_t count) : alloc_size{count * sizeof(T)} { 24 explicit VirtualBuffer(std::size_t count) : alloc_size{count * sizeof(T)} {
19 base_ptr = reinterpret_cast<T*>(AllocateMemoryPages(alloc_size)); 25 base_ptr = reinterpret_cast<T*>(AllocateMemoryPages(alloc_size));