| 1 | ################################################################################ | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Version 2.x, Copyright (C) 2007-2013, Marcus Holland-Moritz <mhx@cpan.org>. | ||||||||||||||||||||||||||||
| 4 | # Version 1.x, Copyright (C) 1997, Graham Barr <gbarr@pobox.com>. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # This program is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||
| 7 | # modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | ################################################################################ | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | package IPC::SharedMem; | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use IPC::SysV qw(IPC_STAT IPC_RMID shmat shmdt memread memwrite); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | $VERSION = '2.09'; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # Figure out if we have support for native sized types | ||||||||||||||||||||||||||||
| 21 | 2 | ▶ | my $N = do { my $foo = eval { pack "L!", 0 }; $@ ? '' : '!' }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | { | ||||||||||||||||||||||||||||
| 24 | 2 | package IPC::SharedMem::stat; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 2 | ▶ | use Class::Struct qw(struct); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 2 | struct 'IPC::SharedMem::stat' => [ | |||||||||||||||||||||||||||
| 29 | uid => '$', | ||||||||||||||||||||||||||||
| 30 | gid => '$', | ||||||||||||||||||||||||||||
| 31 | cuid => '$', | ||||||||||||||||||||||||||||
| 32 | cgid => '$', | ||||||||||||||||||||||||||||
| 33 | mode => '$', | ||||||||||||||||||||||||||||
| 34 | segsz => '$', | ||||||||||||||||||||||||||||
| 35 | lpid => '$', | ||||||||||||||||||||||||||||
| 36 | cpid => '$', | ||||||||||||||||||||||||||||
| 37 | nattch => '$', | ||||||||||||||||||||||||||||
| 38 | atime => '$', | ||||||||||||||||||||||||||||
| 39 | dtime => '$', | ||||||||||||||||||||||||||||
| 40 | ctime => '$', | ||||||||||||||||||||||||||||
| 41 | ]; | ||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | sub new | ||||||||||||||||||||||||||||
| 45 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 1 | ▶ | @_ == 4 or croak 'IPC::SharedMem->new(KEY, SIZE, FLAGS)'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 1 | my($class, $key, $size, $flags) = @_; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1 | my $id = shmget $key, $size, $flags; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 1 | ▶ | return undef unless defined $id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 1 | bless { _id => $id, _addr => undef, _isrm => 0 }, $class | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | sub id | ||||||||||||||||||||||||||||
| 57 | 8 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
id: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 59 | 8 | $self->{_id}; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | sub addr | ||||||||||||||||||||||||||||
| 63 | 19 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
addr: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | 19 | my $self = shift; | |||||||||||||||||||||||||||
| 65 | 19 | $self->{_addr}; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | sub stat | ||||||||||||||||||||||||||||
| 69 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
stat: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 71 | 2 | my $data = ''; | |||||||||||||||||||||||||||
| 72 | 2 | ▶ | shmctl $self->id, IPC_STAT, $data or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 2 | IPC::SharedMem::stat->new->unpack($data); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | sub attach | ||||||||||||||||||||||||||||
| 77 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
attach: called CC=5 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | 1 | ▶ | @_ >= 1 && @_ <= 2 or croak '$shm->attach([FLAG])'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ >= 1 and @_ <= 2
MC/DC0%0/2
@_ >= 1 and @_ <= 2
@_ >= 1
@_ <= 2
Truth table33%1/3
A and B
A= @_ >= 1B= @_ <= 2
| |||||||||||||||||||||||||||||
| 79 | 1 | my($self, $flag) = @_; | |||||||||||||||||||||||||||
| 80 | 1 | ▶ | defined $self->addr and return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 1 | ▶ | $self->{_addr} = shmat($self->id, undef, $flag || 0); | ||||||||||||||||||||||||||
|
Condition50%1/2
$flag || 0
MC/DC0%0/1
$flag || 0
$flag
Truth table50%1/2
A
A= $flag
| |||||||||||||||||||||||||||||
| 82 | 1 | defined $self->addr; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | sub detach | ||||||||||||||||||||||||||||
| 86 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
detach: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 87 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 88 | 1 | ▶ | defined $self->addr or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 1 | my $rv = defined shmdt($self->addr); | |||||||||||||||||||||||||||
| 90 | 1 | ▶ | undef $self->{_addr} if $rv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 1 | $rv; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | sub remove | ||||||||||||||||||||||||||||
| 95 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
remove: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 96 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 97 | 1 | ▶ | return undef if $self->is_removed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 1 | my $rv = shmctl $self->id, IPC_RMID, 0; | |||||||||||||||||||||||||||
| 99 | 1 | ▶ | $self->{_isrm} = 1 if $rv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 1 | return $rv; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | sub is_removed | ||||||||||||||||||||||||||||
| 104 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
is_removed: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 105 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 106 | 3 | $self->{_isrm}; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | sub read | ||||||||||||||||||||||||||||
| 110 | 5 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
read: called CC=5 SCAR=16.5
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 5 | ▶ | @_ == 3 or croak '$shm->read(POS, SIZE)'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 5 | my($self, $pos, $size) = @_; | |||||||||||||||||||||||||||
| 113 | 5 | my $buf = ''; | |||||||||||||||||||||||||||
| 114 | 5 | ▶ | if (defined $self->addr) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 3 | ▶ | memread($self->addr, $buf, $pos, $size) or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | else { | ||||||||||||||||||||||||||||
| 118 | 2 | ▶ | shmread($self->id, $buf, $pos, $size) or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | 5 | $buf; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | sub write | ||||||||||||||||||||||||||||
| 124 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | 4 | ▶ | @_ == 4 or croak '$shm->write(STRING, POS, SIZE)'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 4 | my($self, $str, $pos, $size) = @_; | |||||||||||||||||||||||||||
| 127 | 4 | ▶ | if (defined $self->addr) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 2 | return memwrite($self->addr, $str, $pos, $size); | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | else { | ||||||||||||||||||||||||||||
| 131 | 2 | return shmwrite($self->id, $str, $pos, $size); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 2 | 1; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | __END__ | ||||||||||||||||||||||||||||