C/C++ API Reference
Loading...
Searching...
No Matches
pw::allocator::internal::GenericBuddyAllocator Class Reference

Overview

Size-independent buddy allocator.

This allocator allocates blocks of memory whose sizes are powers of two. See also https://en.wikipedia.org/wiki/Buddy_memory_allocation.

Compared to BuddyAllocator, this implementation is size-agnostic with respect to the number of buckets.

Inheritance diagram for pw::allocator::internal::GenericBuddyAllocator:
pw::Allocator pw::Deallocator pw::allocator::BuddyAllocator< kMinOuterSize_, kNumBuckets >

Public Member Functions

void InitBuckets (span< UnorderedBucket< BuddyBlock > > buckets, size_t min_outer_size)
 
void Init (ByteSpan region)
 Sets the memory used to allocate blocks.
 
- Public Member Functions inherited from pw::Allocator
void * Allocate (Layout layout)
 
template<typename T , int &... kExplicitGuard, typename... Args>
std::enable_if_t<!std::is_array_v< T >, 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 , int &... kExplicitGuard, std::enable_if_t< is_unbounded_array_v< T >, int > = 0>
UniquePtr< T > MakeUnique (size_t size)
 
template<typename T , int &... kExplicitGuard, std::enable_if_t< is_unbounded_array_v< T >, int > = 0>
UniquePtr< T > MakeUnique (size_t size, size_t alignment)
 
template<typename T , int &... kExplicitGuard, 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 , int &... kExplicitGuard, std::enable_if_t< is_unbounded_array_v< T >, int > = 0>
SharedPtr< T > MakeShared (size_t size)
 
template<typename T , int &... kExplicitGuard, std::enable_if_t< is_unbounded_array_v< T >, int > = 0>
SharedPtr< T > MakeShared (size_t size, size_t alignment)
 
template<typename T , int &... kExplicitGuard, 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< allocator::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)
 

Static Public Attributes

static constexpr Capabilities kCapabilities
 
static constexpr size_t kDefaultMinOuterSize = 16
 
static constexpr size_t kDefaultNumBuckets = 16
 

Protected Member Functions

void CrashIfAllocated ()
 
- Protected Member Functions inherited from pw::Allocator
constexpr Allocator ()=default
 TODO(b/326509341): Remove when downstream consumers migrate.
 
constexpr Allocator (const Capabilities &capabilities)
 
virtual std::optional< allocator::FragmentationDoMeasureFragmentation () const
 
- Protected Member Functions inherited from pw::Deallocator
constexpr Deallocator ()=default
 TODO(b/326509341): Remove when downstream consumers migrate.
 
constexpr Deallocator (const Capabilities &capabilities)
 

Private Member Functions

void * DoAllocate (Layout layout) override
 
void DoDeallocate (void *ptr) override
 
Result< LayoutDoGetInfo (InfoType info_type, const void *ptr) const override
 

Additional Inherited Members

- Public Types inherited from pw::Deallocator
using Capabilities = allocator::Capabilities
 
using Capability = allocator::Capability
 
using Layout = allocator::Layout
 
- Static Protected Attributes inherited from pw::Deallocator
template<typename T >
static constexpr bool is_bounded_array_v
 
template<typename T >
static constexpr bool is_unbounded_array_v
 

Member Function Documentation

◆ CrashIfAllocated()

void pw::allocator::internal::GenericBuddyAllocator::CrashIfAllocated ( )
protected

Ensures all allocations have been freed. Crashes with a diagnostic message If any allocations remain outstanding.

◆ DoAllocate()

void * pw::allocator::internal::GenericBuddyAllocator::DoAllocate ( Layout  layout)
overrideprivatevirtual

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.

◆ DoDeallocate()

void pw::allocator::internal::GenericBuddyAllocator::DoDeallocate ( void *  ptr)
overrideprivate

◆ DoGetInfo()

Result< Layout > pw::allocator::internal::GenericBuddyAllocator::DoGetInfo ( InfoType  info_type,
const void *  ptr 
) const
overrideprivate

◆ InitBuckets()

void pw::allocator::internal::GenericBuddyAllocator::InitBuckets ( span< UnorderedBucket< BuddyBlock > >  buckets,
size_t  min_outer_size 
)

Sets up buckets for a buddy allocator.

Parameters
[in]bucketsStorage for buckets of free blocks.
[in]min_outer_sizeOuter size of the blocks in the first bucket.

Member Data Documentation

◆ kCapabilities

constexpr Capabilities pw::allocator::internal::GenericBuddyAllocator::kCapabilities
staticconstexpr
Initial value:
=
kImplementsGetUsableLayout | kImplementsGetAllocatedLayout |
kImplementsGetCapacity | kImplementsRecognizes

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