Allocator that does not automatically delete.
A "bump" or "arena" allocator provides memory by simply incrementing a pointer to a memory region in order to "allocate" memory for requests, and doing nothing on deallocation. All memory is freed only when the allocator itself is destroyed. As a result, this allocator is extremely fast.
Bump allocators are useful when short-lived to allocate objects from small buffers with almost zero overhead. Since memory is not deallocated, a bump allocator with a longer lifespan than any of its allocations will end up holding unusable memory. An example of a good use case might be decoding RPC messages that may require a variable amount of space only for as long as it takes to decode a single message.
On destruction, the destructors for any objects allocated using New or MakeUnique are NOT called. To have these destructors invoked, you can allocate "owned" objects using NewOwned and MakeUniqueOwned. This adds a small amount of overhead to the allocation.
Public Member Functions | |
| constexpr | BumpAllocator () |
| Constructs a BumpAllocator without initializing it. | |
| BumpAllocator (ByteSpan region) | |
| Constructs a BumpAllocator and initializes it. | |
| void | Init (ByteSpan region) |
| Sets the memory region to be used by the allocator. | |
| template<typename T , int &... kExplicitGuard, typename... Args> | |
| T * | NewOwned (Args &&... args) |
| template<typename T , int &... kExplicitGuard, typename... Args> | |
| UniquePtr< T > | MakeUniqueOwned (Args &&... args) |
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) |
Static Public Attributes | |
| static constexpr Capabilities | kCapabilities = kSkipsDestroy |
Protected Member Functions | |
| void * | DoAllocate (Layout layout) override |
| void | DoDeallocate (void *) override |
| size_t | DoGetAllocated () const override |
Protected Member Functions inherited from pw::allocator::AbstractAllocator | |
| constexpr | AbstractAllocator (Capabilities capabilities) |
| bool | DoResize (void *, size_t) 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, const void *) const override |
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) |
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 |
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> |
|
overrideprotectedvirtual |
Allocates a block of memory with the specified size and alignment.
Returns nullptr if the allocation cannot be made, or the layout has a size of 0.
| [in] | layout | Describes the memory to be allocated. |
Implements pw::Allocator.
|
overrideprotected |
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. |
|
inlineoverrideprotectedvirtual |
Returns the total bytes that have been allocated by this allocator, or size_t(-1) if this allocator does not track its total allocated bytes.
Reimplemented from pw::allocator::AbstractAllocator.
| UniquePtr< T > pw::allocator::BumpAllocator::MakeUniqueOwned | ( | Args &&... | args | ) |
Constructs and object of type T from the given args, and wraps it in a UniquePtr
Owned objects will have their destructors invoked when the allocator goes out of scope.
The returned value may contain null if allocating memory for the object fails. Callers must check for null before using the UniquePtr.
| [in] | args... | Arguments passed to the object constructor. |
| T * pw::allocator::BumpAllocator::NewOwned | ( | Args &&... | args | ) |
Constructs an "owned" object of type T from the given args
Owned objects will have their destructors invoked when the allocator goes out of scope.
The return value is nullable, as allocating memory for the object may fail. Callers must check for this error before using the resulting pointer.
| [in] | args... | Arguments passed to the object constructor. |