riccaticpp
Loading...
Searching...
No Matches
riccati::arena_alloc Class Reference

#include <memory.hpp>

Public Types

using byte_t = unsigned char
 

Public Member Functions

RICCATI_NO_INLINE byte_tmove_to_next_block (size_t len)
 
RICCATI_NO_INLINE arena_alloc (size_t initial_nbytes)
 
RICCATI_NO_INLINE arena_alloc ()
 
 arena_alloc (const arena_alloc &)=delete
 
arena_allocoperator= (const arena_alloc &)=delete
 
 arena_alloc (arena_alloc &&)=delete
 
arena_allocoperator= (arena_alloc &&)=delete
 
RICCATI_NO_INLINE ~arena_alloc ()
 
RICCATI_ALWAYS_INLINE void * alloc (size_t len)
 
template<typename T >
RICCATI_ALWAYS_INLINE T * alloc_array (size_t n)
 
RICCATI_ALWAYS_INLINE void recover_all ()
 
void free_all ()
 
size_t bytes_allocated () const
 
bool in_stack (const void *ptr) const
 

Public Attributes

std::vector< byte_t * > blocks_
 
std::vector< size_t > sizes_
 
size_t cur_block_
 
byte_tcur_block_end_
 
byte_tnext_loc_
 

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 78 of file memory.hpp.

Member Typedef Documentation

◆ byte_t

using riccati::arena_alloc::byte_t = unsigned char

Definition at line 80 of file memory.hpp.

Constructor & Destructor Documentation

◆ arena_alloc() [1/4]

RICCATI_NO_INLINE riccati::arena_alloc::arena_alloc ( size_t 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 134 of file memory.hpp.

References blocks_, and unlikely.

◆ arena_alloc() [2/4]

RICCATI_NO_INLINE riccati::arena_alloc::arena_alloc ( )
inline

Definition at line 144 of file memory.hpp.

References blocks_, and unlikely.

◆ arena_alloc() [3/4]

riccati::arena_alloc::arena_alloc ( const arena_alloc & )
delete

◆ arena_alloc() [4/4]

riccati::arena_alloc::arena_alloc ( arena_alloc && )
delete

◆ ~arena_alloc()

RICCATI_NO_INLINE riccati::arena_alloc::~arena_alloc ( )
inline

Destroy this memory allocator.

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

Definition at line 166 of file memory.hpp.

References blocks_.

Member Function Documentation

◆ alloc()

RICCATI_ALWAYS_INLINE void * riccati::arena_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. If the number of bytes requested is not a multiple of 8, the reserved space will be padded up to the next multiple of 8.

This function may call C++'s malloc() function, with any exceptions percolated through this function.

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

Definition at line 189 of file memory.hpp.

References cur_block_end_, move_to_next_block(), next_loc_, and unlikely.

◆ alloc_array()

template<typename T >
RICCATI_ALWAYS_INLINE T * riccati::arena_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 211 of file memory.hpp.

References alloc().

◆ bytes_allocated()

size_t riccati::arena_alloc::bytes_allocated ( ) const
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 267 of file memory.hpp.

References cur_block_, and sizes_.

◆ free_all()

void riccati::arena_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 245 of file memory.hpp.

References blocks_, recover_all(), and sizes_.

◆ in_stack()

bool riccati::arena_alloc::in_stack ( const void * ptr) const
inline

Indicates whether the memory in the pointer is in the stack.

Parameters
[in]ptrmemory location
Returns
true if the pointer is in the stack, false otherwise.

Definition at line 283 of file memory.hpp.

References blocks_, cur_block_, next_loc_, and sizes_.

◆ move_to_next_block()

RICCATI_NO_INLINE byte_t * riccati::arena_alloc::move_to_next_block ( size_t len)
inline

Moves us to the next block of memory, allocating that block if necessary, and allocates len bytes of memory within that block.

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

Definition at line 97 of file memory.hpp.

References blocks_, cur_block_, cur_block_end_, riccati::internal::eight_byte_aligned_malloc(), next_loc_, sizes_, and unlikely.

◆ operator=() [1/2]

arena_alloc & riccati::arena_alloc::operator= ( arena_alloc && )
delete

◆ operator=() [2/2]

arena_alloc & riccati::arena_alloc::operator= ( const arena_alloc & )
delete

◆ recover_all()

RICCATI_ALWAYS_INLINE void riccati::arena_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. If more than one block exists, all memory is freed and then one large allocation takes place to allow only a single block of memory. To free memory back to the system, use the function free_all().

Definition at line 223 of file memory.hpp.

References blocks_, cur_block_, cur_block_end_, riccati::internal::eight_byte_aligned_malloc(), next_loc_, sizes_, and unlikely.

Member Data Documentation

◆ blocks_

std::vector<byte_t*> riccati::arena_alloc::blocks_

Definition at line 81 of file memory.hpp.

◆ cur_block_

size_t riccati::arena_alloc::cur_block_

Definition at line 84 of file memory.hpp.

◆ cur_block_end_

byte_t* riccati::arena_alloc::cur_block_end_

Definition at line 85 of file memory.hpp.

◆ next_loc_

byte_t* riccati::arena_alloc::next_loc_

Definition at line 86 of file memory.hpp.

◆ sizes_

std::vector<size_t> riccati::arena_alloc::sizes_

Definition at line 83 of file memory.hpp.


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