| 1 | package IPC::Mmap::Share; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | ▶ | use IPC::Mmap; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 1 | ▶ | use Storable qw(freeze thaw); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 1 | ▶ | use base qw(Exporter); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 1 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 1 | our $VERSION = 0.03; | |||||||||||||||||||
| 14 | } | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 1 | my $DEBUG = 0; | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 1 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=18.9
Pod
documented
| |||||||||||||||||||||
| 19 | 1 | ▶ | my $self = shift(@_) or croak "Error!..I do not even know who I am!.."; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 20 | 1 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||
| 21 | 1 | ▶ | my $size = shift(@_) or croak "Incorrect call to new"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 22 | 1 | ▶ | open(my $FILE,"<","/dev/zero") || die $!; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 23 | 1 | my $mmap = create_mmap($size); | |||||||||||||||||||
| 24 | 0 | return bless { mmap => $mmap , size => $size },$class; | |||||||||||||||||||
| 25 | } | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 1 | ▶ | sub create_mmap { | ||||||||||||||||||
|
Subroutine
create_mmap: called CC=4 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 28 | 1 | ▶ | my $size = shift(@_) or die "Internal error!..Size was not passed to create_mmap"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 29 | 1 | ▶ | open(my $FILE,"<","/dev/zero") || croak $!; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 30 | 1 | ▶ | my $mmap = IPC::Mmap->new($FILE, $size, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS) or croak $!; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 31 | 0 | return $mmap; | |||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 0 | ▶ | sub set_mmap { | ||||||||||||||||||
|
Subroutine
set_mmap: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 35 | 0 | ▶ | my $self = shift(@_) or croak "Error...set_mmap is a method call"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 36 | 0 | ▶ | my $mmap = shift(@_) or croak "Error...incorrect call to set_mmap...expected an argument"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | 0 | $self->{mmap} = $mmap; | |||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 0 | ▶ | sub get_mmap { | ||||||||||||||||||
|
Subroutine
get_mmap: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 41 | 0 | ▶ | my $self = shift(@_) or croak "Error...get_mmap is a method call"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | 0 | return $self->{mmap}; | |||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 0 | ▶ | sub get_size { | ||||||||||||||||||
|
Subroutine
get_size: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 46 | 0 | ▶ | my $self = shift(@_) or croak "Error...get_size is a method call"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 47 | 0 | return $self->{size}; | |||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 0 | ▶ | sub set { | ||||||||||||||||||
|
Subroutine
set: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||
| 51 | 0 | my $self = shift(@_); | |||||||||||||||||||
| 52 | 0 | ▶ | my $var = shift(@_) or croak "Error...You passed no argument or undef to set method"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 0 | my $var_ref = \$var; #saying $var=\$var would create a circular reference, caution!! | |||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 0 | ▶ | my $serialized = freeze($var_ref) or croak "Internal error...freeze failed..."; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | #check that the structure fits the mmaped area... | ||||||||||||||||||||
| 59 | 0 | ▶ | croak "Error...you cannot store a structure that is bigger than the size you have allocated in new. Please go back and try an allocation size of at least ".length($serialized)."\n" if (length($serialized) > $self->get_size); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 0 | ▶ | $DEBUG&&print STDERR "Size is ",length($serialized),"\n"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 62 | 0 | my $mmap = $self->get_mmap(); | |||||||||||||||||||
| 63 | 0 | my $size = $self->get_size(); | |||||||||||||||||||
| 64 | 0 | ▶ | $mmap->lock() || croak "Internal error...lock failed"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | 0 | ▶ | $mmap->write($serialized,0,$size) || croak "Internal error...write failed"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 66 | 0 | ▶ | $mmap->unlock() || croak "Internal error...unlock failed"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 0 | ▶ | sub get { | ||||||||||||||||||
|
Subroutine
get: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||
| 71 | 0 | my $self = shift(@_); | |||||||||||||||||||
| 72 | 0 | my $mmap = $self->get_mmap(); | |||||||||||||||||||
| 73 | 0 | my $size = $self->get_size(); | |||||||||||||||||||
| 74 | 0 | my $unserialized; | |||||||||||||||||||
| 75 | 0 | ▶ | $mmap->lock() || croak "Internal error...lock failed"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | 0 | ▶ | $mmap->read($unserialized,0,$size) || croak "Internal error...read failed"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | 0 | ▶ | $mmap->unlock() || croak "Internal error...unlock failed"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | 0 | ▶ | my $var_ref = thaw($unserialized) or croak "Error...Unfreezing returned an undefined value..."; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | 0 | return ${$var_ref}; | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 0 | ▶ | sub lock { | ||||||||||||||||||
|
Subroutine
lock: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 83 | 0 | ▶ | my $self = shift(@_) or croak "I could not shift the reference to my self"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 84 | 0 | ▶ | my $mmap = $self->get_mmap() or croak "Failed to retrieve mmap object"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 85 | 0 | ▶ | $mmap->lock() || croak "Internal error...lock failed"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | 0 | $self->{locked} = 1; | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 0 | ▶ | sub unlock { | ||||||||||||||||||
|
Subroutine
unlock: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 91 | 0 | ▶ | my $self = shift(@_) or croak "I could not shift the reference to my self"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | 0 | ▶ | my $mmap = $self->get_mmap() or croak "Failed to retrieve mmap object"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | 0 | ▶ | $mmap->unlock() || croak "Internal error...lock failed"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 0 | $self->{locked} = 0; | |||||||||||||||||||
| 95 | } | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 0 | ▶ | sub is_locked { | ||||||||||||||||||
|
Subroutine
is_locked: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 98 | 0 | ▶ | my $self = shift(@_) or croak "I could not shift the reference to my self"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | 0 | ▶ | return 1 if ($self->{locked} == 1); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 100 | 0 | return; | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 0 | ▶ | sub DESTROY { | ||||||||||||||||||
|
Subroutine
DESTROY: not called CC=5 SCAR=34.0
| |||||||||||||||||||||
| 104 | 0 | ▶ | my $self = shift(@_) or croak "I could not shift the reference to my self"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 105 | 0 | ▶ | my $mmap = $self->get_mmap() or croak "Failed to retrieve mmap object"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | 0 | ▶ | if ($self->is_locked) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 107 | 0 | ▶ | $mmap->unlock() || croak "Internal error...unlock failed"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | 0 | return; | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 1 | 1; | |||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | __END__ | ||||||||||||||||||||