Files
EchoionCore/src/MemoryPool.cpp
T

61 lines
1.4 KiB
C++

#include "echoion/core/Logger.hpp"
#include <MemoryPool.hpp>
#include <format>
#include <vector>
namespace Echoion::Api
{
AudioBufferPool& AudioBufferPool::instance()
{
static AudioBufferPool instance;
return instance;
}
void AudioBufferPool::initialize(size_t bufferSize, size_t poolSize)
{
bufferSize_ = bufferSize;
pool_ = std::make_unique<Void::BufferPool::BufferPool<int16_t>>();
// Pre-warm the size-class bucket: acquire then immediately drop
// `poolSize` buffers so subsequent acquireBuffer() calls reuse storage.
{
std::vector<AudioSampleBuffer> warm;
warm.reserve(poolSize);
for (size_t i = 0; i < poolSize; ++i) {
warm.push_back(pool_->acquire(bufferSize));
}
} // buffers returned to the pool here
ECHOION_LOG_INFO(std::format(
"AudioBufferPool initialized: {} buffers of {} samples (~{} KB)",
poolSize, bufferSize,
(bufferSize * poolSize * sizeof(int16_t)) / 1024));
}
void AudioBufferPool::shutdown()
{
pool_.reset();
ECHOION_LOG_INFO("AudioBufferPool shutdown");
}
AudioSampleBuffer AudioBufferPool::acquireBuffer()
{
if (!pool_) {
return AudioSampleBuffer{};
}
return pool_->acquire(bufferSize_);
}
size_t AudioBufferPool::getAvailableBuffers() const
{
return pool_ ? pool_->stats().pooled : 0;
}
size_t AudioBufferPool::getTotalBuffers() const
{
return pool_ ? static_cast<size_t>(pool_->stats().allocations) : 0;
}
} // namespace Echoion::Api