Shm/Segment.pm

filter: Statementstmt 63.1% 77 / 122 Branchbran 35.9% 23 / 64 Conditioncond 42.8% 3 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 87.5% 14 / 16 Podpod 100% 11 / 11 totaltotal 57.5% 129 / 224 CC 36 SCAR 25.0
CC
36
Cov
53%
vartype
42.0
standin_type
19.6
named
12.0
CRAP 12.2
« Shm/Make.pm ↑ Summary Shm/Tied.pm »
1 package IPC::Shm::Segment;
2 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 #
6 # Copyright (c) 2014 by Kevin Cody-Little <kcody@cpan.org>
7 #
8 # This code may be modified or redistributed under the terms
9 # of either the Artistic or GNU General Public licenses, at
10 # the modifier or redistributor's discretion.
11 #
12  
13 =head1 NAME
14  
15 IPC::Shm::Segment
16  
17 =head1 SYNOPSIS
18  
19 This class is part of the IPC::Shm implementation. You should probably
20 not be using it directly.
21  
22 =head1 CONSTRUCTORS
23  
24 =head2 $class->named( $varname )
25  
26 Attach to a named variable's segment, creating if necessary. The contents
27 of the varname string are just how the variable is typed in perl code.
28  
29 =head2 $class->anonymous
30  
31 Create a new anonymous segment.
32  
33 =head2 $class->anonymous( $cookie )
34  
35 Attach to an existing anonymous segment using a cookie value.
36 Those cookies might be retrieved with the varanon method,
37 or might be stored in a standin. See below.
38  
39 =head1 ATTRIBUTES
40  
41 =head2 $this->varname
42  
43 Returns the variable's name, if any.
44  
45 =head2 $this->varname( $varname );
46  
47 Sets the variable's name.
48  
49 =head2 $this->varanon
50  
51 Returns the anonymous variable identifier cookie, if any.
52  
53 =head2 $this->varanon( $cookie )
54  
55 Sets the anonymous variable identifier cookie.
56  
57 =head2 $this->vartype
58  
59 Returns 'HASH', 'ARRAY', or 'SCALAR'.
60  
61 =head2 $this->vartype( $vartype )
62  
63 Stores the variable type. Only meaningful for anonymous segments.
64  
65 =head2 $this->varid
66  
67 Retrieves a human-redable string identifying the variable.
68  
69 =head1 OVERRIDDES
70  
71 =head2 $this->remove
72  
73 Overridden from IPC::Shm::Simple to remove table entries for named variables.
74  
75 =head2 $this->DETACH
76  
77 Called by IPC::Shm::Simple when the last in-process instance
78 is being DESTROYed.
79  
80 =head1 STAND-IN REFERENCES
81  
82 =head2 $this->standin
83  
84 This is a shared memory analogue of a reference. It is stored in the
85 shared memory variable that holds the reference.
86  
87 Returns a reference to an anonymous hash containing suitable identifiers.
88  
89 =head2 $class->standin_type( $standin )
90  
91 Returns the variable type that the standin points to.
92  
93 =head2 $class->standin_shmid( $standin )
94  
95 Returns the shmid where the standin points to.
96  
97 =head2 $class->standin_restand( $standin )
98  
99 Returns the original object that generated the standin, or
100 an exactly equal copy of that object.
101  
102 =head2 $class->standin_discard( $standin )
103  
104 Indicates that the standin reference is going away.
105  
106 Returns the original object as C<standin_restand>.
107  
108 =cut
109  
110 ###############################################################################
111 # library dependencies
112  
113 6 ▶use base 'IPC::Shm::Simple';
Subroutine BEGIN: called CC=1 SCAR=0.0
114  
115 6 ▶use Digest::SHA1 qw( sha1_hex );
Subroutine BEGIN: called CC=1 SCAR=0.0
116  
117  
118 ###############################################################################
119 # package variables
120  
121 6 my $IPCKEY = 0xdeadbeef;
122  
123 6 our %Attrib = (
124 varname => undef,
125 varanon => undef
126 );
127  
128  
129 ###############################################################################
130 ###############################################################################
131  
132 ###############################################################################
133 # get the segment for a variable (by symbol), creating if needed
134  
135 25 ▶sub named {
Subroutine named: called CC=7 SCAR=24.9
Pod documented
136 25 my ( $class, $sym ) = @_;
137 25 my ( $rv );
138  
139 25 ▶ unless ( $sym ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sym) 0 25 25
140 0 carp __PACKAGE__ . ' cannot cope with a null symbol name';
141 0 return;
142 }
143  
144 25 ▶ if ( $sym eq '%IPC::Shm::NAMEVARS' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sym eq '%IPC::Shm::NAMEVARS') { } 6 19 25
elsif (my $shmid = $IPC::Shm::NAMEVARS{$sym}) { } 0 19 19
145 6 ▶ unless ( $rv = $class->bind( $IPCKEY ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rv = $class->bind($IPCKEY)) 0 6 6
146 0 carp "shmbind failed: $!";
147 0 return;
148 }
149 }
150  
151 elsif ( my $shmid = $IPC::Shm::NAMEVARS{$sym} ) {
152 0 ▶ unless ( $rv = $class->shmat( $shmid ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rv = $class->shmat($shmid)) 0 0 0
153 0 carp "shmattach failed: $!";
154 0 return;
155 }
156 }
157  
158 else {
159 19 ▶ unless ( $rv = $class->create ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rv = $class->create) 0 19 19
160 0 carp "shmcreate failed: $!";
161 0 return;
162 }
163 19 $rv->incref;
164 19 $rv->unlock;
165 19 $IPC::Shm::NAMEVARS{$sym} = $rv->shmid;
166 }
167  
168 25 $rv->varname( $sym );
169  
170 25 return $rv;
171 }
172  
173  
174 ###############################################################################
175 # attach to an anonymous segment by cookie, or create a new one
176  
177 7 ▶sub anonymous {
Subroutine anonymous: called CC=5 SCAR=22.4
Pod documented
178 7 my ( $class, $aname ) = @_;
179 7 my ( $rv, $shmid );
180  
181 7 ▶ if ( defined $aname ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $aname) { } 0 7 7
182  
183 0 ▶ unless ( $shmid = $IPC::Shm::ANONVARS{$aname} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($shmid = $IPC::Shm::ANONVARS{$aname}) 0 0 0
184 0 carp "no such anonymous segment $aname";
185 0 return;
186 }
187  
188 0 ▶ unless ( $rv = $class->shmat( $shmid ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rv = $class->shmat($shmid)) 0 0 0
189 0 carp "failed to attach to shmid $shmid: $!";
190 0 return;
191 }
192  
193 }
194  
195 else {
196 7 ▶ unless ( $rv = $class->create ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rv = $class->create) 0 7 7
197 0 carp "shmcreate failed: $!";
198 0 return;
199 }
200  
201 7 $rv->unlock;
202 7 $aname = sha1_hex( rand( 10000 ) . ' ' . $$ );
203 7 $IPC::Shm::ANONVARS{$aname} = $rv->shmid;
204  
205 }
206  
207 7 $rv->varanon( $aname );
208  
209 7 return $rv;
210 }
211  
212  
213 ###############################################################################
214 ###############################################################################
215  
216 ###############################################################################
217 # produce a human-readable identifier for the variable
218  
219 0 ▶sub varid {
Subroutine varid: not called CC=3 SCAR=24.8
Pod documented
220 0 my ( $this ) = @_;
221  
222 0 ▶ if ( my $vname = $this->varname ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $vname = $this->varname) 0 0 0
223 0 return 'NAME=' . $vname;
224 }
225  
226 0 ▶ if ( my $vanon = $this->varanon ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $vanon = $this->varanon) 0 0 0
227 0 return 'ANON=' . $vanon;
228 }
229  
230 0 return "UNKNOWN!";
231 }
232  
233 ###############################################################################
234 # determine the variable type based on its name or cookie
235  
236 0 ▶sub vartype {
Subroutine vartype: not called CC=6 SCAR=37.4
Pod documented
237 0 my ( $this ) = @_;
238  
239 0 ▶ if ( my $vanon = $this->varanon ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $vanon = $this->varanon) 0 0 0
240 0 ▶ return $IPC::Shm::ANONTYPE{$vanon} || 'INVALID';
Condition0%0/2
$IPC::Shm::ANONTYPE{$vanon} || 'INVALID'
l!l
00
MC/DC0%0/1
$IPC::Shm::ANONTYPE{$vanon} || 'INVALID'
$IPC::Shm::ANONTYPE{$vanon}
Truth table0%0/2
A
A= $IPC::Shm::ANONTYPE{$vanon}
Aresult
11
00
241 }
242  
243 0 my $vname = $this->varname;
244  
245 0 ▶ return 'HASH' if $vname =~ /^%/;
Branch: 1 branch
BranchTrueFalseTotal
if $vname =~ /^%/ 0 0 0
246 0 ▶ return 'ARRAY' if $vname =~ /^@/;
Branch: 1 branch
BranchTrueFalseTotal
if $vname =~ /^@/ 0 0 0
247 0 ▶ return 'SCALAR' if $vname =~ /^\$/;
Branch: 1 branch
BranchTrueFalseTotal
if $vname =~ /^\$/ 0 0 0
248  
249 0 return 'INVALID';
250 }
251  
252  
253 ###############################################################################
254 # deliberate removal override
255  
256 12 ▶sub remove {
Subroutine remove: called CC=2 SCAR=6.9
Pod documented
257 12 my ( $this ) = @_;
258  
259 12 ▶ if ( my $vname = $this->varname ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $vname = $this->varname) 5 7 12
260 5 delete $IPC::Shm::NAMEVARS{$vname};
261 5 $this->decref;
262 5 $this->CLEAR;
263 }
264  
265 12 return $this->SUPER::remove();
266 }
267  
268  
269 ###############################################################################
270 # disconnect-time cleanups
271  
272 10 ▶sub DETACH {
Subroutine DETACH: called CC=4 SCAR=13.9
Pod documented
273 10 my ( $this ) = @_;
274  
275 10 ▶ unless ( $this->nrefs or $this->varname ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->nrefs or $this->varname) 3 7 10
Condition66%2/3
$this->nrefs or $this->varname
l!l&&r!l&&!r
703
MC/DC50%1/2
$this->nrefs or $this->varname
$this->nrefs $this->varname
Truth table66%2/3
A or B
A= $this->nrefsB= $this->varname
ABresult
1X1
011
000
276  
277 3 my $vanon = $this->varanon;
278  
279 3 $this->writelock;
280  
281 3 ▶ if ( $this->nconns == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->nconns == 1) 3 0 3
282  
283 3 $this->CLEAR;
284 3 $this->SUPER::remove;
285  
286 3 delete $IPC::Shm::ANONVARS{$vanon};
287 3 delete $IPC::Shm::ANONTYPE{$vanon};
288  
289 }
290  
291 }
292  
293 10 $this->SUPER::DETACH();
294  
295 }
296  
297  
298 ###############################################################################
299 ###############################################################################
300  
301 ###############################################################################
302 # generate a stand-in hashref containing one identifier or another
303  
304 6 ▶sub standin {
Subroutine standin: called CC=3 SCAR=14.2
Pod documented
305 6 my ( $this ) = @_;
306  
307 6 ▶ if ( my $vname = $this->varname ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $vname = $this->varname) { } 0 6 6
elsif (my $vanon = $this->varanon) { } 6 0 6
308 0 return { varname => $vname };
309 }
310  
311 elsif ( my $vanon = $this->varanon ) {
312 6 return { varanon => $vanon };
313 }
314  
315 else {
316 0 carp __PACKAGE__ . ' object has no identifier';
317 0 return;
318 }
319  
320 }
321  
322  
323 ###############################################################################
324 # determine the standin variable type based on its name or cookie
325  
326 2 ▶sub standin_type {
Subroutine standin_type: called CC=6 SCAR=29.7
Pod documented
327 2 my ( $callclass, $standin ) = @_;
328  
329 2 ▶ if ( my $vanon = $standin->{varanon} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $vanon = $standin->{"varanon"}) 2 0 2
330 2 ▶ return $IPC::Shm::ANONTYPE{$vanon} || 'INVALID';
Condition50%1/2
$IPC::Shm::ANONTYPE{$vanon} || 'INVALID'
l!l
20
MC/DC0%0/1
$IPC::Shm::ANONTYPE{$vanon} || 'INVALID'
$IPC::Shm::ANONTYPE{$vanon}
Truth table50%1/2
A
A= $IPC::Shm::ANONTYPE{$vanon}
Aresult
11
00
331 }
332  
333 0 my $vname = $standin->{varname};
334  
335 0 ▶ return 'HASH' if $vname =~ /^%/;
Branch: 1 branch
BranchTrueFalseTotal
if $vname =~ /^%/ 0 0 0
336 0 ▶ return 'ARRAY' if $vname =~ /^@/;
Branch: 1 branch
BranchTrueFalseTotal
if $vname =~ /^@/ 0 0 0
337 0 ▶ return 'SCALAR' if $vname =~ /^\$/;
Branch: 1 branch
BranchTrueFalseTotal
if $vname =~ /^\$/ 0 0 0
338  
339 0 return 'INVALID';
340 }
341  
342  
343 ###############################################################################
344 # get back the shared memory id given a standin from above
345  
346 14 ▶sub standin_shmid {
Subroutine standin_shmid: called CC=3 SCAR=12.7
Pod documented
347 14 my ( $callclass, $standin ) = @_;
348  
349 14 ▶ if ( my $vname = $standin->{varname} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $vname = $standin->{"varname"}) 0 14 14
350 0 return $IPC::Shm::NAMEVARS{$vname};
351 }
352  
353 14 ▶ if ( my $vanon = $standin->{varanon} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $vanon = $standin->{"varanon"}) 14 0 14
354 14 return $IPC::Shm::ANONVARS{$vanon};
355 }
356  
357 0 return 0;
358 }
359  
360  
361 ###############################################################################
362 # get back the object given a standin from above
363  
364 2 ▶sub standin_restand {
Subroutine standin_restand: called CC=5 SCAR=18.5
Pod documented
365 2 my ( $callclass, $standin ) = @_;
366  
367 2 my $shmid = $callclass->standin_shmid( $standin );
368  
369 2 ▶ unless ( $shmid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($shmid) 0 2 2
370 0 carp "could not get shmid for standin";
371 0 return;
372 }
373  
374 2 my $class = 'IPC::Shm::Tied::' . $callclass->standin_type( $standin );
375  
376 2 my $rv = $class->shmat( $shmid );
377  
378 2 ▶ unless ( $rv ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rv) 0 2 2
379 0 carp "restand_obj shmat failed: $!\n";
380 0 return;
381 }
382  
383 2 ▶ $rv->varname( $standin->{varname} ) if $standin->{varname};
Branch: 1 branch
BranchTrueFalseTotal
if $standin->{"varname"} 0 2 2
384 2 ▶ $rv->varanon( $standin->{varanon} ) if $standin->{varanon};
Branch: 1 branch
BranchTrueFalseTotal
if $standin->{"varanon"} 2 0 2
385  
386 2 return $rv;
387 }
388  
389  
390 ###############################################################################
391 # indicate a standin is being thrown away, and return the object
392  
393 2 ▶sub standin_discard {
Subroutine standin_discard: called CC=2 SCAR=7.0
Pod documented
394 2 my ( $callclass, $standin ) = @_;
395  
396 2 ▶ my $rv = $callclass->standin_restand( $standin )
Branch: 1 branch
BranchTrueFalseTotal
my $rv = $callclass->standin_restand($standin) or return 2 0 2
397 or return;
398  
399 2 $rv->decref;
400  
401 2 return $rv;
402 }
403  
404  
405 ###############################################################################
406 ###############################################################################
407  
408 =head1 AUTHOR
409  
410 Kevin Cody-Little <kcody@cpan.org>
411  
412 =cut
413  
414 6 1;
« Shm/Make.pm ↑ Summary Shm/Tied.pm »