use strict;
use warnings;
use highlight; # SWIG-generated stuff
+my $hl;
sub _parse_filetypes ($) {
my $ft_conf = $_[0]->searchFile('filetypes.conf') or
(\%ext2lang, \@shebang);
}
+# We only need one instance, so we don't need to do
+# highlight::CodeGenerator::deleteInstance
sub new {
my ($class) = @_;
- my $dir = highlight::DataDir->new;
- $dir->initSearchDirectories('');
- my ($ext2lang, $shebang) = _parse_filetypes($dir);
- bless {
- -dir => $dir,
- -ext2lang => $ext2lang,
- -shebang => $shebang,
- }, $class;
+ $hl ||= do {
+ my $dir = highlight::DataDir->new;
+ $dir->initSearchDirectories('');
+ my ($ext2lang, $shebang) = _parse_filetypes($dir);
+ bless {
+ -dir => $dir,
+ -ext2lang => $ext2lang,
+ -shebang => $shebang,
+ }, $class;
+ };
}
sub _shebang2lang ($$) {
\$out;
}
-# SWIG instances aren't reference-counted, but $self is;
-# so we need to delete all the CodeGenerator instances manually
-# at our own destruction
-sub DESTROY {
- my ($self) = @_;
- foreach my $gen (values %$self) {
- if (ref($gen) eq 'highlight::CodeGenerator') {
- highlight::CodeGenerator::deleteInstance($gen);
- }
- }
-}
-
1;
PublicInbox::HlMod->new;
};
-# we need to trigger highlight::CodeGenerator::deleteInstance
-# in HlMod::DESTROY before the rest of Perl shuts down to avoid
-# a segfault at shutdown
-END { $hl = undef };
-
my %QP_MAP = ( A => 'oid_a', B => 'oid_b', a => 'path_a', b => 'path_b' );
my $max_size = 1024 * 1024; # TODO: configurable
my $BIN_DETECT = 8000; # same as git
}
}
-my $nr = $ENV{TEST_MEMLEAK};
-if ($nr && -r "/proc/$$/status") {
- my $fh;
- open $fh, '<', "/proc/$$/status";
- diag "starting at memtest at ".join('', grep(/VmRSS:/, <$fh>));
- PublicInbox::HlMod->new->do_hl(\$orig) for (1..$nr);
- open $fh, '<', "/proc/$$/status";
- diag "creating $nr instances: ".join('', grep(/VmRSS:/, <$fh>));
- my $hls = PublicInbox::HlMod->new;
- $hls->do_hl(\$orig) for (1..$nr);
- $hls = undef;
- open $fh, '<', "/proc/$$/status";
- diag "reused instance $nr times: ".join('', grep(/VmRSS:/, <$fh>));
-}
-
done_testing;