scratch_buffer.h 3.1 KB

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