/usr/share/perl5/Test/Memory/Cycle.pm is in libtest-memory-cycle-perl 1.04-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | package Test::Memory::Cycle;
use strict;
use warnings;
=head1 NAME
Test::Memory::Cycle - Check for memory leaks and circular memory references
=head1 VERSION
Version 1.04
=cut
our $VERSION = '1.04';
=head1 SYNOPSIS
Perl's garbage collection has one big problem: Circular references
can't get cleaned up. A circular reference can be as simple as two
reference that refer to each other:
my $mom = {
name => "Marilyn Lester",
};
my $me = {
name => "Andy Lester",
mother => $mom,
};
$mom->{son} = $me;
C<Test::Memory::Cycle> is built on top of C<Devel::Cycle> to give
you an easy way to check for these circular references.
use Test::Memory::Cycle;
my $object = new MyObject;
# Do stuff with the object.
memory_cycle_ok( $object );
You can also use C<memory_cycle_exists()> to make sure that you have a
cycle where you expect to have one.
=cut
use Devel::Cycle qw( find_cycle find_weakened_cycle );
use Test::Builder;
my $Test = Test::Builder->new;
sub import {
my $self = shift;
my $caller = caller;
no strict 'refs';
*{$caller.'::memory_cycle_ok'} = \&memory_cycle_ok;
*{$caller.'::memory_cycle_exists'} = \&memory_cycle_exists;
*{$caller.'::weakened_memory_cycle_ok'} = \&weakened_memory_cycle_ok;
*{$caller.'::weakened_memory_cycle_exists'} = \&weakened_memory_cycle_exists;
*{$caller.'::memory_cycle_exists'} = \&memory_cycle_exists;
*{$caller.'::weakened_memory_cycle_ok'} = \&weakened_memory_cycle_ok;
*{$caller.'::weakened_memory_cycle_exists'} = \&weakened_memory_cycle_exists;
$Test->exported_to($caller);
$Test->plan(@_);
return;
}
=head1 FUNCTIONS
=head2 C<memory_cycle_ok( I<$reference>, I<$msg> )>
Checks that I<$reference> doesn't have any circular memory references.
=cut
sub memory_cycle_ok {
my $ref = shift;
my $msg = shift;
my $cycle_no = 0;
my @diags;
# Callback function that is called once for each memory cycle found.
my $callback = sub {
my $path = shift;
$cycle_no++;
push( @diags, "Cycle #$cycle_no" );
foreach (@$path) {
my ($type,$index,$ref,$value) = @$_;
my $str = 'Unknown! This should never happen!';
my $refdisp = _ref_shortname( $ref );
my $valuedisp = _ref_shortname( $value );
$str = sprintf( ' %s => %s', $refdisp, $valuedisp ) if $type eq 'SCALAR';
$str = sprintf( ' %s => %s', "${refdisp}->[$index]", $valuedisp ) if $type eq 'ARRAY';
$str = sprintf( ' %s => %s', "${refdisp}->{$index}", $valuedisp ) if $type eq 'HASH';
$str = sprintf( ' closure %s => %s', "${refdisp}, $index", $valuedisp ) if $type eq 'CODE';
push( @diags, $str );
}
};
find_cycle( $ref, $callback );
my $ok = !$cycle_no;
$Test->ok( $ok, $msg );
$Test->diag( join( "\n", @diags, '' ) ) unless $ok;
return $ok;
} # memory_cycle_ok
=head2 C<memory_cycle_exists( I<$reference>, I<$msg> )>
Checks that I<$reference> B<does> have any circular memory references.
=cut
sub memory_cycle_exists {
my $ref = shift;
my $msg = shift;
my $cycle_no = 0;
# Callback function that is called once for each memory cycle found.
my $callback = sub { $cycle_no++ };
find_cycle( $ref, $callback );
my $ok = $cycle_no;
$Test->ok( $ok, $msg );
return $ok;
} # memory_cycle_exists
=head2 C<weakened_memory_cycle_ok( I<$reference>, I<$msg> )>
Checks that I<$reference> doesn't have any circular memory references, but unlike
C<memory_cycle_ok> this will also check for weakened cycles produced with
Scalar::Util's C<weaken>.
=cut
sub weakened_memory_cycle_ok {
my $ref = shift;
my $msg = shift;
my $cycle_no = 0;
my @diags;
# Callback function that is called once for each memory cycle found.
my $callback = sub {
my $path = shift;
$cycle_no++;
push( @diags, "Cycle #$cycle_no" );
foreach (@$path) {
my ($type,$index,$ref,$value,$is_weakened) = @$_;
my $str = "Unknown! This should never happen!";
my $refdisp = _ref_shortname( $ref );
my $valuedisp = _ref_shortname( $value );
my $weak = $is_weakened ? 'w->' : '';
$str = sprintf( ' %s%s => %s', $weak, $refdisp, $valuedisp ) if $type eq 'SCALAR';
$str = sprintf( ' %s%s => %s', $weak, "${refdisp}->[$index]", $valuedisp ) if $type eq 'ARRAY';
$str = sprintf( ' %s%s => %s', $weak, "${refdisp}->{$index}", $valuedisp ) if $type eq 'HASH';
push( @diags, $str );
}
};
find_weakened_cycle( $ref, $callback );
my $ok = !$cycle_no;
$Test->ok( $ok, $msg );
$Test->diag( join( "\n", @diags, "" ) ) unless $ok;
return $ok;
} # weakened_memory_cycle_ok
=head2 C<weakened_memory_cycle_exists( I<$reference>, I<$msg> )>
Checks that I<$reference> B<does> have any circular memory references, but unlike
C<memory_cycle_exists> this will also check for weakened cycles produced with
Scalar::Util's C<weaken>.
=cut
sub weakened_memory_cycle_exists {
my $ref = shift;
my $msg = shift;
my $cycle_no = 0;
# Callback function that is called once for each memory cycle found.
my $callback = sub { $cycle_no++ };
find_weakened_cycle( $ref, $callback );
my $ok = $cycle_no;
$Test->ok( $ok, $msg );
return $ok;
} # weakened_memory_cycle_exists
my %shortnames;
my $new_shortname = "A";
sub _ref_shortname {
my $ref = shift;
my $refstr = "$ref";
my $refdisp = $shortnames{ $refstr };
if ( !$refdisp ) {
my $sigil = ref($ref) . " ";
$sigil = '%' if $sigil eq "HASH ";
$sigil = '@' if $sigil eq "ARRAY ";
$sigil = '$' if $sigil eq "REF ";
$sigil = '&' if $sigil eq "CODE ";
$refdisp = $shortnames{ $refstr } = $sigil . $new_shortname++;
}
return $refdisp;
}
=head1 AUTHOR
Written by Andy Lester, C<< <andy @ petdance.com> >>.
=head1 BUGS
Please report any bugs or feature requests to
C<bug-test-memory-cycle at rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Memory-Cycle>.
I will be notified, and then you'll automatically be notified of progress on
your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Test::Memory::Cycle
You can also look for information at:
=over 4
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/Test-Memory-Cycle>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/Test-Memory-Cycle>
=item * RT: CPAN's request tracker
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Test-Memory-Cycle>
=item * Search CPAN
L<http://search.cpan.org/dist/Test-Memory-Cycle>
=back
=head1 ACKNOWLEDGEMENTS
Thanks to the contributions of Stevan Little, and to Lincoln Stein for writing Devel::Cycle.
=head1 COPYRIGHT
Copyright 2006, Andy Lester, All Rights Reserved.
You may use, modify, and distribute this package under the
same terms as Perl itself.
=cut
1;
|