![]() |
Stan
1.0
probability, sampling & optimization
|
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... | |
| void | recover_all () |
| Recover all the memory used by the stack allocator. More... | |
| void | free_all () |
| Free all memory used by the stack allocator other than the initial block allocation back to the system. 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 79 of file stack_alloc.hpp.
|
inline |
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 130 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 147 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 166 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 193 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 182 of file stack_alloc.hpp.