2024-03-06 06:26:38 +01:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
2022-04-23 10:59:50 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2021-02-06 03:11:23 +01:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <type_traits>
|
2021-02-06 06:38:22 +01:00
|
|
|
#include <utility>
|
2021-02-06 03:11:23 +01:00
|
|
|
|
|
|
|
namespace Shader {
|
|
|
|
|
2021-02-15 04:09:11 +01:00
|
|
|
template <typename T>
|
2023-01-29 21:54:13 +01:00
|
|
|
requires std::is_destructible_v<T>
|
2021-09-24 07:21:07 +02:00
|
|
|
class ObjectPool {
|
2021-02-06 03:11:23 +01:00
|
|
|
public:
|
2021-02-15 04:09:11 +01:00
|
|
|
explicit ObjectPool(size_t chunk_size = 8192) : new_chunk_size{chunk_size} {
|
|
|
|
node = &chunks.emplace_back(new_chunk_size);
|
2021-02-06 03:11:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
2023-01-29 21:54:13 +01:00
|
|
|
requires std::is_constructible_v<T, Args...>
|
2021-09-24 07:21:07 +02:00
|
|
|
[[nodiscard]] T* Create(Args&&... args) {
|
2021-02-06 03:11:23 +01:00
|
|
|
return std::construct_at(Memory(), std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReleaseContents() {
|
2021-02-15 04:09:11 +01:00
|
|
|
if (chunks.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Chunk& root{chunks.front()};
|
|
|
|
if (root.used_objects == root.num_objects) {
|
|
|
|
// Root chunk has been filled, squash allocations into it
|
|
|
|
const size_t total_objects{root.num_objects + new_chunk_size * (chunks.size() - 1)};
|
|
|
|
chunks.clear();
|
|
|
|
chunks.emplace_back(total_objects);
|
|
|
|
} else {
|
|
|
|
root.Release();
|
|
|
|
chunks.resize(1);
|
2021-02-06 03:11:23 +01:00
|
|
|
}
|
2021-03-24 02:07:14 +01:00
|
|
|
chunks.shrink_to_fit();
|
|
|
|
node = &chunks.front();
|
2021-02-06 03:11:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct NonTrivialDummy {
|
|
|
|
NonTrivialDummy() noexcept {}
|
|
|
|
};
|
|
|
|
|
|
|
|
union Storage {
|
|
|
|
Storage() noexcept {}
|
|
|
|
~Storage() noexcept {}
|
|
|
|
|
|
|
|
NonTrivialDummy dummy{};
|
|
|
|
T object;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Chunk {
|
2021-02-15 04:09:11 +01:00
|
|
|
explicit Chunk() = default;
|
|
|
|
explicit Chunk(size_t size)
|
|
|
|
: num_objects{size}, storage{std::make_unique<Storage[]>(size)} {}
|
|
|
|
|
|
|
|
Chunk& operator=(Chunk&& rhs) noexcept {
|
|
|
|
Release();
|
|
|
|
used_objects = std::exchange(rhs.used_objects, 0);
|
|
|
|
num_objects = std::exchange(rhs.num_objects, 0);
|
|
|
|
storage = std::move(rhs.storage);
|
2021-07-26 10:01:01 +02:00
|
|
|
return *this;
|
2021-02-15 04:09:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Chunk(Chunk&& rhs) noexcept
|
|
|
|
: used_objects{std::exchange(rhs.used_objects, 0)},
|
|
|
|
num_objects{std::exchange(rhs.num_objects, 0)}, storage{std::move(rhs.storage)} {}
|
|
|
|
|
|
|
|
~Chunk() {
|
|
|
|
Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Release() {
|
|
|
|
std::destroy_n(storage.get(), used_objects);
|
|
|
|
used_objects = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t used_objects{};
|
|
|
|
size_t num_objects{};
|
|
|
|
std::unique_ptr<Storage[]> storage;
|
2021-02-06 03:11:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
[[nodiscard]] T* Memory() {
|
|
|
|
Chunk* const chunk{FreeChunk()};
|
2021-02-15 04:09:11 +01:00
|
|
|
return &chunk->storage[chunk->used_objects++].object;
|
2021-02-06 03:11:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] Chunk* FreeChunk() {
|
2021-02-15 04:09:11 +01:00
|
|
|
if (node->used_objects != node->num_objects) {
|
2021-02-06 03:11:23 +01:00
|
|
|
return node;
|
|
|
|
}
|
2021-02-15 04:09:11 +01:00
|
|
|
node = &chunks.emplace_back(new_chunk_size);
|
2021-02-06 03:11:23 +01:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-02-15 04:09:11 +01:00
|
|
|
Chunk* node{};
|
|
|
|
std::vector<Chunk> chunks;
|
|
|
|
size_t new_chunk_size{};
|
2021-02-06 03:11:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Shader
|