malloc(3)


NAME
    malloc, mfree, realloc - userspace heap allocation


SYNOPSIS
    void* montauk::malloc(uint64_t size);
    void montauk::mfree(void* ptr);
    void* montauk::realloc(void* ptr, uint64_t size);


DESCRIPTION
    The userspace heap provides dynamic memory allocation on top of
    anonymous virtual memory. The Montauk C++ API and libc's
    malloc/free API use the same process-wide allocator.


   malloc
    Allocates 'size' bytes from the free list. Returns a 16-byte
    aligned pointer, or nullptr on failure. When the free list is
    empty, it reserves more pages from the kernel. Physical pages
    are committed as they are first touched.

        char* buf = (char*)montauk::malloc(1024);


   mfree
    Returns the block to the userspace allocator. Arena blocks are
    immediately reusable; large direct mappings are returned to the
    kernel, including their virtual address range.
    Passing nullptr is a safe no-op.

        montauk::mfree(buf);


   realloc
    Resizes the allocation to 'size' bytes. A block with sufficient
    capacity is retained; otherwise a new block is allocated, the
    smaller of old/new requested sizes is copied, and the old block
    is freed. Integer overflow fails without changing the old block.
    If ptr is nullptr, behaves like malloc.

        buf = (char*)montauk::realloc(buf, 2048);


IMPLEMENTATION
    The allocator uses segregated size-class bins and a coalescing
    address-ordered overflow list. Headers retain both requested size
    and actual block extent. A process-wide lock serializes C and C++
    allocation calls. All returned pointers are 16-byte aligned.

    Allocations of 256 KiB or more use direct page mappings so they
    can be released promptly. Smaller allocations use growing arenas.


LOW-LEVEL PAGE API
    For large allocations or when direct page control is needed:

        void* montauk::alloc(uint64_t size);  // SYS_ALLOC
        void  montauk::free(void* ptr);       // SYS_FREE

    alloc() reserves zero-filled, read/write, non-executable pages.
    Size is rounded up to 4 KiB. Freed ranges are reusable.


SEE ALSO
    syscalls(2), file(2)

Back to Documentation Index