Pigweed
|
#include <bump_allocator.h>
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 &... ExplicitGuard, typename... Args> | |
T * | NewOwned (Args &&... args) |
template<typename T , int &... ExplicitGuard, typename... Args> | |
UniquePtr< T > | MakeUniqueOwned (Args &&... args) |
![]() | |
void * | Allocate (Layout layout) |
template<typename T , int &... ExplicitGuard, typename... Args> | |
T * | New (Args &&... args) |
template<typename T , int &... ExplicitGuard, typename... Args> | |
UniquePtr< T > | MakeUnique (Args &&... args) |
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) |
allocator::AsPmrAllocator | as_pmr () |
![]() | |
const Capabilities & | capabilities () const |
bool | HasCapability (Capability capability) const |
Returns whether a given capabilityis enabled for this object. | |
void | Deallocate (void *ptr) |
void | Deallocate (void *ptr, Layout layout) |
template<typename T > | |
void | Delete (T *ptr) |
StatusWithSize | GetCapacity () const |
bool | IsEqual (const Deallocator &other) const |
Static Public Attributes | |
static constexpr Capabilities | kCapabilities = kSkipsDestroy |
Additional Inherited Members | |
![]() | |
using | Capabilities = allocator::Capabilities |
using | Capability = allocator::Capability |
using | Layout = allocator::Layout |
![]() | |
constexpr | Allocator ()=default |
TODO(b/326509341): Remove when downstream consumers migrate. | |
constexpr | Allocator (const Capabilities &capabilities) |
![]() | |
constexpr | Deallocator ()=default |
TODO(b/326509341): Remove when downstream consumers migrate. | |
constexpr | Deallocator (const Capabilities &capabilities) |
template<typename T > | |
UniquePtr< T > | WrapUnique (T *ptr) |
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.
|
inline |
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. |
|
inline |
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. |