mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-13 15:01:49 +00:00
BufferPool docs + fix
This commit is contained in:
parent
4e5f6a5a7d
commit
0cbaa1ccc6
1 changed files with 5 additions and 0 deletions
|
|
@ -9,6 +9,8 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
namespace util {
|
namespace util {
|
||||||
|
/// @brief Thread-safe pool of same-sized buffers
|
||||||
|
/// @tparam T array type
|
||||||
template<class T>
|
template<class T>
|
||||||
class BufferPool {
|
class BufferPool {
|
||||||
std::vector<std::unique_ptr<T[]>> buffers;
|
std::vector<std::unique_ptr<T[]>> buffers;
|
||||||
|
|
@ -19,6 +21,8 @@ namespace util {
|
||||||
BufferPool(size_t bufferSize) : bufferSize(bufferSize) {
|
BufferPool(size_t bufferSize) : bufferSize(bufferSize) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief Retrieve a buffer for use
|
||||||
|
/// @return pointer that brings buffer back to the pool when destroyed
|
||||||
std::shared_ptr<T[]> get() {
|
std::shared_ptr<T[]> get() {
|
||||||
std::lock_guard lock(mutex);
|
std::lock_guard lock(mutex);
|
||||||
if (freeBuffers.empty()) {
|
if (freeBuffers.empty()) {
|
||||||
|
|
@ -28,6 +32,7 @@ namespace util {
|
||||||
auto* buffer = freeBuffers.front();
|
auto* buffer = freeBuffers.front();
|
||||||
freeBuffers.pop();
|
freeBuffers.pop();
|
||||||
return std::shared_ptr<T[]>(buffer, [this](T* ptr) {
|
return std::shared_ptr<T[]>(buffer, [this](T* ptr) {
|
||||||
|
std::lock_guard lock(mutex);
|
||||||
freeBuffers.push(ptr);
|
freeBuffers.push(ptr);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue