Sys::Mmap (3)
Leading comments
Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35) Standard preamble: ========================================================================
NAME
Sys::Mmap - uses mmap to map in a file as a Perl variableSYNOPSIS
use Sys::Mmap; new Mmap $str, 8192, 'structtest2.pl' or die $!; new Mmap $var, 8192 or die $!; mmap($foo, 0, PROT_READ, MAP_SHARED, FILEHANDLE) or die "mmap: $!"; @tags = $foo =~ /<(.*?)>/g; munmap($foo) or die "munmap: $!"; mmap($bar, 8192, PROT_READ|PROT_WRITE, MAP_SHARED, FILEHANDLE); substr($bar, 1024, 11) = "Hello world"; mmap($baz, 8192, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, STDOUT); $addr = mmap($baz, 8192, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, STDOUT); Sys::Mmap::hardwire($qux, $addr, 8192);
DESCRIPTION
The Mmap module uses theNote that PerlIO now defines a ":mmap" tag and presents mmap'd files as regular files, if that is your cup of joe.
Several processes may share one copy of the file or string, saving memory, and concurrently making changes to portions of the file or string. When not used with a file, it is an alternative to SysV shared memory. Unlike SysV shared memory, there are no arbitrary size limits on the shared memory area, and sparce memory usage is handled optimally on most modern
Using the "new()" method provides a "tie()"'d interface to "mmap()" that allows you to use the variable as a normal variable. If a filename is provided, the file is opened and mapped in. If the file is smaller than the length provided, the file is grown to that length. If no filename is provided, anonymous shared inheritable memory is used. Assigning to the variable will replace a section in the file corresponding to the length of the variable, leaving the remainder of the file intact and unmodified. Using "substr()" allows you to access the file at an offset, and does not place any requirements on the length argument to substr() or the length of the variable being inserted, provided it does not exceed the length of the memory region. This protects you from the pathological cases involved in using "mmap()" directly, documented below.
When calling "mmap()" or "hardwire()" directly, you need to be careful how you use the variable. Some programming constructs may create copies of a string which, while unimportant for smallish strings, are far less welcome if you're mapping in a file which is a few gigabytes big. If you use
- new Mmap VARIABLE, LENGTH, OPTIONALFILENAME
-
Maps LENGTHbytes of (the contents of)OPTIONALFILENAMEifOPTINALFILENAMEis provided, otherwise uses anonymous, shared inheritable memory. This memory region is inherited by any "fork()"ed children.VARIABLEwill now refer to the contents of that file. Any change toVARIABLEwill make an identical change to the file. IfLENGTHis zero and a file is specified, the current length of the file will be used. IfLENGTHis larger then the file, andOPTIONALFILENAMEis provided, the file is grown to that length before being mapped. This is the preferred interface, as it requires much less caution in handling the variable.VARIABLEwill be tied into the ``Mmap'' package, and "mmap()" will be called for you.
Assigning to
VARIABLEwill overwrite the beginning of the file for a length of the value being assigned in. The rest of the file or memory region after that point will be left intact. You may use substr() to assign at a given position:substr(
VARIABLE, POSITION, LENGTH) =NEWVALUE - mmap(VARIABLE, LENGTH, PROTECTION, FLAGS, FILEHANDLE, OFFSET)
-
Maps LENGTHbytes of (the underlying contents of)FILEHANDLEinto your address space, starting at offsetOFFSETand makesVARIABLErefer to that memory. TheOFFSETargument can be omitted in which case it defaults to zero. TheLENGTHargument can be zero in which case a stat is done onFILEHANDLEand the size of the underlying file is used instead.
The
PROTECTIONargument should be some ORed combination of the constantsPROT_READ, PROT_WRITEandPROT_EXECor elsePROT_NONE.The constantsPROT_EXECandPROT_NONEare unlikely to be useful here but are included for completeness.The
FLAGSargument must include eitherMAP_SHAREDorMAP_PRIVATE(the latter is unlikely to be useful here). If your platform supports it, you may also useMAP_ANONorMAP_ANONYMOUS.If your platform suppliesMAP_FILEas a non-zero constant (necessarily non-POSIX) then you should also include that inFLAGS. POSIX.1b does not specifyMAP_FILEas aFLAGargument and most if not all versions of Unix haveMAP_FILEas zero.mmap returns undef on failure, and the address in memory where the variable was mapped to on success.
- munmap(VARIABLE)
-
Unmaps the part of your address space which was previously mapped in
with a call to "mmap(VARIABLE, ...)" and makes VARIABLEbecome undefined.
munmap returns 1 on success and undef on failure.
- hardwire(VARIABLE, ADDRESS, LENGTH)
- Specifies the address in memory of a variable, possibly within a region you've "mmap()"ed another variable to. You must use the same percaustions to keep the variable from being reallocated, and use "substr()" with an exact length. If you "munmap()" a region that a "hardwire()"ed variable lives in, the "hardwire()"ed variable will not automatically be "undef"ed. You must do this manually.
- Constants
-
The Mmap module exports the following constants into your namespace
MAP_SHARED MAP_PRIVATE MAP_ANON MAP_ANONYMOUS MAP_FILE
PROT_EXEC PROT_NONE PROT_READ PROT_WRITEOf the constants beginning
MAP_,onlyMAP_SHAREDandMAP_PRIVATEare defined inPOSIX.1b and onlyMAP_SHAREDis likely to be useful.
BUGS
Scott Walters doesn't knowThe tie() interface makes writing to a substring of the variable much less efficient. One user cited his application running 10-20 times slower when ``new Mmap'' is used than when mmap() is called directly.
Malcolm Beattie has not reviewed Scott's work and is not responsible for any bugs, errors, omissions, stylistic failings, importabilities, or design flaws in this version of the code.
There should be a tied interface to hardwire() as well.
Scott Walter's spelling is awful.
hardwire() will segfault Perl if the mmap() area it was refering to is munmap()'d out from under it.
munmap() will segfault Perl if the variable was not successfully mmap()'d previously, or if it has since been reallocated by Perl.
AUTHOR
Todd Rinaldo cleaned up code, modernized again, and merged in many fixes, 2010-2011.Scott Walters updated for Perl 5.6.x, additions, 2002.
Malcolm Beattie, 21 June 1996.