| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | ################################################################################ | 
| 2 |  |  |  |  |  |  | # | 
| 3 |  |  |  |  |  |  | #  Version 2.x, Copyright (C) 2007-2013, Marcus Holland-Moritz . | 
| 4 |  |  |  |  |  |  | #  Version 1.x, Copyright (C) 1997, Graham Barr . | 
| 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 | 1 |  |  | 1 |  | 588 | use IPC::SysV qw(IPC_STAT IPC_RMID shmat shmdt memread memwrite); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 60 |  | 
| 14 | 1 |  |  | 1 |  | 6 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 23 |  | 
| 15 | 1 |  |  | 1 |  | 6 | use vars qw($VERSION); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 33 |  | 
| 16 | 1 |  |  | 1 |  | 5 | use Carp; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 134 |  | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | $VERSION = '2.08'; | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | # Figure out if we have support for native sized types | 
| 21 |  |  |  |  |  |  | my $N = do { my $foo = eval { pack "L!", 0 }; $@ ? '' : '!' }; | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | { | 
| 24 |  |  |  |  |  |  | package IPC::SharedMem::stat; | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 1 |  |  | 1 |  | 566 | use Class::Struct qw(struct); | 
|  | 1 |  |  |  |  | 1920 |  | 
|  | 1 |  |  |  |  | 6 |  | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | 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 |  |  |  |  |  |  | { | 
| 46 | 1 | 50 |  | 1 | 1 | 4 | @_ == 4 or croak 'IPC::SharedMem->new(KEY, SIZE, FLAGS)'; | 
| 47 | 1 |  |  |  |  | 3 | my($class, $key, $size, $flags) = @_; | 
| 48 |  |  |  |  |  |  |  | 
| 49 | 1 |  |  |  |  | 51 | my $id = shmget $key, $size, $flags; | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 1 | 50 |  |  |  | 6 | return undef unless defined $id; | 
| 52 |  |  |  |  |  |  |  | 
| 53 | 1 |  |  |  |  | 24 | bless { _id => $id, _addr => undef, _isrm => 0 }, $class | 
| 54 |  |  |  |  |  |  | } | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | sub id | 
| 57 |  |  |  |  |  |  | { | 
| 58 | 8 |  |  | 8 | 1 | 11 | my $self = shift; | 
| 59 | 8 |  |  |  |  | 201 | $self->{_id}; | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | sub addr | 
| 63 |  |  |  |  |  |  | { | 
| 64 | 19 |  |  | 19 | 1 | 28 | my $self = shift; | 
| 65 | 19 |  |  |  |  | 123 | $self->{_addr}; | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | sub stat | 
| 69 |  |  |  |  |  |  | { | 
| 70 | 2 |  |  | 2 | 1 | 1146 | my $self = shift; | 
| 71 | 2 |  |  |  |  | 5 | my $data = ''; | 
| 72 | 2 | 50 |  |  |  | 5 | shmctl $self->id, IPC_STAT, $data or return undef; | 
| 73 | 2 |  |  |  |  | 59 | IPC::SharedMem::stat->new->unpack($data); | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | sub attach | 
| 77 |  |  |  |  |  |  | { | 
| 78 | 1 | 50 | 33 | 1 | 1 | 11 | @_ >= 1 && @_ <= 2 or croak '$shm->attach([FLAG])'; | 
| 79 | 1 |  |  |  |  | 3 | my($self, $flag) = @_; | 
| 80 | 1 | 50 |  |  |  | 3 | defined $self->addr and return undef; | 
| 81 | 1 |  | 50 |  |  | 4 | $self->{_addr} = shmat($self->id, undef, $flag || 0); | 
| 82 | 1 |  |  |  |  | 6 | defined $self->addr; | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | sub detach | 
| 86 |  |  |  |  |  |  | { | 
| 87 | 1 |  |  | 1 | 1 | 3 | my $self = shift; | 
| 88 | 1 | 50 |  |  |  | 3 | defined $self->addr or return undef; | 
| 89 | 1 |  |  |  |  | 3 | my $rv = defined shmdt($self->addr); | 
| 90 | 1 | 50 |  |  |  | 36 | undef $self->{_addr} if $rv; | 
| 91 | 1 |  |  |  |  | 7 | $rv; | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | sub remove | 
| 95 |  |  |  |  |  |  | { | 
| 96 | 1 |  |  | 1 | 1 | 4 | my $self = shift; | 
| 97 | 1 | 50 |  |  |  | 2 | return undef if $self->is_removed; | 
| 98 | 1 |  |  |  |  | 3 | my $rv = shmctl $self->id, IPC_RMID, 0; | 
| 99 | 1 | 50 |  |  |  | 7 | $self->{_isrm} = 1 if $rv; | 
| 100 | 1 |  |  |  |  | 5 | return $rv; | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | sub is_removed | 
| 104 |  |  |  |  |  |  | { | 
| 105 | 3 |  |  | 3 | 1 | 7 | my $self = shift; | 
| 106 | 3 |  |  |  |  | 12 | $self->{_isrm}; | 
| 107 |  |  |  |  |  |  | } | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | sub read | 
| 110 |  |  |  |  |  |  | { | 
| 111 | 5 | 50 |  | 5 | 1 | 1565 | @_ == 3 or croak '$shm->read(POS, SIZE)'; | 
| 112 | 5 |  |  |  |  | 12 | my($self, $pos, $size) = @_; | 
| 113 | 5 |  |  |  |  | 8 | my $buf = ''; | 
| 114 | 5 | 100 |  |  |  | 11 | if (defined $self->addr) { | 
| 115 | 3 | 50 |  |  |  | 16 | memread($self->addr, $buf, $pos, $size) or return undef; | 
| 116 |  |  |  |  |  |  | } | 
| 117 |  |  |  |  |  |  | else { | 
| 118 | 2 | 50 |  |  |  | 5 | shmread($self->id, $buf, $pos, $size) or return undef; | 
| 119 |  |  |  |  |  |  | } | 
| 120 | 5 |  |  |  |  | 58 | $buf; | 
| 121 |  |  |  |  |  |  | } | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | sub write | 
| 124 |  |  |  |  |  |  | { | 
| 125 | 4 | 50 |  | 4 | 1 | 1963 | @_ == 4 or croak '$shm->write(STRING, POS, SIZE)'; | 
| 126 | 4 |  |  |  |  | 12 | my($self, $str, $pos, $size) = @_; | 
| 127 | 4 | 100 |  |  |  | 10 | if (defined $self->addr) { | 
| 128 | 2 |  |  |  |  | 5 | return memwrite($self->addr, $str, $pos, $size); | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  | else { | 
| 131 | 2 |  |  |  |  | 6 | return shmwrite($self->id, $str, $pos, $size); | 
| 132 |  |  |  |  |  |  | } | 
| 133 |  |  |  |  |  |  | } | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | 1; | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | __END__ |