BufferPool docs + fix

This commit is contained in:
MihailRis 2024-04-15 00:55:35 +03:00
parent 4e5f6a5a7d
commit 0cbaa1ccc6

View File

@ -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);
});
}