Automatic Differentiation
 
Loading...
Searching...
No Matches
stan::math::stack_alloc Class Reference

Detailed Description

An instance of this class provides a memory pool through which blocks of raw memory may be allocated and then collected simultaneously.

This class is useful in settings where large numbers of small objects are allocated and then collected all at once. This may include objects whose destructors have no effect.

Memory is allocated on a stack of blocks. Each block allocated is twice as large as the previous one. The memory may be recovered, with the blocks being reused, or all blocks may be freed, resetting the stack of blocks to its original state.

Every allocation is at least 8 byte aligned. Larger alignments can be requested through the alignment argument of alloc().

Definition at line 84 of file stack_alloc.hpp.

#include <stack_alloc.hpp>

Public Member Functions

 stack_alloc (size_t initial_nbytes=internal::DEFAULT_INITIAL_NBYTES)
 Construct a resizable stack allocator initially holding the specified number of bytes.
 
 ~stack_alloc ()
 Destroy this memory allocator.
 
void * alloc (size_t len, size_t alignment=8)
 Return a newly allocated block of memory of the appropriate size managed by the stack allocator.
 
template<typename T >
T * alloc_array (size_t n)
 Allocate an array on the arena of the specified size to hold values of the specified template parameter type.
 
void recover_all ()
 Recover all the memory used by the stack allocator.
 
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.
 
void free_all ()
 Free all memory used by the stack allocator other than the initial block allocation back to the system.
 
size_t bytes_allocated () const
 Return number of bytes allocated to this instance by the heap.
 
size_t approx_bytes_used () const
 Return the approximate number of bytes handed out through calls to alloc().
 
bool in_stack (const void *ptr) const
 Indicates whether the memory in the pointer is in the stack.
 

Private Member Functions

char * move_to_next_block (size_t len, size_t alignment)
 Moves us to the next block of memory, allocating that block if necessary, and allocates len bytes of memory within that block, aligned to alignment bytes.
 

Private Attributes

std::vector< char * > blocks_
 
std::vector< size_t > sizes_
 
size_t cur_block_
 
char * cur_block_end_
 
char * next_loc_
 
std::vector< size_t > nested_cur_blocks_
 
std::vector< char * > nested_next_locs_
 
std::vector< char * > nested_cur_block_ends_
 

The documentation for this class was generated from the following file: