map_anonymous#

Allocate a fresh memory region backed by no file and attach it to a scalar.

Synopsis#

map_anonymous my $buf, 4096;               # shared (default)
map_anonymous my $buf, 4096, 'shared';     # visible to forked children
map_anonymous my $buf, 1 << 20, 'private'; # process-local

Arguments#

  • $lvalue — scalar to receive the mapping.

  • $length — size of the region in bytes. Must be greater than zero.

  • $type'shared' or 'private'. A shared region is inherited by child processes created with fork; a private region is not. Defaults to 'shared'.

Examples#

Share a buffer with a forked child:

map_anonymous my $counter, 16, 'shared';
substr $counter, 0, 16, pack('Q', 0) . pack('Q', 0);
if (fork() == 0) {
    substr $counter, 0, 8, pack('Q', 42);
    exit;
}

Allocate a private scratch region:

map_anonymous my $scratch, 1_000_000, 'private';
substr $scratch, 0, 5, "hello";

Edge cases#

  • Zero-length anonymous maps are rejected.

  • Any $type other than 'shared' or 'private' raises an exception.

Differences from upstream#

Fully compatible with upstream File::Map 0.71.

See also#

  • map_file — map a file instead of raw memory.

  • sys_map — take raw mmap(2) flags.

  • remap — grow or shrink a private anonymous mapping on Linux.