An Allocator that allows storing source location info with each allocation.
Source location is not stored automatically in order to preserve the Allocator API. Instead callers can utilize the fact that SetOrigin accepts nullptr and returns the given pointer to easily add origination information to allocations:
Alternatively, the PW_ALLOCATOR_SET_ORIGIN macro can do the same a bit more succinctly for methods that return a pointer:
Public Member Functions | |
| constexpr | OriginAllocator (Allocator &allocator) |
| SourceLocation | GetOrigin (const void *ptr) const |
| template<typename T > | |
| SourceLocation | GetOrigin (const UniquePtr< T > &ptr) const |
Returns the source location that made the given UniquePtr allocation. | |
| template<typename T > | |
| SourceLocation | GetOrigin (const SharedPtr< T > &ptr) const |
| template<typename T > | |
| T * | SetOrigin (SourceLocation origin, T *ptr) |
| template<typename T > | |
| UniquePtr< T > | SetOrigin (SourceLocation origin, UniquePtr< T > ptr) |
| template<typename T > | |
| SharedPtr< T > | SetOrigin (SourceLocation origin, SharedPtr< T > ptr) |
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) |
| template<typename ElementType > | |
| void | Destroy (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) |
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 |
Protected Types inherited from pw::allocator::FramingAllocator< SourceLocation > | |
| using | Base = internal::BaseFramingAllocator< internal::DefaultFrameErrorHandler > |
Protected Types inherited from pw::Deallocator | |
| enum class | InfoType { kRequestedLayoutOf , kUsableLayoutOf , kAllocatedLayoutOf , kCapacity , kRecognizes } |
Protected Member Functions inherited from pw::allocator::FramingAllocator< SourceLocation > | |
| constexpr | FramingAllocator (pw::Allocator &allocator) |
| void * | DoAllocate (Layout layout) override |
| void | DoDeallocate (void *ptr) override |
| bool | DoResize (void *ptr, size_t new_size) override |
| bool | DoBeforeReallocate (void *ptr, Layout new_layout) override |
| void | DoAfterReallocateCopy (void *ptr, Layout new_layout, void *new_ptr) override |
| Result< Layout > | DoGetInfo (Deallocator::InfoType info_type, const void *ptr) const override |
| void * | GetFrame (void *data) const |
| const void * | GetFrame (const void *data) const |
| SourceLocation * | GetPrefix (void *data) const |
| const SourceLocation * | GetPrefix (const void *data) const |
| void * | GetData (void *frame) const |
| const void * | GetData (const void *data) const |
| void * | GetSuffix (void *data) const |
| const void * | GetSuffix (const void *data) const |
Protected Member Functions inherited from pw::allocator::internal::BaseFramingAllocator< ErrorHandler > | |
| constexpr | BaseFramingAllocator (Allocator &allocator) |
| ByteSpan | AllocateFrame (Layout prefix, Layout data, Layout suffix) |
| bool | ResizeFrame (void *data, size_t new_size, Layout suffix, void *tmp_suffix) |
| Result< Layout > | GetFrameInfo (InfoType info_type, const void *data, Layout suffix) const |
| Result< size_t > | GetFrameSize (const void *frame) const |
Protected Member Functions inherited from pw::allocator::ForwardingAllocator | |
| constexpr | ForwardingAllocator (const Capabilities &capabilities) noexcept |
| constexpr | ForwardingAllocator (pw::Allocator &allocator) noexcept |
| constexpr void | Init (pw::Allocator &allocator) |
| constexpr pw::Allocator & | allocator () |
| constexpr const pw::Allocator & | allocator () const |
| void * | DoAllocate (Layout layout) override |
| void | DoDeallocate (void *ptr) override |
| bool | DoResize (void *ptr, size_t new_size) override |
| bool | DoBeforeReallocate (void *ptr, Layout new_layout) override |
| void | DoAfterReallocateCopy (void *ptr, Layout new_layout, void *new_ptr) override |
| void | DoAfterReallocateDone (Layout new_layout, void *new_ptr) override |
| size_t | DoGetAllocated () const override |
| std::optional< Fragmentation > | DoMeasureFragmentation () const override |
| Returns fragmentation information for the allocator's memory region. | |
| Result< Layout > | DoGetInfo (InfoType info_type, const void *ptr) const override |
Protected Member Functions inherited from pw::Allocator | |
| constexpr | Allocator () noexcept=default |
| TODO(b/326509341): Remove when downstream consumers migrate. | |
| constexpr | Allocator (const Capabilities &capabilities) noexcept |
| virtual void * | DoAllocate (Layout layout)=0 |
| virtual bool | DoResize (void *ptr, size_t new_size) |
| virtual void * | DoReallocate (void *ptr, Layout new_layout) |
| virtual bool | DoBeforeReallocate (void *ptr, Layout new_layout) |
| virtual void | DoAfterReallocateCopy (void *ptr, Layout new_layout, void *new_ptr) |
| virtual void | DoAfterReallocateDone (Layout new_layout, void *new_ptr) |
| 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 inherited from pw::allocator::internal::BaseFramingAllocator< ErrorHandler > | |
| static constexpr size_t | Distance (const void *start, const void *end) |
| Returns the distance between two pointers in bytes. | |
| static void | UpdateOffsets (void *frame, void *data, void *suffix) |
| static void * | GetFrame (void *data, bool has_suffix, bool checked=true) |
| static const void * | GetFrame (const void *data, bool has_suffix, bool checked=true) |
| static void * | GetData (void *frame, Result< size_t > frame_size, Layout prefix) |
| static const void * | GetData (const void *frame, Result< size_t > frame_size, Layout prefix) |
| static void * | GetSuffix (void *data, size_t data_size, Layout suffix) |
| static const void * | GetSuffix (const void *data, size_t data_size, Layout suffix) |
Static Protected Member Functions inherited from pw::Allocator | |
| static bool | BeforeReallocate (Allocator &allocator, void *ptr, Layout new_layout) |
| static void | AfterReallocateCopy (Allocator &allocator, void *ptr, Layout new_layout, void *new_ptr) |
| static void | AfterReallocateDone (Allocator &allocator, Layout new_layout, void *new_ptr) |
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) |
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> |
|
inline |
Returns the source location that made the given SharedPtr allocation.
For shared pointers created via MakeShared, this returns the location where the shared object was created. For shared pointers converted from an existing allocation (e.g. from a UniquePtr), this returns the location associated with the shared pointer conversion rather than the underlying data object.
| SourceLocation pw::allocator::OriginAllocator::GetOrigin | ( | const void * | ptr | ) | const |
Returns the source location that made the given allocation.
Pointer must be to an allocation from this allocator.
|
inline |
Sets the originating source location and returns the given shared pointer.
Use this method like this:
MakeShared. If used with a shared pointer converted from an existing allocation (such as one created from a UniquePtr), the origin information will be attached to the shared pointer's reference-tracking metadata rather than the underlying data object.
|
inline |
Sets the originating source location and returns the given pointer.
Pointer must be null or to an allocation from this allocator.
Use this method like this:
|
inline |
Sets the originating source location and returns the given unique pointer.
Use this method like this: