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:
layout.size() bytes and aligned on a layout.alignment() boundary follows the prefix.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.
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) |
| virtual void * | DoAllocate (Layout layout)=0 |
| virtual bool | DoResize (void *ptr, size_t new_size) |
| virtual void * | DoReallocate (void *ptr, Layout new_layout) |
| virtual size_t | DoGetAllocated () const |
| virtual std::optional< Fragmentation > | DoMeasureFragmentation () const |
| Returns fragmentation information for the allocator's memory region. | |
Protected Member Functions inherited from pw::Deallocator | |
| constexpr | Deallocator ()=default |
| TODO(b/326509341): Remove when downstream consumers migrate. | |
| constexpr | Deallocator (const Capabilities &capabilities) |
| Result< Layout > | GetInfo (InfoType info_type, const void *ptr) const |
| Result< Layout > | GetRequestedLayout (const void *ptr) const |
| Result< Layout > | GetUsableLayout (const void *ptr) const |
| Result< Layout > | GetAllocatedLayout (const void *ptr) const |
| bool | Recognizes (const void *ptr) const |
| virtual void | DoDeallocate (void *ptr)=0 |
| virtual Result< Layout > | DoGetInfo (InfoType, const void *) const |
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) |
Static Protected Member Functions inherited from pw::Deallocator | |
| static Result< Layout > | GetInfo (const Deallocator &deallocator, InfoType info_type, const void *ptr) |
| static Result< Layout > | GetRequestedLayout (const Deallocator &deallocator, const void *ptr) |
| static Result< Layout > | GetUsableLayout (const Deallocator &deallocator, const void *ptr) |
| static Result< Layout > | GetAllocatedLayout (const Deallocator &deallocator, const void *ptr) |
| static bool | Recognizes (const Deallocator &deallocator, const void *ptr) |
Additional Inherited Members | |
Public Types inherited from pw::Allocator | |
| using | Fragmentation = allocator::Fragmentation |
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, 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 (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 , std::enable_if_t< is_unbounded_array_v< T >, int > = 0> | |
| UniquePtr< T > | MakeUnique (size_t size) |
| template<typename T , std::enable_if_t< is_unbounded_array_v< T >, int > = 0> | |
| UniquePtr< T > | MakeUnique (size_t size, size_t alignment) |
| template<typename T , 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 , std::enable_if_t< is_unbounded_array_v< T >, int > = 0> | |
| SharedPtr< T > | MakeShared (size_t size) |
| template<typename T , std::enable_if_t< is_unbounded_array_v< T >, int > = 0> | |
| SharedPtr< T > | MakeShared (size_t size, size_t alignment) |
| template<typename T , 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< Fragmentation > | MeasureFragmentation () const |
| Returns fragmentation information for the allocator's memory region. | |
Public Member Functions inherited from pw::Deallocator | |
| constexpr const Capabilities & | capabilities () 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) |
Protected Types inherited from pw::Deallocator | |
| enum class | InfoType { kRequestedLayoutOf , kUsableLayoutOf , kAllocatedLayoutOf , kCapacity , kRecognizes } |
Static Protected Attributes inherited from pw::Deallocator | |
| template<typename T > | |
| static constexpr bool | is_bounded_array_v = ::pw::is_bounded_array_v<T> |
| template<typename T > | |
| static constexpr bool | is_unbounded_array_v = ::pw::is_unbounded_array_v<T> |
|
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.
|
staticprotected |
Modifies a layout to include guard values.
As this method does not touch allocatable memory, it is inherently thread-safe.
|
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.
|
staticprotected |
Returns whether the allocation given by ptr and size has a valid prefix and suffix.
|
staticprotected |
Takes a ptr to a guarded region and returns the pointer to the original allocation.