2#include <ossia/detail/pod_vector.hpp>
3#include <ossia/detail/lockfree_queue.hpp>
7template <
typename Obj_T>
10 mpmc_queue<Obj_T> buffers;
15 buffers.try_dequeue(b);
19 void release(Obj_T b) { buffers.enqueue(std::move(b)); }
31template <
typename Obj_T,
typename Alloc, std::size_t... sz>
32struct sized_object_pool
34 using SizeSpec = std::integer_sequence<std::size_t, sz...>;
36 static const constexpr auto buckets = SizeSpec::size() + 1;
37 std::array<mpmc_queue<Obj_T>, buckets> queues;
39 template <std::size_t Sz, std::size_t... Szs>
40 static constexpr int find_bucket_impl(std::size_t size, std::size_t k)
noexcept
44 else if constexpr(
sizeof...(Szs) > 0)
45 return find_bucket_impl<Szs...>(size, k + 1);
50 static constexpr int find_bucket(std::size_t size)
noexcept
52 return find_bucket_impl<sz...>(size, 0);
55 Obj_T acquire_sized(std::size_t req_size = 1024) noexcept
58 if(!queues[find_bucket(req_size)].try_dequeue(b))
60 Alloc::resize(b, req_size);
65 Obj_T acquire_reserved(std::size_t req_size = 1024) noexcept
68 if(!queues[find_bucket(req_size)].try_dequeue(b))
70 Alloc::reserve(b, req_size);
75 void release(Obj_T b)
noexcept
77 queues[find_bucket(Alloc::size(b))].enqueue(std::move(b));
80 static sized_object_pool& instance() noexcept
82 static sized_object_pool p;
88struct container_memory_manager
90 static constexpr void resize(T& t, std::size_t sz)
noexcept { t.resize(sz); }
91 static constexpr void reserve(T& t, std::size_t sz)
noexcept { t.reserve(sz); }
92 static constexpr std::size_t size(
const T& t)
noexcept {
return t.size(); }
97 using buffer = ossia::pod_vector<char>;
99 mpmc_queue<buffer> buffers;
101 buffer acquire(std::size_t req_size = 1024)
105 buffers.try_dequeue(b);
106 b.resize(req_size, boost::container::default_init);
110 void release(buffer b)
113 buffers.enqueue(std::move(b));
116 static buffer_pool& instance()
118 static buffer_pool p;