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

Classes

Public Types

Member Functions

Construction & Destruction
Accessors
Alloc & Release
Statistics

Member Enumeration Documentation

JitAllocator::Options : uint32_tenum

ConstantDescription
kOptionUseDualMapping 

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.

kOptionUseMultiplePools 

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.

kOptionFillUnusedMemory 

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. 
kOptionImmediateRelease 

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.

kOptionCustomFillPattern 

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

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(uint32_t resetPolicy = Globals::kResetSoft)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

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.

Remarks
This function is thread-safe.

Error JitAllocator::release(void* ro)noexcept

Release a memory returned by alloc().

Remarks
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.

Remarks
This function is thread-safe.

Statistics JitAllocator::statistics() constnoexcept

Returns JIT allocator statistics.

Remarks
This function is thread-safe.

Member Data Documentation

Impl* JitAllocator::_impl

Allocator implementation (private).