1 #ifndef STAN__MEMORY__STACK_ALLOC_HPP
2 #define STAN__MEMORY__STACK_ALLOC_HPP
9 #include <msinttypes.h>
33 return (reinterpret_cast<uintptr_t>(ptr) % bytes_aligned) == 0U;
38 const size_t DEFAULT_INITIAL_NBYTES = 1 << 16;
43 inline char* eight_byte_aligned_malloc(
size_t size) {
44 char* ptr =
static_cast<char*
>(malloc(size));
48 s <<
"invalid alignment to 8 bytes, ptr="
49 <<
reinterpret_cast<uintptr_t
>(ptr)
51 throw std::runtime_error(s.str());
78 std::vector<char*> blocks_;
79 std::vector<size_t> sizes_;
85 std::vector<size_t> nested_cur_blocks_;
86 std::vector<char*> nested_next_locs_;
87 std::vector<char*> nested_cur_block_ends_;
98 char* move_to_next_block(
size_t len) {
102 while ((cur_block_ < blocks_.size()) && (sizes_[cur_block_] < len))
105 if (
unlikely(cur_block_ >= blocks_.size())) {
107 size_t newsize = sizes_.back() * 2;
110 blocks_.push_back(eight_byte_aligned_malloc(newsize));
112 throw std::bad_alloc();
113 sizes_.push_back(newsize);
115 result = blocks_[cur_block_];
117 next_loc_ = result + len;
118 cur_block_end_ = result + sizes_[cur_block_];
134 blocks_(1, eight_byte_aligned_malloc(initial_nbytes)),
135 sizes_(1,initial_nbytes),
137 cur_block_end_(blocks_[0] + initial_nbytes),
138 next_loc_(blocks_[0]) {
140 throw std::bad_alloc();
151 for (
size_t i = 0; i < blocks_.size(); ++i)
170 char* result = next_loc_;
173 if (
unlikely(next_loc_ >= cur_block_end_))
174 result = move_to_next_block(len);
175 return (
void*)result;
186 next_loc_ = blocks_[0];
187 cur_block_end_ = next_loc_ + sizes_[0];
195 nested_cur_blocks_.push_back(cur_block_);
196 nested_next_locs_.push_back(next_loc_);
197 nested_cur_block_ends_.push_back(cur_block_end_);
204 if (
unlikely(nested_cur_blocks_.empty()))
207 cur_block_ = nested_cur_blocks_.back();
208 nested_cur_blocks_.pop_back();
210 next_loc_ = nested_next_locs_.back();
211 nested_next_locs_.pop_back();
213 cur_block_end_ = nested_cur_block_ends_.back();
214 nested_cur_block_ends_.pop_back();
224 for (
size_t i = 1; i < blocks_.size(); ++i)
244 for (
size_t i = 0; i <= cur_block_; ++i) {
size_t bytes_allocated()
Return number of bytes allocated to this instance by the heap.
void free_all()
Free all memory used by the stack allocator other than the initial block allocation back to the syste...
An instance of this class provides a memory pool through which blocks of raw memory may be allocated ...
fvar< T > sum(const Eigen::Matrix< fvar< T >, R, C > &m)
~stack_alloc()
Destroy this memory allocator.
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...
int size(const std::vector< T > &x)
bool is_aligned(T *ptr, unsigned int bytes_aligned)
Return true if the specified pointer is aligned on the number of bytes.
void start_nested()
Store current positions before doing nested operation so can recover back to start.
void recover_nested()
recover memory back to the last start_nested call.
stack_alloc(size_t initial_nbytes=DEFAULT_INITIAL_NBYTES)
Construct a resizable stack allocator initially holding the specified number of bytes.
void recover_all()
Recover all the memory used by the stack allocator.