The mmap Reference Manual

This is the mmap Reference Manual, version 1.1.0, generated automatically by Declt version 4.0 beta 2 "William Riker" on Mon Feb 26 17:21:43 2024 GMT+0.

Table of Contents


1 Introduction


2 Systems

The main system appears first, followed by any subsystem dependency.


2.1 mmap

Portable mmap (file memory mapping) utility library.

Maintainer

Yukari Hafner <>

Author

Yukari Hafner <>

Home Page

https://shinmera.github.io/mmap/

Source Control

(GIT https://github.com/Shinmera/mmap.git)

Bug Tracker

https://github.com/Shinmera/mmap/issues

License

zlib

Version

1.1.0

Defsystem Dependency

trivial-features (system).

Dependencies
  • documentation-utils (system).
  • cffi (system).
Source

mmap.asd.

Child Components

3 Files

Files are sorted by type and then listed depth-first from the systems components trees.


3.1 Lisp


3.1.1 mmap/mmap.asd

Source

mmap.asd.

Parent Component

mmap (system).

ASDF Systems

mmap.


3.1.2 mmap/package.lisp

Source

mmap.asd.

Parent Component

mmap (system).

Packages

mmap.


3.1.3 mmap/generic.lisp

Dependency

package.lisp (file).

Source

mmap.asd.

Parent Component

mmap (system).

Public Interface
Internals

3.1.4 mmap/posix.lisp

If Feature

:unix

Dependency

generic.lisp (file).

Source

mmap.asd.

Parent Component

mmap (system).

Public Interface
Internals

3.1.5 mmap/windows.lisp

If Feature

:windows

Dependencies
Source

mmap.asd.

Parent Component

mmap (system).


3.1.6 mmap/documentation.lisp

Dependencies
Source

mmap.asd.

Parent Component

mmap (system).


4 Packages

Packages are listed by definition order.


4.1 mmap

Source

package.lisp.

Nickname

org.shirakumo.fraf.trial.mmap

Use List

common-lisp.

Public Interface
Internals

5 Definitions

Definitions are sorted by export status, category, package, and then by lexicographic order.


5.1 Public Interface


5.1.1 Macros

Macro: with-mmap ((addr fd size path &rest args &key dont-close &allow-other-keys) &body body)

Map the file or number of bytes to a memory region within the body.

This is a convenience macro that calls MMAP with the given arguments, binds the results to the variables ADDR, FD, and SIZE, and automatically ensures that MUNMAP is called with the correct values when the body is exited.

If the flag DONT-CLOSE is set, WITH-MMAP will not free the file descriptor on unwind. This is useful primarily if you pass in an FD
for the path yourself and are either not responsible for closing it,
or would like to continue using it for other purposes.

It is safe to change the ADDR, FD, and SIZE bindings, though probably not very good style to do so. It is NOT safe to save the ADDR and SIZE values somewhere and use them outside of the dynamic scope of the body. Attempting to do so is very likely going to burn your process to the ground.

See MMAP
See MUNMAP

Package

mmap.

Source

generic.lisp.


5.1.2 Compiler macros

Compiler Macro: madvise (addr size advice)
Package

mmap.

Source

posix.lisp.

Compiler Macro: mmap (path &key open protection mmap size offset)
Package

mmap.

Source

posix.lisp.

Compiler Macro: mprotect (addr size protection)
Package

mmap.

Source

posix.lisp.

Compiler Macro: msync (addr fd size &key flags)
Package

mmap.

Source

posix.lisp.


5.1.3 Ordinary functions

Function: madvise (addr size advice)

Gives hints about the usage patterns of the memory to better tune mapping behaviour.

The values passed to this function must be the ones retrieved from a call
to MMAP.

The following advice hints are supported:

:NORMAL — [POSIX] This is the default.
:SEQUENTIAL — [POSIX] Expect memory to be addressed sequentially. :RANDOM — [POSIX] Expect memory to be addressed randomly.
:WILL-NEED — [POSIX] Expect the memory to be used very soon.
:DONT-NEED — [POSIX] Expect the memory to not be needed any
time soon. This will most likely cause
pages to be offloaded until they are
accessed again.
:FREE — [LINUX] The pages in the specified range are no
longer needed and can be freed at any
time, for instance to make space in case
of memory pressure.
:REMOVE — [LINUX] Free the given pages and the associated
backing store.
:DONT-FORK — [LINUX] Don’t make changes available in children.
:DO-FORK — [LINUX] Undo :DONT-FORK behaviour.
:MERGEABLE — [LINUX] The pages in the specified range may be
merged with ones with identical content. :UNMERGEABLE — [LINUX] Undo :MERGEABLE behaviour.
:HUGE-PAGE — [LINUX] Enable transparent huge pages for the
specified page range.
:NO-HUGE-PAGE — [LINUX] Ensure that the memory in the given
range is not backed by transparent huge pages.
:DONT-DUMP — [LINUX] The pages in the specified range should
be excluded from core dumps.
:DO-DUMP — [LINUX] Undo :DONT-DUMP behaviour.
:WIPE-ON-FORK — [LINUX] Memory in the given range is zeroed out
for children.
:KEEP-ON-FORK — [LINUX] Undo :WIPE-ON-FORK behaviour.
:COLD — [LINUX] Deactivate the given range of
pages. This makes them a more likely
target for reclamation in the presence
of memory pressure.
:PAGEOUT — [LINUX] The pages in the specified range should
be reclaimed and their data flushed out.

This function returns nothing useful.

This function may signal an MMAP-ERROR in case the operating system notices
a problem.

See MMAP
See MMAP-ERROR
See https://pubs.opengroup.org/onlinepubs/007904875/functions/posix_madvise.html See https://man7.org/linux/man-pages/man2/madvise.2.html

Package

mmap.

Source

posix.lisp.

Function: mmap (path &key open protection mmap size offset)

Map the given path or number of bytes into the address space.

PATH can be either a pathname designator, FD, or NIL. If it is NIL, an
anonymous file is mapped and the MMAP flag list must include the flag
:ANONYMOUS. If it is a path or an open POSIX file descriptor, then the
contents of the given file on the file system are mapped into the
address space. The file contents can then be read, written, or
executed depending on the given flags as if normal memory was
accessed. If the file is NIL or its size cannot be automatically
determined, you must pass a valid SIZE. You may optionally pass an
OFFSET (in bytes) into the file from which the mapping begins.

If the map attempt fails, an error of type MMAP-ERROR is signalled.
If the call succeeds, three values are returned:

PTR — A CFFI:FOREIGN-POINTER that points to the start of the place in
memory where the file contents have been mapped. The contents
should be placed in increasing address order, unless the flag
:GROWS-DOWN is active.
FD — An opaque file descriptor. You should not touch this.
SIZE — The size of the region of memory that has been mapped in bytes.

All three values need to be passed on to MUNMAP completely unchanged. Any
change could cause severe issues.

The three options OPEN, PROTECTION, and MMAP are lists of flags. Not all of
those flags are portable, some are only allowed on Linux, some only on non-
Windows systems. To indicate support, the flags are marked as EVERY, POSIX
(non-Windows), LINUX, or WINDOWS.

OPEN
:READ — [EVERY] Opens the file for read access.
:WRITE — [EVERY] Opens the file for write access.
:CREATE — [EVERY] Creates the file if it does not exist yet.
:ENSURE-CREATE — [EVERY] Creates the file if it does not exist yet and
errors if it does.
:TRUNCATE — [EVERY] Truncates the file and replaces it if it exists.
:DIRECT — [EVERY] Causes system buffers to be bypassed.
:FILE-SYNC — [EVERY] Causes writes to the file to be flushed asap.
:DATA-SYNC — [POSIX] Similar to FILE-SYNC, but uses data integrity
semantics rather than file integrity semantics. :DONT-CLAIM-TTY— [POSIX] If the file is a tty and the process does not
already have a controlling tty, this file will
not become the process’ controlling tty.
:NON-BLOCK — [POSIX] Attempt to open the file in non-blocking mode,
causing operations on the fd to return asap.
:NO-FOLLOW — [LINUX] Errors if the file is a symlink.
:ASYNC — [LINUX] Enable signal driven IO.
:DIRECTORY — [LINUX] Errors if the file is not a directory.
:LARGE-FILE — [LINUX] Allows opening files with size not representable
by a 32 bit unsigned integer.

PROTECTION
:READ — [EVERY] Allows reading from the memory region. The OPEN
flag :READ is required for this protection mode.
This flag is required on windows.
:WRITE — [EVERY] Allows writing to the memory region.
:EXEC — [EVERY] Allows executing code in the memory region.
:NONE — [POSIX] Prevents accessing the memory region.

MMAP
:PRIVATE — [EVERY] The underlying file is not changed if the memory
area is written to. Copy-on-write is employed to
ensure separation.
:SHARED — [EVERY] The underlying file is changed if the memory
area is written to and the change will be
visible to other processes. In this case the
OPEN flag :WRITE must be specified.
:ANONYMOUS — [LINUX/WINDOWS] The path should be a number of bytes to
map to memory. The memory region is then mapped
against an "anonymous" file.
:NO-RESERVE — [LINUX] Don’t reserve swap for this mapping. If memory
runs out, a segfault will be generated instead.
:LOCKED — [LINUX] Locks the region to RAM, preventing it from
being swapped out.
:GROWS-DOWN — [LINUX] Causes the memory region to be mapped with a
decreasing address, like in a stack.
:POPULATE — [LINUX] Pre-populate the memory region with the file
contents, which can help performance.
:NON-BLOCK — [LINUX] Only useful with :POPULATE – do not perform a read-ahead.

The default values for the flags are:
:OPEN (:READ) :PROTECTION (:READ) :MMAP (:PRIVATE)

Note that if you are intending to use MPROTECT to change the protection of
the mapped file at a later date, you need to call MMAP with the maximal
combination of protection flags first. If this is not the protection that
you want to start out with, call MPROTECT with the correct combination
immediately after. For instance, if you would like to start with (:READ) and
later want to change it to (:READ :WRITE), call MMAP with (:READ :WRITE),
and immediately after call MPROTECT with (:READ).

See MUNMAP
See WITH-MMAP
See MMAP-ERROR
See http://pubs.opengroup.org/onlinepubs/7908799/xsh/mmap.html
See http://pubs.opengroup.org/onlinepubs/009604499/functions/stat.html
See http://man7.org/linux/man-pages/man2/mmap.2.html
See http://man7.org/linux/man-pages/man2/stat.2.html
See https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-createfilew See https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfilesize See https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-createfilemappinga See https://msdn.microsoft.com/en-us/library/windows/desktop/aa366761(v=vs.85).aspx

Package

mmap.

Source

posix.lisp.

Function: mprotect (addr size protection)

Changes the access protection of the mapped memory region.

The values passed to this function must be the ones retrieved from a call
to MMAP.

The following protection flags are supported:

:READ — [EVERY] Allows reading from the memory region. The OPEN flag :READ is required for this protection mode. This flag is required on windows.
:WRITE — [EVERY] Allows writing to the memory region.
:EXEC — [EVERY] Allows executing code in the memory region.
:NONE — [POSIX] Prevents accessing the memory region.

This function returns nothing useful.

This function may signal an MMAP-ERROR in case the operating system notices
a problem.

See MMAP
See MMAP-ERROR
See http://pubs.opengroup.org/onlinepubs/9699919799/functions/mprotect.html
See http://man7.org/linux/man-pages/man2/mprotect.2.html
See https://msdn.microsoft.com/en-us/library/windows/desktop/aa366898(v=vs.85).aspx

Package

mmap.

Source

posix.lisp.

Function: msync (addr fd size &key flags)

Causes writes to the mapped file area to be written to disk.

The values passed to this function must be the ones retrieved from a call
to MMAP.

The following flags are supported:

:SYNC — [EVERY] Writing is synchronous. A call to this function
will not return until the data is flushed to disk.
:ASYNC — [EVERY] Writing is asynchronous and a call will return immediately.
:INVALIDATE — [POSIX] Asks to invalidate other mappings of the same
file, ensuring the view is synchronised.

This function returns nothing useful.

This function may signal an MMAP-ERROR in case the operating system notices
a problem.

See MMAP
See MMAP-ERROR
See http://pubs.opengroup.org/onlinepubs/000095399/functions/msync.html
See http://man7.org/linux/man-pages/man2/msync.2.html
See https://msdn.microsoft.com/en-us/library/windows/desktop/aa366563(v=vs.85).aspx
See https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-flushfilebuffers

Package

mmap.

Source

posix.lisp.

Function: munmap (addr fd size)

Unmaps the memory region, freeing the address space and its file.

The values passed to this function must be the ones retrieved from a call
to MMAP. Calling MUNMAP with the same values more than once will lead to undefined consequences and may very well corrupt your system to crash. The
same goes for calling MUNMAP with values not directly returned by MMAP,
calling it with changed values returned by MMAP, or attempting to
dereference the PTR after a call to MUNMAP.

This function returns nothing useful.

On POSIX systems you may pass NIL for the FD argument, in which case
the file descriptor is not closed. It is then your responsibility to
close it appropriately at a later point.

This function may signal an MMAP-ERROR in case the operating system notices
a problem.

See MMAP
See MMAP-ERROR
See WITH-MMAP
See http://pubs.opengroup.org/onlinepubs/9699919799/functions/mprotect.html
See http://man7.org/linux/man-pages/man2/mprotect.2.html
See https://msdn.microsoft.com/en-us/library/windows/desktop/aa366882(v=vs.85).aspx See https://msdn.microsoft.com/en-us/library/windows/desktop/ms724211(v=vs.85).aspx

Package

mmap.

Source

posix.lisp.


5.1.4 Generic functions

Generic Reader: code (condition)

The OS-specific error code returned for the mmap failure.

See MMAP-ERROR

Package

mmap.

Methods
Reader Method: code ((condition mmap-error))
Source

generic.lisp.

Target Slot

code.

Generic Reader: message (condition)

The (hopefully) user-readable error message for the mmap failure.

See MMAP-ERROR

Package

mmap.

Methods
Reader Method: message ((condition mmap-error))
Source

generic.lisp.

Target Slot

message.


5.1.5 Conditions

Condition: mmap-error

Error signalled if the mmap attempt fails for some reason.

Possible reasons include, but are not limited to:
- File not found
- File access denied
- Out of memory
- Out of address space
- Mapping not allowed
- Invalid combination of flags

See MMAP
See CODE
See MESSAGE

Package

mmap.

Source

generic.lisp.

Direct superclasses

simple-error.

Direct methods
Direct slots
Slot: code
Initargs

:code

Readers

code.

Writers

This slot is read-only.

Slot: message
Initargs

:message

Readers

message.

Writers

This slot is read-only.


5.2 Internals


5.2.1 Symbol macros

Symbol Macro: errno
Package

mmap.

Source

posix.lisp.


5.2.2 Ordinary functions

Function: %mmap (path size offset open protection mmap)
Package

mmap.

Source

posix.lisp.

Function: %var-accessor-errno ()
Package

mmap.

Source

posix.lisp.

Function: (setf %var-accessor-errno) ()
Package

mmap.

Source

posix.lisp.

Function: cfold (env form &rest vars)
Package

mmap.

Source

generic.lisp.

Function: check-posix (result)
Package

mmap.

Source

posix.lisp.

Function: error-mmap (code message)
Package

mmap.

Source

generic.lisp.

Function: strerror (errnum)
Package

mmap.

Source

posix.lisp.

Function: translate-path (path)
Package

mmap.

Source

generic.lisp.

Function: u-close (fd)
Package

mmap.

Source

posix.lisp.

Function: u-madvise (address length advice)
Package

mmap.

Source

posix.lisp.

Function: u-mmap (address length protection flags fd offset)
Package

mmap.

Source

posix.lisp.

Function: u-mprotect (address length flags)
Package

mmap.

Source

posix.lisp.

Function: u-msync (address length flags)
Package

mmap.

Source

posix.lisp.

Function: u-munmap (address length)
Package

mmap.

Source

posix.lisp.

Function: u-open (pathname mode)
Package

mmap.

Source

posix.lisp.


Appendix A Indexes


A.1 Concepts


A.2 Functions

Jump to:   %   (  
C   E   F   G   M   S   T   U   W  
Index Entry  Section

%
%mmap: Private ordinary functions
%var-accessor-errno: Private ordinary functions

(
(setf %var-accessor-errno): Private ordinary functions

C
cfold: Private ordinary functions
check-posix: Private ordinary functions
code: Public generic functions
code: Public generic functions
Compiler Macro, madvise: Public compiler macros
Compiler Macro, mmap: Public compiler macros
Compiler Macro, mprotect: Public compiler macros
Compiler Macro, msync: Public compiler macros

E
error-mmap: Private ordinary functions

F
Function, %mmap: Private ordinary functions
Function, %var-accessor-errno: Private ordinary functions
Function, (setf %var-accessor-errno): Private ordinary functions
Function, cfold: Private ordinary functions
Function, check-posix: Private ordinary functions
Function, error-mmap: Private ordinary functions
Function, madvise: Public ordinary functions
Function, mmap: Public ordinary functions
Function, mprotect: Public ordinary functions
Function, msync: Public ordinary functions
Function, munmap: Public ordinary functions
Function, strerror: Private ordinary functions
Function, translate-path: Private ordinary functions
Function, u-close: Private ordinary functions
Function, u-madvise: Private ordinary functions
Function, u-mmap: Private ordinary functions
Function, u-mprotect: Private ordinary functions
Function, u-msync: Private ordinary functions
Function, u-munmap: Private ordinary functions
Function, u-open: Private ordinary functions

G
Generic Function, code: Public generic functions
Generic Function, message: Public generic functions

M
Macro, with-mmap: Public macros
madvise: Public compiler macros
madvise: Public ordinary functions
message: Public generic functions
message: Public generic functions
Method, code: Public generic functions
Method, message: Public generic functions
mmap: Public compiler macros
mmap: Public ordinary functions
mprotect: Public compiler macros
mprotect: Public ordinary functions
msync: Public compiler macros
msync: Public ordinary functions
munmap: Public ordinary functions

S
strerror: Private ordinary functions

T
translate-path: Private ordinary functions

U
u-close: Private ordinary functions
u-madvise: Private ordinary functions
u-mmap: Private ordinary functions
u-mprotect: Private ordinary functions
u-msync: Private ordinary functions
u-munmap: Private ordinary functions
u-open: Private ordinary functions

W
with-mmap: Public macros


A.3 Variables