1 #ifndef STAN_MATH_MEMORY_STACK_ALLOC_HPP
2 #define STAN_MATH_MEMORY_STACK_ALLOC_HPP
7 #include <msinttypes.h>
35 return (reinterpret_cast<uintptr_t>(ptr) % bytes_aligned) == 0U;
40 const size_t DEFAULT_INITIAL_NBYTES = 1 << 16;
45 inline char* eight_byte_aligned_malloc(
size_t size) {
46 char* ptr =
static_cast<char*
>(malloc(size));
50 s <<
"invalid alignment to 8 bytes, ptr="
51 <<
reinterpret_cast<uintptr_t
>(ptr)
53 throw std::runtime_error(s.str());
80 std::vector<char*> blocks_;
82 std::vector<size_t> sizes_;
88 std::vector<size_t> nested_cur_blocks_;
89 std::vector<char*> nested_next_locs_;
90 std::vector<char*> nested_cur_block_ends_;
101 char* move_to_next_block(
size_t len) {
105 while ((cur_block_ < blocks_.size()) && (sizes_[cur_block_] < len))
108 if (
unlikely(cur_block_ >= blocks_.size())) {
110 size_t newsize = sizes_.back() * 2;
113 blocks_.push_back(eight_byte_aligned_malloc(newsize));
115 throw std::bad_alloc();
116 sizes_.push_back(newsize);
118 result = blocks_[cur_block_];
120 next_loc_ = result + len;
121 cur_block_end_ = result + sizes_[cur_block_];
135 explicit stack_alloc(
size_t initial_nbytes = DEFAULT_INITIAL_NBYTES) :
136 blocks_(1, eight_byte_aligned_malloc(initial_nbytes)),
137 sizes_(1, initial_nbytes),
139 cur_block_end_(blocks_[0] + initial_nbytes),
140 next_loc_(blocks_[0]) {
142 throw std::bad_alloc();
153 for (
size_t i = 0; i < blocks_.size(); ++i)
172 char* result = next_loc_;
175 if (
unlikely(next_loc_ >= cur_block_end_))
176 result = move_to_next_block(len);
177 return reinterpret_cast<void*
>(result);
188 template <
typename T>
191 return static_cast<T*
>(
alloc(n *
sizeof(T)));
203 next_loc_ = blocks_[0];
204 cur_block_end_ = next_loc_ + sizes_[0];
212 nested_cur_blocks_.push_back(cur_block_);
213 nested_next_locs_.push_back(next_loc_);
214 nested_cur_block_ends_.push_back(cur_block_end_);
221 if (
unlikely(nested_cur_blocks_.empty()))
224 cur_block_ = nested_cur_blocks_.back();
225 nested_cur_blocks_.pop_back();
227 next_loc_ = nested_next_locs_.back();
228 nested_next_locs_.pop_back();
230 cur_block_end_ = nested_cur_block_ends_.back();
231 nested_cur_block_ends_.pop_back();
241 for (
size_t i = 1; i < blocks_.size(); ++i)
261 for (
size_t i = 0; i <= cur_block_; ++i) {
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
~stack_alloc()
Destroy this memory allocator.
void recover_nested()
recover memory back to the last start_nested call.
void free_all()
Free all memory used by the stack allocator other than the initial block allocation back to the syste...
void recover_all()
Recover all the memory used by the stack allocator.
size_t bytes_allocated()
Return number of bytes allocated to this instance by the heap.
T * alloc_array(size_t n)
Allocate an array on the arena of the specified size to hold values of the specified template paramet...
int size(const std::vector< T > &x)
void start_nested()
Store current positions before doing nested operation so can recover back to start.
bool is_aligned(T *ptr, unsigned int bytes_aligned)
Return true if the specified pointer is aligned on the number of bytes.
stack_alloc(size_t initial_nbytes=DEFAULT_INITIAL_NBYTES)
Construct a resizable stack allocator initially holding the specified number of bytes.
An instance of this class provides a memory pool through which blocks of raw memory may be allocated ...
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...