2022-12-18 23:08:20 +00:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-05-23 14:45:54 +01:00
|
|
|
#include <iterator>
|
|
|
|
|
2022-12-18 23:08:20 +00:00
|
|
|
#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:
|
2023-05-23 14:45:54 +01:00
|
|
|
using element_type = T;
|
2023-06-25 00:58:45 +01:00
|
|
|
using value_type = T;
|
|
|
|
using size_type = size_t;
|
|
|
|
using difference_type = std::ptrdiff_t;
|
|
|
|
using pointer = T*;
|
|
|
|
using const_pointer = const T*;
|
|
|
|
using reference = T&;
|
|
|
|
using const_reference = const T&;
|
|
|
|
using iterator = pointer;
|
|
|
|
using const_iterator = const_pointer;
|
|
|
|
using iterator_category = std::random_access_iterator_tag;
|
|
|
|
using iterator_concept = std::contiguous_iterator_tag;
|
2023-05-23 14:45:54 +01:00
|
|
|
|
2022-12-18 23:08:20 +00:00
|
|
|
ScratchBuffer() = default;
|
|
|
|
|
2023-06-25 00:58:45 +01:00
|
|
|
explicit ScratchBuffer(size_type initial_capacity)
|
2022-12-19 17:30:52 +00:00
|
|
|
: last_requested_size{initial_capacity}, buffer_capacity{initial_capacity},
|
2022-12-18 23:08:20 +00:00
|
|
|
buffer{Common::make_unique_for_overwrite<T[]>(initial_capacity)} {}
|
|
|
|
|
|
|
|
~ScratchBuffer() = default;
|
2023-05-01 18:46:03 +01:00
|
|
|
ScratchBuffer(const ScratchBuffer&) = delete;
|
|
|
|
ScratchBuffer& operator=(const ScratchBuffer&) = delete;
|
2023-02-22 05:26:07 +00:00
|
|
|
ScratchBuffer(ScratchBuffer&&) = default;
|
2023-05-01 18:46:03 +01:00
|
|
|
ScratchBuffer& operator=(ScratchBuffer&&) = default;
|
2022-12-18 23:08:20 +00:00
|
|
|
|
|
|
|
/// This will only grow the buffer's capacity if size is greater than the current capacity.
|
2022-12-20 03:40:50 +00:00
|
|
|
/// The previously held data will remain intact.
|
2023-06-25 00:58:45 +01:00
|
|
|
void resize(size_type size) {
|
2022-12-20 03:40:50 +00: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.
|
2023-06-25 00:58:45 +01:00
|
|
|
void resize_destructive(size_type size) {
|
2022-12-19 17:30:52 +00:00
|
|
|
if (size > buffer_capacity) {
|
|
|
|
buffer_capacity = size;
|
|
|
|
buffer = Common::make_unique_for_overwrite<T[]>(buffer_capacity);
|
2022-12-18 23:08:20 +00:00
|
|
|
}
|
|
|
|
last_requested_size = size;
|
|
|
|
}
|
|
|
|
|
2023-06-25 00:58:45 +01:00
|
|
|
[[nodiscard]] pointer data() noexcept {
|
2022-12-18 23:08:20 +00:00
|
|
|
return buffer.get();
|
|
|
|
}
|
|
|
|
|
2023-06-25 00:58:45 +01:00
|
|
|
[[nodiscard]] const_pointer data() const noexcept {
|
2022-12-18 23:08:20 +00:00
|
|
|
return buffer.get();
|
|
|
|
}
|
|
|
|
|
2023-06-25 00:58:45 +01:00
|
|
|
[[nodiscard]] iterator begin() noexcept {
|
2022-12-18 23:08:20 +00:00
|
|
|
return data();
|
|
|
|
}
|
|
|
|
|
2023-06-25 00:58:45 +01:00
|
|
|
[[nodiscard]] const_iterator begin() const noexcept {
|
2022-12-18 23:08:20 +00:00
|
|
|
return data();
|
|
|
|
}
|
|
|
|
|
2023-06-25 00:58:45 +01:00
|
|
|
[[nodiscard]] iterator end() noexcept {
|
2022-12-18 23:08:20 +00:00
|
|
|
return data() + last_requested_size;
|
|
|
|
}
|
|
|
|
|
2023-06-25 00:58:45 +01:00
|
|
|
[[nodiscard]] const_iterator end() const noexcept {
|
2022-12-18 23:08:20 +00:00
|
|
|
return data() + last_requested_size;
|
|
|
|
}
|
|
|
|
|
2023-06-25 00:58:45 +01:00
|
|
|
[[nodiscard]] reference operator[](size_type i) {
|
2022-12-18 23:08:20 +00:00
|
|
|
return buffer[i];
|
|
|
|
}
|
|
|
|
|
2023-06-25 00:58:45 +01:00
|
|
|
[[nodiscard]] const_reference operator[](size_type i) const {
|
2022-12-20 03:40:50 +00:00
|
|
|
return buffer[i];
|
|
|
|
}
|
|
|
|
|
2023-06-25 00:58:45 +01:00
|
|
|
[[nodiscard]] size_type size() const noexcept {
|
2022-12-18 23:08:20 +00:00
|
|
|
return last_requested_size;
|
|
|
|
}
|
|
|
|
|
2023-06-25 00:58:45 +01:00
|
|
|
[[nodiscard]] size_type capacity() const noexcept {
|
2022-12-19 17:30:52 +00:00
|
|
|
return buffer_capacity;
|
|
|
|
}
|
|
|
|
|
2023-05-01 18:46:03 +01: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-18 23:08:20 +00:00
|
|
|
private:
|
2023-06-25 00:58:45 +01:00
|
|
|
size_type last_requested_size{};
|
|
|
|
size_type buffer_capacity{};
|
2022-12-18 23:08:20 +00:00
|
|
|
std::unique_ptr<T[]> buffer{};
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Common
|