Pigweed
 
Loading...
Searching...
No Matches
pw::allocator::internal::GenericBuddyAllocator Class Referencefinal

#include <buddy_allocator.h>

Public Types

using BucketType = UnorderedBucket< BuddyBlock >
 

Public Member Functions

 GenericBuddyAllocator (span< BucketType > buckets, size_t min_outer_size)
 
void Init (ByteSpan region)
 Sets the memory used to allocate blocks.
 
void * Allocate (Layout layout)
 
void Deallocate (void *ptr)
 
size_t GetCapacity () const
 Returns the total capacity of this allocator.
 
Result< LayoutGetLayout (const void *ptr) const
 Returns the allocated layout for a given pointer.
 
void CrashIfAllocated ()
 

Static Public Attributes

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

Detailed Description

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.

Constructor & Destructor Documentation

◆ GenericBuddyAllocator()

pw::allocator::internal::GenericBuddyAllocator::GenericBuddyAllocator ( span< BucketType buckets,
size_t  min_outer_size 
)

Constructs a buddy allocator.

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

Member Function Documentation

◆ Allocate()

void * pw::allocator::internal::GenericBuddyAllocator::Allocate ( Layout  layout)

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.

◆ CrashIfAllocated()

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

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

◆ Deallocate()

void pw::allocator::internal::GenericBuddyAllocator::Deallocate ( void *  ptr)

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.

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: