#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 &... kExplicitGuard, typename... Args> | |
T * | NewOwned (Args &&... args) |
template<typename T , int &... kExplicitGuard, typename... Args> | |
UniquePtr< T > | MakeUniqueOwned (Args &&... args) |
![]() | |
void * | Allocate (Layout layout) |
template<typename T , int &... kExplicitGuard, typename... Args> | |
std::enable_if_t<!std::is_array_v< T >, T * > | New (Args &&... args) |
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) |
template<typename T > | |
T * | NewArray (size_t count) |
template<typename T > | |
T * | NewArray (size_t count, size_t alignment) |
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 , int &... kExplicitGuard, std::enable_if_t< is_unbounded_array_v< T >, int > = 0> | |
UniquePtr< T > | MakeUnique (size_t size) |
template<typename T , int &... kExplicitGuard, std::enable_if_t< is_unbounded_array_v< T >, int > = 0> | |
UniquePtr< T > | MakeUnique (size_t size, size_t alignment) |
template<typename T > | |
UniquePtr< T[]> | MakeUniqueArray (size_t size) |
template<typename T > | |
UniquePtr< T[]> | MakeUniqueArray (size_t size, size_t alignment) |
template<typename T , int &... kExplicitGuard, std::enable_if_t< is_bounded_array_v< T >, int > = 0, typename... Args> | |
void | MakeUnique (Args &&...)=delete |
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 , int &... kExplicitGuard, std::enable_if_t< is_unbounded_array_v< T >, int > = 0> | |
SharedPtr< T > | MakeShared (size_t size) |
template<typename T , int &... kExplicitGuard, std::enable_if_t< is_unbounded_array_v< T >, int > = 0> | |
SharedPtr< T > | MakeShared (size_t size, size_t alignment) |
template<typename T , int &... kExplicitGuard, std::enable_if_t< is_bounded_array_v< T >, int > = 0, typename... Args> | |
std::enable_if_t< is_bounded_array_v< T > > | MakeShared (Args &&...)=delete |
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) |
size_t | GetAllocated () const |
![]() | |
constexpr 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 |
template<typename T , int &... kExplicitGuard, std::enable_if_t< is_bounded_array_v< T >, int > = 0, typename... Args> | |
void | WrapUnique (Args &&...)=delete |
Static Public Attributes | |
static constexpr Capabilities | kCapabilities = kSkipsDestroy |
Private Member Functions | |
void * | DoAllocate (Layout layout) override |
void | DoDeallocate (void *) override |
size_t | DoGetAllocated () const override |
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 , std::enable_if_t<!std::is_array_v< T >, int > = 0> | |
UniquePtr< T > | WrapUnique (T *ptr) |
template<typename T , int &... kExplicitGuard, typename ElementType = std::remove_extent_t<T>, std::enable_if_t< is_unbounded_array_v< T >, int > = 0> | |
UniquePtr< T > | WrapUnique (ElementType *ptr, size_t size) |
template<typename T > | |
UniquePtr< T[]> | WrapUniqueArray (T *ptr, size_t size) |
![]() | |
template<typename T > | |
static constexpr bool | is_bounded_array_v |
template<typename T > | |
static constexpr bool | is_unbounded_array_v |
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.
|
overrideprivatevirtual |
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.
|
overrideprivate |
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. |
|
inlineoverrideprivatevirtual |
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.
|
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. |