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

Overview

Generic base class for a GuardedAllocator.

This allocator detects heap overflows by inserting "guard" values before and after the usable allocated memory regions provided to the caller. In order to reserve space for these values and preserve alignment, it modifies the requested memory layout to include a suffix and an alignment-preserving prefix.

The resulting layout ensures that:

  1. The usable space is proceeded by a prefix of at least two words.
  2. The first word of the prefix is the size of the prefix.
  3. The second-to-last word of the prefix is the size of the prefix. This may overlap with the first word if the prefix is only two words.
  4. The last word of the prefix is a guard value whose integrity can be checked.
  5. Usable space of at least layout.size() bytes and aligned on a layout.alignment() boundary follows the prefix.
  6. A suffix of one word follows the usable space. It is another guard value.

Visually, this resembles: [size, ...] | size | guard | usable_space... | guard

Since this base class is only concerned with adjusting layouts and sizes and is agnostic to specific allocator and block details, all of its methods are static.

Inheritance diagram for pw::allocator::internal::GenericGuardedAllocator:
pw::Allocator pw::Deallocator pw::allocator::GuardedAllocator< BlockAllocatorType, LockType >

Protected Member Functions

constexpr GenericGuardedAllocator (const Capabilities &capabilities)
 
- 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 , int &... kExplicitGuard, typename ElementType = std::remove_extent_t<T>, std::enable_if_t< is_unbounded_array_v< T >, int > = 0>
UniquePtr< T > WrapUnique (ElementType *ptr, size_t size)
 

Static Protected Member Functions

static Layout AdjustLayout (Layout layout)
 
static size_t AdjustSize (void *ptr, size_t inner_size)
 
static void * GetOriginal (void *ptr)
 
static void * AddPrefix (void *ptr, size_t alignment)
 
static void AddSuffix (void *ptr, size_t size)
 Adds a suffix a the end of the allocation given by ptr and size.
 
static bool CheckPrefixAndSuffix (void *ptr, size_t size)
 

Additional Inherited Members

- Public Types inherited from pw::Deallocator
using Capabilities = allocator::Capabilities
 
using Capability = allocator::Capability
 
using Layout = allocator::Layout
 
- 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 >
T * NewArray (size_t count)
 
template<typename T >
T * NewArray (size_t count, size_t alignment)
 
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 >
UniquePtr< T[]> MakeUniqueArray (size_t size)
 
template<typename T >
UniquePtr< T[]> MakeUniqueArray (size_t size, size_t alignment)
 
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)
 
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)
 
size_t GetAllocated () const
 
- Public Member Functions inherited from pw::Deallocator
constexpr const Capabilitiescapabilities () const
 
bool HasCapability (Capability capability) const
 Returns whether a given capability is enabled for this object.
 
void Deallocate (void *ptr)
 
void Deallocate (void *ptr, Layout layout)
 
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 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

◆ AddPrefix()

static void * pw::allocator::internal::GenericGuardedAllocator::AddPrefix ( void *  ptr,
size_t  alignment 
)
staticprotected

Adds a prefix a the start of the allocation given by ptr and size, and returns a a pointer to the guarded region following it.

◆ AdjustLayout()

static Layout pw::allocator::internal::GenericGuardedAllocator::AdjustLayout ( Layout  layout)
staticprotected

Modifies a layout to include guard values.

As this method does not touch allocatable memory, it is inherently thread-safe.

◆ AdjustSize()

static size_t pw::allocator::internal::GenericGuardedAllocator::AdjustSize ( void *  ptr,
size_t  inner_size 
)
staticprotected

Modifies an inner size parameter to account for the prefix and suffix.

This is used by methods that deal with inner sizes and allocated blocks, e.g. Resize.

◆ CheckPrefixAndSuffix()

static bool pw::allocator::internal::GenericGuardedAllocator::CheckPrefixAndSuffix ( void *  ptr,
size_t  size 
)
staticprotected

Returns whether the allocation given by ptr and size has a valid prefix and suffix.

◆ GetOriginal()

static void * pw::allocator::internal::GenericGuardedAllocator::GetOriginal ( void *  ptr)
staticprotected

Takes a ptr to a guarded region and returns the pointer to the original allocation.


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