Public Documentation

Functions

The following functions are available on all Unix systems.

UnixMmap.mmapFunction
mmap(file::Union{IO,AbstractString}[, ::Type{Array{T}}, dims]; kws...)
mmap(::Type{Array{T}}, dims; kws...)

Memory maps an Array from a file file of size dims (either a scalar Integer or a tuple of dimension lengths), or creates an anonymous mapping not backed by a file if no file is given.

If not specified, the array type defaults to Array{UInt8}, and dim defaults to a vector with length equal to the number of elements remaining in the file (accounting for a non-zero position in the file stream io).

Extended help

This function provides a relatively simple wrapper around the underlying mmap system call. In particular, it is the user's responsibility to ensure the combination(s) of stream state (e.g. read/write or read-only, file length), protection flags, and memory map flags must form a valid mmap request, as they are not validated here.

See your system mmap man page for details on the behaviors of each flag.

Keywords

The following keywords are available:

  • flags::MmapFlags — any of the MAP_* system-specific constants. For files, this defaults to MAP_SHARED, and anonymous mappings default to MAP_SHARED | MAP_ANONYMOUS.

  • prot::MmapProtection — any of the PROT_* flags. The default is PROT_READ | PROT_WRITE for all anonymous maps and memory maps of non-existent files (in which case the file will be created). If the file already exists, it is opened read-only and the default is PROT_READ only.

  • offset::Integer — offset in bytes from the beginning of the file, defaulting to the current stream position. This keyword is not valid for anonymous maps.

  • grow::Bool — Whether to grow a file to accomodate the memory map, if the file is writable. Defaults to true. This keyword is not valid for anonymous maps.

source
UnixMmap.mincoreFunction
mincore(array::Array)

Returns a boolean array (of length fld1(sizeof(m), PAGESIZE)) indicating whether the pages of the memory-mapped array are resident in RAM. Pages corresponding to false values will cause a fault if referenced.

Note

Memory maps are always page-aligned, so an offset which is not a multiple of PAGESIZE will result in a return array where the first element does not correspond to the first PAGESIZE ÷ sizeof(eltype(array)) elements of array — i.e. the first page may cover memory addresses before the first element of array.

source
UnixMmap.madvise!Function
madvise!(array, flag::AdviseFlags = MADV_NORMAL)

Advises the kernel on the intended usage of the memory-mapped array, with the intent flag being one of the available MADV_* constants.

source
UnixMmap.msync!Function
msync!(array, flag::SyncFlags = MS_SYNC)

Synchronizes the memory-mapped array and its backing file on disk.

source

OS-specific constants for use with mmap and madvise! are defined at (pre)compile time. The following systems are supported based on the available OS predicates via Julia's Base.Sys.KERNEL and Base.Sys.is* functions (some of which are only available for Julia v1.1+).

Constants — Linux

MmapProtection MmapFlags AdviseFlags SyncFlags
PROT_NONE
PROT_READ
PROT_WRITE
PROT_EXEC
MAP_FILE
MAP_SHARED
MAP_PRIVATE
MAP_FIXED
MAP_ANONYMOUS
MAP_32BIT
MAP_GROWSDOWN
MAP_DENYWRITE
MAP_EXECUTABLE
MAP_LOCKED
MAP_NORESERVE
MAP_POPULATE
MAP_NONBLOCK
MAP_STACK
MAP_HUGETLB
MAP_SYNC
MAP_FIXED_NOREPLACE
MADV_NORMAL
MADV_RANDOM
MADV_SEQUENTIAL
MADV_WILLNEED
MADV_DONTNEED
MADV_FREE
MADV_REMOVE
MADV_DONTFORK
MADV_DOFORK
MADV_MERGEABLE
MADV_UNMERGEABLE
MADV_HUGEPAGE
MADV_NOHUGEPAGE
MADV_DONTDUMP
MADV_DODUMP
MADV_WIPEONFORK
MADV_KEEPONFORK
MADV_COLD
MADV_PAGEOUT
MADV_HWPOISON
MADV_SOFT_OFFLINE
MS_ASYNC
MS_INVALIDATE
MS_SYNC

