Stan Math Library
5.0.0
Automatic Differentiation
|
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.
Alignment up to 8 byte boundaries guaranteed for the first malloc, and after that it's up to the caller. On 64-bit architectures, all struct values should be padded to 8-byte boundaries if they contain an 8-byte member or a virtual function.
Definition at line 72 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) |
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. | |
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) |
Moves us to the next block of memory, allocating that block if necessary, and allocates len bytes of memory within that block. | |
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_ |