C/C++ API Reference
Loading...
Searching...
No Matches
pw::multibuf::v1_adapter::internal::ChunkAllocator Class Referenceabstract

Overview

Allocator interface that specializes in producing v1-compatible chunks.

This allocator does not store allocation inline with allocated memory, like a BlockAllocator. Instead, it has a reference to a second allocator that it can use provide space for metadata tracking which regions of an overall memory arena are allocated. This allows the allocator to fully allocate the arena without using any of it for overhead.

The details of how the second allocator is used are implemented by the derived classes.

Inheritance diagram for pw::multibuf::v1_adapter::internal::ChunkAllocator:
pw::Allocator pw::Deallocator pw::multibuf::v1_adapter::internal::SingleChunkAllocator pw::multibuf::v1_adapter::SingleChunkRegionTracker

Public Member Functions

constexpr Allocatormetadata_allocator () const
 
constexpr size_t alignment () const
 
constexpr size_t available () const
 
constexpr ByteSpan buffer () const
 
std::optional< OwnedChunkAllocateChunk (size_t min_size, size_t desired_size)
 
std::optional< OwnedChunkAllocateChunk (size_t size)
 
- Public Member Functions inherited from pw::Allocator
void * Allocate (Layout layout)
 
template<typename T , int &... kExplicitGuard, std::enable_if_t<!std::is_array_v< T >, int > = 0, typename... Args>
T * New (Args &&... args)
 
template<typename T , int &... kExplicitGuard, typename ElementType = std::remove_extent_t<T>, std::enable_if_t< is_bounded_array_v< T >, int > = 0>
ElementType * New ()
 
template<typename T , int &... kExplicitGuard, typename ElementType = std::remove_extent_t<T>, std::enable_if_t< is_unbounded_array_v< T >, int > = 0>
ElementType * New (size_t count)
 
template<typename T , int &... kExplicitGuard, typename ElementType = std::remove_extent_t<T>, std::enable_if_t< is_unbounded_array_v< T >, int > = 0>
ElementType * New (size_t count, size_t alignment)
 Constructs an alignment-byte aligned array of count objects.
 
template<typename T , int &... kExplicitGuard, std::enable_if_t<!std::is_array_v< T >, int > = 0, typename... Args>
UniquePtr< T > MakeUnique (Args &&... args)
 
template<typename T , std::enable_if_t< is_unbounded_array_v< T >, int > = 0>
UniquePtr< T > MakeUnique (size_t size)
 
template<typename T , std::enable_if_t< is_unbounded_array_v< T >, int > = 0>
UniquePtr< T > MakeUnique (size_t size, size_t alignment)
 
template<typename T , std::enable_if_t< is_bounded_array_v< T >, int > = 0>
UniquePtr< T > MakeUnique ()
 
template<typename T , int &... kExplicitGuard, std::enable_if_t<!std::is_array_v< T >, int > = 0, typename... Args>
SharedPtr< T > MakeShared (Args &&... args)
 
template<typename T , std::enable_if_t< is_unbounded_array_v< T >, int > = 0>
SharedPtr< T > MakeShared (size_t size)
 
template<typename T , std::enable_if_t< is_unbounded_array_v< T >, int > = 0>
SharedPtr< T > MakeShared (size_t size, size_t alignment)
 
template<typename T , std::enable_if_t< is_bounded_array_v< T >, int > = 0>
SharedPtr< T > MakeShared ()
 
bool Resize (void *ptr, size_t new_size)
 
void * Reallocate (void *ptr, Layout new_layout)
 
size_t GetAllocated () const
 
std::optional< FragmentationMeasureFragmentation () const
 Returns fragmentation information for the allocator's memory region.
 
- Public Member Functions inherited from pw::Deallocator
constexpr const Capabilitiescapabilities () const
 
constexpr bool HasCapability (Capability capability) const
 Returns whether a given capability is enabled for this object.
 
void Deallocate (void *ptr)
 
template<typename ElementType >
void DeleteArray (ElementType *ptr, size_t count)
 
StatusWithSize GetCapacity () const
 
bool IsEqual (const Deallocator &other) const
 
template<typename T , int &... kExplicitGuard, std::enable_if_t<!std::is_array_v< T >, int > = 0>
void Delete (T *ptr)
 
template<typename T , int &... kExplicitGuard, typename ElementType = std::remove_extent_t<T>, std::enable_if_t< is_bounded_array_v< T >, int > = 0>
void Delete (ElementType *ptr)
 
template<typename T , int &... kExplicitGuard, typename ElementType = std::remove_extent_t<T>, std::enable_if_t< is_unbounded_array_v< T >, int > = 0>
void Delete (ElementType *ptr, size_t count)
 

Protected Types

using ControlBlock = allocator::internal::ControlBlock
 
- Protected Types inherited from pw::Deallocator
enum class  InfoType {
  kRequestedLayoutOf , kUsableLayoutOf , kAllocatedLayoutOf , kCapacity ,
  kRecognizes
}
 

Protected Member Functions

 PW_PACKED (struct) Region
 
 ChunkAllocator (Allocator &metadata_allocator, size_t alignment)
 
constexpr size_t num_allocations () const
 
void SetRegion (ByteSpan region)
 
void * DoAllocate (allocator::Layout layout) override
 
void DoDeallocate (void *ptr) override
 
Result< LayoutDoGetInfo (InfoType info_type, const void *ptr) const override
 
virtual Region * AllocateRegion (size_t min_size, size_t desired_size)=0
 
