LibSWOC++ 1.5.14
Solid Wall of C++
Loading...
Searching...
No Matches
MemArena::Block Struct Reference

Simple internal arena block of memory. Maintains the underlying memory. More...

#include <MemArena.h>

Collaboration diagram for MemArena::Block:
Collaboration graph

Classes

struct  Linkage
 

Public Member Functions

char * data ()
 Get the start of the data in this block.
 
const char * data () const
 Get the start of the data in this block.
 
char * allocated_data_end ()
 
const char * allocated_data_end () const
 
size_t remaining () const
 Amount of unallocated storage.
 
bool satisfies (size_t n, size_t align) const
 
MemSpan< void > remnant ()
 Span of unallocated storage.
 
MemSpan< void > alloc (size_t n, size_t=DEFAULT_ALIGNMENT)
 
Blockdiscard ()
 
bool contains (const void *ptr) const
 
bool is_full () const
 

Static Public Member Functions

static size_t align_padding (void const *ptr, size_t align)
 

Static Public Attributes

static constexpr size_t MIN_FREE_SPACE = 16
 A block must have at least this much free space to not be "full".
 

Protected Member Functions

 Block (size_t n) noexcept
 

Static Protected Member Functions

static void operator delete (void *ptr) noexcept
 
static void operator delete (void *ptr, void *place) noexcept
 

Protected Attributes

friend MemArena
 Container.
 
size_t size
 Actual block size.
 
size_t allocated {0}
 Current allocated (in use) bytes.
 
struct swoc::MemArena::Block::Linkage _link
 Intrusive list support.
 

Detailed Description

Simple internal arena block of memory. Maintains the underlying memory.

Definition at line 60 of file MemArena.h.

Constructor & Destructor Documentation

◆ Block()

swoc::MemArena::Block::Block ( size_t n)
explicitprotectednoexcept

Construct to have n bytes of available storage.

Note this is descriptive - this presumes use via placement new and the size value describes memory already allocated immediately after this instance.

Parameters
nThe amount of storage.

Member Function Documentation

◆ align_padding()

static size_t swoc::MemArena::Block::align_padding ( void const * ptr,
size_t align )
static

Compute the padding needed such adding it to ptr is a multiple of align.

Parameters
ptrBase pointer.
alignAlignment requirement (must be a power of 2).
Returns
Value to add to ptr to achieve align.

◆ alloc()

MemSpan< void > swoc::MemArena::Block::alloc ( size_t n,
size_t = DEFAULT_ALIGNMENT )

Allocate n bytes from this block.

Parameters
nNumber of bytes to allocate.
alignAlignment requirement (default, no alignment).
Returns
The span of memory allocated.

◆ allocated_data_end() [1/2]

char * swoc::MemArena::Block::allocated_data_end ( )
Returns
The first byte past allocated storage.

◆ allocated_data_end() [2/2]

const char * swoc::MemArena::Block::allocated_data_end ( ) const
Returns
The first byte past allocated storage.

◆ contains()

bool swoc::MemArena::Block::contains ( const void * ptr) const

Check if the byte at address ptr is in this block.

Parameters
ptrAddress of byte to check.
Returns
true if ptr is in this block, false otherwise.

◆ discard()

Block & swoc::MemArena::Block::discard ( )

Discard allocations.

Reset the block state to empty.

Returns
this.

◆ is_full()

bool swoc::MemArena::Block::is_full ( ) const
Returns
true if the block has at least MIN_FREE_SPACE bytes free.

◆ operator delete() [1/2]

static void swoc::MemArena::Block::operator delete ( void * ptr)
staticprotectednoexcept

Override operator delete.

This is required because the allocated memory size is larger than the class size which requires calling free directly, skipping the destructor and avoiding complaints about size mismatches.

Parameters
ptrMemory to be de-allocated.

◆ operator delete() [2/2]

static void swoc::MemArena::Block::operator delete ( void * ptr,
void * place )
staticprotectednoexcept

Override placement (non-allocated) delete.

Parameters
ptrPointer returned from new
placeValue passed to new.

This is called only when the class constructor throws an exception during placement new.

Note
I think the parameters are described correctly, the documentation I can find is a bit vague on the source of these values. It is required even if the constructor is marked noexcept. Both are kept in order to be documented.

◆ satisfies()

bool swoc::MemArena::Block::satisfies ( size_t n,
size_t align ) const
inline

Check if there is n bytes of space at align.

Parameters
nSize required.
alignAlignment required.
Returns
true if there is space, false if not.

Definition at line 16 of file MemArena.cc.

Member Data Documentation

◆ allocated

size_t swoc::MemArena::Block::allocated {0}
protected

Current allocated (in use) bytes.

Definition at line 163 of file MemArena.h.

◆ MemArena

friend swoc::MemArena::Block::MemArena
protected

Container.

Definition at line 125 of file MemArena.h.

◆ MIN_FREE_SPACE

size_t swoc::MemArena::Block::MIN_FREE_SPACE = 16
staticconstexpr

A block must have at least this much free space to not be "full".

Definition at line 62 of file MemArena.h.

◆ size

size_t swoc::MemArena::Block::size
protected

Actual block size.

Definition at line 162 of file MemArena.h.


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