Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
ForwardAllocator Class Reference

An arena allocator, which is optimized to not be able to free individual entries, only the entire allocator. More...

#include <ForwardAllocator.hxx>

Public Member Functions

void * allocate (size_t size, size_t align)
 Allocates a block of memory.
 

Public Attributes

size_t allocSize_ {0}
 Total number of bytes allocated.
 
size_t allocWasted_ {0}
 Number of bytes lost due to alignment and end-of-block chunks.
 

Static Public Attributes

static constexpr size_t BLOCK_BYTE_SIZE = 1024
 Allocated blocks will be this size.
 

Private Types

typedef uint64_t primitive_t
 A primitive type that has sufficient alignment to what we support.
 
typedef primitive_t AlignedPayload[BLOCK_BYTE_SIZE/sizeof(primitive_t)]
 An array type with the BLOCK_BYTE_SIZE size.
 
typedef Buffer< AlignedPayloadBufferType
 Buffer type that we will be allocating from the pool.
 

Private Member Functions

uint8_t * head_ptr ()
 

Private Attributes

TypedQueue< BufferTypeallocatedBlocks_
 Holds all blocks that we allocated (ever).
 
size_t offsetInLast_ {BLOCK_BYTE_SIZE + 1}
 Points into the last allocated block to the next emtpy space.
 
OSMutex lock_
 Lock that protects the offset in last variable and the queue of blocks.
 

Static Private Attributes

static DynamicPoolkbytePool_
 This buffer pool will have one bucket to allocate 1kb objects each and kep them on a free list.
 

Detailed Description

An arena allocator, which is optimized to not be able to free individual entries, only the entire allocator.

Integrated with a buffer pool to reuse large blocks of heap memory without being exposed to heap fragmentation.

Definition at line 51 of file ForwardAllocator.hxx.

Member Typedef Documentation

◆ AlignedPayload

typedef primitive_t ForwardAllocator::AlignedPayload[BLOCK_BYTE_SIZE/sizeof(primitive_t)]
private

An array type with the BLOCK_BYTE_SIZE size.

Definition at line 74 of file ForwardAllocator.hxx.

◆ BufferType

Buffer type that we will be allocating from the pool.

Definition at line 78 of file ForwardAllocator.hxx.

◆ primitive_t

typedef uint64_t ForwardAllocator::primitive_t
private

A primitive type that has sufficient alignment to what we support.

Definition at line 72 of file ForwardAllocator.hxx.

Constructor & Destructor Documentation

◆ ForwardAllocator()

ForwardAllocator::ForwardAllocator ( )

Definition at line 56 of file ForwardAllocator.cxx.

◆ ~ForwardAllocator()

ForwardAllocator::~ForwardAllocator ( )

Definition at line 60 of file ForwardAllocator.cxx.

Member Function Documentation

◆ allocate()

void * ForwardAllocator::allocate ( size_t  size,
size_t  align 
)

Allocates a block of memory.

Thread-safe.

Parameters
sizethe number of bytes to allocate.
alignalignment of allocated object in bytes (1 to 8).

Definition at line 69 of file ForwardAllocator.cxx.

◆ head_ptr()

uint8_t * ForwardAllocator::head_ptr ( )
inlineprivate
Returns
pointer to the beginning of the memory area in the latest allocated block, cast to uint8_t*, or nullptr if no block has been allocated yet.

Definition at line 83 of file ForwardAllocator.hxx.

Member Data Documentation

◆ allocatedBlocks_

TypedQueue<BufferType> ForwardAllocator::allocatedBlocks_
private

Holds all blocks that we allocated (ever).

They will all be freed in the destructor.

Definition at line 94 of file ForwardAllocator.hxx.

◆ allocSize_

size_t ForwardAllocator::allocSize_ {0}

Total number of bytes allocated.

Definition at line 100 of file ForwardAllocator.hxx.

◆ allocWasted_

size_t ForwardAllocator::allocWasted_ {0}

Number of bytes lost due to alignment and end-of-block chunks.

Definition at line 102 of file ForwardAllocator.hxx.

◆ BLOCK_BYTE_SIZE

constexpr size_t ForwardAllocator::BLOCK_BYTE_SIZE = 1024
staticconstexpr

Allocated blocks will be this size.

Definition at line 55 of file ForwardAllocator.hxx.

◆ kbytePool_

DynamicPool * ForwardAllocator::kbytePool_
staticprivate
Initial value:
=
static Bucket * init(int s,...)
Allocate a Bucket array off of the heap initialized with sizes.
Definition Buffer.hxx:406
A specialization of a pool which can allocate new elements dynamically upon request.
Definition Buffer.hxx:491
Buffer< AlignedPayload > BufferType
Buffer type that we will be allocating from the pool.

This buffer pool will have one bucket to allocate 1kb objects each and kep them on a free list.

The freelist is shared between different allocators.

Definition at line 110 of file ForwardAllocator.hxx.

◆ lock_

OSMutex ForwardAllocator::lock_
private

Lock that protects the offset in last variable and the queue of blocks.

Definition at line 106 of file ForwardAllocator.hxx.

◆ offsetInLast_

size_t ForwardAllocator::offsetInLast_ {BLOCK_BYTE_SIZE + 1}
private

Points into the last allocated block to the next emtpy space.

Definition at line 96 of file ForwardAllocator.hxx.


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