virtual size_t TryDeallocateRegion (void *ptr)=0
 
- Protected Member Functions inherited from pw::Allocator
constexpr Allocator ()=default
 TODO(b/326509341): Remove when downstream consumers migrate.
 
constexpr Allocator (const Capabilities &capabilities)
 
virtual void * DoAllocate (Layout layout)=0
 
virtual bool DoResize (void *ptr, size_t new_size)
 
virtual void * DoReallocate (void *ptr, Layout new_layout)
 
virtual size_t DoGetAllocated () const
 
virtual std::optional< FragmentationDoMeasureFragmentation () const
 Returns fragmentation information for the allocator's memory region.
 
- Protected Member Functions inherited from pw::Deallocator
constexpr Deallocator ()=default
 TODO(b/326509341): Remove when downstream consumers migrate.
 
constexpr Deallocator (const Capabilities &capabilities)
 
Result< LayoutGetInfo (InfoType info_type, const void *ptr) const
 
Result< LayoutGetRequestedLayout (const void *ptr) const
 
Result< LayoutGetUsableLayout (const void *ptr) const
 
Result< LayoutGetAllocatedLayout (const void *ptr) const
 
bool Recognizes (const void *ptr) const
 
virtual void DoDeallocate (void *ptr)=0
 
virtual Result< LayoutDoGetInfo (InfoType, const void *) const
 

Additional Inherited Members

- Public Types inherited from pw::Allocator
using Fragmentation = allocator::Fragmentation
 
- Public Types inherited from pw::Deallocator
using Capabilities = allocator::Capabilities
 
using Capability = allocator::Capability
 
using Layout = allocator::Layout
 
- Static Protected Member Functions inherited from pw::Deallocator
static Result< LayoutGetInfo (const Deallocator &deallocator, InfoType info_type, const void *ptr)
 
static Result< LayoutGetRequestedLayout (const Deallocator &deallocator, const void *ptr)
 
static Result< LayoutGetUsableLayout (const Deallocator &deallocator, const void *ptr)
 
static Result< LayoutGetAllocatedLayout (const Deallocator &deallocator, const void *ptr)
 
static bool Recognizes (const Deallocator &deallocator, const void *ptr)
 
- Static Protected Attributes inherited from pw::Deallocator
template<typename T >
static constexpr bool is_bounded_array_v = ::pw::is_bounded_array_v<T>
 
template<typename T >
static constexpr bool is_unbounded_array_v = ::pw::is_unbounded_array_v<T>
 

Member Function Documentation

◆ AllocateChunk() [1/2]

std::optional< OwnedChunk > pw::multibuf::v1_adapter::internal::ChunkAllocator::AllocateChunk ( size_t  min_size,
size_t  desired_size 
)

Allocates a chunk of at least min_size and up to desired_size bytes and returns it, or return std::nullopt if allocation fails.

◆ AllocateChunk() [2/2]

std::optional< OwnedChunk > pw::multibuf::v1_adapter::internal::ChunkAllocator::AllocateChunk ( size_t  size)
inline

Allocates a chunk of size bytes and returns it, or return std::nullopt if allocation fails.

◆ AllocateRegion()

virtual Region * pw::multibuf::v1_adapter::internal::ChunkAllocator::AllocateRegion ( size_t  min_size,
size_t  desired_size 
)
protectedpure virtual

Returns a pointer to a region describing allocated memory of at least min_size and up to desired_size, or null if allocation failed.

Implemented in pw::multibuf::v1_adapter::internal::SingleChunkAllocator.

◆ DoAllocate()

void * pw::multibuf::v1_adapter::internal::ChunkAllocator::DoAllocate ( allocator::Layout  layout)
overrideprotectedvirtual

Allocates a block of memory with the specified size and alignment.

Returns nullptr if the allocation cannot be made, or the layout has a size of 0.

Parameters
[in]layoutDescribes the memory to be allocated.

Implements pw::Allocator.

Reimplemented in pw::multibuf::v1_adapter::internal::SingleChunkAllocator.

◆ DoDeallocate()

void pw::multibuf::v1_adapter::internal::ChunkAllocator::DoDeallocate ( void *  ptr)
overrideprotectedvirtual

Releases a previously-allocated block of memory.

The given pointer must have been previously provided by this memory resource; otherwise the behavior is undefined.

Parameters
[in]ptrPointer to previously-allocated memory.

Implements pw::Deallocator.

Reimplemented in pw::multibuf::v1_adapter::internal::SingleChunkAllocator.

◆ DoGetInfo()

Result< Layout > pw::multibuf::v1_adapter::internal::ChunkAllocator::DoGetInfo ( InfoType  info_type,
const void *  ptr 
) const
overrideprotectedvirtual

Returns deallocator-specific information about allocations.

Deallocators may support any number of InfoTypes. See that type for what each supported type returns. For unsupported types, this method returns UNIMPLEMENTED.

Reimplemented from pw::Deallocator.

◆ SetRegion()

void pw::multibuf::v1_adapter::internal::ChunkAllocator::SetRegion ( ByteSpan  region)
protected

Sets the region used to provide chunks.

At most one region may be set by this method or a constructor.

◆ TryDeallocateRegion()

virtual size_t pw::multibuf::v1_adapter::internal::ChunkAllocator::TryDeallocateRegion ( void *  ptr)
protectedpure virtual

Returns the number of bytes deallocated if the given pointer refers to memory described by a region previously returned by AllocateRegion, or 0 if it does not.

Implemented in pw::multibuf::v1_adapter::internal::SingleChunkAllocator.


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