C/C++ API Reference
Loading...
Searching...
No Matches
pw::allocator::TypedPool< T > Class Template Reference

Overview

template<typename T>
class pw::allocator::TypedPool< T >

Typed pool that can be used for slab allocation.

This class is a special purpose pool designed to allocate objects of one specific type, T. It is useful when you need a dynamic pool of objects with very low performance and memory overhead costs. For example, a dispatcher might use such an allocator to manage memory for a set of task objects.

Template Parameters
TThe type of object to allocate memory for.
Inheritance diagram for pw::allocator::TypedPool< T >:
pw::allocator::ChunkPool pw::allocator::Pool pw::Deallocator

Classes

struct  Buffer
 Provides aligned storage for kNumObjects of type T. More...
 

Public Member Functions

template<size_t kNumObjects>
 TypedPool (Buffer< kNumObjects > &buffer)
 
 TypedPool (ByteSpan region)
 
template<int &... kExplicitGuard, typename... Args>
T * New (Args &&... args)
 
template<int &... kExplicitGuard, typename... Args>
UniquePtr< T > MakeUnique (Args &&... args)
 
- Public Member Functions inherited from pw::allocator::ChunkPool
 ChunkPool (ByteSpan region, const Layout &layout)
 
- Public Member Functions inherited from pw::allocator::Pool
constexpr Pool (const Capabilities &capabilities, const Layout &layout)
 
constexpr const Layoutlayout () const
 
void * Allocate ()
 
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 ()
 
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_bounded_array_v< T >, int > = 0>
UniquePtr< T > MakeUnique ()
 
template<typename T , int &... kExplicitGuard, std::enable_if_t< is_unbounded_array_v< T >, int > = 0>
UniquePtr< T > MakeUnique ()
 
- 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 Member Functions

static constexpr size_t SizeNeeded (size_t num_objects)
 Returns the amount of memory needed to allocate num_objects.
 
static constexpr size_t AlignmentNeeded ()
 Returns the optimal alignment for the backing memory region.
 

Additional Inherited Members

- Public Types inherited from pw::Deallocator
using Capabilities = allocator::Capabilities
 
using Capability = allocator::Capability
 
using Layout = allocator::Layout
 
- Static Public Attributes inherited from pw::allocator::ChunkPool
static constexpr Capabilities kCapabilities
 
static constexpr size_t kMinSize = sizeof(void*)
 
static constexpr size_t kMinAlignment = alignof(void*)
 
- Protected Member Functions inherited from pw::Deallocator
constexpr Deallocator ()=default
 TODO(b/326509341): Remove when downstream consumers migrate.
 
constexpr Deallocator (const Capabilities &capabilities)
 
- 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
 

Constructor & Destructor Documentation

◆ TypedPool() [1/2]

template<typename T >
template<size_t kNumObjects>
pw::allocator::TypedPool< T >::TypedPool ( Buffer< kNumObjects > &  buffer)
inline

Construct a TypedPool.

This constructor uses the Buffer type to minimize wasted memory. For example:

TypedPool<MyObject> my_pool(buffer);
Definition: typed_pool.h:36
Provides aligned storage for kNumObjects of type T.
Definition: typed_pool.h:52
Parameters
bufferThe memory to allocate from.

◆ TypedPool() [2/2]

template<typename T >
pw::allocator::TypedPool< T >::TypedPool ( ByteSpan  region)
inline

Construct a TypedPool.

To minimize wasted memory, align the buffer provided to the allocator to the object type's alignment. For example:

alignas(MyObject) std::array<std::byte, 0x1000> buffer;
TypedPool<MyObject> my_pool(buffer);
Parameters
regionThe memory to allocate from. Must be large enough to allocate memory for at least one object.

Member Function Documentation

◆ MakeUnique()

template<typename T >
template<int &... kExplicitGuard, typename... Args>
UniquePtr< T > pw::allocator::TypedPool< T >::MakeUnique ( Args &&...  args)
inline

Constructs and object from the given args, and wraps it in a UniquePtr

This method is similar to Allocator::MakeUnique, except that it is specific to the pool's object type.

Parameters
[in]args...Arguments passed to the object constructor.

◆ New()

template<typename T >
template<int &... kExplicitGuard, typename... Args>
T * pw::allocator::TypedPool< T >::New ( Args &&...  args)
inline

Constructs and object from the given args

This method is similar to Allocator::New, except that it is specific to the pool's object type.

Parameters
[in]args...Arguments passed to the object constructor.

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