C/C++ API Reference
Loading...
Searching...
No Matches
pw::multibuf::v1_adapter::SimpleAllocator Class Reference

Overview

A MultiBufAllocator that provides its best-fit allocator and mimics v1::SimpleAllocator.

This type can be used as a drop-in replacement for v1::SimpleAllocator while migrating to using pw_multibuf/v2.

Like the v1 version, this type is thread-safe.

Inheritance diagram for pw::multibuf::v1_adapter::SimpleAllocator:
pw::multibuf::v1_adapter::MultiBufAllocator

Public Member Functions

 SimpleAllocator (ByteSpan region, Allocator &metadata_allocator, size_t alignment=1)
 
bool TryReserveRegions (size_t num_regions)
 
- Public Member Functions inherited from pw::multibuf::v1_adapter::MultiBufAllocator
 MultiBufAllocator (MultiBufAllocator &)=delete
 MultiBufAllocator is not copyable or movable.
 
MultiBufAllocatoroperator= (MultiBufAllocator &)=delete
 
 MultiBufAllocator (MultiBufAllocator &&)=delete
 
MultiBufAllocatoroperator= (MultiBufAllocator &&)=delete
 
std::optional< MultiBufAllocate (size_t min_size)
 
std::optional< MultiBufAllocate (size_t min_size, size_t desired_size)
 
std::optional< MultiBufAllocateContiguous (size_t min_size)
 
std::optional< MultiBufAllocateContiguous (size_t min_size, size_t desired_size)
 
std::optional< size_t > GetBackingCapacity ()
 

Private Member Functions

std::optional< MultiBufDoAllocate (size_t min_size, size_t desired_size, bool contiguous) override
 
std::optional< size_t > DoGetBackingCapacity () override
 

Member Function Documentation

◆ DoAllocate()

std::optional< MultiBuf > pw::multibuf::v1_adapter::SimpleAllocator::DoAllocate ( size_t  min_size,
size_t  desired_size,
bool  contiguous 
)
overrideprivatevirtual

Attempts to allocate a MultiBuf of exactly size bytes.

Memory allocated by an arbitrary MultiBufAllocator does not provide any alignment requirements, preferring instead to allow the allocator maximum flexibility for placing regions (especially discontiguous regions).

Return values
``MultiBuf``if the allocation was successful.
``nullopt_t``if the memory is not currently available.

Implements pw::multibuf::v1_adapter::MultiBufAllocator.

◆ DoGetBackingCapacity()

std::optional< size_t > pw::multibuf::v1_adapter::SimpleAllocator::DoGetBackingCapacity ( )
overrideprivatevirtual

Returns the total amount of memory provided by this object.

This is an optional method. Some memory providers may not have an easily defined capacity, e.g. the system allocator.

Return values
thetotal memory if known.
``nullopt_t``if the total memory is not knowable.

Implements pw::multibuf::v1_adapter::MultiBufAllocator.

◆ TryReserveRegions()

bool pw::multibuf::v1_adapter::SimpleAllocator::TryReserveRegions ( size_t  num_regions)

Attempts to allocate enough capacity to track num_regions.

This method can be used to pre-allocate space for region metadata to mitigate allocation failure due to insufficient space for metadata.

The guarantueed minimum number of allocated regions for a given value of num_regions is floor(num_regions / 2), as consecutive free regions are merged.


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