Stan Math Library  2.12.0
reverse mode automatic differentiation
Public Member Functions | List of all members
stan::math::stack_alloc Class Reference

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...
 

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.

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 71 of file stack_alloc.hpp.

Constructor & Destructor Documentation

stan::math::stack_alloc::stack_alloc ( size_t  initial_nbytes = DEFAULT_INITIAL_NBYTES)
inlineexplicit

Construct a resizable stack allocator initially holding the specified number of bytes.

Parameters
initial_nbytesInitial number of bytes for the allocator. Defaults to (1 << 16) = 64KB initial bytes.
Exceptions
std::runtime_errorif the underlying malloc is not 8-byte aligned.

Definition at line 127 of file stack_alloc.hpp.

stan::math::stack_alloc::~stack_alloc ( )
inline

Destroy this memory allocator.

This is implemented as a no-op as there is no destruction required.

Definition at line 143 of file stack_alloc.hpp.

Member Function Documentation

void* stan::math::stack_alloc::alloc ( size_t  len)
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.

Parameters
lenNumber of bytes to allocate.
Returns
A pointer to the allocated memory.

Definition at line 162 of file stack_alloc.hpp.

template<typename T >
T* stan::math::stack_alloc::alloc_array ( size_t  n)
inline

Allocate an array on the arena of the specified size to hold values of the specified template parameter type.

Template Parameters
Ttype of entries in allocated array.
Parameters
[in]nsize of array to allocate.
Returns
new array allocated on the arena.

Definition at line 182 of file stack_alloc.hpp.

size_t stan::math::stack_alloc::bytes_allocated ( )
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?)

Returns
number of bytes allocated to this instance

Definition at line 250 of file stack_alloc.hpp.

void stan::math::stack_alloc::free_all ( )
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 230 of file stack_alloc.hpp.

void stan::math::stack_alloc::recover_all ( )
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 192 of file stack_alloc.hpp.

void stan::math::stack_alloc::recover_nested ( )
inline

recover memory back to the last start_nested call.

Definition at line 211 of file stack_alloc.hpp.

void stan::math::stack_alloc::start_nested ( )
inline

Store current positions before doing nested operation so can recover back to start.

Definition at line 202 of file stack_alloc.hpp.


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

     [ Stan Home Page ] © 2011–2016, Stan Development Team.