45 virtual size_t Size()
const = 0;
54 template <
size_t object_size>
57 static constexpr
size_t kObjectSize = object_size;
60 : block_size_(block_size * kObjectSize), block_pos_(0) {
66 const auto byte_size = size * kObjectSize;
67 if (byte_size * kAllocFit > block_size_) {
71 return blocks_.back().get();
73 if (block_pos_ + byte_size > block_size_) {
80 auto *ptr = &blocks_.front()[block_pos_];
81 block_pos_ += byte_size;
85 size_t Size()
const override {
return kObjectSize; }
88 const size_t block_size_;
90 std::list<std::unique_ptr<std::byte[]>> blocks_;
103 virtual size_t Size()
const = 0;
112 template <
size_t object_size>
115 static constexpr
size_t kObjectSize = object_size;
118 std::byte buf[kObjectSize];
123 : mem_arena_(pool_size), free_list_(nullptr) {}
127 if (free_list_ ==
nullptr) {
128 link =
static_cast<Link *
>(mem_arena_.Allocate(1));
129 link->next =
nullptr;
132 free_list_ = link->next;
139 auto *link =
static_cast<Link *
>(ptr);
140 link->next = free_list_;
145 size_t Size()
const override {
return kObjectSize; }
163 template <
typename T>
168 : internal::MemoryPoolImpl<sizeof(T)>(pool_size) {}
176 : block_size_(block_size) {}
178 template <
typename T>
180 if (
sizeof(T) >= arenas_.size()) arenas_.resize(
sizeof(T) + 1);
181 auto &arena = arenas_[
sizeof(T)];
182 if (arena ==
nullptr) {
183 arena = std::make_unique<MemoryArena<T>>(block_size_);
192 std::vector<std::unique_ptr<MemoryArenaBase>> arenas_;
200 : pool_size_(pool_size) {}
202 template <
typename T>
204 if (
sizeof(T) >= pools_.size()) pools_.resize(
sizeof(T) + 1);
205 auto &pool = pools_[
sizeof(T)];
206 if (pool ==
nullptr) pool = std::make_unique<MemoryPool<T>>(pool_size_);
214 std::vector<std::unique_ptr<MemoryPoolBase>> pools_;
226 template <
typename T>
236 template <
typename U>
238 : arenas_(arena_alloc.Arenas()) {}
241 if (n * kAllocFit <= kAllocSize) {
242 return static_cast<T *
>(Arena()->Allocate(n));
245 return std::allocator_traits<Allocator>::allocate(allocator, n, hint);
250 if (n * kAllocFit > kAllocSize)
Allocator().deallocate(p, n);
253 std::shared_ptr<MemoryArenaCollection>
Arenas()
const {
return arenas_; }
258 std::shared_ptr<MemoryArenaCollection> arenas_;
261 template <
typename T,
typename U>
267 template <
typename T,
typename U>
281 template <
typename T>
291 template <
typename U>
293 : pools_(pool_alloc.Pools()) {}
297 return static_cast<T *
>(Pool<1>()->Allocate());
299 return static_cast<T *
>(Pool<2>()->Allocate());
301 return static_cast<T *
>(Pool<4>()->Allocate());
303 return static_cast<T *
>(Pool<8>()->Allocate());
304 }
else if (n <= 16) {
305 return static_cast<T *
>(Pool<16>()->Allocate());
306 }
else if (n <= 32) {
307 return static_cast<T *
>(Pool<32>()->Allocate());
308 }
else if (n <= 64) {
309 return static_cast<T *
>(Pool<64>()->Allocate());
312 return std::allocator_traits<Allocator>::allocate(allocator, n, hint);
325 }
else if (n <= 16) {
327 }
else if (n <= 32) {
329 }
else if (n <= 64) {
336 std::shared_ptr<MemoryPoolCollection>
Pools()
const {
return pools_; }
346 return pools_->Pool<TN<n>>();
349 std::shared_ptr<MemoryPoolCollection> pools_;
352 template <
typename T,
typename U>
358 template <
typename T,
typename U>
366 #endif // FST_MEMORY_H_
BlockAllocator(const BlockAllocator< U > &arena_alloc)
typename Allocator::value_type value_type
T * allocate(size_type n, const void *hint=nullptr)
MemoryArenaCollection(size_t block_size=kAllocSize)
std::allocator< T > Allocator
virtual size_t Size() const =0
void deallocate(T *p, size_type n)
T * allocate(size_type n, const void *hint=nullptr)
typename Allocator::size_type size_type
MemoryArenaImpl(size_t block_size=kAllocSize)
typename Allocator::value_type value_type
bool operator!=(const ErrorWeight &, const ErrorWeight &)
MemoryPool(size_t pool_size=kAllocSize)
void deallocate(T *p, size_type n)
std::shared_ptr< MemoryArenaCollection > Arenas() const
std::unique_ptr< T > make_unique_for_overwrite()
void * Allocate(size_t size)
constexpr To implicit_cast(typename internal::type_identity_t< To > to)
virtual ~MemoryArenaBase()=default
typename Allocator::size_type size_type
MemoryPoolImpl(size_t pool_size)
size_t Size() const override
size_t Size() const override
PoolAllocator(size_t pool_size=kAllocSize)
bool operator==(const ErrorWeight &, const ErrorWeight &)
PoolAllocator(const PoolAllocator< U > &pool_alloc)
MemoryPoolCollection(size_t pool_size=kAllocSize)
BlockAllocator(size_t block_size=kAllocSize)
MemoryArena< T > * Arena()
std::shared_ptr< MemoryPoolCollection > Pools() const
std::allocator< T > Allocator