blib/lib/IPC/Mmap.pm

filter: Statementstmt 93.3% 56 / 60 Branchbran 70.8% 17 / 24 Conditioncond 60.0% 3 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 78.2% 18 / 23 Podpod 0.0% 0 / 18 totaltotal 70.6% 94 / 133 SCAR 26.8
CC
14
Cov
85%
new
5.7
read
5.1
write
5.0
CRAP 14.6
« blib/lib/IPC/Mmap/Win32.pm ↑ Summary Mmap.xs »
1 #/**
2 # Provides a minimal interface to POSIX mmap(), and its
3 # Win32 equivalent. Abstract base class that is used by
4 # the IPC::Mmap::POSIX and IPC::Mmap::Win32 implementations.
5 # <p>
6 # Permission is granted to use this software under the same terms as Perl itself.
7 # Refer to the <a href='http://perldoc.perl.org/perlartistic.html'>Perl Artistic License</a>
8 # for details.
9 #
10 # @author D. Arnold
11 # @since 2006-05-01
12 # @self $self
13 # @exports MAP_SHARED permit the mmap'd area to be shared with other processes
14 # @exports MAP_PRIVATE do not permit the mmap'ed area to be shared with other processes
15 # @exports MAP_ANON do not use a backing file
16 # @exports MAP_ANONYMOUS same as MAP_ANON
17 # @exports MAP_FILE use a backing file for the memory mapped area
18 # @exports PROT_READ permit read access to the mmap'ed area
19 # @exports PROT_WRITE permit write access to the mmap'ed area
20 #*/
21 package IPC::Mmap;
22  
23 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use DynaLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 our @ISA = qw(Exporter DynaLoader);
27 4 our $VERSION = '0.22_01';
28  
29 4 bootstrap IPC::Mmap $VERSION;
30  
31 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 4 our @EXPORT = qw(MAP_ANON MAP_ANONYMOUS MAP_FILE MAP_PRIVATE MAP_SHARED
35 PROT_READ PROT_WRITE);
36  
37 4 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 4 4
38 0 require IPC::Mmap::Win32;
39 }
40 else {
41 4 require IPC::Mmap::POSIX;
42 }
43 #
44 # these are generated within the appropriate XS code;
45 # gruesome, perhaps, but gets the job done
46 #
47 16 ▶sub MAP_ANON { return constant('MAP_ANON', 0); }
Subroutine MAP_ANON: called CC=1 SCAR=0.0
Pod undocumented
48 0 ▶sub MAP_ANONYMOUS { return constant('MAP_ANONYMOUS', 0); }
Subroutine MAP_ANONYMOUS: not called CC=1 SCAR=6.9
Pod undocumented
49 7 ▶sub MAP_FILE { return constant('MAP_FILE', 0); }
Subroutine MAP_FILE: called CC=1 SCAR=0.0
Pod undocumented
50 0 ▶sub MAP_PRIVATE { return constant('MAP_PRIVATE', 0); }
Subroutine MAP_PRIVATE: not called CC=1 SCAR=6.9
Pod undocumented
51 7 ▶sub MAP_SHARED { return constant('MAP_SHARED', 0); }
Subroutine MAP_SHARED: called CC=1 SCAR=0.0
Pod undocumented
52 33 ▶sub PROT_READ { return constant('PROT_READ', 0); }
Subroutine PROT_READ: called CC=1 SCAR=0.0
Pod undocumented
53 22 ▶sub PROT_WRITE { return constant('PROT_WRITE', 0); }
Subroutine PROT_WRITE: called CC=1 SCAR=0.0
Pod undocumented
54  
55  
56 #/**
57 # Constructor. Maps the specified number of bytes of the specified file
58 # into the current process's address space. Read/write access protection
59 # (default is read-only), and mmap() control flags may be specified
60 # (default is MAP_SHARED). If no length is given, maps the file's current length.
61 # <p>
62 # The specified file will be created if needed, and openned in an
63 # access mode that is compatible with the specified access protection.
64 # If the size of the file is less than the specified length, and the access flags
65 # include write access, the file will be extended with NUL characters to the
66 # specified length.
67 # <p>
68 # <b>Note</b> that for Win32, the specified file is used as a <i>"namespace"</i>,
69 # rather than physical file, if an "anonymous" mmap() is requested.
70 # <p>
71 # On POSIX platforms, an anonymous, private shared memory region
72 # can be created <i>(to be inherited by any fork()'ed
73 # child processes)</i> by using a zero-length filename, and
74 # "private" (MAP_PRIVATE) mmap() flags.
75 # <p>
76 # On Win32 platforms, the default behavior is to create a "namespace", and use the
77 # Windows swap file for the backing file. However, by including MAP_FILE in the
78 # mmap() flags parameter, the specified file will be opened and/or created,
79 # and used for the backing file, rather than the system swap file.
80 #
81 # @param $filename name of file (or namespace) to be mapped
82 # @param $length optional number of bytes to be mapped
83 # @param $protflags optional read/write access flags
84 # @param $mmapflags optional mmap() control flags
85 #
86 # @return the IPC::Mmap object on success; undef on failure
87 #*/
88 sub new {
89 8 ▶ my ($class, $file, $length, $prot, $mmap) = @_;
Subroutine new: called CC=5 SCAR=17.5
Pod undocumented
90  
91 8 ▶ $length = 0 unless defined($length);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $length 0 8 8
92 8 ▶ $prot = PROT_READ unless defined($prot);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prot 0 8 8
93 8 ▶ $mmap = MAP_SHARED unless defined($mmap);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mmap 0 8 8
94 #
95 # we're just a factory for the platform-specific objects
96 #
97 8 ▶ return ($^O eq 'MSWin32') ?
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 8 8
98 IPC::Mmap::Win32->new($file, $length, $prot, $mmap) :
99 IPC::Mmap::POSIX->new($file, $length, $prot, $mmap);
100 }
101  
102 #/**
103 # Reads data from a specific area of the mmap()'ed file.
104 #
105 # @param $data scalar to receive the data
106 # @param $offset optional offset into mmap'ed area; default is zero
107 # @param $length optional length to read; default is from the offset to the end of the file
108 #
109 # @return the number of bytes actually read on success; undef on failure
110 #*/
111 sub read {
112 12 ▶ my $self = shift;
Subroutine read: called CC=5 SCAR=16.2
Pod undocumented
113  
114 croak "Invalid access on write-only region",
115 return undef
116 12 ▶ unless $self->{_access} & PROT_READ;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_access"} & PROT_READ() 1 11 12
117  
118 11 ▶ my $off = ($_[1] || 0) + $self->{_slop};
Condition50%1/2
$_[1] || 0
l!l
110
MC/DC0%0/1
$_[1] || 0
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
119 11 ▶ my $len = $_[2] || $self->{_maxlen};
Condition66%2/3
$_[2] || $self->{'_maxlen'}
l!l&&r!l&&!r
740
MC/DC0%0/2
$_[2] || $self->{'_maxlen'}
$_[2] $self->{'_maxlen'}
Truth table66%2/3
A || B
A= $_[2]B= $self->{'_maxlen'}
ABresult
1X1
011
000
120  
121 croak "read failed: offset exceeds region length",
122 return undef
123 11 ▶ if ($off >= $self->{_maxlen});
Branch: 1 branch
BranchTrueFalseTotal
if $off >= $self->{"_maxlen"} 2 9 11
124  
125 return mmap_read($self->{_addr}, $self->{_maxlen},
126 9 $off, $_[0], $len);
127 }
128  
129 #/**
130 # Write data to the mmap()'ed region.
131 # Writes the specified number of bytes of the specified scalar variable
132 # to the mmap'ed region starting at the specified offset. If not specified,
133 # offset defaults to zero, and length> defaults to the length of the scalar.
134 # If the specified length exceeds the available length of the mmap'ed region
135 # starting from the offset, only the available region length will be written.
136 #
137 # @param $data the data to be written
138 # @param $offset optional offset where the data should be written; default is zero
139 # @param $length optional length of the data to write; default is the length of the data
140 #
141 # @return on success, returns the actual number of bytes written; returns undef
142 # if the offset exceeds the length of the region
143 #*/
144 sub write {
145 10 ▶ my ($self, $var, $off, $len) = @_;
Subroutine write: called CC=5 SCAR=16.1
Pod undocumented
146  
147 croak "Invalid access on read-only region",
148 return undef
149 10 ▶ unless $self->{_access} & PROT_WRITE;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_access"} & PROT_WRITE() 1 9 10
150  
151 9 ▶ $off = 0 unless defined($off);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $off 0 9 9
152 9 $off += $self->{_slop};
153 9 ▶ $len = length($var) unless $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $len 7 2 9
154  
155 croak "write failed: offset exceeds region length",
156 return undef
157 9 ▶ if ($off >= $self->{_maxlen});
Branch: 1 branch
BranchTrueFalseTotal
if $off >= $self->{"_maxlen"} 1 8 9
158  
159 return mmap_write($self->{_addr}, $self->{_maxlen},
160 8 $off, $var, $len);
161 }
162 #/**
163 # Packs a list of values according to the specified pack string, and writes
164 # the binary result to the mmap'ed region at specified offset.
165 # If the offset plus the packed data length extends beyond the end of the
166 # region, only the available number of bytes will be written.
167 #
168 # @param $offset offset to write the packed data
169 # @param $packstr pack string to be applied to the data
170 # @param @values list of values to pack and write
171 #
172 # @return undef if $offset is beyond the end of the mmap'ed region; otherwise,
173 # the total number of bytes written
174 #*/
175 sub pack {
176 2 ▶ my $self = shift;
Subroutine pack: called CC=1 SCAR=0.0
Pod undocumented
177 2 my $off = shift;
178 2 my $packstr = shift;
179 2 return $self->write(CORE::pack($packstr, @_), $off);
180 }
181 #/**
182 # Read the specified number of bytes starting at the specified offset
183 # and unpack into Perl scalars using the specified pack() string.
184 #
185 # @param $offset offset to start reading from
186 # @param $length number of bytes to read
187 # @param $packstr pack string to apply to the read data
188 #
189 # @return on success, the list of unpacked values; undef on failure
190 #*/
191 sub unpack {
192 2 ▶ my ($self, $off, $length, $packstr) = @_;
Subroutine unpack: called CC=2 SCAR=7.0
Pod undocumented
193 2 my $val;
194 return undef
195 2 ▶ unless $self->read($val, $off, $length);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->read($val, $off, $length) 0 2 2
196 2 return CORE::unpack($packstr, $val);
197 }
198  
199 #/**
200 # Locks the mmap'ed region. Pure virtual function to be implemented
201 # in the OS-specific implementation subclass.
202 #*/
203 ▶sub lock { }
Subroutine lock: not called CC=1 SCAR=0.0
Pod undocumented
204  
205 #/**
206 # Unlock the mmap'ed region. Pure virtual function
207 # to be implemented in the OS-specific implementation
208 # subclass.
209 #
210 #*/
211 ▶sub unlock { }
Subroutine unlock: not called CC=1 SCAR=0.0
Pod undocumented
212 #/**
213 # Get the filename (or namespace on Win32) for the mmap'ed file.
214 #
215 # @return the mmap'ed filename.
216 #*/
217 1 ▶sub getFilename { return $_[0]->{_file}; }
Subroutine getFilename: called CC=1 SCAR=0.0
Pod undocumented
218  
219 #/**
220 # Get the filehandle for the mmap'ed file. If MAP_ANON
221 # was specified for POSIX platforms, or MAP_FILE was <b>not</b> specified
222 # on Win32 platforms, returns undef.
223 #
224 # @return the file handle used for the mmap'ed file.
225 #*/
226 0 ▶sub getFileHandle { return $_[0]->{_fh}; }
Subroutine getFileHandle: not called CC=1 SCAR=6.9
Pod undocumented
227 #/**
228 # Get the length of the mmap'ed region
229 #
230 # @return the length of the mmap()ed region.
231 #*/
232 2 ▶sub getLength { return $_[0]->{_maxlen}; }
Subroutine getLength: called CC=1 SCAR=0.0
Pod undocumented
233 #/**
234 # Get the base address to which the mmap'ed region was mapped.
235 #
236 # @return the address of the mmap()ed region.
237 #*/
238 1 ▶sub getAddress { return $_[0]->{_addr}; }
Subroutine getAddress: called CC=1 SCAR=0.0
Pod undocumented
239  
240  
241 4 1;
« blib/lib/IPC/Mmap/Win32.pm ↑ Summary Mmap.xs »