File Coverage

blib/lib/IPC/Shareable/SharedMem.pm
Criterion Covered Total %
statement 37 58 63.7
branch 5 22 22.7
condition 3 7 42.8
subroutine 9 15 60.0
pod 0 10 0.0
total 54 112 48.2


line stmt bran cond sub pod time code
1             package IPC::Shareable::SharedMem;
2              
3 37     37   241 use warnings;
  37         68  
  37         1163  
4 37     37   169 use strict;
  37         61  
  37         751  
5              
6 37     37   149 use Carp qw(carp croak confess);
  37         74  
  37         1713  
7 37     37   214 use IPC::SysV qw(IPC_RMID);
  37         64  
  37         2198  
8              
9             our $VERSION = '1.11';
10              
11 37   50 37   232 use constant DEBUGGING => ($ENV{SHM_DEBUG} or 0);
  37         94  
  37         22092  
12              
13             my $default_size = 1024;
14              
15             sub default_size {
16 0     0 0 0 my $class = shift;
17 0 0       0 $default_size = shift if @_;
18 0         0 return $default_size;
19             }
20              
21             sub new {
22 2     2 0 6 my($class, $key, $size, $flags, $type) = @_;
23              
24 2 50       4 defined $key or do {
25 0         0 confess "usage: IPC::SharedMem->new(KEY, [ SIZE, [ FLAGS ] ])";
26             };
27              
28 2   33     7 $size ||= $default_size;
29 2   50     8 $flags ||= 0;
30              
31 2         165 my $id = shmget($key, $size, $flags);
32              
33 2 50       7 defined $id or do {
34 0 0       0 if ($! =~ /File exists/){
35 0         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         0 return undef;
40             };
41              
42 2         22 my $sh = {
43             _id => $id,
44             _key => $key,
45             _size => $size,
46             _flags => $flags,
47             _type => $type,
48             };
49              
50 2         13 return bless $sh => $class;
51             }
52             sub id {
53 7     7 0 8 my $self = shift;
54              
55 7 50       23 $self->{_id} = shift if @_;
56 7         40 return $self->{_id};
57             }
58             sub key {
59 0     0 0 0 my $self = shift;
60              
61 0 0       0 $self->{_key} = shift if @_;
62 0         0 return $self->{_key};
63             }
64             sub flags {
65 0     0 0 0 my $self = shift;
66              
67 0 0       0 $self->{_flags} = shift if @_;
68 0         0 return $self->{_flags};
69             }
70             sub size {
71 0     0 0 0 my $self = shift;
72              
73 0 0       0 $self->{_size} = shift if @_;
74 0         0 return $self->{_size};
75             }
76             sub type {
77 0     0 0 0 my $self = shift;
78              
79 0 0       0 $self->{_type} = shift if @_;
80 0         0 return $self->{_type};
81             }
82             sub shmwrite {
83 0     0 0 0 my($self, $data) = @_;
84 0         0 return shmwrite($self->{_id}, $data, 0, $self->{_size});
85             }
86             sub shmread {
87 2     2 0 3 my $self = shift;
88              
89 2         7 my $data = '';
90 2 50       317 shmread($self->{_id}, $data, 0, $self->{_size}) or return;
91 2         11 return $data;
92             }
93             sub remove {
94 1     1 0 2 my $to_remove = shift;
95              
96 1         2 my $id;
97              
98 1 50       3 if (ref $to_remove eq __PACKAGE__){
99 1         2 $id = $to_remove->{_id};
100             }
101              
102 1         2 my $arg = 0;
103              
104 1         5 my $ret = shmctl($id, IPC_RMID, $arg);
105 1         57 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