asmjit::JitAllocator Class Reference

A simple implementation of memory manager that uses asmjit::VirtMem functions to manage virtual memory for JIT compiled code.

Implementation notes:

  • Granularity of allocated blocks is different than granularity for a typical C malloc. In addition, the allocator can use several memory pools having a different granularity to minimize the maintenance overhead. Multiple pools feature requires kFlagUseMultiplePools flag to be set.
  • The allocator doesn't store any information in executable memory, instead, the implementation uses two bit-vectors to manage allocated memory of each allocator-block. The first bit-vector called 'used' is used to track used memory (where each bit represents memory size defined by granularity) and the second bit vector called 'stop' is used as a sentinel to mark where the allocated area ends.
  • Internally, the allocator also uses RB tree to keep track of all blocks across all pools. Each inserted block is added to the tree so it can be matched fast during release() and shrink().

Public Members

Members

Classes

Member Functions

Construction & Destruction
Accessors
Alloc & Release
Statistics

Write Operations

Constructor & Destructor Documentation

JitAllocator::JitAllocator(const CreateParams* params = nullptr)explicitnoexcept◆ 

Creates a JitAllocator instance.

JitAllocator::~JitAllocator()noexcept◆ 

Destroys the JitAllocator instance and release all blocks held.

Member Function Documentation

void JitAllocator::reset(ResetPolicy resetPolicy = ResetPolicy::kSoft)noexcept◆ 

Free all allocated memory - makes all pointers returned by alloc() invalid.

Remarks
This function is not thread-safe as it's designed to be used when nobody else is using allocator. The reason is that there is no point of calling reset() when the allocator is still in use.

JitAllocatorOptions JitAllocator::options() constnoexcept◆ 

Returns allocator options, see Flags.

bool JitAllocator::hasOption(JitAllocatorOptions option) constnoexcept◆ 

Tests whether the allocator has the given option set.

uint32_t JitAllocator::blockSize() constnoexcept◆ 

Returns a base block size (a minimum size of block that the allocator would allocate).

uint32_t JitAllocator::granularity() constnoexcept◆ 

Returns granularity of the allocator.

uint32_t JitAllocator::fillPattern() constnoexcept◆ 

Returns pattern that is used to fill unused memory if kFlagUseFillPattern is set.

Error JitAllocator::alloc(Span& out, size_t size)noexcept◆ 

Allocates a new memory span of the requested size.

Error JitAllocator::release(void* rx)noexcept◆ 

Releases a memory block returned by alloc().

Remarks
This function is thread-safe.

Error JitAllocator::shrink(Span& span, size_t newSize)noexcept◆ 

Frees extra memory allocated with rx by shrinking it to the given newSize.

Remarks
This function is thread-safe.

Error JitAllocator::query(Span& out, void* rx) constnoexcept◆ 

Queries information about an allocated memory block that contains the given rx, and writes it to out.

If the pointer is matched, the function returns kErrorOk and fills out with the corresponding span.

Statistics JitAllocator::statistics() constnoexcept◆ 

Returns JIT allocator statistics.

Remarks
This function is thread-safe.

Member Data Documentation

Impl* JitAllocator::_impl◆ 

Allocator implementation (private).