Constants — Apple

MmapProtection MmapFlags AdviseFlags SyncFlags
PROT_NONE
PROT_READ
PROT_WRITE
PROT_EXEC
MAP_FILE
MAP_SHARED
MAP_PRIVATE
MAP_FIXED
MAP_RENAME
MAP_NORESERVE
MAP_INHERIT
MAP_NOEXTEND
MAP_HASSEMAPHORE
MAP_ANONYMOUS
MADV_NORMAL
MADV_RANDOM
MADV_SEQUENTIAL
MADV_WILLNEED
MADV_DONTNEED
MADV_FREE
MS_ASYNC
MS_INVALIDATE
MS_SYNC

Constants — DragonFly BSD

MmapProtection MmapFlags AdviseFlags SyncFlags
PROT_NONE
PROT_READ
PROT_WRITE
PROT_EXEC
MAP_FILE
MAP_SHARED
MAP_PRIVATE
MAP_FIXED
MAP_RENAME
MAP_NORESERVE
MAP_INHERIT
MAP_NOEXTEND
MAP_HASSEMAPHORE
MAP_STACK
MAP_NOSYNC
MAP_ANONYMOUS
MAP_VPAGETABLE
MAP_TRYFIXED
MAP_NOCORE
MAP_SIZEALIGN
MADV_NORMAL
MADV_RANDOM
MADV_SEQUENTIAL
MADV_WILLNEED
MADV_DONTNEED
MADV_FREE
MADV_NOSYNC
MADV_AUTOSYNC
MADV_NOCORE
MADV_CORE
MADV_INVAL
MADV_SETMAP
MS_ASYNC
MS_INVALIDATE
MS_SYNC

Constants — FreeBSD

MmapProtection MmapFlags AdviseFlags SyncFlags
PROT_NONE
PROT_READ
PROT_WRITE
PROT_EXEC
MAP_FILE
MAP_SHARED
MAP_PRIVATE
MAP_FIXED
MAP_STACK
MAP_NOSYNC
MAP_ANONYMOUS
MAP_GUARD
MAP_EXCL
MAP_NOCORE
MAP_32BIT
MADV_NORMAL
MADV_RANDOM
MADV_SEQUENTIAL
MADV_WILLNEED
MADV_DONTNEED
MADV_FREE
MADV_NOSYNC
MADV_AUTOSYNC
MADV_NOCORE
MADV_CORE
MADV_PROTECT
MS_ASYNC
MS_INVALIDATE
MS_SYNC

Constants — NetBSD

MmapProtection MmapFlags AdviseFlags SyncFlags
PROT_NONE
PROT_READ
PROT_WRITE
PROT_EXEC
MAP_FILE
MAP_SHARED
MAP_PRIVATE
MAP_REMAPDUP
MAP_FIXED
MAP_RENAME
MAP_NORESERVE
MAP_INHERIT
MAP_NOEXTEND
MAP_HASSEMAPHORE
MAP_TRYFIXED
MAP_WIRED
MAP_ANONYMOUS
MAP_STACK
MADV_NORMAL
MADV_RANDOM
MADV_SEQUENTIAL
MADV_WILLNEED
MADV_DONTNEED
MADV_SPACEAVAIL
MADV_FREE
MS_ASYNC
MS_INVALIDATE
MS_SYNC

Constants — OpenBSD

MmapProtection MmapFlags AdviseFlags SyncFlags
PROT_NONE
PROT_READ
PROT_WRITE
PROT_EXEC
MAP_FILE
MAP_SHARED
MAP_PRIVATE
MAP_FIXED
MAP_ANONYMOUS
MAP_STACK
MAP_CONCEAL
MADV_NORMAL
MADV_RANDOM
MADV_SEQUENTIAL
MADV_WILLNEED
MADV_DONTNEED
MADV_SPACEAVAIL
MADV_FREE
MS_ASYNC
MS_INVALIDATE
MS_SYNC