Stan  1.0
probability, sampling & optimization
Public Member Functions | List of all members
stan::memory::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...
 
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...
 

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

Constructor & Destructor Documentation

◆ stack_alloc()

stan::memory::stack_alloc::stack_alloc ( size_t  initial_nbytes = DEFAULT_INITIAL_NBYTES)
inline

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

◆ ~stack_alloc()

stan::memory::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 147 of file stack_alloc.hpp.

Member Function Documentation

◆ alloc()

void* stan::memory::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 166 of file stack_alloc.hpp.

◆ free_all()

void stan::memory::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 193 of file stack_alloc.hpp.

◆ recover_all()

void stan::memory::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 182 of file stack_alloc.hpp.


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

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