asmjit::Zone Class Reference

Zone memory.

Zone 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 Zone::alloc() and friends.

Zone has no function to release the allocated memory. It has to be released all at once by calling reset(). If you need a more friendly allocator that also supports release(), consider using Zone with ZoneAllocator.

Member Functions

Construction & Destruction
Accessors
Utilities
Allocation

Constructor & Destructor Documentation

Zone::Zone(size_t blockSize, size_t blockAlignment = 1)explicitnoexcept[1/2]

Creates a new Zone.

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

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

Zone::Zone(Zone&& other)noexcept[2/2]

Moves an existing Zone.

Note
You cannot move an existing ZoneTmp as it uses embedded storage. Attempting to move ZoneTmp would result in assertion failure in debug mode and undefined behavior in release mode.

Zone::~Zone()noexcept

Destroys the Zone instance.

This will destroy the Zone instance and release all blocks of memory allocated by it. It performs implicit reset(Globals::kResetHard).

Member Function Documentation

void Zone::reset(uint32_t resetPolicy = Globals::kResetSoft)noexcept

Resets the Zone invalidating all blocks allocated.

See Globals::ResetPolicy for more details.

bool Zone::isTemporary() constnoexcept

Tests whether this Zone is actually a ZoneTmp that uses temporary memory.

size_t Zone::blockSize() constnoexcept

Returns the default block size.

size_t Zone::blockAlignment() constnoexcept

Returns the default block alignment.

size_t Zone::remainingSize() constnoexcept

Returns remaining size of the current block.

template<typename T = uint8_t>
T* Zone::ptr()noexcept

Returns the current zone 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* Zone::end()noexcept

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

template<typename T>
void Zone::setPtr(T* ptr)noexcept

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

template<typename T>
void Zone::setEnd(T* end)noexcept

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

void Zone::align(size_t alignment)noexcept

Aligns the current pointer to alignment.

Error Zone::ensure(size_t size)noexcept

Ensures the remaining size is at least equal or greater than size.

Note
This function doesn't respect any alignment. If you need to ensure there is enough room for an aligned allocation you need to call align() before calling ensure().

void* Zone::alloc(size_t size)noexcept[1/2]

Allocates the requested memory specified by size.

Pointer returned is valid until the Zone 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 Zone with default block size of approximately 65536 bytes.
Zone zone(65536 - Zone::kBlockOverhead);
// Create your objects using zone object allocating, for example:
Object* obj = static_cast<Object*>( zone.alloc(sizeof(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 `Zone`.
zone.reset();

void* Zone::alloc(size_t size, size_t alignment)noexcept[2/2]

Allocates the requested memory specified by size and alignment.

void* Zone::allocNoCheck(size_t size)noexcept[1/2]

Allocates the requested memory specified by size without doing any checks.

Can only be called if remainingSize() returns size at least equal to size.

void* Zone::allocNoCheck(size_t size, size_t alignment)noexcept[2/2]

Allocates the requested memory specified by size and alignment without doing any checks.

Performs the same operation as Zone::allocNoCheck(size) with alignment applied.

void* Zone::allocZeroed(size_t size, size_t alignment = 1)noexcept

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

template<typename T>
T* Zone::allocT(size_t size = sizeof(T), size_t alignment = alignof(T))noexcept

Like alloc(), but the return pointer is casted to T*.

template<typename T>
T* Zone::allocNoCheckT(size_t size = sizeof(T), size_t alignment = alignof(T))noexcept

Like allocNoCheck(), but the return pointer is casted to T*.

template<typename T>
T* Zone::allocZeroedT(size_t size = sizeof(T), size_t alignment = alignof(T))noexcept

Like allocZeroed(), but the return pointer is casted to T*.

template<typename T>
T* Zone::newT()noexcept[1/2]

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

template<typename T, typename... Args>
T* Zone::newT(Args&&... args)noexcept[2/2]

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

void* Zone::dup(const void* data, size_t size, bool nullTerminate = false)noexcept

Helper to duplicate data.

void* Zone::dupAligned(const void* data, size_t size, size_t alignment, bool nullTerminate = false)noexcept

Helper to duplicate data.

char* Zone::sformat(const char* str, ...)noexcept

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