Sys-Mmap-0.16/000750 000765 000024 00000000000 11623535563 013207 5ustar00toddstaff000000 000000 Sys-Mmap-0.16/Artistic000644 000765 000024 00000013740 11423743331 014716 0ustar00toddstaff000000 000000 The "Artistic License" Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder as specified below. "Copyright Holder" is whoever is named in the copyright or copyrights for the package. "You" is you, if you're thinking about copying or distributing this Package. "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as uunet.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) give non-standard executables non-standard names, and clearly document the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. You may embed this Package's interpreter within an executable of yours (by linking); this shall be construed as a mere form of aggregation, provided that the complete Standard Version of the interpreter is so embedded. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. If such scripts or library files are aggregated with this Package via the so-called "undump" or "unexec" methods of producing a binary executable image, then distribution of such an image shall neither be construed as a distribution of this Package nor shall it fall under the restrictions of Paragraphs 3 and 4, provided that you do not represent such an executable image as a Standard Version of this Package. 7. C subroutines (or comparably compiled subroutines in other languages) supplied by you and linked into this Package in order to emulate subroutines and variables of the language defined by this Package shall not be considered part of this Package, but are the equivalent of input as in Paragraph 6, provided these subroutines do not change the language in any way that would cause it to fail the regression tests for the language. 8. Aggregation of this Package with a commercial distribution is always permitted provided that the use of this Package is embedded; that is, when no overt attempt is made to make this Package's interfaces visible to the end user of the commercial distribution. Such use shall not be construed as a distribution of this Package. 9. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 10. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End Sys-Mmap-0.16/Changes000644 000765 000024 00000004155 11623523772 014513 0ustar00toddstaff000000 000000 Todd Rinaldo August 19 2011 v0.16 * Testing looks stable on all major perl versions and distros. * Release with only version bump Todd Rinaldo August 18 2011 v0.15_02 * Pause blocked. Todd Rinaldo August 18 2011 v0.15_01 * Patch from RT 70333 (Syohei YOSHIDA) to make error testing locale neutral * Add test for GLOB mmap * Make temp file larger for testing additional cases. Todd Rinaldo April 4 2011 v0.15 * cpantesters looks good besides Win32. Waiting for someone from windows world to care/submit patch Todd Rinaldo March 29 2011 v0.14_01 * Mmap could not handle files larger than 2GB if perl was not using 64bit int. Resolved the problem by taking a string and converting it via atoll, depending on _FILE_OFFSET_BITS > 32 * Added test and fix to croak if a negative offset is passed in. * initialize global variable pagesize = 0 so it'll set right the first time * Remove typemap which was only used by mmap sub. Now we're using SV, it's no longer an issue. Todd Rinaldo Sept 7 2010 v0.14 * Tests look stable. Releasing prod version of module. Todd Rinaldo Sept 1 2010 v0.13_03 * Skip unmapping of invalid variables on BSD kernels which seem to segfault on this event. Todd Rinaldo July 29 2010 v0.13_02 * Do a little better undef detection in the SV before trying to blindly unmap it. * Add tests to detect these cases Todd Rinaldo July 27 2010 v0.13_01 * RT 59754 AUTOLOAD does quoted eval. * RT 59753 Mmap.xs:14:0: warning: "_POSIX_C_SOURCE" redefined * RT 15905 bug/segmentation fault on ia64 (altix) * RT 22615 munmap($unseen) segfaults * RT 59750 Update Makefile.PL to show Module dependencies. * RT 59749 use Test::More and test interface more thoroughly. * RT 59748 use XSLoader if possible for more lightweight load * RT 59747 Don't import Carp into module by default * update META.yml Aaron Kaplan, Sep 2003, v 0.12 . cleaned up style and usage in C to fit ANSI-C . contributed documentation Scott Walters, Feb 2002, v 0.11 . tied interface to help reduce foot shooting and hoop jumping . hardwire() utility method . perl 5.6.x compatability Malcolm Beattie, 21 June 1996, v 0.10 Original author, original version . Sys-Mmap-0.16/Copying000644 000765 000024 00000030307 11423743331 014542 0ustar00toddstaff000000 000000 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. 675 Mass Ave, Cambridge, MA 02139, USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! Sys-Mmap-0.16/Makefile.PL000644 000765 000024 00000002214 11623524125 015155 0ustar00toddstaff000000 000000 use strict; use warnings; use ExtUtils::MakeMaker; WriteMakefile( NAME => 'Sys::Mmap', AUTHOR => 'Scott Walters ', VERSION_FROM => 'Mmap.pm', ABSTRACT_FROM => 'Mmap.pm', PL_FILES => {}, ($ExtUtils::MakeMaker::VERSION >= 6.3002 ? ('LICENSE' => 'perl', ) : ()), PREREQ_PM => { ($] >= 5.006 ? ('XSLoader' => 0) : ()), 'DynaLoader' => 0, 'Test::More' => 0, }, dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', }, clean => { FILES => 'Sys-Mmap-*' }, META_MERGE => { build_requires => { 'Test::More' => 0, # For testing 'Errno' => 0, # Locale independant failure validation. }, resources => { license => 'http://dev.perl.org/licenses/', homepage => 'http://wiki.github.com/toddr/Sys-Mmap', # bugtracker => 'http://code.google.com/p/Sys-Mmap/issues/list', repository => 'http://github.com/toddr/Sys-Mmap/tree/master', # MailingList => 'http://groups.google.com/group/Sys-Mmap', }, }, ); Sys-Mmap-0.16/MANIFEST000644 000765 000024 00000000221 11623523660 014333 0ustar00toddstaff000000 000000 Artistic Changes Copying Makefile.PL MANIFEST META.yml Module meta-data (added by MakeMaker) Mmap.pm Mmap.xs README t/mmap.t t/munmap_errors.t Sys-Mmap-0.16/META.yml000640 000765 000024 00000001473 11623535563 014466 0ustar00toddstaff000000 000000 --- #YAML:1.0 name: Sys-Mmap version: 0.16 abstract: uses mmap to map in a file as a Perl variable author: - Scott Walters license: perl distribution_type: module configure_requires: ExtUtils::MakeMaker: 0 build_requires: Errno: 0 ExtUtils::MakeMaker: 0 Test::More: 0 requires: DynaLoader: 0 Test::More: 0 XSLoader: 0 resources: homepage: http://wiki.github.com/toddr/Sys-Mmap license: http://dev.perl.org/licenses/ repository: http://github.com/toddr/Sys-Mmap/tree/master no_index: directory: - t - inc generated_by: ExtUtils::MakeMaker version 6.56 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 Sys-Mmap-0.16/Mmap.pm000644 000765 000024 00000023462 11623524013 014437 0ustar00toddstaff000000 000000 package Sys::Mmap; =head1 NAME Sys::Mmap - uses mmap to map in a file as a Perl variable =head1 SYNOPSIS 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); =head1 DESCRIPTION The Mmap module uses the POSIX L call to map in a file as a Perl variable. Memory access by mmap may be shared between threads or forked processes, and may be a disc file that has been mapped into memory. L depends on your operating system supporting UNIX or POSIX.1b mmap, of course. B that L now defines a C<: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 UNIX implementations. Using the C method provides a C'd interface to C 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 C 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 C directly, documented below. When calling C or C 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 PROT_WRITE and attempt to write to the file via the variable you need to be even more careful. One of the few ways in which you can safely write to the string in-place is by using C as an lvalue and ensuring that the part of the string that you replace is exactly the same length. Other functions will allocate other storage for the variable, and it will no longer overlay the mapped in file. =over 4 =item new Mmap VARIABLE, LENGTH, OPTIONALFILENAME Maps LENGTH bytes of (the contents of) OPTIONALFILENAME if OPTINALFILENAME is provided, otherwise uses anonymous, shared inheritable memory. This memory region is inherited by any Ced children. VARIABLE will now refer to the contents of that file. Any change to VARIABLE will make an identical change to the file. If LENGTH is zero and a file is specified, the current length of the file will be used. If LENGTH is larger then the file, and OPTIONALFILENAME is 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. VARIABLE will be tied into the "Mmap" package, and C will be called for you. Assigning to VARIABLE will 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 =item mmap(VARIABLE, LENGTH, PROTECTION, FLAGS, FILEHANDLE, OFFSET) Maps LENGTH bytes of (the underlying contents of) FILEHANDLE into your address space, starting at offset OFFSET and makes VARIABLE refer to that memory. The OFFSET argument can be omitted in which case it defaults to zero. The LENGTH argument can be zero in which case a stat is done on FILEHANDLE and the size of the underlying file is used instead. The PROTECTION argument should be some ORed combination of the constants PROT_READ, PROT_WRITE and PROT_EXEC or else PROT_NONE. The constants PROT_EXEC and PROT_NONE are unlikely to be useful here but are included for completeness. The FLAGS argument must include either MAP_SHARED or MAP_PRIVATE (the latter is unlikely to be useful here). If your platform supports it, you may also use MAP_ANON or MAP_ANONYMOUS. If your platform supplies MAP_FILE as a non-zero constant (necessarily non-POSIX) then you should also include that in FLAGS. POSIX.1b does not specify MAP_FILE as a FLAG argument and most if not all versions of Unix have MAP_FILE as zero. mmap returns undef on failure, and the address in memory where the variable was mapped to on success. =item munmap(VARIABLE) Unmaps the part of your address space which was previously mapped in with a call to C and makes VARIABLE become undefined. munmap returns 1 on success and undef on failure. =item hardwire(VARIABLE, ADDRESS, LENGTH) Specifies the address in memory of a variable, possibly within a region you've Ced another variable to. You must use the same percaustions to keep the variable from being reallocated, and use C with an exact length. If you C a region that a Ced variable lives in, the Ced variable will not automatically be Ced. You must do this manually. =item 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_WRITE Of the constants beginning MAP_, only MAP_SHARED and MAP_PRIVATE are defined in POSIX.1b and only MAP_SHARED is likely to be useful. =back =head1 BUGS Scott Walters doesn't know XS, and is just winging it. There must be a better way to tell Perl not to reallocate a variable in memory... The 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. =head1 AUTHOR Malcolm Beattie, 21 June 1996. Updated for Perl 5.6.x, additions, Scott Walters, Feb 2002. Aaron Kaplan kindly contributed patches to make the C ANSI compliant and contributed documentation as well. =cut use strict; our ($VERSION, @ISA, @EXPORT, $AUTOLOAD); require Exporter; @ISA = qw(Exporter); @EXPORT = qw(mmap munmap MAP_ANON MAP_ANONYMOUS MAP_FILE MAP_PRIVATE MAP_SHARED PROT_EXEC PROT_NONE PROT_READ PROT_WRITE); $VERSION = '0.16'; sub new { if(scalar @_ < 3) { warn 'Usage: new Mmap $var, $desiredSize, $optFile;'; return undef; } my $type = $_[0]; my $leng = $_[2]; tie $_[1], $_[0], @_[2 .. scalar(@_)-1 ]; # tie $_[1], $type, $leng; } sub TIESCALAR { if(scalar @_ < 2) { # print "debug: got args: ", join ', ', @_, "\n"; warn 'Usage: tie $var, "Mmap", $desiredSize, $optionalFile;'; return undef; } my $me; my $fh; my $type = shift; my $leng = shift; my $file = shift; my $flags = constant('MAP_INHERIT',0)| constant('MAP_SHARED',0); if($file) { open $fh, '+>>', $file or do { warn "mmap: could not open file '$file' for append r/w"; return undef; }; # if we dont pad the file out to the specified length, we coredump my $fhsize = -s $fh; if($leng > $fhsize) { $fhsize = $leng - $fhsize; print $fh ("\000" x $fhsize) or die $!; # print $fh pack("a$fhsize", '') or die $!; # while($fhsize) { print $fh "\000"; $fhsize--; } } $flags |= constant('MAP_FILE',0); } else { $flags |= constant('MAP_ANON',0); } my $addr = mmap( $me, $leng, constant('PROT_READ',0)|constant('PROT_WRITE',0), $flags, $file ? $fh : *main::STDOUT ) or die $!; bless \$me, $type; # XXX return $addr somehow... } sub STORE { my $me = shift; my $newval = shift; substr($$me, 0, length($newval), $newval); $$me; } sub FETCH { my $me = shift; $$me; } sub AUTOLOAD { # This AUTOLOAD is used to 'autoload' constants from the constant() # XS function. If a constant is not found then control is passed # to the AUTOLOAD in AutoLoader. if ($AUTOLOAD =~ /::(_?[a-z])/) { $AutoLoader::AUTOLOAD = $AUTOLOAD; goto &AutoLoader::AUTOLOAD; } local $! = 0; my $constname = $AUTOLOAD; $constname =~ s/.*:://; return if $constname eq 'DESTROY'; my $val = constant($constname, @_ ? $_[0] : 0); if ($! == 0) { no strict 'refs'; *$AUTOLOAD = sub { $val }; } else { require Carp; Carp::croak("Your vendor has not defined Mmap macro $constname"); } goto &$AUTOLOAD; } eval { require XSLoader; XSLoader::load( 'Sys::Mmap', $VERSION ); } or do { require DynaLoader; push @ISA, 'DynaLoader'; bootstrap Sys::Mmap $VERSION; }; 1; __END__ Sys-Mmap-0.16/Mmap.xs000644 000765 000024 00000012507 11623523660 014462 0ustar00toddstaff000000 000000 #ifdef __cplusplus extern "C" { #endif #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include #ifdef __cplusplus } #endif #include #include #ifndef MMAP_RETTYPE #ifndef _POSIX_C_SOURCE #define _POSIX_C_SOURCE 199309 #endif #ifdef _POSIX_VERSION #if _POSIX_VERSION >= 199309 #define MMAP_RETTYPE void * #endif #endif #endif #ifndef MMAP_RETTYPE #define MMAP_RETTYPE caddr_t #endif #ifndef MAP_FAILED #define MAP_FAILED ((caddr_t)-1) #endif static int not_here(s) char *s; { croak("%s not implemented on this architecture", s); return -1; } static double constant(name, arg) char *name; int arg; { errno = 0; switch (*name) { case 'M': if (strEQ(name, "MAP_ANON")) #ifdef MAP_ANON return MAP_ANON; #else goto not_there; #endif if (strEQ(name, "MAP_ANONYMOUS")) #ifdef MAP_ANONYMOUS return MAP_ANONYMOUS; #else goto not_there; #endif if (strEQ(name, "MAP_FILE")) #ifdef MAP_FILE return MAP_FILE; #else goto not_there; #endif if (strEQ(name, "MAP_PRIVATE")) #ifdef MAP_PRIVATE return MAP_PRIVATE; #else goto not_there; #endif if (strEQ(name, "MAP_SHARED")) #ifdef MAP_SHARED return MAP_SHARED; #else goto not_there; #endif break; case 'P': if (strEQ(name, "PROT_EXEC")) #ifdef PROT_EXEC return PROT_EXEC; #else goto not_there; #endif if (strEQ(name, "PROT_NONE")) #ifdef PROT_NONE return PROT_NONE; #else goto not_there; #endif if (strEQ(name, "PROT_READ")) #ifdef PROT_READ return PROT_READ; #else goto not_there; #endif if (strEQ(name, "PROT_WRITE")) #ifdef PROT_WRITE return PROT_WRITE; #else goto not_there; #endif break; default: break; } errno = EINVAL; return 0; not_there: errno = ENOENT; return 0; } static size_t pagesize = 0; #if _FILE_OFFSET_BITS > 32 #define get_off(a) (atoll(a)) #else #define get_off(a) (atoi(a)) #endif MODULE = Sys::Mmap PACKAGE = Sys::Mmap double constant(name,arg) char * name int arg SV * hardwire(var, addr, len) SV * var IV addr size_t len PROTOTYPE: $$$ CODE: ST(0) = &PL_sv_undef; SvUPGRADE(var, SVt_PV); SvPVX(var) = (char *) addr; SvCUR_set(var, len); SvLEN_set(var, 0); SvPOK_only(var); /*printf("ok, that var is now stuck at addr %lx\n", addr);*/ ST(0) = &PL_sv_yes; SV * mmap(var, len, prot, flags, fh = 0, off_string) SV * var size_t len int prot int flags FILE * fh SV * off_string int fd = NO_INIT MMAP_RETTYPE addr = NO_INIT off_t slop = NO_INIT off_t off = NO_INIT PROTOTYPE: $$$$*;$ CODE: if(!SvTRUE(off_string)) { off = 0; } else { off = get_off(SvPVbyte_nolen(off_string)); } if(off < 0) { croak("mmap: Cannot operate on a negative offset (%s) ", SvPVbyte_nolen(off_string)); } ST(0) = &PL_sv_undef; if(flags&MAP_ANON) { fd = -1; if (!len) { /* i WANT to return undef and set $! but perlxs and perlxstut dont tell me how... waa! */ croak("mmap: MAP_ANON specified, but no length specified. cannot infer length from file"); } } else { fd = fileno(fh); if (fd < 0) { croak("mmap: file not open or does not have associated fileno"); } if (!len) { struct stat st; if (fstat(fd, &st) == -1) { croak("mmap: no len provided, fstat failed, unable to infer length"); } len = st.st_size; } } if (pagesize == 0) { pagesize = getpagesize(); } slop = (size_t) off % pagesize; addr = mmap(0, len + slop, prot, flags, fd, off - slop); if (addr == MAP_FAILED) { croak("mmap: mmap call failed: errno: %d errmsg: %s ", errno, strerror(errno)); } SvUPGRADE(var, SVt_PV); if (!(prot & PROT_WRITE)) SvREADONLY_on(var); /* would sv_usepvn() be cleaner/better/different? would still try to realloc... */ SvPVX(var) = (char *) addr + slop; SvCUR_set(var, len); SvLEN_set(var, slop); SvPOK_only(var); ST(0) = sv_2mortal(newSVnv((IV) addr)); SV * munmap(var) SV * var PROTOTYPE: $ CODE: ST(0) = &PL_sv_undef; /* XXX refrain from dumping core if this var wasnt previously mmap'd */ if(!SvOK(var)) { /* Detect if variable is undef */ croak("undef variable not unmappable"); return; } if(SvTYPE(var) != SVt_PV) { croak("variable is not a string"); return; } if (munmap((MMAP_RETTYPE) SvPVX(var) - SvLEN(var), SvCUR(var) + SvLEN(var)) == -1) { croak("munmap failed! errno %d %s\n", errno, strerror(errno)); return; } SvREADONLY_off(var); SvPVX(var) = 0; SvCUR_set(var, 0); SvLEN_set(var, 0); SvOK_off(var); ST(0) = &PL_sv_yes; void DESTROY(var) SV * var PROTOTYPE: $ CODE: /* XXX refrain from dumping core if this var wasnt previously mmap'd*/ if (munmap((MMAP_RETTYPE) SvPVX(var), SvCUR(var)) == -1) { croak("munmap failed! errno %d %s\n", errno, strerror(errno)); return; } SvREADONLY_off(var); SvPVX(var) = 0; SvCUR_set(var, 0); SvLEN_set(var, 0); SvOK_off(var); /* printf("destroy ran fine, thanks\n"); */ ST(0) = &PL_sv_yes; Sys-Mmap-0.16/README000644 000765 000024 00000006425 11423743331 014073 0ustar00toddstaff000000 000000 Mmap perl module, version alpha2 Copyright (c) 1996, Malcolm Beattie Copyright (c) 2002, Scott Walters This program is free software; you can redistribute it and/or modify it under the terms of either: a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" which comes with this kit. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the Artistic License for more details. You should have received a copy of the Artistic License with this kit, in the file named "Artistic". If not, you can get one from the Perl distribution. You should also have received a copy of the GNU General Public License, in the file named "Copying". If not, you can get one from the Perl distribution or else write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. DESCRIPTION The Mmap module lets you use mmap to map in a file as a perl variable rather than reading the file into dynamically allocated memory. Multiple programs may map the same file into memory, and immediately see changes by each other. Memory may be allocated not attached to a file, and shared with subprocesses. It depends on your operating system supporting UNIX or POSIX.1b mmap, of course. You may use the new OO interface, or the old map() and unmap() interface. If you use the old interface, you need to be careful. 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 PROT_WRITE and attempt to write to the file via the variable you need to be even more careful. One of the few ways in which you can safely write to the string in-place is by using substr as an lvalue and ensuring that the part of the string that you replace is exactly the same length. The new interface does not suffer from these problems. INSTALLATION Prerequisites are perl 5.002 and an operating system that has an implementation of mmap(2) with either the traditional or the POSIX.1b API. Perl 5.7.2 was tested with this release. Type perl Makefile.PL to write a personalised Makefile for your system. If your platform supports dynamic loading then just type make make test and provided you see "All tests successful" you can install it with make install If you need/want instead to make a statically linked perl which contains the module, then type make perl make test Documentation is included in pod format in Mmap.pm itself. The "make install" step will install an Mmap(3) man page or else you can use perldoc Mmap which will find the documentation whether it has been installed or if you run it from the Mmap distribution directory. BUGS You must explicitly use munmap() rather than letting perl itself clean up after you since perl 5.002 can't cope with variables whose string contents live in non-malloc'd memory. Malcolm Beattie 21 Jun 1996 Documentation updated to include information on OO interface 6/2002 Scott Walters Sys-Mmap-0.16/t/000750 000765 000024 00000000000 11623535563 013452 5ustar00toddstaff000000 000000 Sys-Mmap-0.16/t/mmap.t000644 000765 000024 00000003012 11623523660 014565 0ustar00toddstaff000000 000000 #! perl -T BEGIN { use strict; use warnings; use Test::More tests => 8; use_ok('Sys::Mmap'); } use FileHandle; $temp_file = "mmap.tmp"; my $temp_file_contents = "ABCD1234" x 1000; sysopen(FOO, $temp_file, O_WRONLY|O_CREAT|O_TRUNC) or die "$temp_file: $!\n"; print FOO $temp_file_contents; close FOO; my $foo; sysopen(FOO, $temp_file, O_RDONLY) or die "$temp_file: $!\n"; # Test negative offsets fail. is(eval { mmap($foo, 0, PROT_READ, MAP_SHARED, FOO, -100); 1}, undef, "Negative seek fails."); like($@, qr/^\Qmmap: Cannot operate on a negative offset (-100)\E/, "croaks when negative offset is passed in"); # Now map the file for real mmap($foo, 0, PROT_READ, MAP_SHARED, FOO); close FOO; is($foo, $temp_file_contents, "RO access to the file produces valid data"); munmap($foo); sysopen(FOO, $temp_file, O_RDWR) or die "$temp_file: $!\n"; mmap($foo, 0, PROT_READ|PROT_WRITE, MAP_SHARED, FOO); close FOO; substr($foo, 3, 1) = "Z"; substr($temp_file_contents, 3, 1) = "Z"; is($foo, $temp_file_contents, 'Foo can be altered in RW mode'); munmap($foo); sysopen(FOO, $temp_file, O_RDONLY) or die "$temp_file: $!\n"; my $bar = ; is($bar, $temp_file_contents, 'Altered foo reflects on disk'); { my $foo; my $file_size = -s $temp_file; open(my $fh, "<", $temp_file) or die; isa_ok($fh, 'GLOB'); mmap($foo, $file_size, &Sys::Mmap::PROT_READ, &Sys::Mmap::MAP_SHARED, $fh); is($foo, $temp_file_contents, 'Read $foo, when it comes from a FileHandle'); munmap($foo); } unlink($temp_file); Sys-Mmap-0.16/t/munmap_errors.t000644 000765 000024 00000002122 11623523660 016525 0ustar00toddstaff000000 000000 #!perl use strict; use warnings; use Test::More tests => 14; use Sys::Mmap; use Errno qw(EINVAL); { my $foo; eval {munmap($foo)}; like($@, qr/^undef variable not unmappable /, "munmap detects undef perl variables and fails"); } { my $foo = "234"; undef($foo); eval {munmap($foo)}; like($@, qr/^undef variable not unmappable /, "munmap detects undef perl variables and fails"); } { eval {munmap(undef)}; like($@, qr/^undef variable not unmappable /, "munmap detects undef perl variables and fails"); } SKIP: { skip "BSD kernels can't unmap a bad pointer like linux kernels can", 4 if($^O =~ m/bsd/i || $^O =~ m/darwin/i); foreach my $foo ("", "1234", "1.232", "abcdefg" ){ eval {munmap($foo)}; ok($! == EINVAL, "Unmapped strings die"); } } SKIP: { skip "BSD kernels can't unmap a bad pointer like linux kernels can", 7 if($^O =~ m/bsd/i || $^O =~ m/darwin/i); foreach my $foo (-1283843, -1, 0, 1, 2222131, 2.3451, -1213.12 ){ eval {munmap($foo)}; like($@, qr/^variable is not a string/, "munmap detects ints and floats and fails"); } }