riccaticpp
|
#include <memory.hpp>
Public Types | |
using | byte_t = unsigned char |
Public Member Functions | |
RICCATI_NO_INLINE byte_t * | move_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_alloc & | operator= (const arena_alloc &)=delete |
arena_alloc (arena_alloc &&)=delete | |
arena_alloc & | operator= (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_t * | cur_block_end_ |
byte_t * | next_loc_ |
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.
using riccati::arena_alloc::byte_t = unsigned char |
Definition at line 80 of file memory.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 134 of file memory.hpp.
|
inline |
Definition at line 144 of file memory.hpp.
|
delete |
|
delete |
|
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_.
|
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.
len | Number of bytes to allocate. |
Definition at line 189 of file memory.hpp.
References cur_block_end_, move_to_next_block(), next_loc_, and unlikely.
|
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 211 of file memory.hpp.
References alloc().
|
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 267 of file memory.hpp.
References cur_block_, and sizes_.
|
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_.
|
inline |
Indicates whether the memory in the pointer is in the stack.
[in] | ptr | memory location |
Definition at line 283 of file memory.hpp.
References blocks_, cur_block_, next_loc_, and sizes_.
|
inline |
Moves us to the next block of memory, allocating that block if necessary, and allocates len bytes of memory within that block.
len | Number of bytes to allocate. |
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.
|
delete |
|
delete |
|
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.
std::vector<byte_t*> riccati::arena_alloc::blocks_ |
Definition at line 81 of file memory.hpp.
size_t riccati::arena_alloc::cur_block_ |
Definition at line 84 of file memory.hpp.
byte_t* riccati::arena_alloc::cur_block_end_ |
Definition at line 85 of file memory.hpp.
byte_t* riccati::arena_alloc::next_loc_ |
Definition at line 86 of file memory.hpp.
std::vector<size_t> riccati::arena_alloc::sizes_ |
Definition at line 83 of file memory.hpp.