Perl/memfd.pm

filter: Statementstmt 81.5% 31 / 38 Branchbran 42.8% 6 / 14 Conditioncond 66.6% 4 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 71.0% 49 / 69 CC 10 SCAR 24.4
CC
10
Cov
71%
new
16.6
BEGIN
1.0
BEGIN
1.0
CRAP 11.4
↑ Summary Perl/aio.pm »
1 package Linux::Perl::memfd;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =encoding utf-8
7  
8 =head1 NAME
9  
10 Linux::Perl::memfd
11  
12 =head1 SYNOPSIS
13  
14 my $fh = Linux::Perl::memfd->(
15 name => 'whatever', #optional
16 flags => ['CLOEXEC'], #optional
17  
18 huge_page_size => '64KB', #optional
19 );
20  
21 syswrite( $fh, 'some data' );
22  
23 sysseek( $fh, 0, 0 );
24  
25 sysread( $fh, my $buf, 9 );
26  
27 =head1 DESCRIPTION
28  
29 This is a Perl interface to memfd.
30  
31 See C<man 2 memfd_create> for more details about this functionality.
32  
33 =cut
34  
35 5 ▶use Linux::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 5 ▶use Linux::Perl::ParseFlags;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 5 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
39 5 _flag_CLOEXEC => 1,
40 _flag_ALLOW_SEALING => 2,
41  
42 _MAX_NAME_LENGTH => 249,
43  
44 _hugetlb_flag => 4,
45 _hugetlb_flag_encode_shift => 26,
46 _hugetlb_size_num => {
47 '64KB' => 16,
48 '512KB' => 19,
49 '1MB' => 20,
50 '2MB' => 21,
51 '8MB' => 23,
52 '16MB' => 24,
53 '256MB' => 28,
54 '1GB' => 30,
55 '2GB' => 31,
56 '16GB' => 34,
57 },
58 5 };
59  
60 =head1 METHODS
61  
62 =head2 I<CLASS>->new( %OPTS )
63  
64 Instantiates this class. Instances of the class are regular
65 Perl file handle objects and can be interacted with as such.
66  
67 %OPTS are:
68  
69 =over
70  
71 =item * C<name>: Optional, as described in C<man 2 memfd_create>.
72 An empty string is used by default.
73  
74 =item * C<flags>: Optional, may contain any or all of: C<CLOEXEC>,
75 C<ALLOW_SEALING>.
76  
77 =item * C<huge_page_size>: Optional, must be one of: C<64KB>, C<512KB>,
78 C<1MB>, C<2MB>, C<8MB>, C<16MB>, C<256MB>, C<1GB>, C<2GB>, C<16GB>. Your
79 kernel may or may not support this functionality.
80  
81 =back
82  
83 =cut
84  
85 8 ▶sub new {
Subroutine new: called CC=10 SCAR=28.1
Pod documented
86 8 my ($class, %opts) = @_;
87  
88 8 local ($!, $^E);
89  
90 8 ▶ my $arch_module = $class->can('NR_memfd_create') && $class;
Condition66%2/3
$class->can('NR_memfd_create') && $class
!ll&&!rl&&r
404
MC/DC50%1/2
$class->can('NR_memfd_create') && $class
$class->can('NR_memfd_create') $class
Truth table66%2/3
A && B
A= $class->can('NR_memfd_create')B= $class
ABresult
0X0
100
111
91 8 ▶ $arch_module ||= do {
Condition66%2/3
$arch_module ||= do { require Linux::Perl::ArchLoader; Linux::Perl::ArchLoader::get_arch_module($class) }
l!l&&r!l&&!r
440
MC/DC0%0/2
$arch_module ||= do { require Linux::Perl::ArchLoader; Linux::Perl::ArchLoader::get_arch_module($class) }
$arch_module do { require Linux::Perl::ArchLoader; Linux::Perl::ArchLoader::get_arch_module($class) }
Truth table66%2/3
A ||= B
A= $arch_moduleB= do { require Linux::Perl::ArchLoader; Linux::Perl::ArchLoader::get_arch_module($class) }
ABresult
1X1
011
000
92 4 require Linux::Perl::ArchLoader;
93 4 Linux::Perl::ArchLoader::get_arch_module($class);
94 };
95  
96 8 ▶ if ($opts{'name'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts{'name'}) { } 6 2 8
elsif (not defined $opts{'name'}) { } 2 0 2
97 6 ▶ if ($opts{'name'} =~ tr<\0><>) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"name"} =~ tr/\000//) 0 6 6
98 0 die "'name' cannot contain NUL bytes!";
99 }
100  
101 6 ▶ if (length($opts{'name'}) > _MAX_NAME_LENGTH()) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $opts{"name"} > 249) 0 6 6
102 0 die sprintf( "'name' (%d bytes) cannot exceed %d bytes.", length($opts{'name'}), _MAX_NAME_LENGTH() );
103 }
104 }
105 elsif (!defined $opts{'name'}) {
106 2 $opts{'name'} = q<>;
107 }
108  
109 8 my $flags = Linux::Perl::ParseFlags::parse($arch_module, $opts{'flags'});
110  
111 8 ▶ if ( my $huge = $opts{'huge_page_size'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $huge = $opts{"huge_page_size"}) 0 8 8
112 0 ▶ if ($flags & _flag_ALLOW_SEALING()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 2) 0 0 0
113 0 die "Huge page sizes cannot be used with ALLOW_SEALING!";
114 }
115  
116 0 ▶ my $page_size_num = _hugetlb_size_num()->{$huge} or do {
Branch: 1 branch
BranchTrueFalseTotal
my $page_size_num = {"16GB" => 34,"16MB" => 24,"1GB" => 30,"1MB" => 20,"256MB" => 28,"2GB" => 31,"2MB" => 21,"512KB" => 19,"64KB" => 16,"8MB" => 23}->{$huge} or do { die "Unknown huge page size: $huge\n" } 0 0 0
117 0 die "Unknown huge page size: $huge\n";
118 };
119  
120 0 $flags |= (0 + _hugetlb_flag()) | ($page_size_num << _hugetlb_flag_encode_shift());
121 }
122  
123 my $fd = Linux::Perl::call(
124 $arch_module->NR_memfd_create(),
125 8 $opts{'name'},
126 0 + $flags,
127 );
128  
129 #Force CLOEXEC if the flag was given.
130 #local $^F = 0 if $flags & $arch_module->_flag_CLOEXEC();
131  
132 8 open my $fh, '+<&=' . $fd;
133  
134 8 return bless $fh, $arch_module;
135 }
136  
137 5 1;
↑ Summary Perl/aio.pm »