mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-06 03:21: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>
|
||||
|
||||
namespace util {
|
||||
/// @brief Thread-safe pool of same-sized buffers
|
||||
/// @tparam T array type
|
||||
template<class T>
|
||||
class BufferPool {
|
||||
std::vector<std::unique_ptr<T[]>> buffers;
|
||||
|
|
@ -19,6 +21,8 @@ namespace util {
|
|||
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::lock_guard lock(mutex);
|
||||
if (freeBuffers.empty()) {
|
||||
|
|
@ -28,6 +32,7 @@ namespace util {
|
|||
auto* buffer = freeBuffers.front();
|
||||
freeBuffers.pop();
|
||||
return std::shared_ptr<T[]>(buffer, [this](T* ptr) {
|
||||
std::lock_guard lock(mutex);
|
||||
freeBuffers.push(ptr);
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue