6 #ifndef TURI_SFRAME_BUFFER_POOL_HPP 7 #define TURI_SFRAME_BUFFER_POOL_HPP 11 #include <core/parallel/pthread_tools.hpp> 24 explicit inline buffer_pool(
size_t buffer_size = 128) {
32 inline void init(
size_t buffer_size) {
33 m_buffer_size = buffer_size;
41 if (m_free_buffers.empty()) {
42 std::lock_guard<turi::mutex> guard(m_buffer_lock);
44 for (
size_t i = 0;i < m_buffer_pool.size(); ++i) {
45 if (m_buffer_pool[i].unique()) m_free_buffers.push(m_buffer_pool[i]);
48 if (!m_free_buffers.empty()) {
49 std::lock_guard<turi::mutex> guard(m_buffer_lock);
50 if (!m_free_buffers.empty()) {
51 auto ret = m_free_buffers.top();
57 std::shared_ptr<T> new_buffer = std::make_shared<T>();
58 std::lock_guard<turi::mutex> guard(m_buffer_lock);
59 if (m_buffer_pool.size() < m_buffer_size) m_buffer_pool.push_back(new_buffer);
68 const size_t BUFFER_CAPACITY_LIMIT = 1024 * 1024;
71 if (buffer->capacity() >= BUFFER_CAPACITY_LIMIT)
72 buffer->shrink_to_fit();
73 if (m_buffer_pool.size() + m_free_buffers.size() < m_buffer_size) {
74 std::lock_guard<turi::mutex> guard(m_buffer_lock);
75 m_free_buffers.push(std::move(buffer));
94 std::vector<std::shared_ptr<T> > m_buffer_pool;
95 std::stack<std::shared_ptr<T> > m_free_buffers;
void release_buffer(std::shared_ptr< T > &&buffer)
void init(size_t buffer_size)
std::shared_ptr< T > get_new_buffer()