3 # A place to put some simple leak tests. Uses XS::APItest to make
4 # PL_sv_count available, allowing us to run a bit a code multiple times and
5 # see if the count increases.
12 eval { require XS::APItest; XS::APItest->import('sv_count'); 1 }
13 or skip_all("XS::APItest not available");
18 # run some code N times. If the number of SVs at the end of loop N is
19 # greater than (N-1)*delta at the end of loop 1, we've got a leak
22 my ($n, $delta, $code, @rest) = @_;
28 $sv0 = $sv1 if $i == 1;
30 cmp_ok($sv1-$sv0, '<=', ($n-1)*$delta, @rest);
33 # run some expression N times. The expr is concatenated N times and then
34 # evaled, ensuring that that there are no scope exits between executions.
35 # If the number of SVs at the end of expr N is greater than (N-1)*delta at
36 # the end of expr 1, we've got a leak
39 my ($n, $delta, $expr, @rest) = @_;
42 my $true = 1; # avoid stuff being optimised away
43 my $code1 = "($expr || \$true)";
44 my $code = "$code1 && (\$sv0 = sv_count())" . ("&& $code1" x 4)
45 . " && (\$sv1 = sv_count())";
47 cmp_ok($sv1-$sv0, '<=', ($n-1)*$delta, @rest);
50 fail("eval @rest: $@");
57 leak(5, 0, sub {}, "basic check 1 of leak test infrastructure");
58 leak(5, 0, sub {push @a,1;pop @a}, "basic check 2 of leak test infrastructure");
59 leak(5, 1, sub {push @a,1;}, "basic check 3 of leak test infrastructure");
61 sub TIEARRAY { bless [], $_[0] }
62 sub FETCH { $_[0]->[$_[1]] }
63 sub STORE { $_[0]->[$_[1]] = $_[2] }
65 # local $tied_elem[..] leaks <20020502143736.N16831@dansat.data-plan.com>"
68 leak(5, 0, sub {local $a[0]}, "local \$tied[0]");
71 # [perl #74484] repeated tries leaked SVs on the tmps stack
73 leak_expr(5, 0, q{"YYYYYa" =~ /.+?(a(.+?)|b)/ }, "trie leak");