virtual_buffer.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include "common/common_funcs.h"
  6. namespace Common {
  7. void* AllocateMemoryPages(std::size_t size);
  8. void FreeMemoryPages(void* base, std::size_t size);
  9. template <typename T>
  10. class VirtualBuffer final : NonCopyable {
  11. public:
  12. constexpr VirtualBuffer() = default;
  13. explicit VirtualBuffer(std::size_t count) : alloc_size{count * sizeof(T)} {
  14. base_ptr = reinterpret_cast<T*>(AllocateMemoryPages(alloc_size));
  15. }
  16. ~VirtualBuffer() {
  17. FreeMemoryPages(base_ptr, alloc_size);
  18. }
  19. void resize(std::size_t count) {
  20. FreeMemoryPages(base_ptr, alloc_size);
  21. alloc_size = count * sizeof(T);
  22. base_ptr = reinterpret_cast<T*>(AllocateMemoryPages(alloc_size));
  23. }
  24. constexpr const T& operator[](std::size_t index) const {
  25. return base_ptr[index];
  26. }
  27. constexpr T& operator[](std::size_t index) {
  28. return base_ptr[index];
  29. }
  30. constexpr T* data() {
  31. return base_ptr;
  32. }
  33. constexpr const T* data() const {
  34. return base_ptr;
  35. }
  36. constexpr std::size_t size() const {
  37. return alloc_size / sizeof(T);
  38. }
  39. private:
  40. std::size_t alloc_size{};
  41. T* base_ptr{};
  42. };
  43. } // namespace Common