xentara-utils v2.0.4
The Xentara Utility Library
Loading...
Searching...
No Matches
xentara::utils::memory::ControlBlockMemoryResource< ControlBlock > Class Template Referencefinal

A memory resource that allocates an additional control block. More...

#include <xentara/utils/memory/ControlBlockMemoryResource.hpp>

Public Member Functions

 ControlBlockMemoryResource (std::reference_wrapper< std::pmr::memory_resource > upstream= *std::pmr::get_default_resource())
 Constructor.
 
template<typename... ControlBlockArguments>
requires std::is_constructible_v<ControlBlock, ControlBlockArguments &&...>
auto allocate (std::size_t size, std::size_t alignment=alignof(std::max_align_t), ControlBlockArguments &&...controlBlockArguments) -> void *
 Allocates a block.
 
auto deallocate (void *address, std::size_t size, std::size_t alignment=alignof(std::max_align_t)) -> void
 Deallocates a block previously allocated with allocate()
 
auto upstream () const noexcept -> std::pmr::memory_resource &
 Gets the upstream memory resource.
 
auto is_equal (const ControlBlockMemoryResource &other) const noexcept -> bool
 Determines if this memory resource is interchangable with another resource.
 

Static Public Member Functions

static auto controlBlock (void *address) noexcept -> ControlBlock &
 Retreives the control block from a pointer to the allocated block.
 
static auto payload (const ControlBlock &block) noexcept -> void *
 Returns a pointer to the allocated block belonging to a control block.
 

Detailed Description

template<typename ControlBlock>
class xentara::utils::memory::ControlBlockMemoryResource< ControlBlock >

A memory resource that allocates an additional control block.

This class is just like a regular std::pmr::memory_resource, but allows you to add a control block to each allocated block. You can store meta-information or housekeeping information in the control block, without the client of the memory resource having to know about them. You can retreive the control block for an allocated block using the function controlBlock().

This class has a similar interface to std::pmr::memory_resource, but is not derived from it.

Template Parameters
ControlBlockThe control block type

Constructor & Destructor Documentation

◆ ControlBlockMemoryResource()

template<typename ControlBlock >
xentara::utils::memory::ControlBlockMemoryResource< ControlBlock >::ControlBlockMemoryResource ( std::reference_wrapper< std::pmr::memory_resource upstream = *std::pmr::get_default_resource())

Constructor.

Parameters
upstreamThe upstream memory resource

Member Function Documentation

◆ allocate()

template<typename ControlBlock >
requires std::is_constructible_v<ControlBlock, ControlBlockArguments &&...>
template<typename... ControlBlockArguments>
requires std::is_constructible_v<ControlBlock, ControlBlockArguments &&...>
auto xentara::utils::memory::ControlBlockMemoryResource< ControlBlock >::allocate ( std::size_t  size,
std::size_t  alignment = alignof(std::max_align_t),
ControlBlockArguments &&...  controlBlockArguments 
) -> void *

Allocates a block.

Parameters
sizeThe number of bytes to allocate
alignmentThe alignment requirements of the block
controlBlockArgumentsArguments to be forwarded to the constructor of the control block
Returns
A pointer to the allocated memory

◆ controlBlock()

template<typename ControlBlock >
static auto xentara::utils::memory::ControlBlockMemoryResource< ControlBlock >::controlBlock ( void *  address) -> ControlBlock &
staticnoexcept

Retreives the control block from a pointer to the allocated block.

◆ deallocate()

template<typename ControlBlock >
auto xentara::utils::memory::ControlBlockMemoryResource< ControlBlock >::deallocate ( void *  address,
std::size_t  size,
std::size_t  alignment = alignof(std::max_align_t) 
) -> void

Deallocates a block previously allocated with allocate()

Parameters
addressThe address of the block, as returned by allocate()
sizeThis must be the same value that was passed to allocate() when the block was allocated
alignmentThis must be the same value that was passed to allocate() when the block was allocated

◆ is_equal()

template<typename ControlBlock >
auto xentara::utils::memory::ControlBlockMemoryResource< ControlBlock >::is_equal ( const ControlBlockMemoryResource< ControlBlock > &  other) const -> bool
noexcept

Determines if this memory resource is interchangable with another resource.

Parameters
otherThe other resource
Returns
Returns true if memory allocated with this resource can be freed by other, and vice versa

◆ payload()

template<typename ControlBlock >
static auto xentara::utils::memory::ControlBlockMemoryResource< ControlBlock >::payload ( const ControlBlock &  block) -> void *
staticnoexcept

Returns a pointer to the allocated block belonging to a control block.

◆ upstream()

template<typename ControlBlock >
auto xentara::utils::memory::ControlBlockMemoryResource< ControlBlock >::upstream ( ) const -> std::pmr::memory_resource &
noexcept

Gets the upstream memory resource.