Pigweed
Loading...
Searching...
No Matches
pw::allocator::BlockAllocator< OffsetType, kPoisonInterval, kAlign > Class Template Referenceabstract

#include <block_allocator.h>

Public Types

using BlockType = Block< OffsetType, kAlign, kPoisonInterval !=0 >
 
using Range = typename BlockType::Range
 
- Public Types inherited from pw::Deallocator
using Capabilities = allocator::Capabilities
 
using Capability = allocator::Capability
 
using Layout = allocator::Layout
 

Public Member Functions

constexpr BlockAllocator ()
 Constexpr constructor. Callers must explicitly call Init.
 
 BlockAllocator (ByteSpan region)
 
Range blocks () const
 Returns a Range of blocks tracking the memory of this allocator.
 
void Init (ByteSpan region)
 
void Init (BlockType *begin)
 
virtual void Init (BlockType *begin, BlockType *end)
 
Fragmentation MeasureFragmentation () const
 Returns fragmentation information for the block allocator's memory region.
 
void Reset ()
 
- Public Member Functions inherited from pw::allocator::internal::GenericBlockAllocator
 GenericBlockAllocator (const GenericBlockAllocator &)=delete
 
GenericBlockAllocatoroperator= (const GenericBlockAllocator &)=delete
 
 GenericBlockAllocator (GenericBlockAllocator &&)=delete
 
GenericBlockAllocatoroperator= (GenericBlockAllocator &&)=delete
 
- Public Member Functions inherited from pw::Allocator
void * Allocate (Layout layout)
 
template<typename T , int &... ExplicitGuard, typename... Args>
T * New (Args &&... args)
 
template<typename T , int &... ExplicitGuard, typename... Args>
UniquePtr< T > MakeUnique (Args &&... args)
 
bool Resize (void *ptr, size_t new_size)
 
bool Resize (void *ptr, Layout layout, size_t new_size)
 
void * Reallocate (void *ptr, Layout new_layout)
 
void * Reallocate (void *ptr, Layout old_layout, size_t new_size)
 
allocator::AsPmrAllocator as_pmr ()
 
- Public Member Functions inherited from pw::Deallocator
const Capabilitiescapabilities () const
 
bool HasCapability (Capability capability) const
 Returns whether a given capabilityis enabled for this object.
 
void Deallocate (void *ptr)
 
void Deallocate (void *ptr, Layout layout)
 
template<typename T >
void Delete (T *ptr)
 
StatusWithSize GetCapacity () const
 
bool IsEqual (const Deallocator &other) const
 

Protected Types

using ReverseRange = typename BlockType::ReverseRange
 

Protected Member Functions

ReverseRange rblocks ()
 
template<typename PtrType , typename BlockPtrType = std::conditional_t< std::is_const_v<std::remove_pointer_t<PtrType>>, const BlockType*, BlockType*>>
Result< BlockPtrType > FromUsableSpace (PtrType ptr) const
 
virtual void ReserveBlock (BlockType *)
 
virtual void RecycleBlock (BlockType *)
 
- Protected Member Functions inherited from pw::Allocator
constexpr Allocator ()=default
 TODO(b/326509341): Remove when downstream consumers migrate.
 
constexpr Allocator (const Capabilities &capabilities)
 
- Protected Member Functions inherited from pw::Deallocator
constexpr Deallocator ()=default
 TODO(b/326509341): Remove when downstream consumers migrate.
 
constexpr Deallocator (const Capabilities &capabilities)
 
template<typename T >
UniquePtr< T > WrapUnique (T *ptr)
 

Additional Inherited Members

- Static Public Attributes inherited from pw::allocator::internal::GenericBlockAllocator
static constexpr Capabilities kCapabilities
 
- Static Protected Member Functions inherited from pw::allocator::internal::GenericBlockAllocator
static void CrashOnAllocated (void *allocated)
 
static void CrashOnInvalidFree (void *freed)
 
static void CrashOnDoubleFree (void *freed)
 Crashes with an informational message that a given block was freed twice.
 

Detailed Description

template<typename OffsetType, uint16_t kPoisonInterval, uint16_t kAlign>
class pw::allocator::BlockAllocator< OffsetType, kPoisonInterval, kAlign >

A memory allocator that uses a list of blocks.

This class does not implement ChooseBlock and cannot be used directly. Instead, use one of its specializations.

NOTE!! Do NOT use memory returned from this allocator as the backing for another allocator. If this is done, the Query method may incorrectly think pointers returned by that allocator were created by this one, and report that this allocator can de/reallocate them.

Constructor & Destructor Documentation

