Abstract interface for releasing memory.
Public Types | |
| using | Capabilities = allocator::Capabilities |
| using | Capability = allocator::Capability |
| using | Layout = allocator::Layout |
Public Member Functions | |
| 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 | |
| enum class | InfoType { kRequestedLayoutOf , kUsableLayoutOf , kAllocatedLayoutOf , kCapacity , kRecognizes } |
Protected Member Functions | |
| 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 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) |
|
strongprotected |
Indicates what kind of information to retrieve using GetInfo.
Note that this enum is considered open, and may be extended in the future. As a result, implementers of DoGetInfo should include a default case that handles unrecognized info types. If building with -Wswitch-enum, you will also want to locally disable that diagnostic and build with -Wswitch-default instead, e.g.:
See also GetInfo.
| Enumerator | |
|---|---|
| kRequestedLayoutOf | If supported, The requested layout may differ from either the layout of usable memory, the layout of memory used to fulfill the request, or both. For example, it may have a smaller size than the usable memory if the latter was padded to an alignment boundary, or may have a less strict alignment than the actual memory. |
| kUsableLayoutOf | If supported, For example, it may have a larger size than the requested layout if it was padded to an alignment boundary, but may be less than the acutal memory if the object includes some overhead for metadata. |
| kAllocatedLayoutOf | If supported, The layout of memory used to fulfill a request may differ from either the requested layout, the layout of the usable memory, or both. For example, it may have a larger size than the requested layout or the layout of usable memory if the object includes some overhead for metadata. |
| kCapacity | If supported, The given pointer is ignored. |
| kRecognizes | If supported, This MUST only be used to dispatch between two or more objects associated with non-overlapping regions of memory. Do NOT use it to determine if this object can deallocate pointers. Callers MUST only deallocate memory using the same |
|
inline |
Releases a previously-allocated block of memory.
The given pointer must have been previously provided by this memory resource; otherwise the behavior is undefined.
| [in] | ptr | Pointer to previously-allocated memory. |
| void pw::Deallocator::Delete | ( | T * | ptr | ) |
Destroys the object and deallocates the associated memory.
The given pointer must have been previously obtained from a call to New using the same object; otherwise the behavior is undefined.
new/new[] and delete/delete[], it is an error to call a specialization of Delete other than the one that corresponds to the specialization of New that was used to allocate the object.It is especially important to avoid passing the pointer return by a call to New<T[]> or New<T[kN] to Delete, as this will only delete the first object in the array. For this reason, it is recommended to use DeleteArray or explicitly specify the array type as a template parameter to either Delete<T[]> or Delete<T[kN]>.
Using an allocator with the kImplementsGetRequestedLayout capability and configuring the hardening level to PW_ALLOCATOR_HARDENING_DEBUG will detect such mismatches when Delete is called.
| [in] | ptr | Pointer to previously-allocated object. |
| void pw::Deallocator::DeleteArray | ( | ElementType * | ptr, |
| size_t | count | ||
| ) |
Destroys the array and deallocates the associated memory.
The given pointer must be to an array with count elements that was previously obtained from a call to New using the same object; otherwise the behavior is undefined.
This method MUST be used to delete arrays with deallocators that do not have the capability to recover the layout that was used to request memory, i.e. Capability::kImplementsGetRequestedLayout.
| [in] | ptr | Pointer to previously-allocated array. |
| [in] | count | Number of items in the array. |
|
protectedpure virtual |
Releases a previously-allocated block of memory.
The given pointer must have been previously provided by this memory resource; otherwise the behavior is undefined.
| [in] | ptr | Pointer to previously-allocated memory. |
Implemented in pw::allocator::AllocatorAsPool, pw::allocator::AsyncPool, pw::allocator::BlockAllocator< BlockType_ >, pw::allocator::BlockAllocator< BucketBlock<> >, pw::allocator::BlockAllocator< BestFitBlock< uintptr_t > >, pw::allocator::BlockAllocator< DlBlock< uintptr_t > >, pw::allocator::BlockAllocator< FirstFitBlock< uintptr_t > >, pw::allocator::BlockAllocator< TlsfBlock< uint32_t > >, pw::allocator::BlockAllocator< WorstFitBlock< uintptr_t > >, pw::allocator::BlockAllocator< BlockType >, pw::allocator::BlockAllocator< pw::allocator::TinyBlock >, pw::allocator::internal::GenericBuddyAllocator, pw::allocator::BumpAllocator, pw::allocator::ChunkPool, pw::allocator::FallbackAllocator, pw::allocator::test::FaultInjectingAllocator, pw::allocator::GuardedAllocator< BlockAllocatorType, LockType >, pw::allocator::LibCAllocator, pw::allocator::NullAllocator, pw::allocator::SynchronizedAllocator< LockType >, pw::allocator::test::AllocatorForTest< kBufferSize, BlockType_, MetricsType_ >, pw::allocator::TrackingAllocator< MetricsType >, pw::multibuf::v1_adapter::internal::ChunkAllocator, and pw::multibuf::v1_adapter::internal::SingleChunkAllocator.
|
inlineprotectedvirtual |
Returns deallocator-specific information about allocations.
Deallocators may support any number of InfoTypes. See that type for what each supported type returns. For unsupported types, this method returns UNIMPLEMENTED.
The default implementation simply returns UNIMPLEMENTED.
Reimplemented in pw::allocator::AllocatorAsPool, pw::allocator::BlockAllocator< BlockType_ >, pw::allocator::BlockAllocator< BucketBlock<> >, pw::allocator::BlockAllocator< BestFitBlock< uintptr_t > >, pw::allocator::BlockAllocator< DlBlock< uintptr_t > >, pw::allocator::BlockAllocator< FirstFitBlock< uintptr_t > >, pw::allocator::BlockAllocator< TlsfBlock< uint32_t > >, pw::allocator::BlockAllocator< WorstFitBlock< uintptr_t > >, pw::allocator::BlockAllocator< BlockType >, pw::allocator::BlockAllocator< pw::allocator::TinyBlock >, pw::allocator::internal::GenericBuddyAllocator, pw::allocator::ChunkPool, pw::allocator::FallbackAllocator, pw::allocator::GuardedAllocator< BlockAllocatorType, LockType >, pw::allocator::SynchronizedAllocator< LockType >, pw::allocator::test::AllocatorForTest< kBufferSize, BlockType_, MetricsType_ >, pw::allocator::TrackingAllocator< MetricsType >, pw::multibuf::v1_adapter::internal::ChunkAllocator, and pw::allocator::AbstractAllocator.
|
inlinestaticprotected |
Static version of GetAllocatedLayout that allows forwarding allocators to call it on wrapped allocators.
Convenience wrapper of DoGetInfo for getting the allocated layout associated with a pointer.
|
inline |
Returns the total amount of memory provided by this object.
This is an optional method. Some memory providers may not have an easily defined capacity, e.g. the system allocator. If implemented, the returned capacity may be less than the memory originally given to an allocator, e.g. if the allocator must align the region of memory, its capacity may be reduced.
|
inlinestaticprotected |
Returns deallocator-specific information about allocations.
Deallocators may support any number of InfoTypes. See that type for what each supported type returns. For unsupported types, this method returns UNIMPLEMENTED.
This method is protected in order to restrict it to object implementations. It is static and takes an deallocator parameter in order to allow forwarding allocators to call it on wrapped allocators.
|
inlineprotected |
Returns deallocator-specific information about allocations.
Deallocators may support any number of InfoTypes. See that type for what each supported type returns. For unsupported types, this method returns UNIMPLEMENTED.
|
inlinestaticprotected |
Static version of GetRequestedLayout that allows forwarding allocators to call it on wrapped allocators.
Convenience wrapper of DoGetInfo for getting the requested layout associated with a pointer.
|
inlinestaticprotected |
Static version of GetUsableLayout that allows forwarding allocators to call it on wrapped allocators.
Convenience wrapper of DoGetInfo for getting the usable layout associated with a pointer.
|
inline |
Returns whether the given object is the same as this one.
This method is used instead of operator== in keeping with std::pmr::memory_resource::is_equal. There currently is no corresponding virtual DoIsEqual, as objects that would require dynamic_cast to properly determine equality are not supported. This method will allow the interface to remain unchanged should a future need for such objects arise.
| [in] | other | Object to compare with this object. |
|
inlinestaticprotected |
Static version of Recognizes that allows forwarding allocators to call it on wrapped allocators.
|
inlineprotected |
Convenience wrapper of DoGetInfo for getting whether the allocator recognizes a pointer.