Skip to main content
Pigweed's logo Pigweed
code_xml
  1. Home
  2. Reference
  3. C/C++
  4. Memory & storage
  5. pw_allocator
  6. Forwarding allocators
Forwarding allocators

Overview

Allocator implementations that don’t allocate memory directly and instead rely on other allocators while providing additional behaviors.

Learn more: Forwarding allocator concept

Classes

class  pw::allocator::AllocatorAsPool
 Implementation of Pool that satisfies requests using an Allocator. More...
 
class  pw::allocator::FallbackAllocator
 
class  pw::allocator::FramingAllocator< Prefix, Suffix, ErrorHandler >
 
class  pw::allocator::GuardedAllocator< BlockAllocatorType, LockType, ErrorHandler >
 
class  pw::allocator::PmrAllocator
 
class  pw::allocator::SynchronizedAllocator< LockType >
 
struct  pw::allocator::AddTrackingAllocatorAsChild
 
class  pw::allocator::TrackingAllocator< MetricsType >
 

Typedefs

using pw::allocator::NoSync = pw::sync::NoLock
 

Functions

constexpr void pw::allocator::TrackingAllocator< MetricsType >::Init (Allocator &allocator)
 
void * pw::allocator::TrackingAllocator< MetricsType >::DoAllocate (Layout layout) override
 
void pw::allocator::TrackingAllocator< MetricsType >::DoDeallocate (void *ptr) override
 
bool pw::allocator::TrackingAllocator< MetricsType >::DoResize (void *ptr, size_t new_size) override
 
bool pw::allocator::TrackingAllocator< MetricsType >::DoBeforeReallocate (void *ptr, Layout new_layout) override
 
void pw::allocator::TrackingAllocator< MetricsType >::DoAfterReallocateDone (Layout new_layout, void *new_ptr) override
 

Variables

static constexpr struct pw::allocator::AddTrackingAllocatorAsChild pw::allocator::kAddTrackingAllocatorAsChild = {}
 

Typedef Documentation

◆ NoSync

Tag type used to indicate synchronization is NOT desired.

This can be useful with allocator parameters for module configuration, e.g. PW_MALLOC_LOCK_TYPE.

Function Documentation

◆ DoAfterReallocateDone()

template<typename MetricsType >
void pw::allocator::TrackingAllocator< MetricsType >::DoAfterReallocateDone ( Layout  new_layout,
void *  new_ptr 
)
overrideprotectedvirtual

Called as part of the default implementation of DoReallocate just before returning.

By default, does nothing. Derived types may implement this method to add additional behavior. If they do, they must call DoAfterReallocateDone on their base type before performing any other action.

new_ptr is guaranteed to be non-null, and new_layout.size() is guaranteed to be non-zero.

Reimplemented from pw::allocator::ForwardingAllocator.

◆ DoAllocate()

template<typename MetricsType >
void * pw::allocator::TrackingAllocator< MetricsType >::DoAllocate ( 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.

Reimplemented from pw::allocator::ForwardingAllocator.

◆ DoBeforeReallocate()

template<typename MetricsType >
bool pw::allocator::TrackingAllocator< MetricsType >::DoBeforeReallocate ( void *  ptr,
Layout  new_layout 
)
overrideprotectedvirtual

Called at the start of the default implementation of DoReallocate.

By default, returns true. Derived types may implement this method to add additional behavior. If they do, they must return false or the value returned by DoBeforeReallocate. If false is returned, the reallocation fails and returns null.

ptr is guaranteed to be non-null, and new_layout.size() is guaranteed to be non-zero.

Reimplemented from pw::allocator::ForwardingAllocator.

◆ DoDeallocate()

template<typename MetricsType >
void pw::allocator::TrackingAllocator< MetricsType >::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.

Reimplemented from pw::allocator::ForwardingAllocator.

◆ DoResize()

template<typename MetricsType >
bool pw::allocator::TrackingAllocator< MetricsType >::DoResize ( void *  ptr,
size_t  new_size 
)
overrideprotectedvirtual

Modifies the size of an previously-allocated block of memory without copying any data.

Returns true if its size was changed without copying data to a new allocation; otherwise returns false.

In particular, it always returns true if the old_layout.size() equals new_size, and always returns false if the given pointer is null, the old_layout.size() is 0, or the new_size is 0.

Parameters
[in]ptrPointer to previously-allocated memory.
[in]new_sizeRequested new size for the memory allocation.

Reimplemented from pw::allocator::ForwardingAllocator.

◆ Init()

template<typename MetricsType >
constexpr void pw::allocator::TrackingAllocator< MetricsType >::Init ( Allocator &  allocator)
constexpr

Sets the wrapped allocator after two-phase construction and verifies that allocator supports all metrics enabled by MetricsType.

Parameters
[in,out]allocatorWrapped allocator to forward requests to. Must remain valid for the lifetime of this object.
Precondition
No wrapped allocator has been set yet, and if allocator itself uses two-phase initialization (such as SynchronizedAllocator), allocator.Init(...) must be called first when requested_bytes or allocated_bytes is enabled.