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


Public Types

Member Functions

Construction & Destruction
Alloc & Release

Member Enumeration Documentation

JitAllocator::Options : uint32_tenum


Enables the use of an anonymous memory-mapped memory that is mapped into two buffers having a different pointer.

The first buffer has read and execute permissions and the second buffer has read+write permissions.

See VirtMem::allocDualMapping() for more details about this feature.


Enables the use of multiple pools with increasing granularity instead of a single pool.

This flag would enable 3 internal pools in total having 64, 128, and 256 bytes granularity.

This feature is only recommended for users that generate a lot of code and would like to minimize the overhead of JitAllocator itself by having blocks of different allocation granularities. Using this feature only for few allocations won't pay off as the allocator may need to create more blocks initially before it can take the advantage of variable block granularity.


Always fill reserved memory by a fill-pattern.

Causes a new block to be cleared by the fill pattern and freshly
released memory to be cleared before making it ready for another use. 

When this flag is set the allocator would immediately release unused blocks during release() or reset().

When this flag is not set the allocator would keep one empty block in each pool to prevent excessive virtual memory allocations and deallocations in border cases, which involve constantly allocating and deallocating a single block caused by repetitive calling alloc() and release() when the allocator has either no blocks or have all blocks fully occupied.


Use a custom fill pattern, must be combined with kFlagFillUnusedMemory.

Constructor & Destructor Documentation

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

Creates a JitAllocator instance.


Destroys the JitAllocator instance and release all blocks held.

Member Function Documentation

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

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

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

uint32_t JitAllocator::options() constnoexcept

Returns allocator options, see Flags.

bool JitAllocator::hasOption(uint32_t 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(void** roPtrOut, void** rwPtrOut, size_t size)noexcept

Allocate size bytes of virtual memory.

This function is thread-safe.

Error JitAllocator::release(void* ro)noexcept

Release a memory returned by alloc().

This function is thread-safe.

Error JitAllocator::shrink(void* ro, size_t newSize)noexcept

Free extra memory allocated with p by restricting it to newSize size.

This function is thread-safe.

Statistics JitAllocator::statistics() constnoexcept

Returns JIT allocator statistics.

This function is thread-safe.

Member Data Documentation

Impl* JitAllocator::_impl

Allocator implementation (private).