blib/lib/IPC/Shareable/SharedMem.pm

filter: Statementstmt 93.1% 136 / 146 Branchbran 75.7% 50 / 66 Conditioncond 56.6% 17 / 30 MC/DCmcdc 50.0% 9 / 18 Subroutinesub 100% 23 / 23 Podpod 100% 15 / 15 totaltotal 83.8% 250 / 298 CC 47 SCAR 29.0
CC
47
Cov
84%
stat
66.4
new
12.0
remove
4.2
CRAP 18.1
« blib/lib/IPC/Shareable.pm ↑ Summary Shareable.xs »
1 package IPC::Shareable::SharedMem;
2  
3 90 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 90 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 90 ▶use Carp qw(carp croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 90 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 90 ▶use Errno qw(EEXIST EPERM);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 90 ▶use IPC::SysV qw(IPC_RMID IPC_STAT);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 90 our $VERSION = '1.19';
12  
13 90 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 90 DEFAULT_SEG_SIZE => 1024,
15 DEFAULT_SEG_FLAGS => 0000,
16 DEFAULT_SEG_MODE => 0666,
17 90 };
18  
19 {
20 90 package IPC::Shareable::SharedMem::stat;
21  
22 90 ▶ use Class::Struct qw(struct);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 90 struct 'IPC::Shareable::SharedMem::stat' => [
25 uid => '$',
26 gid => '$',
27 cuid => '$',
28 cgid => '$',
29 mode => '$',
30 segsz => '$',
31 lpid => '$',
32 cpid => '$',
33 nattch => '$',
34 atime => '$',
35 dtime => '$',
36 ctime => '$',
37 ];
38 }
39  
40 737 ▶sub new {
Subroutine new: called CC=12 SCAR=24.9
Pod documented
41 737 my ($class, %params) = @_;
42  
43 737 my $self = bless {}, $class;
44  
45 737 ▶ if (defined $params{key} && $params{key} =~ /^0x[0-9a-fA-F]+$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"key"} and $params{"key"} =~ /^0x[0-9a-fA-F]+$/i) 1 736 737
Condition100%3/3
defined $params{"key"} and $params{"key"} =~ /^0x[0-9a-fA-F]+$/i
!ll&&!rl&&r
17351
MC/DC100%2/2
defined $params{"key"} and $params{"key"} =~ /^0x[0-9a-fA-F]+$/i
defined $params{"key"} $params{"key"} =~ /^0x[0-9a-fA-F]+$/i
Truth table100%3/3
A and B
A= defined $params{"key"}B= $params{"key"} =~ /^0x[0-9a-fA-F]+$/i
ABresult
0X0
100
111
46 1 $params{key} = hex($params{key});
47 }
48  
49 737 ▶ if (! defined $params{key} || $params{key} !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{"key"} and $params{"key"} =~ /^\d+$/) 2 735 737
Condition100%3/3
defined $params{"key"} and $params{"key"} =~ /^\d+$/
!ll&&!rl&&r
11735
MC/DC100%2/2
defined $params{"key"} and $params{"key"} =~ /^\d+$/
defined $params{"key"} $params{"key"} =~ /^\d+$/
Truth table100%3/3
A and B
A= defined $params{"key"}B= $params{"key"} =~ /^\d+$/
ABresult
0X0
100
111
50 2 croak "new() requires a 'key' parameter with an integer value";
51 }
52  
53 735 $self->key($params{key});
54 735 $self->key_hex($self->key);
55  
56 735 ▶ $self->size($params{size} || DEFAULT_SEG_SIZE);
Condition100%2/2
$params{'size'} || 1024
l!l
72213
MC/DC100%1/1
$params{'size'} || 1024
$params{'size'}
Truth table100%2/2
A
A= $params{'size'}
Aresult
11
00
57  
58 734 ▶ $self->mode($params{mode} || DEFAULT_SEG_MODE);
Condition100%2/2
$params{'mode'} || 438
l!l
584150
MC/DC100%1/1
$params{'mode'} || 438
$params{'mode'}
Truth table100%2/2
A
A= $params{'mode'}
Aresult
11
00
59 734 ▶ $self->flags(($params{flags} || DEFAULT_SEG_FLAGS) | $self->mode);
Condition100%2/2
$params{'flags'} || 0
l!l
589145
MC/DC100%1/1
$params{'flags'} || 0
$params{'flags'}
Truth table100%2/2
A
A= $params{'flags'}
Aresult
11
00
60  
61 734 $self->type($params{type});
62  
63 734 my $id = shmget($self->key, $self->size, $self->flags);
64  
65 734 ▶ defined $id or do {
Branch: 1 branch
BranchTrueFalseTotal
defined $id or do { my $key = $self->key_hex; if ($!) { if ($!{"EEXIST"} or $!{"EPERM"}) { croak("\nERROR: IPC::Shareable::SharedMem: shmget ${key}: $!\n\n" . 'Are you using exclusive, but trying to create multiple ' . "instances?\n\n"); } ; return undef; } } 724 10 734
66 10 my $key = $self->key_hex;
67  
68 10 ▶ if ($!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!) 10 0 10
69 10 ▶ if ($!{EEXIST} || $!{EPERM}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!{"EEXIST"} or $!{"EPERM"}) 6 4 10
Condition66%2/3
$!{"EEXIST"} or $!{"EPERM"}
l!l&&r!l&&!r
604
MC/DC50%1/2
$!{"EEXIST"} or $!{"EPERM"}
$!{"EEXIST"} $!{"EPERM"}
Truth table66%2/3
A or B
A= $!{"EEXIST"}B= $!{"EPERM"}
ABresult
1X1
011
000
70 6 croak "\nERROR: IPC::Shareable::SharedMem: shmget $key: $!\n\n" .
71 "Are you using exclusive, but trying to create multiple " .
72 "instances?\n\n";
73 }
74  
75 4 return undef;
76 }
77 };
78  
79 724 $self->id($id);
80  
81 724 return $self;
82 }
83 13143 ▶sub id {
Subroutine id: called CC=3 SCAR=11.0
Pod documented
84 13143 my ($self, $id) = @_;
85  
86 13143 ▶ if (defined $id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) 731 12412 13143
87 731 ▶ if ($self->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"id"}) 1 730 731
88 1 warn "Can't set id() after object already instantiated";
89 1 return $self->{id};
90 }
91 730 $self->{id} = $id;
92 }
93 13142 return $self->{id};
94 }
95 2239 ▶sub key {
Subroutine key: called CC=3 SCAR=11.0
Pod documented
96 2239 my ($self, $key) = @_;
97  
98 2239 ▶ if (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 736 1503 2239
99 736 ▶ if ($self->id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->id) 1 735 736
100 1 croak "Can't set the 'key' attribute after object is already established";
101 }
102  
103 735 $self->{key} = $key;
104 }
105  
106 2238 return $self->{key};
107 }
108 1310 ▶sub key_hex {
Subroutine key_hex: called CC=2 SCAR=6.9
Pod documented
109 1310 my ($self, $key_int) = @_;
110  
111 1310 ▶ if (defined $key_int) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key_int) 735 575 1310
112 735 $self->{key_hex} = sprintf "0x%08x", $key_int;
113 }
114  
115 1310 return $self->{key_hex};
116 }
117 2486 ▶sub flags {
Subroutine flags: called CC=3 SCAR=11.0
Pod documented
118 2486 my ($self, $flags) = @_;
119  
120 2486 ▶ if (defined $flags) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flags) 735 1751 2486
121 735 ▶ if ($self->id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->id) 1 734 735
122 1 warn "Can't set flags() after object already instantiated";
123 1 return $self->{flags};
124 }
125  
126 734 $self->{flags} = $flags;
127 }
128 2485 return $self->{flags};
129 }
130 1472 ▶sub mode {
Subroutine mode: called CC=3 SCAR=11.0
Pod documented
131 1472 my ($self, $mode) = @_;
132  
133 1472 ▶ if (defined $mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mode) 735 737 1472
134 735 ▶ if ($self->id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->id) 1 734 735
135 1 warn "Can't set mode() after object already instantiated";
136 1 return $self->{mode};
137 }
138  
139 734 $self->{mode} = $mode;
140 }
141  
142 1471 return $self->{mode};
143 }
144 7202 ▶sub size {
Subroutine size: called CC=4 SCAR=13.9
Pod documented
145 7202 my ($self, $size) = @_;
146  
147 7202 ▶ if (defined $size) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $size) 736 6466 7202
148 736 ▶ if ($self->id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->id) 1 735 736
149 1 warn "Can't set size() after object already instantiated";
150 1 return $self->{size};
151 }
152 735 ▶ if ($size !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($size =~ /^\d+$/) 1 734 735
153 1 croak "size() requires an integer as parameter";
154 }
155  
156 734 $self->{size} = $size;
157 }
158 7200 return $self->{size};
159 }
160 737 ▶sub type {
Subroutine type: called CC=3 SCAR=11.0
Pod documented
161 737 my ($self, $type) = @_;
162  
163 737 ▶ if (defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) 584 153 737
164 584 ▶ if ($self->id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->id) 1 583 584
165 1 warn "Can't set type() after object already instantiated";
166 1 return $self->{type};
167 }
168  
169 583 $self->{type} = $type;
170 }
171  
172 736 return $self->{type};
173 }
174 1724 ▶sub data {
Subroutine data: called CC=3 SCAR=11.0
Pod documented
175 1724 my ($self) = @_;
176  
177 1724 my $data = $self->shmread;
178  
179 1724 ▶ return if ! defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 6 1718 1724
180  
181 1718 my $pos = index($data, "\x00");
182 1718 ▶ $data = $pos >= 0 ? substr($data, 0, $pos) : $data;
Branch: 1 branch
BranchTrueFalseTotal
$pos >= 0 ? : 1718 0 1718
183  
184 1718 return $data;
185 }
186 25 ▶sub stat {
Subroutine stat: called CC=15 SCAR=41.9
Pod documented
187 25 my ($self) = @_;
188 25 my $data = '';
189 25 ▶ shmctl($self->id, IPC_STAT, $data) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
shmctl $self->id, IPC_STAT(), $data or return undef 24 1 25
190  
191 24 my %values;
192  
193 24 ▶ if ($^O eq 'linux') {
Branch: 5 branches
BranchTrueFalseTotal
if ($^O eq 'linux') { } 24 0 24
elsif ($^O eq 'freebsd' and $IPC::Shareable::SharedMem::Config{'longsize'} == 8) { } 0 0 0
elsif ($^O eq 'solaris') { } 0 0 0
elsif ($^O eq 'openbsd' and $IPC::Shareable::SharedMem::Config{'longsize'} == 8) { } 0 0 0
elsif ($^O eq 'dragonfly' and $IPC::Shareable::SharedMem::Config{'longsize'} == 8) { } 0 0 0
194 24 ▶ if ($Config{longsize} == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IPC::Shareable::SharedMem::Config{'longsize'} == 8) { } 24 0 24
195 # 64-bit Linux: ipc64_perm is 48 bytes.
196 # ipc64_perm: key(4) uid(4) gid(4) cuid(4) cgid(4) mode(4)
197 # seq(2) pad2(2) [4-byte align-pad] unused1(8) unused2(8)
198 # shmid_ds: segsz(8) atime(8) dtime(8) ctime(8) cpid(4) lpid(4) nattch(8)
199  
200 24 @values{qw(uid gid cuid cgid mode segsz atime dtime ctime cpid lpid nattch)}
201 = unpack('x[4] L L L L L x[24] Q q q q l l Q', $data);
202 }
203 else {
204 # 32-bit Linux: ipc64_perm is 36 bytes (unsigned long = 4 bytes).
205 # ipc64_perm: key(4) uid(4) gid(4) cuid(4) cgid(4) mode(4)
206 # seq(2) pad2(2) unused1(4) unused2(4)
207 # shmid_ds: segsz(4) atime(4) atime_nsec(4) dtime(4) dtime_nsec(4)
208 # ctime(4) ctime_nsec(4) cpid(4) lpid(4) nattch(4)
209  
210 0 @values{qw(uid gid cuid cgid mode segsz atime dtime ctime cpid lpid nattch)}
211 = unpack('x[4] L L L L L x[12] L L x[4] L x[4] L x[4] l l L', $data);
212 }
213 }
214 ▶ elsif ($^O eq 'freebsd' && $Config{longsize} == 8) {
Condition0%0/3
$^O eq "freebsd" and $IPC::Shareable::SharedMem::Config{"longsize"} == 8
!ll&&!rl&&r
000
MC/DC0%0/2
$^O eq "freebsd" and $IPC::Shareable::SharedMem::Config{"longsize"} == 8
$^O eq "freebsd" $IPC::Shareable::SharedMem::Config{"longsize"} == 8
Truth table0%0/3
A and B
A= $^O eq "freebsd"B= $IPC::Shareable::SharedMem::Config{"longsize"} == 8
ABresult
0X0
100
111
215 # 64-bit FreeBSD: ipc_perm is 32 bytes.
216 # ipc_perm: cuid(4) cgid(4) uid(4) gid(4) mode(2) _seq(2) pad(4) _key(8)
217 # shmid_ds: segsz(8) lpid(4) cpid(4) nattch(8) atime(8) dtime(8) ctime(8)
218 # (key_t = long = 8 bytes on FreeBSD 64-bit, with 4 bytes of alignment padding)
219  
220 0 @values{qw(cuid cgid uid gid mode segsz lpid cpid nattch atime dtime ctime)}
221 = unpack('L L L L S x[14] Q l l Q q q q', $data);
222 }
223 elsif ($^O eq 'solaris') {
224 0 ▶ if ($Config{longsize} == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IPC::Shareable::SharedMem::Config{'longsize'} == 8) { } 0 0 0
225 # 64-bit Solaris/illumos _LP64 shmid_ds (136 bytes on OmniOS r151058):
226 # ipc_perm (28 bytes): uid(4) gid(4) cuid(4) cgid(4) mode(4) seq(4) key(4)
227 # [pad 4] segsz(8) [gap 8] lkcnt(2) [pad 2] lpid(4) cpid(4)
228 # [pad 4] nattch(8) cnattch(8) atime(8) dtime(8) ctime(8)
229 # shmatt_t = 8 bytes mode_t = uint_t = 4 bytes
230 # Offsets verified on OmniOS r151058 via offsetof().
231  
232 0 @values{qw(uid gid cuid cgid mode segsz lpid cpid nattch atime dtime ctime)}
233 = unpack('L L L L L x[12] Q x[12] l l x[4] Q x[8] q q q', $data);
234 }
235 else {
236 # 32-bit Solaris/illumos shmid_ds (108 bytes):
237 # ipc_perm (44 bytes): uid(4) gid(4) cuid(4) cgid(4) mode(4) seq(4) key(4) pad[4](16)
238 # segsz(4) lpid(4) cpid(4) lkcnt(2) [pad 2] nattch(4) cnattch(4)
239 # atime(4) pad1(4) dtime(4) pad2(4) ctime(4) pad3(4) pad4[4](16)
240 # mode_t = uint_t = 4 bytes
241  
242 0 @values{qw(uid gid cuid cgid mode segsz lpid cpid nattch atime dtime ctime)}
243 = unpack('L4 L x[24] L l l x[4] L x[4] l x[4] l x[4] l x[20]', $data);
244 }
245 }
246 ▶ elsif ($^O eq 'openbsd' && $Config{longsize} == 8) {
Condition0%0/3
$^O eq "openbsd" and $IPC::Shareable::SharedMem::Config{"longsize"} == 8
!ll&&!rl&&r
000
MC/DC0%0/2
$^O eq "openbsd" and $IPC::Shareable::SharedMem::Config{"longsize"} == 8
$^O eq "openbsd" $IPC::Shareable::SharedMem::Config{"longsize"} == 8
Truth table0%0/3
A and B
A= $^O eq "openbsd"B= $IPC::Shareable::SharedMem::Config{"longsize"} == 8
ABresult
0X0
100
111
247 # 64-bit OpenBSD shmid_ds (104 bytes), struct layout from sys/shm.h:
248 # ipc_perm (32 bytes): uid(4) gid(4) cuid(4) cgid(4) mode(4/int)
249 # +12 bytes (key/seq/pad)
250 # segsz(4/int) lpid(4/pid_t) cpid(4/pid_t) nattch(2/shmatt_t) [pad 2]
251 # atime(8/time_t) __shm_atimensec(8/long)
252 # dtime(8/time_t) __shm_dtimensec(8/long)
253 # ctime(8/time_t) __shm_ctimensec(8/long)
254 # shm_internal(8/ptr)
255  
256 0 @values{qw(uid gid cuid cgid mode segsz lpid cpid nattch atime dtime ctime)}
257 = unpack('L L L L L x[12] L l l S x[2] q x[8] q x[8] q', $data);
258 }
259 ▶ elsif ($^O eq 'dragonfly' && $Config{longsize} == 8) {
Condition0%0/3
$^O eq "dragonfly" and $IPC::Shareable::SharedMem::Config{"longsize"} == 8
!ll&&!rl&&r
000
MC/DC0%0/2
$^O eq "dragonfly" and $IPC::Shareable::SharedMem::Config{"longsize"} == 8
$^O eq "dragonfly" $IPC::Shareable::SharedMem::Config{"longsize"} == 8
Truth table0%0/3
A and B
A= $^O eq "dragonfly"B= $IPC::Shareable::SharedMem::Config{"longsize"} == 8
ABresult
0X0
100
111
260 # 64-bit DragonFly BSD shmid_ds (sys/sys/shm.h, sys/sys/ipc.h).
261 # ipc_perm (28 bytes):
262 # uid(4) gid(4) cuid(4) cgid(4) mode(4) _seq(2) [2 pad] _key(4)
263 # [4 pad to align segsz]
264 # shmid_ds:
265 # segsz(8) lpid(4) cpid(4) nattch(8)
266 # atime(8) [atimensec(8)?] dtime(8) [dtimensec(8)?] ctime(8)
267 #
268 # Some DragonFly versions include __shm_*timensec fields (108 bytes
269 # total), others omit them (88 bytes). Detect via data length.
270  
271 0 ▶ if (length($data) > 96) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data > 96) { } 0 0 0
272 0 @values{qw(uid gid cuid cgid mode segsz lpid cpid nattch atime dtime ctime)}
273 = unpack('L L L L L x[12] Q l l Q q x[8] q x[8] q', $data);
274 }
275 else {
276 0 @values{qw(uid gid cuid cgid mode segsz lpid cpid nattch atime dtime ctime)}
277 = unpack('L L L L L x[12] Q l l Q q q q', $data);
278 }
279 }
280 else {
281 # macOS shmid_ds / ipc_perm layout (XNU kernel):
282 #
283 # ipc_perm (24 bytes): uid(4) gid(4) cuid(4) cgid(4) mode(2/ushort) seq(2) key(4)
284 # shmid_ds: segsz(8) lpid(4) cpid(4) nattch(2/ushort) [pad 2] atime(8) dtime(8) ctime(8)
285 #
286 # Fields happen to match stat_list() order, so a linear unpack works.
287  
288 0 @values{stat_list()} = unpack('L L L L S x[6] Q l l S x[2] q q q', $data);
289 }
290  
291 24 my @struct_initializers;
292 24 for (stat_list()) {
293 288 my $value = $values{$_};
294 288 ▶ if ($_ eq 'mode') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq 'mode') { } 24 264 288
295 24 $value = $value & 0777;
296 24 push @struct_initializers, $_ => sprintf("%#o", $value);
297 }
298 else {
299 264 push @struct_initializers, $_ => $value;
300 }
301 }
302  
303 24 return IPC::Shareable::SharedMem::stat->new(@struct_initializers);
304 }
305 1 ▶sub stats {
Subroutine stats: called CC=2 SCAR=6.9
Pod documented
306 1 my ($self) = @_;
307 1 my @stat_list = stat_list();
308  
309 1 my $stat = $self->stat;
310  
311 1 my %stats;
312  
313 1 for (@stat_list) {
314 12 $stats{$_} = $stat->$_;
315 }
316  
317 1 return \%stats;
318 }
319 26 ▶sub stat_list {
Subroutine stat_list: called CC=1 SCAR=0.0
Pod documented
320 26 return qw(
321 uid
322 gid
323 cuid
324 cgid
325 mode
326 segsz
327 lpid
328 cpid
329 nattch
330 atime
331 dtime
332 ctime
333 );
334 }
335  
336 3744 ▶sub shmread {
Subroutine shmread: called CC=2 SCAR=6.9
Pod documented
337 3744 my ($self) = @_;
338  
339 3744 my $data = '';
340 3744 ▶ shmread($self->id, $data, 0, $self->size) or return;
Branch: 1 branch
BranchTrueFalseTotal
CORE::shmread $self->id, $data, 0, $self->size or return 3724 20 3744
341 3724 return $data;
342 }
343 993 ▶sub shmwrite {
Subroutine shmwrite: called CC=1 SCAR=0.0
Pod documented
344 993 my($self, $data) = @_;
345 993 return shmwrite($self->id, $data, 0, $self->size);
346 }
347 544 ▶sub remove {
Subroutine remove: called CC=4 SCAR=14.3
Pod documented
348 544 my ($self) = @_;
349 544 my $os_return_value = shmctl($self->id, IPC_RMID, 0);
350  
351 544 ▶ if (defined $os_return_value && ($os_return_value eq '0 but true' || $os_return_value == 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $os_return_value and $os_return_value eq '0 but true' || $os_return_value == 1) { } 543 1 544
Condition50%3/6
defined $os_return_value and $os_return_value eq "0 but true" || $os_return_value == 1
!ll&&!rl&&r
10543
$os_return_value eq "0 but true" or $os_return_value == 1
l!l&&r!l&&!r
54300
MC/DC33%1/3
defined $os_return_value and $os_return_value eq "0 but true" || $os_return_value == 1
defined $os_return_value $os_return_value eq "0 but true" $os_return_value == 1
Truth table50%2/4
A and B or C
A= defined $os_return_valueB= $os_return_value eq "0 but true"C= $os_return_value == 1
ABCresult
0XX0
1000
11X1
1011
352 543 return 1;
353 }
354 else {
355 1 return 0;
356 }
357 }
358  
359 90 1;
360  
361 =head1 NAME
362  
363 IPC::Shareable::SharedMem - Allows access to a shared memory segment via an
364 object oriented interface.
365  
366 =head1 DESCRIPTION
367  
368 This module provides object oriented access to a shared memory segment. Although
369 it can be used standalone, it was designed for use specifically within the
370 L<< IPC::Shareable >> library.
371  
372 =for html
373 <a href="https://github.com/stevieb9/ipc-shareable/actions"><img src="https://github.com/stevieb9/ipc-shareable/workflows/CI/badge.svg"/></a>
374 <a href='https://coveralls.io/github/stevieb9/ipc-shareable?branch=master'><img src='https://coveralls.io/repos/stevieb9/ipc-shareable/badge.svg?branch=master&service=github' alt='Coverage Status' /></a>
375  
376 =head1 SYNOPSIS
377  
378 use IPC::Shareable::SharedMem;
379  
380 my $seg = IPC::Shareable::SharedMem->new(
381 key => 1234,
382 size => 65536,
383 );
384  
385 $seg->shmwrite($data);
386  
387 my $data = $seg->data;
388  
389 =head1 METHODS
390  
391 =head2 new(%params)
392  
393 Instantiates and returns an object that represents a shared memory segment.
394  
395 If for any reason we can't create the shared memory segment, we'll return
396 C<undef>.
397  
398 Parameters (must be in key => value pairs):
399  
400 =head3 key
401  
402 I<< Mandatory, Integer >>: An integer that references the shared memory segment.
403  
404 =head3 size
405  
406 I<Optional, Integer>: An integer representing the size in bytes of the
407 shared memory segment. The maximum is Operating System independent.
408  
409 I<Default>: 1024
410  
411 =head3 flags
412  
413 I<Optional, Bitwise Mask>: A bitwise mask of options logically OR'd together
414 with any or all of C<IPC_CREAT> (create segment if it doesn't exist),
415 C<IPC_EXCL> (exclusive access; if the segment already exists,
416 we'll C<croak>) and C<IPC_RDONLY> (create a read only segment).
417  
418 See L<IPC::SysV> for further details.
419  
420 I<Default>: C<0> (ie. no flags).
421  
422 =head3 mode
423  
424 I<Optional, Octal Integer>: An octal number representing the access permissions
425 for the shared memory segment. Exactly the same as a Unix file system
426 permissions.
427  
428 I<Default>: 0666 (User RW, Group RW, World RW).
429  
430 =head3 type
431  
432 I<Optional, String>: The type of data that will be stored in the shared memory
433 segment. L<IPC::Shareable> uses C<SCALAR>, C<ARRAY> or C<HASH>.
434  
435 =head2 id
436  
437 Sets/gets the identification number that references the shared memory segment.
438  
439 A warning will be thrown if you try to set the ID after the object is already
440 instantiated, and no change will occur.
441  
442 =head2 key
443  
444 Sets/gets the key used to identify the shared memory segment.
445  
446 Setting this attribute should only be done internally. If it is sent in after
447 the object is already associated with a shared memory segment, we will C<croak>.
448  
449 See L</key> for further details.
450  
451 =head2 key_hex($key)
452  
453 Returns the hex formatted key which appears in C<ipcs> calls.
454  
455 Parameters:
456  
457 =head3 $key
458  
459 I<< Optional, String >>: This is always sent in during initialization.
460  
461 =head2 size
462  
463 Sets/gets the size of the shared memory segment in bytes. See L</size> for
464 further details.
465  
466 A warning will be thrown if you try to set the size after the object is already
467 instantiated, and no change will occur.
468  
469 =head2 flags
470  
471 Sets/gets the flags that the segment will be created with. See L</flags> for
472 details.
473  
474 A warning will be thrown if you try to set the flags after the object is already
475 instantiated, and no change will occur.
476  
477 =head2 mode
478  
479 Sets/gets the access permissions. See L</mode> for further details.
480  
481 A warning will be thrown if you try to set the mode after the object is already
482 instantiated, and no change will occur.
483  
484 =head2 type
485  
486 Sets/gets the type of data that will be contained in the shared memory segment.
487 See L</type> for details.
488  
489 A warning will be thrown if you try to set the type after the object is already
490 instantiated, and no change will occur.
491  
492 =head2 data
493  
494 Returns the data in the shared memory segment, with all NULL pad bytes removed.
495  
496 Use this method for text data. For binary data where you need all blocks within
497 the segment, use the L</shmread> method.
498  
499 =head2 stat
500  
501 This method has sub methods that display various system-level information about
502 the memory segment. These sub methods are:
503  
504 uid
505 gid
506 cuid
507 cgid
508 mode
509 segsz
510 lpid
511 cpid
512 nattch
513 atime
514 dtime
515 ctime
516  
517 Example call:
518  
519 my $ctime = $seg->stat->ctime;
520  
521 =head2 stats
522  
523 Returns an href of the various system-level stat information:
524  
525 {
526 uid => 501,
527 gid => 20,
528 cuid => 501,
529 cgid => 20,
530 mode => 0666,
531 segsz => 65536,
532 lpid => 61270,
533 cpid => 61270,
534 nattch => 0,
535 atime => 1778791348,
536 dtime => 1778791348,
537 ctime => 1778791348,
538 }
539  
540 =head2 stat_list
541  
542 Returns an array of all the segment's system stat entries. These are what make
543 up the method names of the C<< $seg->stat >> object.
544  
545 =head2 shmread
546  
547 Returns the data (and NULL pad bytes) stored in the shared memory segment.
548  
549 By default, when data is retrieved from the shared memory segment, the data
550 is padded to the right by NULL bytes to fill up the entire size of the segment.
551 This can cause issues when using the space for non serialized data (ie. if you
552 stored "hello" in a 1024 byte segment, the ASCII text wouldn't match).
553  
554 Typically this method is used when you want all blocks of the segment, such as
555 if you've stored binary data.
556  
557 For text/ASCII data, use the L</data> method which automatically strips NULL
558 pad bytes.
559  
560 I<Return>: The data if any is stored, empty string if no data has been stored
561 yet, and C<undef> if a failure to read occurs.
562  
563 =head2 shmwrite($data)
564  
565 Stores the serialized data to the shared memory segment.
566  
567 Parameters:
568  
569 $data
570  
571 I<Mandatory, String>: Typically, the a serialized data structure.
572  
573 I<Return>: True on success, false on failure.
574  
575 =head2 remove
576  
577 Removes the shared memory segment and returns the resources to the system.
578  
579 I<Return>: True (C<1>) on success, false (C<0>) on failure.
580  
581 =head1 AUTHOR
582  
583 Ben Sugars (bsugars@canoe.ca)
584  
585 =head1 MAINTAINED BY
586  
587 Steve Bertrand <steveb@cpan.org>
588  
589 =head1 SEE ALSO
590  
591 L<IPC::Shareable>, L<IPC::Shareable::SharedMem>, L<IPC::ShareLite>
592  
593 =cut
« blib/lib/IPC/Shareable.pm ↑ Summary Shareable.xs »