scratch_buffer.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <iterator>
  5. #include "common/make_unique_for_overwrite.h"
  6. namespace Common {
  7. /**
  8. * ScratchBuffer class
  9. * This class creates a default initialized heap allocated buffer for cases such as intermediate
  10. * buffers being copied into entirely, where value initializing members during allocation or resize
  11. * is redundant.
  12. */
  13. template <typename T>
  14. class ScratchBuffer {
  15. public:
  16. using element_type = T;
  17. using value_type = T;
  18. using size_type = size_t;
  19. using difference_type = std::ptrdiff_t;
  20. using pointer = T*;
  21. using const_pointer = const T*;
  22. using reference = T&;
  23. using const_reference = const T&;
  24. using iterator = pointer;
  25. using const_iterator = const_pointer;
  26. using iterator_category = std::random_access_iterator_tag;
  27. using iterator_concept = std::contiguous_iterator_tag;
  28. ScratchBuffer() = default;
  29. explicit ScratchBuffer(size_type initial_capacity)
  30. : last_requested_size{initial_capacity}, buffer_capacity{initial_capacity},
  31. buffer{Common::make_unique_for_overwrite<T[]>(initial_capacity)} {}
  32. ~ScratchBuffer() = default;
  33. ScratchBuffer(const ScratchBuffer&) = delete;
  34. ScratchBuffer& operator=(const ScratchBuffer&) = delete;
  35. ScratchBuffer(ScratchBuffer&& other) noexcept {
  36. swap(other);
  37. other.last_requested_size = 0;
  38. other.buffer_capacity = 0;
  39. other.buffer.reset();
  40. }
  41. ScratchBuffer& operator=(ScratchBuffer&& other) noexcept {
  42. swap(other);
  43. other.last_requested_size = 0;
  44. other.buffer_capacity = 0;
  45. other.buffer.reset();
  46. return *this;
  47. }
  48. /// This will only grow the buffer's capacity if size is greater than the current capacity.
  49. /// The previously held data will remain intact.
  50. void resize(size_type size) {
  51. if (size > buffer_capacity) {
  52. auto new_buffer = Common::make_unique_for_overwrite<T[]>(size);
  53. std::move(buffer.get(), buffer.get() + buffer_capacity, new_buffer.get());
  54. buffer = std::move(new_buffer);
  55. buffer_capacity = size;
  56. }
  57. last_requested_size = size;
  58. }
  59. /// This will only grow the buffer's capacity if size is greater than the current capacity.
  60. /// The previously held data will be destroyed if a reallocation occurs.
  61. void resize_destructive(size_type size) {
  62. if (size > buffer_capacity) {
  63. buffer_capacity = size;
  64. buffer = Common::make_unique_for_overwrite<T[]>(buffer_capacity);
  65. }
  66. last_requested_size = size;
  67. }
  68. [[nodiscard]] pointer data() noexcept {
  69. return buffer.get();
  70. }
  71. [[nodiscard]] const_pointer data() const noexcept {
  72. return buffer.get();
  73. }
  74. [[nodiscard]] iterator begin() noexcept {
  75. return data();
  76. }
  77. [[nodiscard]] const_iterator begin() const noexcept {
  78. return data();
  79. }
  80. [[nodiscard]] iterator end() noexcept {
  81. return data() + last_requested_size;
  82. }
  83. [[nodiscard]] const_iterator end() const noexcept {
  84. return data() + last_requested_size;
  85. }
  86. [[nodiscard]] reference operator[](size_type i) {
  87. return buffer[i];
  88. }
  89. [[nodiscard]] const_reference operator[](size_type i) const {
  90. return buffer[i];
  91. }
  92. [[nodiscard]] size_type size() const noexcept {
  93. return last_requested_size;
  94. }
  95. [[nodiscard]] size_type capacity() const noexcept {
  96. return buffer_capacity;
  97. }
  98. void swap(ScratchBuffer& other) noexcept {
  99. std::swap(last_requested_size, other.last_requested_size);
  100. std::swap(buffer_capacity, other.buffer_capacity);
  101. std::swap(buffer, other.buffer);
  102. }
  103. private:
  104. size_type last_requested_size{};
  105. size_type buffer_capacity{};
  106. std::unique_ptr<T[]> buffer{};
  107. };
  108. } // namespace Common