line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package IPC::Shm::Make; |
2
|
6
|
|
|
6
|
|
31
|
use warnings; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
164
|
|
3
|
6
|
|
|
6
|
|
31
|
use strict; |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
164
|
|
4
|
6
|
|
|
6
|
|
30
|
use Carp; |
|
6
|
|
|
|
|
9
|
|
|
6
|
|
|
|
|
377
|
|
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# Copyright (c) 2014 by Kevin Cody-Little |
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::Make |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 SYNOPSIS |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
This module is part of the IPC::Shm::Tied::* implementations. You should |
20
|
|
|
|
|
|
|
definitely not be using it directly. |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 FUNCTIONS |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head2 makeshm( $scalar_variable_reference ); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
If the referenced variable contains a plain scalar, nothing is done. |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
If the referenced variable itself contains a reference, the target of that |
29
|
|
|
|
|
|
|
inner reference is tied into shared memory with its contents preserved. |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
The inner reference is then replaced with a stand-in containing an |
32
|
|
|
|
|
|
|
identifier, which can be used to recover the original (now tied) target. |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head2 getback( $standin ); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
Given the standin left by makeshm, returns a reference to the original |
37
|
|
|
|
|
|
|
(now tied into shared memory) data. It's up to the calling program to |
38
|
|
|
|
|
|
|
know whether it expects a scalar, array, or hash reference. |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head2 getback_discard( $standin ); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
The same as C but also decrements the reference counter. |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=cut |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
############################################################################### |
47
|
|
|
|
|
|
|
# library dependencies |
48
|
|
|
|
|
|
|
|
49
|
6
|
|
|
6
|
|
27
|
use base 'Exporter'; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
621
|
|
50
|
|
|
|
|
|
|
our @EXPORT = qw( makeshm getback ); |
51
|
|
|
|
|
|
|
|
52
|
6
|
|
|
6
|
|
8639
|
use Data::Dumper; |
|
6
|
|
|
|
|
80367
|
|
|
6
|
|
|
|
|
4538
|
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
############################################################################### |
55
|
|
|
|
|
|
|
# lower level migration handlers |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub _makeshm_scalar { |
58
|
0
|
|
|
0
|
|
0
|
my ( $ref ) = @_; |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
0
|
my $tmp = $$ref; |
61
|
0
|
|
|
|
|
0
|
my $obj = tie $$ref, 'IPC::Shm::Tied'; |
62
|
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
0
|
$obj->writelock; |
64
|
0
|
|
|
|
|
0
|
$$ref = $tmp; |
65
|
0
|
|
|
|
|
0
|
$obj->incref; |
66
|
0
|
|
|
|
|
0
|
$obj->unlock; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# $obj->reftype( 'SCALAR' ); |
69
|
0
|
|
|
|
|
0
|
$obj->tiedref( $ref ); |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
0
|
return $obj->standin; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub _makeshm_array { |
75
|
0
|
|
|
0
|
|
0
|
my ( $ref ) = @_; |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
0
|
my @tmp = @$ref; |
78
|
0
|
|
|
|
|
0
|
my $obj = tie @$ref, 'IPC::Shm::Tied'; |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
0
|
$obj->writelock; |
81
|
0
|
|
|
|
|
0
|
@$ref = @tmp; |
82
|
0
|
|
|
|
|
0
|
$obj->incref; |
83
|
0
|
|
|
|
|
0
|
$obj->unlock; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# $obj->reftype( 'ARRAY' ); |
86
|
0
|
|
|
|
|
0
|
$obj->tiedref( $ref ); |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
0
|
return $obj->standin; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub _makeshm_hash { |
92
|
4
|
|
|
4
|
|
8
|
my ( $ref ) = @_; |
93
|
|
|
|
|
|
|
|
94
|
4
|
|
|
|
|
17
|
my %tmp = %$ref; |
95
|
4
|
|
|
|
|
34
|
my $obj = tie %$ref, 'IPC::Shm::Tied'; |
96
|
|
|
|
|
|
|
|
97
|
4
|
|
|
|
|
17
|
$obj->writelock; |
98
|
4
|
|
|
|
|
73
|
%$ref = %tmp; |
99
|
4
|
|
|
|
|
32
|
$obj->incref; |
100
|
4
|
|
|
|
|
26
|
$obj->unlock; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# $obj->reftype( 'HASH' ); |
103
|
4
|
|
|
|
|
66
|
$obj->tiedref( $ref ); |
104
|
|
|
|
|
|
|
|
105
|
4
|
|
|
|
|
25
|
return $obj->standin; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
############################################################################### |
110
|
|
|
|
|
|
|
# migrate a variable into anonymous shared memory |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub makeshm { |
113
|
42
|
|
|
42
|
1
|
59
|
my ( $valueref ) = @_; |
114
|
|
|
|
|
|
|
|
115
|
42
|
|
|
|
|
76
|
my $reftype = ref( $$valueref ); |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
# pass plain scalars as-is |
118
|
42
|
100
|
|
|
|
163
|
return unless $reftype; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# save a few dereferences |
121
|
5
|
|
|
|
|
11
|
my $refdata = $$valueref; |
122
|
|
|
|
|
|
|
|
123
|
5
|
100
|
|
|
|
31
|
if ( $reftype eq 'SCALAR' ) { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
124
|
1
|
50
|
|
|
|
4
|
if ( my $obj = tied $$refdata ) { |
125
|
1
|
50
|
|
|
|
19
|
confess "Cannot store tied scalars in shared memory" |
126
|
|
|
|
|
|
|
unless $obj->isa( 'IPC::Shm::Tied' ); |
127
|
1
|
|
|
|
|
9
|
$obj->incref; |
128
|
1
|
|
|
|
|
16
|
$$valueref = $obj->standin; |
129
|
|
|
|
|
|
|
} else { |
130
|
0
|
|
|
|
|
0
|
$$valueref = _makeshm_scalar( $refdata ); |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
elsif ( $reftype eq 'ARRAY' ) { |
135
|
0
|
0
|
|
|
|
0
|
if ( my $obj = tied @$refdata ) { |
136
|
0
|
0
|
|
|
|
0
|
confess "Cannot store tied arrays in shared memory" |
137
|
|
|
|
|
|
|
unless $obj->isa( 'IPC::Shm::Tied' ); |
138
|
0
|
|
|
|
|
0
|
$obj->incref; |
139
|
0
|
|
|
|
|
0
|
$$valueref = $obj->standin; |
140
|
|
|
|
|
|
|
} else { |
141
|
0
|
|
|
|
|
0
|
$$valueref = _makeshm_array( $refdata ); |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
elsif ( $reftype eq 'HASH' ) { |
146
|
4
|
50
|
|
|
|
16
|
if ( my $obj = tied %$refdata ) { |
147
|
0
|
0
|
|
|
|
0
|
confess "Cannot store tied hashes in shared memory" |
148
|
|
|
|
|
|
|
unless $obj->isa( 'IPC::Shm::Tied' ); |
149
|
0
|
|
|
|
|
0
|
$obj->incref; |
150
|
0
|
|
|
|
|
0
|
$$valueref = $obj->standin; |
151
|
|
|
|
|
|
|
} else { |
152
|
4
|
|
|
|
|
18
|
$$valueref = _makeshm_hash( $refdata ); |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
else { |
157
|
0
|
|
|
|
|
0
|
confess "Incompatible reference type $reftype"; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
############################################################################### |
164
|
|
|
|
|
|
|
# get back the (now shared) referenced datum |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
sub getback { |
167
|
13
|
|
|
13
|
1
|
19
|
my ( $standin ) = @_; |
168
|
|
|
|
|
|
|
|
169
|
13
|
|
|
|
|
56
|
return IPC::Shm::Tied->standin_tiedref( $standin ); |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
sub getback_discard { |
173
|
0
|
|
|
0
|
1
|
|
my ( $standin ) = @_; |
174
|
|
|
|
|
|
|
|
175
|
0
|
|
|
|
|
|
my $this = IPC::Shm::Tied->standin_discard( $standin ); |
176
|
|
|
|
|
|
|
|
177
|
0
|
|
|
|
|
|
return $this->tiedref; |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
############################################################################### |
182
|
|
|
|
|
|
|
############################################################################### |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head1 AUTHOR |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Kevin Cody-Little |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=cut |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
1; |