IndexSupportasmjit::Arena

asmjit::Arena Class Reference [¶]

Inheritance diagram for asmjit::Arena:
asmjit::ArenaTmp< N >

Arena allocator is an incremental memory allocator that allocates memory by simply incrementing a pointer. It allocates blocks of memory by using C's malloc(), but divides these blocks into smaller segments requested by calling Arena::alloc() and friends.

Public Members

Members

Static Public Attributes

Member Functions

Construction & Destruction
Accessors
Oneshot Allocation
Reusable Allocation
Statistics

Static Functions

Arena::Arena(
size_t min_block_size
)explicitnoexcept[1/2][¶]

Creates a new Arena.

The min_block_size parameter describes the default size of the block. If the size parameter passed to alloc() is greater than the default size Arena will allocate and use a larger block, but it will not change the default min_block_size.

It's not required, but it's good practice to set min_block_size to a reasonable value that depends on the usage of Arena. Greater block sizes are generally safer and perform better than unreasonably low block sizes.

Arena::Arena(
size_t min_block_size,
Span<uint8_t> static_arena_memory
)noexcept[2/2][¶]

Creates a new Arena with a first block pointing to static_arena_memory.

Arena::~Arena()noexcept[¶]

Destroys the Arena instance.

This will destroy the Arena instance and release all blocks of memory allocated by it. It performs implicit reset(ResetPolicy::kHard).

void Arena::reset()noexcept[¶]

Resets the Arena invalidating all blocks allocated.

See ResetPolicy for more details.

size_t Arena::min_block_size() constnodiscardnoexcept[¶]

Returns a minimum block size.

size_t Arena::max_block_size() constnodiscardnoexcept[¶]

Returns a maximum block size.

uint8_t Arena::has_static_block() constnodiscardnoexcept[¶]

Tests whether this Arena is actually a ArenaTmp that uses temporary memory.

size_t Arena::remaining_size() constnodiscardnoexcept[¶]

Returns remaining size of the current block.

template<typename T = uint8_t>
T* Arena::ptr()nodiscardnoexcept[¶]

Returns the current arena cursor (dangerous).

This is a function that can be used to get exclusive access to the current block's memory buffer.

template<typename T = uint8_t>
T* Arena::end()nodiscardnoexcept[¶]

Returns the end of the current arena block, only useful if you use ptr().

template<typename T>
void Arena::set_ptr(
T* ptr
)noexcept[¶]

Sets the current arena pointer to ptr (must be within the current block).

template<typename T>
void Arena::set_end(
T* end
)noexcept[¶]

Sets the end arena pointer to end (must be within the current block).

template<typename T = void>
T* Arena::alloc_oneshot(
size_t size
)nodiscardnoexcept[¶]

Allocates the requested memory specified by size and optionally casts the returned value to T*.

Pointer returned is valid until the Arena instance is destroyed or reset by calling reset(). If you plan to make an instance of C++ from the given pointer use placement new and delete operators:

using namespace asmjit;
class Object { ... };
// Create Arena with default block size of 65536 bytes (the maximum size per alloc() would be slightly less).
Arena arena(65536);
// Create your objects using arena object allocating, for example:
Object* obj = static_cast<Object*>( arena.alloc(Arena::aligned_size_of<Object>() );
if (!obj) {
// Handle out of memory error.
}
// Placement `new` and `delete` operators can be used to instantiate it.
new(obj) Object();
// ... lifetime of your objects ...
// To destroy the instance (if required).
obj->~Object();
// Reset or destroy `Arena`.
arena.reset();

void* Arena::_alloc_oneshot_zeroed(
size_t size
)nodiscardnoexcept[¶]

Allocates size bytes of zeroed memory. See alloc() for more details.

template<typename T = void>
T* Arena::alloc_oneshot_zeroed(
size_t size
)nodiscardnoexcept[¶]

Allocates size bytes of zeroed memory. See alloc() for more details.

template<typename T>
T* Arena::new_oneshot()nodiscardnoexcept[1/2][¶]

Like new(std::nothrow) T(...), but allocated by Arena.

template<typename T, typename... Args>
T* Arena::new_oneshot(
Args&&... args
)nodiscardnoexcept[2/2][¶]

Like new(std::nothrow) T(...), but allocated by Arena.

void* Arena::dup(
const void* data,
size_t size,
bool null_terminate = false
)nodiscardnoexcept[¶]

Helper to duplicate data.

char* Arena::sformat(
const char* str,
...
)nodiscardnoexcept[¶]

Helper to duplicate a formatted string, maximum size is 256 bytes.

template<typename T = void>
T* Arena::alloc_reusable(
size_t size
)nodiscardnoexcept[1/2][¶]

Allocates size bytes of memory, ideally from an available pool.

Note

size can't be zero, it will assert in debug mode in such case.

template<typename T = void>
T* Arena::alloc_reusable(
size_t size,
Out<size_t> allocated_size
)nodiscardnoexcept[2/2][¶]

Like alloc(size), but provides a second argument allocated_size that provides a way to know how big the block returned actually is. This is useful for containers to prevent growing too early.

template<typename T = void>
T* Arena::alloc_reusable_zeroed(
size_t size
)nodiscardnoexcept[1/2][¶]

Like alloc(size), but returns zeroed memory.

template<typename T = void>
T* Arena::alloc_reusable_zeroed(
size_t size,
Out<size_t> allocated_size
)nodiscardnoexcept[2/2][¶]

Like alloc(size, allocated_size), but returns zeroed memory.

void Arena::free_reusable(
void* p,
size_t size
)noexcept[¶]

Releases the memory previously allocated by alloc(). The size argument has to be either the same size as used to call alloc() or allocated_size returned by alloc().

ArenaStatistics Arena::statistics() constnoexcept[¶]

Calculates and returns statistics related to the current use of this Arena.

Note

This function fills all members, but _pooled_size member (see ArenaStatistics::pooled_size() function) would be assigned to zero as Arena has no clue about the use of the requested memory.

Attention

This function could be relatively expensive depending on the number of blocks that is managed by the allocator. The primary case of this function is to use it during the development to get an idea about the use of Arena (or use of multiple Arenas if the statistics is aggregated).

size_t Arena::kAlignment = 8ustaticconstexpr[¶]

Default alignment of allocation requests to use when using Arena.

uint8_t* Arena::_ptr {}[¶]

Pointer in the current block.

uint8_t* Arena::_end {}[¶]

End of the current block.

ManagedBlock* Arena::_current_block {}[¶]

Current block.

ManagedBlock* Arena::_first_block {}[¶]

First block (single-linked list).

uint8_t Arena::_current_block_size_shift {}[¶]

Current block size shift - reverted to _min_block_size_shift every time the Arena is reset(ResetPolicy::kHard).

uint8_t Arena::_min_block_size_shift {}[¶]

Minimum log2(block_size) to allocate.

uint8_t Arena::_max_block_size_shift {}[¶]

Maximum log2(block_size) to allocate.

uint8_t Arena::_has_static_block {}[¶]

True when the Arena has a static block (static blocks are used by ArenaTmp).

uint32_t Arena::_unused_byte_count {}[¶]

Unused bytes (remaining bytes in blocks that couldn't be used because of size requests).

ReusableSlot* Arena::_reusable_slots[kReusableSlotCount] {}[¶]

Slots that contain reusable memory chunks.

DynamicBlock* Arena::_dynamic_blocks {}[¶]

Large blocks for allocations that either couldn't use slots or one-shot allocation.