asmjit::VirtMem Namespace Reference

Virtual memory management.




Enumeration Type Documentation

VirtMem::Flags : uint32_tenum

Virtual memory and memory mapping flags.


No access flags.


Memory is readable.


Memory is writable (implies read access).


Memory is executable (implies read access).


A combination of kAccessRead | kAccessWrite


Use a MAP_JIT flag available on Apple platforms (OSX Mojave+), which allows JIT code to be executed in OSX bundles.

This flag is not turned on by default, because when a process uses fork() the child process has no access to the pages mapped with MAP_JIT, which could break code that doesn't expect this behavior.


Not an access flag, only used by allocDualMapping() to override the default allocation strategy to always use a 'tmp' directory instead of "/dev/shm" (on POSIX platforms).

Please note that this flag will be ignored if the operating system allows to allocate an executable memory by a different API than open() or shm_open(). For example on Linux memfd_create() is preferred and on BSDs shm_open(SHM_ANON, ...) is used if SHM_ANON is defined.

Function Documentation

Info VirtMem::info()noexcept

Returns virtual memory information, see VirtMem::Info for more details.

Error VirtMem::alloc(void** p, size_t size, uint32_t flags)noexcept

Allocates virtual memory by either using VirtualAlloc() (Windows) or mmap() (POSIX).

size should be aligned to a page size, use VirtMem::info() to obtain it. Invalid size will not be corrected by the implementation and the allocation would not succeed in such case.

Error VirtMem::release(void* p, size_t size)noexcept

Releases virtual memory previously allocated by VirtMem::alloc() or VirtMem::allocDualMapping().

The size must be the same as used by VirtMem::alloc(). If the size is not the same value the call will fail on any POSIX system, but pass on Windows, because of the difference of the implementation.

Error VirtMem::protect(void* p, size_t size, uint32_t flags)noexcept

A cross-platform wrapper around mprotect() (POSIX) and VirtualProtect (Windows).

Error VirtMem::allocDualMapping(DualMapping* dm, size_t size, uint32_t flags)noexcept

Allocates virtual memory and creates two views of it where the first view has no write access.

This is an addition to the API that should be used in cases in which the operating system either enforces W^X security policy or the application wants to use this policy by default to improve security and prevent an accidental (or purposed) self-modifying code.

The memory returned in the dm are two independent mappings of the same shared memory region. You must use VirtMem::releaseDualMapping() to release it when it's no longer needed. Never use VirtMem::release() to release the memory returned by allocDualMapping() as that would fail on Windows.

Both pointers in dm would be set to nullptr if the function fails.

Error VirtMem::releaseDualMapping(DualMapping* dm, size_t size)noexcept

Releases the virtual memory mapping previously allocated by VirtMem::allocDualMapping().

Both pointers in dm would be set to nullptr if the function succeeds.