diff options
| author | 2022-12-19 22:40:50 -0500 | |
|---|---|---|
| committer | 2022-12-19 22:40:50 -0500 | |
| commit | c6590ad07b384762fd90ee8852796ec681a69286 (patch) | |
| tree | c0d8d2d157f3bb4be01331f2da459c0f68ca4d7b /src/common/scratch_buffer.h | |
| parent | tests: Add ScratchBuffer tests (diff) | |
| download | yuzu-c6590ad07b384762fd90ee8852796ec681a69286.tar.gz yuzu-c6590ad07b384762fd90ee8852796ec681a69286.tar.xz yuzu-c6590ad07b384762fd90ee8852796ec681a69286.zip | |
scratch_buffer: Explicitly defing resize and resize_destructive functions
resize keeps previous data intact when the buffer grows
resize_destructive destroys the previous data when the buffer grows
Diffstat (limited to 'src/common/scratch_buffer.h')
| -rw-r--r-- | src/common/scratch_buffer.h | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/common/scratch_buffer.h b/src/common/scratch_buffer.h index 59bb8a9ea..1245a5086 100644 --- a/src/common/scratch_buffer.h +++ b/src/common/scratch_buffer.h | |||
| @@ -25,8 +25,21 @@ public: | |||
| 25 | ~ScratchBuffer() = default; | 25 | ~ScratchBuffer() = default; |
| 26 | 26 | ||
| 27 | /// This will only grow the buffer's capacity if size is greater than the current capacity. | 27 | /// This will only grow the buffer's capacity if size is greater than the current capacity. |
| 28 | /// The previously held data will remain intact. | ||
| 28 | void resize(size_t size) { | 29 | void resize(size_t size) { |
| 29 | if (size > buffer_capacity) { | 30 | if (size > buffer_capacity) { |
| 31 | auto new_buffer = Common::make_unique_for_overwrite<T[]>(size); | ||
| 32 | std::move(buffer.get(), buffer.get() + buffer_capacity, new_buffer.get()); | ||
| 33 | buffer = std::move(new_buffer); | ||
| 34 | buffer_capacity = size; | ||
| 35 | } | ||
| 36 | last_requested_size = size; | ||
| 37 | } | ||
| 38 | |||
| 39 | /// This will only grow the buffer's capacity if size is greater than the current capacity. | ||
| 40 | /// The previously held data will be destroyed if a reallocation occurs. | ||
| 41 | void resize_destructive(size_t size) { | ||
| 42 | if (size > buffer_capacity) { | ||
| 30 | buffer_capacity = size; | 43 | buffer_capacity = size; |
| 31 | buffer = Common::make_unique_for_overwrite<T[]>(buffer_capacity); | 44 | buffer = Common::make_unique_for_overwrite<T[]>(buffer_capacity); |
| 32 | } | 45 | } |
| @@ -61,6 +74,10 @@ public: | |||
| 61 | return buffer[i]; | 74 | return buffer[i]; |
| 62 | } | 75 | } |
| 63 | 76 | ||
| 77 | [[nodiscard]] const T& operator[](size_t i) const { | ||
| 78 | return buffer[i]; | ||
| 79 | } | ||
| 80 | |||
| 64 | [[nodiscard]] size_t size() const noexcept { | 81 | [[nodiscard]] size_t size() const noexcept { |
| 65 | return last_requested_size; | 82 | return last_requested_size; |
| 66 | } | 83 | } |