scratch_buffer.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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(ScratchBuffer&&) = default;
  21. /// This will only grow the buffer's capacity if size is greater than the current capacity.
  22. /// The previously held data will remain intact.
  23. void resize(size_t size) {
  24. if (size > buffer_capacity) {
  25. auto new_buffer = Common::make_unique_for_overwrite<T[]>(size);
  26. std::move(buffer.get(), buffer.get() + buffer_capacity, new_buffer.get());
  27. buffer = std::move(new_buffer);
  28. buffer_capacity = size;
  29. }
  30. last_requested_size = size;
  31. }
  32. /// This will only grow the buffer's capacity if size is greater than the current capacity.
  33. /// The previously held data will be destroyed if a reallocation occurs.
  34. void resize_destructive(size_t size) {
  35. if (size > buffer_capacity) {
  36. buffer_capacity = size;
  37. buffer = Common::make_unique_for_overwrite<T[]>(buffer_capacity);
  38. }
  39. last_requested_size = size;
  40. }
  41. [[nodiscard]] T* data() noexcept {
  42. return buffer.get();
  43. }
  44. [[nodiscard]] const T* data() const noexcept {
  45. return buffer.get();
  46. }
  47. [[nodiscard]] T* begin() noexcept {
  48. return data();
  49. }
  50. [[nodiscard]] const T* begin() const noexcept {
  51. return data();
  52. }
  53. [[nodiscard]] T* end() noexcept {
  54. return data() + last_requested_size;
  55. }
  56. [[nodiscard]] const T* end() const noexcept {
  57. return data() + last_requested_size;
  58. }
  59. [[nodiscard]] T& operator[](size_t i) {
  60. return buffer[i];
  61. }
  62. [[nodiscard]] const T& operator[](size_t i) const {
  63. return buffer[i];
  64. }
  65. [[nodiscard]] size_t size() const noexcept {
  66. return last_requested_size;
  67. }
  68. [[nodiscard]] size_t capacity() const noexcept {
  69. return buffer_capacity;
  70. }
  71. private:
  72. size_t last_requested_size{};
  73. size_t buffer_capacity{};
  74. std::unique_ptr<T[]> buffer{};
  75. };
  76. } // namespace Common