blib/lib/IPC/Mmap/POSIX.pm

filter: Statementstmt 71.2% 67 / 94 Branchbran 33.9% 19 / 56 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 88.8% 16 / 18 Podpod 0.0% 0 / 5 totaltotal 56.1% 105 / 187 SCAR 47.8
CC
32
Cov
56%
new
97.6
close
12.0
oldDESTROY
12.0
CRAP 119.4
↑ Summary blib/lib/IPC/Mmap/Win32.pm »
1 #/**
2 # Concrete implementation of the IPC::Mmap class for OS's supporting
3 # a POSIX mmap().
4 # <p>
5 # Permission is granted to use this software under the same terms as Perl itself.
6 # Refer to the <a href='http://perldoc.perl.org/perlartistic.html'>Perl Artistic License</a>
7 # for details.
8 #
9 # @author D. Arnold
10 # @since 2006-05-01
11 # @self $self
12 #
13 # maintenance and modifications - Athanasios Douitsis aduitsis@cpan.org
14 #*/
15 package IPC::Mmap::POSIX;
16 #
17 # just bootstrap in the XS code
18 #
19 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use Fcntl qw(:flock :mode);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use IPC::SysV qw(IPC_PRIVATE IPC_CREAT);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use IPC::Semaphore;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use IPC::Mmap;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use FindBin qw($Bin $Script);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 use IPC::Mmap qw(MAP_ANON MAP_ANONYMOUS MAP_FILE MAP_PRIVATE MAP_SHARED
27 4 ▶ PROT_READ PROT_WRITE);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 4 ▶use base qw(IPC::Mmap);
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 #use constant MAP_ANON => constant('MAP_ANON', 0);
35 #use constant MAP_ANONYMOUS => constant('MAP_ANONYMOUS', 0);
36 #use constant MAP_FILE => constant('MAP_FILE', 0);
37 #use constant MAP_PRIVATE => constant('MAP_PRIVATE', 0);
38 #use constant MAP_SHARED => constant('MAP_SHARED', 0);
39 #use constant PROT_READ => constant('PROT_READ', 0);
40 #use constant PROT_WRITE => constant('PROT_WRITE', 0);
41  
42 4 our $VERSION = '0.21';
43 #/**
44 # Constructor. mmap()'s using POSIX mmap().
45 #
46 # @param $filename
47 # @param $length optional
48 # @param $protflags optional
49 # @param $mmapflags optional
50 #
51 # @return the IPC::Mmap::POSIX object on success; undef on failure
52 #*/
53 sub new {
54 8 ▶ my ($class, $file, $length, $prot, $mmap) = @_;
Subroutine new: called CC=22 SCAR=45.8
Pod undocumented
55  
56 8 my $fh;
57  
58 #the MAN_ANON case will be handled independentlY
59 8 ▶ if($mmap & MAP_ANON) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mmap & MAP_ANON()) 0 8 8
60 #make sure we weren't given something that is not a pathname
61 0 ▶ croak 'When using anonymous mmap, only a pathname is allowed as the first argument' unless (ref($file) eq '');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $file eq "" 0 0 0
62  
63 #if the file doesn't exist, just touch it
64 0 ▶ if(! -e $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 0 0 0
65 0 ▶ open(my $fd,'>',$file) or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fd, ">", $file or croak($!) 0 0 0
66 0 close $fd;
67 }
68 0 ▶ if(! -r $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $file) 0 0 0
69 0 croak "For anonymous mmap, you must provide an readable filename in order for the ftok(3) to return a valid unique id. Unfortunately $file doesn't seem to be readable. ";
70 }
71  
72 0 my $unique_id = IPC::SysV::ftok($file,1);
73
74 #create a brand new semaphore
75 0 ▶ my $sem = new IPC::Semaphore($unique_id, 1, 0666|IPC_CREAT) or croak "Cannot create semaphore:$!";
Branch: 1 branch
BranchTrueFalseTotal
my $sem = "IPC::Semaphore"->new($unique_id, 1, 438 | IPC_CREAT()) or croak("Cannot create semaphore:$!") 0 0 0
76 ####print STDERR "semaphore is ".Dumper($sem)." \n";
77  
78 #make sure its released
79 0 ▶ $sem->op(0,1,0) or croak "Cannot op(0,1,0) on sem";
Branch: 1 branch
BranchTrueFalseTotal
$sem->op(0, 1, 0) or croak("Cannot op(0,1,0) on sem") 0 0 0
80 #@@#warn "semaphore value is ",$sem->getval(0),"\n";
81  
82 0 my ($mapaddr, $maxlen, $slop) = _mmap_anon($length, $prot, $mmap);
83 0 ▶ croak "mmap() failed" unless defined($mapaddr);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mapaddr 0 0 0
84 0 my $self = {
85 _fh => $fh,
86 _file => $file,
87 _mmap => $mmap,
88 _access => $prot,
89 _addr => $mapaddr,
90 _maxlen => $maxlen,
91 _slop => $slop,
92 semaphore => $sem,
93 };
94  
95 0 return bless $self, $class;
96 }
97  
98  
99 8 ▶ croak 'No filename or filehandle provided.'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file or $mmap & MAP_ANON() 0 8 8
Condition33%1/3
defined $file or $mmap & MAP_ANON()
l!l&&r!l&&!r
800
MC/DC0%0/2
defined $file or $mmap & MAP_ANON()
defined $file $mmap & MAP_ANON()
Truth table33%1/3
A or B
A= defined $fileB= $mmap & MAP_ANON()
ABresult
1X1
011
000
100 unless defined($file) || ($mmap & MAP_ANON);
101  
102 8 ▶ croak 'No filename or filehandle provided.'
Branch: 1 branch
BranchTrueFalseTotal
if defined $file and ref $file and ref $file ne "GLOB" 0 8 8
Condition33%2/6
defined $file and ref $file and ref $file ne "GLOB"
!ll&&!rl&&r
800
defined $file and ref $file
!ll&&!rl&&r
080
MC/DC0%0/3
defined $file and ref $file and ref $file ne "GLOB"
defined $file ref $file ref $file ne "GLOB"
Truth table25%1/4
A and B and C
A= defined $fileB= ref $fileC= ref $file ne "GLOB"
ABCresult
0XX0
10X0
1100
1111
103 if defined($file) && (ref $file) && (ref $file ne 'GLOB');
104  
105 8 ▶ if (ref $file) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $file) { } 0 8 8
elsif (not $mmap & MAP_ANON()) { } 8 0 8
106 0 $fh = $file;
107 }
108 elsif (! ($mmap & MAP_ANON)) {
109 #
110 # specified a filename, we need to open (and maybe create) it
111 # NOTE: POSIX doesn't seem to like mmap'ing write-only files,
112 # so we'll cheat
113 #
114 8 ▶ my $flags = ($prot == PROT_READ) ? O_RDONLY : O_RDWR;
Branch: 1 branch
BranchTrueFalseTotal
$prot == PROT_READ() ? : 1 7 8
115 8 ▶ $flags |= O_CREAT
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 5 3 8
116 unless -e $file;
117 8 ▶ croak "Can't open $file: $!"
Branch: 1 branch
BranchTrueFalseTotal
unless sysopen $fh, $file, $flags 0 8 8
118 unless sysopen($fh, $file, $flags);
119 }
120  
121 8 my @filestats = stat $fh;
122 8 ▶ if ($filestats[7] < $length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filestats[7] < $length) 5 3 8
123 #
124 # if file not big enough, expand if its writable
125 # else throw error
126 #
127 5 ▶ croak "IPC::Mmap::new(): specified file too small"
Branch: 1 branch
BranchTrueFalseTotal
unless $prot & PROT_WRITE() 0 5 5
128 unless ($prot & PROT_WRITE);
129 #
130 # seek to end, then write NULs
131 # NOTE: we need to chunk this out!!!
132 #
133 5 my $tlen = $length - $filestats[7];
134 5 seek($fh, 0, 2);
135 5 syswrite($fh, "\0" x $tlen);
136 }
137 8 my ($mapaddr, $maxlen, $slop) = _mmap($length, $prot, $mmap, $fh);
138 8 ▶ croak "mmap() failed"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mapaddr 0 8 8
139 unless defined($mapaddr);
140 8 my $self = {
141 _fh => $fh,
142 _file => $file,
143 _mmap => $mmap,
144 _access => $prot,
145 _addr => $mapaddr,
146 _maxlen => $maxlen,
147 _slop => $slop,
148 };
149  
150 8 return bless $self, $class;
151 }
152  
153 sub DESTROY {
154 8 ▶ if(defined($_[0]->{semaphore})) {
Subroutine DESTROY: called CC=2 SCAR=10.5
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]{"semaphore"}) 0 8 8
155 0 print STDERR "destroying semaphore ".Dumper($_[0]->{semaphore})."\n";
156 0 $_[0]->{semaphore}->remove;
157 }
158 }
159  
160 #/**
161 # Locks the mmap'ed region. Implemented using flock()
162 # on the mmap()'ed file.
163 # <p>
164 # <b>NOTE:</b> This lock is <b><i>not</i></b> sufficient
165 # for multithreaded access control, but <i>may</i> be sufficient for
166 # multiprocess access control.
167 # <p>
168 # <i>Also note</i> that, due to flock() restrictions on some
169 # platforms, the type of lock is determined by the protection flags
170 # with which the mmap'ed region was created: if only PROT_READ,
171 # then shared access is used; otherwise, an exclusive lock is used.
172 #*/
173 sub lock {
174 6 ▶ my ($self, $offset, $len) = @_;
Subroutine lock: called CC=4 SCAR=15.9
Pod undocumented
175  
176 6 ▶ if(defined($self->{semaphore})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"semaphore"}) 0 6 6
177 #acquire
178 0 ▶ $self->{semaphore}->op(0,-1,0) or croak("Cannot op(0,-1,0) on sem");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"semaphore"}->op(0, -1, 0) or croak("Cannot op(0,-1,0) on sem") 0 0 0
179 #@@#warn "semaphore acquired";
180 0 return 1;
181 }
182
183 6 my $fh = $self->{_fh};
184 6 ▶ my $mmode = ($self->{_access} == PROT_READ) ? LOCK_SH : LOCK_EX;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_access'} == PROT_READ() ? : 1 5 6
185 6 return flock($fh, $mmode);
186 }
187  
188 #/**
189 # Unlocks the mmap'ed region. Implemented using flock()
190 # on the mmap()'ed file.
191 #*/
192 sub unlock {
193 5 ▶ my ($self, $offset, $len) = @_;
Subroutine unlock: called CC=3 SCAR=14.2
Pod undocumented
194  
195 5 ▶ if(defined($self->{semaphore})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"semaphore"}) 0 5 5
196 #release
197 0 ▶ $self->{semaphore}->op(0,1,0) or croak("Cannot op(0,1,0) on sem");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"semaphore"}->op(0, 1, 0) or croak("Cannot op(0,1,0) on sem") 0 0 0
198 #@@#warn "semaphore released";
199 0 return 1;
200 }
201  
202 5 my $fh = $self->{_fh};
203 5 return flock($fh, LOCK_UN);
204 }
205 #/**
206 # Unmap the mmap()ed region.
207 # <p>
208 # <b>CAUTION!!!</b> Use of this method is discouraged and
209 # deprecated. Unmapping from the file in one thread
210 # can cause segmentation in faults in other threads,
211 # so best practice is to just leave the mmap() in place
212 # and let process rundown clean things up.
213 #
214 # @deprecated
215 #*/
216 sub close {
217 0 ▶ my $self = shift;
Subroutine close: not called CC=3 SCAR=24.8
Pod undocumented
218 _munmap($self->{_addr}, $self->{_maxlen})
219 0 ▶ if $self->{_addr};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_addr"} 0 0 0
220 0 ▶ CORE::close $self->{_fh} if $self->{_fh};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_fh"} 0 0 0
221 }
222 #
223 # unmap and close the file
224 # NOTE: do we need a ref count for multithreaded environments ?
225 #
226 sub oldDESTROY {
227 0 ▶ my $self = shift;
Subroutine oldDESTROY: not called CC=3 SCAR=24.8
Pod undocumented
228 0 print STDERR "IPC::Mmap::DESTROY: addr is $self->{_addr} len $self->{_maxlen}\n";
229 _munmap($self->{_addr}, $self->{_maxlen})
230 0 ▶ if $self->{_addr};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_addr"} 0 0 0
231 0 ▶ CORE::close $self->{_fh} if $self->{_fh};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_fh"} 0 0 0
232 }
233  
234 4 1;
↑ Summary blib/lib/IPC/Mmap/Win32.pm »