20 #ifndef MPD_SLICE_BUFFER_HXX
21 #define MPD_SLICE_BUFFER_HXX
54 unsigned n_initialized;
68 size_t CalcAllocationSize()
const {
69 return n_max *
sizeof(Slice);
74 :n_max(_count), n_initialized(0), n_allocated(0),
83 assert(n_allocated == 0);
85 HugeFree(data, CalcAllocationSize());
96 return n_allocated == 0;
100 return n_allocated == n_max;
103 template<
typename... Args>
105 assert(n_initialized <= n_max);
106 assert(n_allocated <= n_initialized);
108 if (available ==
nullptr) {
109 if (n_initialized == n_max) {
111 assert(n_allocated == n_max);
115 available = &data[n_initialized++];
116 available->next =
nullptr;
120 T *value = &available->value;
121 available = available->next;
125 return ::new((
void *)value) T(std::forward<Args>(args)...);
129 assert(n_initialized <= n_max);
130 assert(n_allocated > 0);
131 assert(n_allocated <= n_initialized);
133 Slice *slice =
reinterpret_cast<Slice *
>(value);
134 assert(slice >= data && slice < data + n_max);
140 slice->next = available;
146 if (n_allocated == 0) {
T * Allocate(Args &&...args)
SliceBuffer(unsigned _count)
static void HugeDiscard(void *, size_t) noexcept
SliceBuffer & operator=(const SliceBuffer &other)=delete
unsigned GetCapacity() const
This class pre-allocates a certain number of objects, and allows callers to allocate and free these o...
static void HugeFree(void *_p, size_t) noexcept
static gcc_malloc void * HugeAllocate(size_t size)