2022-12-19 00:08:20 +01:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "common/make_unique_for_overwrite.h"
|
|
|
|
|
|
|
|
namespace Common {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ScratchBuffer class
|
|
|
|
* This class creates a default initialized heap allocated buffer for cases such as intermediate
|
|
|
|
* buffers being copied into entirely, where value initializing members during allocation or resize
|
|
|
|
* is redundant.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
class ScratchBuffer {
|
|
|
|
public:
|
|
|
|
ScratchBuffer() = default;
|
|
|
|
|
|
|
|
explicit ScratchBuffer(size_t initial_capacity)
|
2022-12-19 18:30:52 +01:00
|
|
|
: last_requested_size{initial_capacity}, buffer_capacity{initial_capacity},
|
2022-12-19 00:08:20 +01:00
|
|
|
buffer{Common::make_unique_for_overwrite<T[]>(initial_capacity)} {}
|
|
|
|
|
|
|
|
~ScratchBuffer() = default;
|
2023-05-01 19:46:03 +02:00
|
|
|
ScratchBuffer(const ScratchBuffer&) = delete;
|
|
|
|
ScratchBuffer& operator=(const ScratchBuffer&) = delete;
|
2023-02-22 06:26:07 +01:00
|
|
|
ScratchBuffer(ScratchBuffer&&) = default;
|
2023-05-01 19:46:03 +02:00
|
|
|
ScratchBuffer& operator=(ScratchBuffer&&) = default;
|
2022-12-19 00:08:20 +01:00
|
|
|
|
|
|
|
/// This will only grow the buffer's capacity if size is greater than the current capacity.
|
2022-12-20 04:40:50 +01:00
|
|
|
/// The previously held data will remain intact.
|
2022-12-19 00:08:20 +01:00
|
|
|
void resize(size_t size) {
|
2022-12-20 04:40:50 +01:00
|
|
|
if (size > buffer_capacity) {
|
|
|
|
auto new_buffer = Common::make_unique_for_overwrite<T[]>(size);
|
|
|
|
std::move(buffer.get(), buffer.get() + buffer_capacity, new_buffer.get());
|
|
|
|
buffer = std::move(new_buffer);
|
|
|
|
buffer_capacity = size;
|
|
|
|
}
|
|
|
|
last_requested_size = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This will only grow the buffer's capacity if size is greater than the current capacity.
|
|
|
|
/// The previously held data will be destroyed if a reallocation occurs.
|
|
|
|
void resize_destructive(size_t size) {
|
2022-12-19 18:30:52 +01:00
|
|
|
if (size > buffer_capacity) {
|
|
|
|
buffer_capacity = size;
|
|
|
|
buffer = Common::make_unique_for_overwrite<T[]>(buffer_capacity);
|
2022-12-19 00:08:20 +01:00
|
|
|
}
|
|
|
|
last_requested_size = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] T* data() noexcept {
|
|
|
|
return buffer.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] const T* data() const noexcept {
|
|
|
|
return buffer.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] T* begin() noexcept {
|
|
|
|
return data();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] const T* begin() const noexcept {
|
|
|
|
return data();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] T* end() noexcept {
|
|
|
|
return data() + last_requested_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] const T* end() const noexcept {
|
|
|
|
return data() + last_requested_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] T& operator[](size_t i) {
|
|
|
|
return buffer[i];
|
|
|
|
}
|
|
|
|
|
2022-12-20 04:40:50 +01:00
|
|
|
[[nodiscard]] const T& operator[](size_t i) const {
|
|
|
|
return buffer[i];
|
|
|
|
}
|
|
|
|
|
2022-12-19 00:08:20 +01:00
|
|
|
[[nodiscard]] size_t size() const noexcept {
|
|
|
|
return last_requested_size;
|
|
|
|
}
|
|
|
|
|
2022-12-19 18:30:52 +01:00
|
|
|
[[nodiscard]] size_t capacity() const noexcept {
|
|
|
|
return buffer_capacity;
|
|
|
|
}
|
|
|
|
|
2023-05-01 19:46:03 +02:00
|
|
|
void swap(ScratchBuffer& other) noexcept {
|
|
|
|
std::swap(last_requested_size, other.last_requested_size);
|
|
|
|
std::swap(buffer_capacity, other.buffer_capacity);
|
|
|
|
std::swap(buffer, other.buffer);
|
|
|
|
}
|
|
|
|
|
2022-12-19 00:08:20 +01:00
|
|
|
private:
|
|
|
|
size_t last_requested_size{};
|
2022-12-19 18:30:52 +01:00
|
|
|
size_t buffer_capacity{};
|
2022-12-19 00:08:20 +01:00
|
|
|
std::unique_ptr<T[]> buffer{};
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Common
|