1#ifndef INCLUDE_RICCATI_MEMORY_ARENA_ALLOC_HPP
2#define INCLUDE_RICCATI_MEMORY_ARENA_ALLOC_HPP
29template <
unsigned int Alignment,
typename T>
31 return (
reinterpret_cast<uintptr_t
>(ptr) % Alignment) == 0U;
42 unsigned char* ptr =
static_cast<unsigned char*
>(malloc(size));
49 s <<
"invalid alignment to 8 bytes, ptr="
50 <<
reinterpret_cast<uintptr_t
>(ptr) << std::endl;
51 throw std::runtime_error(s.str());
107 size_t newsize =
sizes_.back() * 2;
113 throw std::bad_alloc();
115 sizes_.push_back(newsize);
135 :
blocks_(1, internal::eight_byte_aligned_malloc(initial_nbytes)),
136 sizes_(1, initial_nbytes),
141 throw std::bad_alloc();
145 :
blocks_(1, internal::eight_byte_aligned_malloc(
146 internal::DEFAULT_INITIAL_NBYTES)),
147 sizes_(1, internal::DEFAULT_INITIAL_NBYTES),
152 throw std::bad_alloc();
190 size_t pad = len % 8 == 0 ? 0 : 8 - len % 8;
199 return reinterpret_cast<void*
>(result);
210 template <
typename T>
212 return static_cast<T*
>(
alloc(n *
sizeof(T)));
226 for (
size_t i = 1; i <
blocks_.size(); ++i) {
247 for (
size_t i = 1; i <
blocks_.size(); ++i) {
309template <
typename T,
typename ArenaType>
315 bool owns_alloc =
false)
322 template <
typename U,
typename UArena>
338 template <
typename T_ = T>
340 return alloc_->template alloc_array<T_>(n);
373 template <
typename T>
375 void* ptr = std::malloc(n *
sizeof(T));
376 ptrs_.push_back(ptr);
377 return reinterpret_cast<T*
>(ptr);
380 template <
typename T>
381 constexpr inline void deallocate(T* p, std::size_t n)
noexcept {}
389 for (
auto ptr :
ptrs_) {
395template <
typename Expr>
397 return eval(std::forward<Expr>(expr));
RICCATI_ALWAYS_INLINE void * alloc(size_t len)
RICCATI_NO_INLINE arena_alloc(size_t initial_nbytes)
size_t bytes_allocated() const
arena_alloc & operator=(const arena_alloc &)=delete
RICCATI_NO_INLINE byte_t * move_to_next_block(size_t len)
std::vector< size_t > sizes_
RICCATI_NO_INLINE arena_alloc()
RICCATI_ALWAYS_INLINE T * alloc_array(size_t n)
RICCATI_ALWAYS_INLINE void recover_all()
arena_alloc(arena_alloc &&)=delete
arena_alloc(const arena_alloc &)=delete
bool in_stack(const void *ptr) const
RICCATI_NO_INLINE ~arena_alloc()
std::vector< byte_t * > blocks_
arena_alloc & operator=(arena_alloc &&)=delete
#define RICCATI_NO_INLINE
#define RICCATI_ALWAYS_INLINE
#define RICCATI_COLD_PATH
constexpr size_t DEFAULT_INITIAL_NBYTES
RICCATI_ALWAYS_INLINE bool is_aligned(T *ptr)
RICCATI_ALWAYS_INLINE unsigned char * eight_byte_aligned_malloc(size_t size)
auto eval(arena_allocator< T, arena_alloc > &arena, const Expr &expr) noexcept
RICCATI_NO_INLINE arena_allocator(const arena_allocator &rhs)
RICCATI_ALWAYS_INLINE void recover_memory() noexcept
RICCATI_NO_INLINE arena_allocator()
RICCATI_NO_INLINE arena_allocator(const arena_allocator< U, UArena > &rhs)
constexpr bool operator!=(const arena_allocator &) const noexcept
constexpr bool operator==(const arena_allocator &) const noexcept
void deallocate(T *, std::size_t) noexcept
RICCATI_ALWAYS_INLINE T_ * allocate(std::size_t n)
RICCATI_NO_INLINE arena_allocator(ArenaType *alloc, bool owns_alloc=false)
constexpr bool operator==(const dummy_allocator &) const noexcept
constexpr void deallocate(T *p, std::size_t n) noexcept
std::vector< void * > ptrs_
T * allocate(std::size_t n)
constexpr bool operator!=(const dummy_allocator &) const noexcept
constexpr void recover_memory() noexcept