summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar ameerj2022-12-18 18:08:20 -0500
committerGravatar ameerj2022-12-19 18:07:51 -0500
commitcfc34dd41d63f45b0587d089b8ec7fc2ed27c04e (patch)
tree0966e445a3e2bd6552703b8243643f8275836d0b /src
parentcommon: add make_unique_for_overwrite (diff)
downloadyuzu-cfc34dd41d63f45b0587d089b8ec7fc2ed27c04e.tar.gz
yuzu-cfc34dd41d63f45b0587d089b8ec7fc2ed27c04e.tar.xz
yuzu-cfc34dd41d63f45b0587d089b8ec7fc2ed27c04e.zip
common: Add ScratchBuffer class
This class creates a default initialized heap allocated buffer for cases where value initializing members during allocation or resize is redundant.
Diffstat (limited to 'src')
-rw-r--r--src/common/CMakeLists.txt1
-rw-r--r--src/common/scratch_buffer.h74
2 files changed, 75 insertions, 0 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index f558f5a58..eb05e46a8 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -102,6 +102,7 @@ add_library(common STATIC
102 ${CMAKE_CURRENT_BINARY_DIR}/scm_rev.cpp 102 ${CMAKE_CURRENT_BINARY_DIR}/scm_rev.cpp
103 scm_rev.h 103 scm_rev.h
104 scope_exit.h 104 scope_exit.h
105 scratch_buffer.h
105 settings.cpp 106 settings.cpp
106 settings.h 107 settings.h
107 settings_input.cpp 108 settings_input.cpp
diff --git a/src/common/scratch_buffer.h b/src/common/scratch_buffer.h
new file mode 100644
index 000000000..afbe2eee1
--- /dev/null
+++ b/src/common/scratch_buffer.h
@@ -0,0 +1,74 @@
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}, 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 void resize(size_t size) {
29 if (size > capacity) {
30 capacity = size;
31 buffer = Common::make_unique_for_overwrite<T[]>(capacity);
32 }
33 last_requested_size = size;
34 }
35
36 [[nodiscard]] T* data() noexcept {
37 return buffer.get();
38 }
39
40 [[nodiscard]] const T* data() const noexcept {
41 return buffer.get();
42 }
43
44 [[nodiscard]] T* begin() noexcept {
45 return data();
46 }
47
48 [[nodiscard]] const T* begin() const noexcept {
49 return data();
50 }
51
52 [[nodiscard]] T* end() noexcept {
53 return data() + last_requested_size;
54 }
55
56 [[nodiscard]] const T* end() const noexcept {
57 return data() + last_requested_size;
58 }
59
60 [[nodiscard]] T& operator[](size_t i) {
61 return buffer[i];
62 }
63
64 [[nodiscard]] size_t size() const noexcept {
65 return last_requested_size;
66 }
67
68private:
69 size_t last_requested_size{};
70 size_t capacity{};
71 std::unique_ptr<T[]> buffer{};
72};
73
74} // namespace Common