![]() |
Stan Math Library
2.8.0
reverse mode automatic differentiation
|
An instance of this class provides a memory pool through which blocks of raw memory may be allocated and then collected simultaneously. More...
#include <stack_alloc.hpp>
Public Member Functions | |
stack_alloc (size_t initial_nbytes=DEFAULT_INITIAL_NBYTES) | |
Construct a resizable stack allocator initially holding the specified number of bytes. More... | |
~stack_alloc () | |
Destroy this memory allocator. More... | |
void * | alloc (size_t len) |
Return a newly allocated block of memory of the appropriate size managed by the stack allocator. More... | |
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. More... | |
void | recover_all () |
Recover all the memory used by the stack allocator. More... | |
void | start_nested () |
Store current positions before doing nested operation so can recover back to start. More... | |
void | recover_nested () |
recover memory back to the last start_nested call. More... | |
void | free_all () |
Free all memory used by the stack allocator other than the initial block allocation back to the system. More... | |
size_t | bytes_allocated () |
Return number of bytes allocated to this instance by the heap. More... | |
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 78 of file stack_alloc.hpp.
|
inlineexplicit |
Construct a resizable stack allocator initially holding the specified number of bytes.
initial_nbytes | Initial number of bytes for the allocator. Defaults to (1 << 16) = 64KB initial bytes. |
std::runtime_error | if the underlying malloc is not 8-byte aligned. |
Definition at line 135 of file stack_alloc.hpp.
|
inline |
Destroy this memory allocator.
This is implemented as a no-op as there is no destruction required.
Definition at line 151 of file stack_alloc.hpp.
|
inline |
Return a newly allocated block of memory of the appropriate size managed by the stack allocator.
The allocated pointer will be 8-byte aligned.
This function may call C++'s malloc()
function, with any exceptions percolated throught this function.
len | Number of bytes to allocate. |
Definition at line 170 of file stack_alloc.hpp.
|
inline |
Allocate an array on the arena of the specified size to hold values of the specified template parameter type.
T | type of entries in allocated array. |
[in] | n | size of array to allocate. |
Definition at line 190 of file stack_alloc.hpp.
|
inline |
Return number of bytes allocated to this instance by the heap.
This is not the same as the number of bytes allocated through calls to memalloc_. The latter number is not calculatable because space is wasted at the end of blocks if the next alloc request doesn't fit. (Perhaps we could trim down to what is actually used?)
Definition at line 259 of file stack_alloc.hpp.
|
inline |
Free all memory used by the stack allocator other than the initial block allocation back to the system.
Note: the destructor will free all memory.
Definition at line 239 of file stack_alloc.hpp.
|
inline |
Recover all the memory used by the stack allocator.
The stack of memory blocks allocated so far will be available for further allocations. To free memory back to the system, use the function free_all().
Definition at line 201 of file stack_alloc.hpp.
|
inline |
recover memory back to the last start_nested call.
Definition at line 220 of file stack_alloc.hpp.
|
inline |
Store current positions before doing nested operation so can recover back to start.
Definition at line 211 of file stack_alloc.hpp.