1 #ifndef __STAN__MEMORY__CHUNK_ALLOC_HPP__
2 #define __STAN__MEMORY__CHUNK_ALLOC_HPP__
11 #define DEFAULT_INITIAL_NCHUNKS (1 << 8)
17 template<
typename T,
size_t Tnchunks_per_block = DEFAULT_INITIAL_NCHUNKS>
20 std::vector<char*> blocks_;
34 blocks_(1, eight_byte_aligned_malloc(sizeof(T)*Tnchunks_per_block)),
38 throw std::bad_alloc();
49 for (
size_t i = 0; i < blocks_.size(); ++i)
62 if (
unlikely(used_ >= Tnchunks_per_block)) {
66 if (
unlikely(cur_block_ >= blocks_.size())) {
67 result = eight_byte_aligned_malloc(Tnchunks_per_block*
sizeof(T));
69 throw std::bad_alloc();
70 blocks_.push_back(result);
72 result = blocks_[cur_block_] +
sizeof(T)*used_;
96 for (
size_t i = 1; i < blocks_.size(); ++i)
void * alloc()
Return a newly allocated chunk of memory of the appropriate size managed by the stack allocator.
void recover_all()
Recover all the memory used by the stack allocator.
chunk_alloc()
Construct a resizable chunk allocator initially holding the specified number of bytes.
void free_all()
Free all memory used by the stack allocator other than the initial block allocation back to the syste...
~chunk_alloc()
Destroy this memory allocator.
Probability, optimization and sampling library.