◆ BlockAllocator()

template<typename OffsetType , uint16_t kPoisonInterval, uint16_t kAlign>
pw::allocator::BlockAllocator< OffsetType, kPoisonInterval, kAlign >::BlockAllocator ( ByteSpan  region)
inlineexplicit

Non-constexpr constructor that automatically calls Init.

Parameters
[in]regionRegion of memory to use when satisfying allocation requests. The region MUST be large enough to fit an aligned block with overhead. It MUST NOT be larger than what is addressable by OffsetType.

Member Function Documentation

◆ FromUsableSpace()

template<typename OffsetType , uint16_t kPoisonInterval, uint16_t kAlign>
template<typename PtrType , typename BlockPtrType >
Result< BlockPtrType > pw::allocator::BlockAllocator< OffsetType, kPoisonInterval, kAlign >::FromUsableSpace ( PtrType  ptr) const
protected

Returns the block associated with a pointer.

If the given pointer is to this allocator's memory region, but not to a valid block, the memory is corrupted and this method will crash to assist in uncovering the underlying bug.

Parameters
ptrPointer to an allocated block's usable space.
Returns
embed:rst:leading-asterisk
 
* 
*  .. pw-status-codes::
* 
*     OK: Result contains a pointer to the block.
* 
*     OUT_OF_RANGE: Given pointer is outside the allocator's memory.
* 
*  

◆ Init() [1/3]

template<typename OffsetType , uint16_t kPoisonInterval, uint16_t kAlign>
void pw::allocator::BlockAllocator< OffsetType, kPoisonInterval, kAlign >::Init ( BlockType begin)
inline

Sets the blocks to be used by this allocator.

This method will use the sequence blocks as-is, which must be valid. The sequence extends to a block marked "last".

Parameters
[in]beginThe first block for this allocator.

◆ Init() [2/3]

template<typename OffsetType , uint16_t kPoisonInterval, uint16_t kAlign>
void pw::allocator::BlockAllocator< OffsetType, kPoisonInterval, kAlign >::Init ( BlockType begin,
BlockType end 
)
virtual

Sets the blocks to be used by this allocator.

This method will use the sequence blocks as-is, which must be valid.

Parameters
[in]beginThe first block for this allocator.
[in]endThe last block for this allocator. May be null, in which case the sequence ends with the first block marked "last".

Reimplemented in pw::allocator::BucketBlockAllocator< OffsetType, kMinBucketChunkSize, kNumBuckets, kAlign >.

◆ Init() [3/3]

template<typename OffsetType , uint16_t kPoisonInterval, uint16_t kAlign>
void pw::allocator::BlockAllocator< OffsetType, kPoisonInterval, kAlign >::Init ( ByteSpan  region)

Sets the memory region to be used by this allocator.

This method will instantiate an initial block using the memory region.

Parameters
[in]regionRegion of memory to use when satisfying allocation requests. The region MUST be large enough to fit an aligned block with overhead. It MUST NOT be larger than what is addressable by OffsetType.

◆ rblocks()

template<typename OffsetType , uint16_t kPoisonInterval, uint16_t kAlign>
BlockAllocator< OffsetType, kPoisonInterval, kAlign >::ReverseRange pw::allocator::BlockAllocator< OffsetType, kPoisonInterval, kAlign >::rblocks
protected

Returns a ReverseRange of blocks tracking the memory of this allocator.

◆ RecycleBlock()

template<typename OffsetType , uint16_t kPoisonInterval, uint16_t kAlign>
virtual void pw::allocator::BlockAllocator< OffsetType, kPoisonInterval, kAlign >::RecycleBlock ( BlockType )
inlineprotectedvirtual

Indicates that a block is now free.

Does nothing by default. Derived class may overload to do additional bookkeeeping.

Parameters
blockThe block being freed.

◆ ReserveBlock()

template<typename OffsetType , uint16_t kPoisonInterval, uint16_t kAlign>
virtual void pw::allocator::BlockAllocator< OffsetType, kPoisonInterval, kAlign >::ReserveBlock ( BlockType )
inlineprotectedvirtual

Indicates that a block will no longer be free.

Does nothing by default. Derived class may overload to do additional bookkeeeping.

Parameters
blockThe block being freed.

◆ Reset()

template<typename OffsetType , uint16_t kPoisonInterval, uint16_t kAlign>
void pw::allocator::BlockAllocator< OffsetType, kPoisonInterval, kAlign >::Reset

Resets the allocator to an uninitialized state.

At the time of the call, there MUST NOT be any outstanding allocated blocks from this allocator.


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