File Coverage

blib/lib/IPC/Shareable/SharedMem.pm
Criterion Covered Total %
statement 15 58 25.8
branch 0 22 0.0
condition 1 7 14.2
subroutine 5 15 33.3
pod 0 10 0.0
total 21 112 18.7


line stmt bran cond sub pod time code
1             package IPC::Shareable::SharedMem;
2              
3 35     35   224 use warnings;
  35         65  
  35         1165  
4 35     35   159 use strict;
  35         61  
  35         723  
5              
6 35     35   137 use Carp qw(carp croak confess);
  35         67  
  35         1615  
7 35     35   194 use IPC::SysV qw(IPC_RMID);
  35         61  
  35         2184  
8              
9             our $VERSION = '1.13';
10              
11 35   50 35   208 use constant DEBUGGING => ($ENV{SHM_DEBUG} or 0);
  35         73  
  35         21771  
12              
13             my $default_size = 1024;
14              
15             sub default_size {
16 0     0 0   my $class = shift;
17 0 0         $default_size = shift if @_;
18 0           return $default_size;
19             }
20              
21             sub new {
22 0     0 0   my($class, $key, $size, $flags, $type) = @_;
23              
24 0 0         defined $key or do {
25 0           confess "usage: IPC::SharedMem->new(KEY, [ SIZE, [ FLAGS ] ])";
26             };
27              
28 0   0       $size ||= $default_size;
29 0   0       $flags ||= 0;
30              
31 0           my $id = shmget($key, $size, $flags);
32              
33 0 0         defined $id or do {
34 0 0         if ($! =~ /File exists/){
35 0           croak "\nERROR: IPC::Shareable::SharedMem: shmget $key: $!\n\n" .
36             "Are you using exclusive, but trying to create multiple " .
37             "instances?\n\n";
38             }
39 0           return undef;
40             };
41              
42 0           my $sh = {
43             _id => $id,
44             _key => $key,
45             _size => $size,
46             _flags => $flags,
47             _type => $type,
48             };
49              
50 0           return bless $sh => $class;
51             }
52             sub id {
53 0     0 0   my $self = shift;
54              
55 0 0         $self->{_id} = shift if @_;
56 0           return $self->{_id};
57             }
58             sub key {
59 0     0 0   my $self = shift;
60              
61 0 0         $self->{_key} = shift if @_;
62 0           return $self->{_key};
63             }
64             sub flags {
65 0     0 0   my $self = shift;
66              
67 0 0         $self->{_flags} = shift if @_;
68 0           return $self->{_flags};
69             }
70             sub size {
71 0     0 0   my $self = shift;
72              
73 0 0         $self->{_size} = shift if @_;
74 0           return $self->{_size};
75             }
76             sub type {
77 0     0 0   my $self = shift;
78              
79 0 0         $self->{_type} = shift if @_;
80 0           return $self->{_type};
81             }
82             sub shmwrite {
83 0     0 0   my($self, $data) = @_;
84 0           return shmwrite($self->{_id}, $data, 0, $self->{_size});
85             }
86             sub shmread {
87 0     0 0   my $self = shift;
88              
89 0           my $data = '';
90 0 0         shmread($self->{_id}, $data, 0, $self->{_size}) or return;
91 0           return $data;
92             }
93             sub remove {
94 0     0 0   my $to_remove = shift;
95              
96 0           my $id;
97              
98 0 0         if (ref $to_remove eq __PACKAGE__){
99 0           $id = $to_remove->{_id};
100             }
101              
102 0           my $arg = 0;
103              
104 0           my $ret = shmctl($id, IPC_RMID, $arg);
105 0           return $ret;
106             }
107              
108             1;
109              
110             =head1 NAME
111              
112             IPC::Shareable::SharedMem - Object oriented interface to shared memory
113              
114             =for html
115            
116             Coverage Status
117              
118              
119             =head1 SYNOPSIS
120              
121             *** No public interface ***
122              
123             =head1 WARNING
124              
125             This module is not intended for public consumption. It is used
126             internally by IPC::Shareable to access shared memory.
127              
128             =head1 DESCRIPTION
129              
130             This module provides and object-oriented framework to access shared
131             memory. Its use is intended to be limited to IPC::Shareable.
132             Therefore I have not documented an interface.
133              
134             =head1 AUTHOR
135              
136             Ben Sugars (bsugars@canoe.ca)
137              
138             =head1 SEE ALSO
139              
140             L, L