1#ifndef STAN_MATH_MEMORY_STACK_ALLOC_HPP
2#define STAN_MATH_MEMORY_STACK_ALLOC_HPP
30 return (
reinterpret_cast<uintptr_t
>(ptr) % bytes_aligned) == 0U;
39 char* ptr =
static_cast<char*
>(malloc(
size));
45 s <<
"invalid alignment to 8 bytes, ptr="
46 <<
reinterpret_cast<uintptr_t
>(ptr) << std::endl;
47 throw std::runtime_error(s.str());
104 size_t newsize =
sizes_.back() * 2;
110 throw std::bad_alloc();
112 sizes_.push_back(newsize);
132 :
blocks_(1, internal::eight_byte_aligned_malloc(initial_nbytes)),
133 sizes_(1, initial_nbytes),
138 throw std::bad_alloc();
172 size_t pad = len % 8 == 0 ? 0 : 8 - len % 8;
181 return reinterpret_cast<void*
>(result);
192 template <
typename T>
194 return static_cast<T*
>(
alloc(n *
sizeof(T)));
244 for (
size_t i = 1; i <
blocks_.size(); ++i) {
char * move_to_next_block(size_t len)
Moves us to the next block of memory, allocating that block if necessary, and allocates len bytes of ...
bool in_stack(const void *ptr) const
Indicates whether the memory in the pointer is in the stack.
T * alloc_array(size_t n)
Allocate an array on the arena of the specified size to hold values of the specified template paramet...
std::vector< char * > blocks_
stack_alloc(size_t initial_nbytes=internal::DEFAULT_INITIAL_NBYTES)
Construct a resizable stack allocator initially holding the specified number of bytes.
std::vector< size_t > sizes_
~stack_alloc()
Destroy this memory allocator.
std::vector< char * > nested_cur_block_ends_
void recover_nested()
recover memory back to the last start_nested call.
std::vector< size_t > nested_cur_blocks_
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator.
void start_nested()
Store current positions before doing nested operation so can recover back to start.
size_t bytes_allocated() const
Return number of bytes allocated to this instance by the heap.
std::vector< char * > nested_next_locs_
void recover_all()
Recover all the memory used by the stack allocator.
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 ...
int64_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
const size_t DEFAULT_INITIAL_NBYTES
char * eight_byte_aligned_malloc(size_t size)
auto block(T_x &&x, size_t i, size_t j, size_t nrows, size_t ncols)
Return a nrows x ncols submatrix starting at (i-1, j-1).
bool is_aligned(T *ptr, unsigned int bytes_aligned)
Return true if the specified pointer is aligned on the number of bytes.
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...