summaryrefslogtreecommitdiff
path: root/src/common/scratch_buffer.h
diff options
context:
space:
mode:
authorGravatar Fernando S2022-12-24 20:26:06 -0500
committerGravatar GitHub2022-12-24 20:26:06 -0500
commit3e6850f00bdd541202a8369438bda7988c8001f5 (patch)
tree34691ecb826bc402f68a075de07f4f6aaebf8c44 /src/common/scratch_buffer.h
parentqt: fix 'Pause' menu item (#9497) (diff)
parentscratch_buffer: Explicitly defing resize and resize_destructive functions (diff)
downloadyuzu-3e6850f00bdd541202a8369438bda7988c8001f5.tar.gz
yuzu-3e6850f00bdd541202a8369438bda7988c8001f5.tar.xz
yuzu-3e6850f00bdd541202a8369438bda7988c8001f5.zip
Merge pull request #9453 from ameerj/scratch-vector
common: Add ScratchBuffer Class
Diffstat (limited to 'src/common/scratch_buffer.h')
-rw-r--r--src/common/scratch_buffer.h95
1 files changed, 95 insertions, 0 deletions
diff --git a/src/common/scratch_buffer.h b/src/common/scratch_buffer.h
new file mode 100644
index 000000000..1245a5086
--- /dev/null
+++ b/src/common/scratch_buffer.h
@@ -0,0 +1,95 @@
1// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#pragma once
5
6#include "common/make_unique_for_overwrite.h"
7
8namespace Common {
9
10/**
11 * ScratchBuffer class
12 * This class creates a default initialized heap allocated buffer for cases such as intermediate
13 * buffers being copied into entirely, where value initializing members during allocation or resize
14 * is redundant.
15 */
16template <typename T>
17class ScratchBuffer {
18public:
19 ScratchBuffer() = default;
20
21 explicit ScratchBuffer(size_t initial_capacity)
22 : last_requested_size{initial_capacity}, buffer_capacity{initial_capacity},
23 buffer{Common::make_unique_for_overwrite<T[]>(initial_capacity)} {}
24
25 ~ScratchBuffer() = default;
26
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.
29 void resize(size_t size) {
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) {
43 buffer_capacity = size;
44 buffer = Common::make_unique_for_overwrite<T[]>(buffer_capacity);
45 }
46 last_requested_size = size;
47 }
48
49 [[nodiscard]] T* data() noexcept {
50 return buffer.get();
51 }
52
53 [[nodiscard]] const T* data() const noexcept {
54 return buffer.get();
55 }
56
57 [[nodiscard]] T* begin() noexcept {
58 return data();
59 }
60
61 [[nodiscard]] const T* begin() const noexcept {
62 return data();
63 }
64
65 [[nodiscard]] T* end() noexcept {
66 return data() + last_requested_size;
67 }
68
69 [[nodiscard]] const T* end() const noexcept {
70 return data() + last_requested_size;
71 }
72
73 [[nodiscard]] T& operator[](size_t i) {
74 return buffer[i];
75 }
76
77 [[nodiscard]] const T& operator[](size_t i) const {
78 return buffer[i];
79 }
80
81 [[nodiscard]] size_t size() const noexcept {
82 return last_requested_size;
83 }
84
85 [[nodiscard]] size_t capacity() const noexcept {
86 return buffer_capacity;
87 }
88
89private:
90 size_t last_requested_size{};
91 size_t buffer_capacity{};
92 std::unique_ptr<T[]> buffer{};
93};
94
95} // namespace Common