lib/Linux/FD/Mem.xs

filter: Statementstmt 92.6% 38 / 41 Branchbran 63.8% 23 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 79.2% 61 / 77 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Linux/FD/Mem.pm »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #include <sys/mman.h>
7 #include <sys/syscall.h>
8 #include <linux/memfd.h>
9  
10 #define die_sys(format) Perl_croak(aTHX_ format, strerror(errno))
11  
12 1 static SV* S_io_fdopen(pTHX_ int fd, const char* classname, char type) {
13 1 PerlIO* pio = PerlIO_fdopen(fd, "r");
14 1 ▶ GV* gv = newGVgen(classname ? classname : "Linux::FD::Mem");
Branch: 1 branch
BranchTrueFalseTotal
GV* gv = newGVgen(classname ? classname : "Linux::FD::Mem"); 1 0 1
15 1 SV* ret = newRV_noinc((SV*)gv);
16 1 ▶ IO* io = GvIOn(gv);
Branch: 5 branches
BranchTrueFalseTotal
IO* io = GvIOn(gv); 1 0 1
IO* io = GvIOn(gv); 0 1 1
IO* io = GvIOn(gv); 0 0 0
IO* io = GvIOn(gv); 1 0 1
IO* io = GvIOn(gv); 0 1 1
17 1 IoTYPE(io) = type;
18 1 IoIFP(io) = pio;
19 1 IoOFP(io) = pio;
20 1 ▶ if (classname) {
Branch: 1 branch
BranchTrueFalseTotal
if (classname) { 1 0 1
21 1 HV* stash = gv_stashpv(classname, FALSE);
22 1 sv_bless(ret, stash);
23 }
24 1 return ret;
25 }
26 #define io_fdopen(fd, classname, type) S_io_fdopen(aTHX_ fd, classname, type)
27  
28 typedef struct { const char* key; size_t length; int value; } map[];
29  
30 static const map mem_flags = {
31 { STR_WITH_LEN("allow-sealing"), MFD_ALLOW_SEALING },
32 #ifdef MFD_HUGETLB
33 { STR_WITH_LEN("huge-table"), MFD_HUGETLB },
34 { STR_WITH_LEN("huge-2mb"), MFD_HUGE_2MB },
35 { STR_WITH_LEN("huge-1gb"), MFD_HUGE_1GB },
36 #endif
37 };
38  
39 1 static UV S_get_mem_flag(pTHX_ SV* flag_name) {
40 int i;
41 1 ▶ for (i = 0; i < sizeof mem_flags / sizeof *mem_flags; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sizeof mem_flags / sizeof *mem_flags; ++i) 1 0 1
42 2 ▶ if (strEQ(SvPV_nolen(flag_name), mem_flags[i].key))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(SvPV_nolen(flag_name), mem_flags[i].key)) 1 0 1
43 1 return mem_flags[i].value;
44 0 Perl_croak(aTHX_ "No such flag '%s' known", SvPV_nolen(flag_name));
45 }
46 #define get_mem_flag(name) S_get_mem_flag(aTHX_ name)
47  
48 static const map seal_flags = {
49 { STR_WITH_LEN("seal"), F_SEAL_SEAL },
50 { STR_WITH_LEN("shrink"), F_SEAL_SHRINK },
51 { STR_WITH_LEN("grow"), F_SEAL_GROW },
52 { STR_WITH_LEN("write"), F_SEAL_WRITE },
53 #ifdef F_SEAL_FUTURE_WRITE
54 { STR_WITH_LEN("future-write"), F_SEAL_FUTURE_WRITE },
55 #endif
56 };
57  
58 3 static UV S_get_seal_flag(pTHX_ SV* flag_name) {
59 int i;
60 8 ▶ for (i = 0; i < sizeof seal_flags / sizeof *seal_flags; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sizeof seal_flags / sizeof *seal_flags; ++i) 8 0 8
61 16 ▶ if (strEQ(SvPV_nolen(flag_name), seal_flags[i].key))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(SvPV_nolen(flag_name), seal_flags[i].key)) 3 5 8
62 3 return seal_flags[i].value;
63 0 Perl_croak(aTHX_ "No such seal '%s' known", SvPV_nolen(flag_name));
64 }
65 #define get_seal_flag(name) S_get_seal_flag(aTHX_ name)
66  
67 #define get_fd(self) PerlIO_fileno(IoOFP(sv_2io(SvRV(self))))
68  
69 MODULE = Linux::FD::Mem PACKAGE = Linux::FD::Mem
70  
71 SV*
72 new(classname, name, ...)
73 const char* classname;
74 const char* name;
75 PREINIT:
76 int memfd;
77 1 int i, flags = MFD_CLOEXEC;
78 CODE:
79 2 ▶ for (i = 2; i < items; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i < items; i++) 1 1 2
80 1 flags |= get_mem_flag(ST(i));
81 1 memfd = syscall(__NR_memfd_create, name, flags);
82 1 ▶ if (memfd < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (memfd < 0) 0 1 1
83 0 die_sys("Couldn't open memfd: %s");
84 1 RETVAL = io_fdopen(memfd, classname, '+');
85 OUTPUT:
86 RETVAL
87  
88 void
89 seal(file_handle, ...)
90 SV* file_handle;
91 PREINIT:
92 3 int fd, seals = 0, i, ret;
93 CODE:
94 3 fd = get_fd(file_handle);
95 6 ▶ for (i = 1; i < items; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) 3 3 6
96 3 seals |= get_seal_flag(ST(i));
97 3 ret = fcntl(fd, F_ADD_SEALS, seals);
98 3 ▶ if (ret < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ret < 0) 1 2 3
99 1 die_sys("Couldn't add seal: %s");
100  
101 void
102 get_seals(file_handle)
103 SV* file_handle;
104 PREINIT:
105 int seals, fd, i;
106 PPCODE:
107 3 fd = get_fd(file_handle);
108 3 seals = fcntl(fd, F_GET_SEALS, 0);
109 18 ▶ for (i = 0; i < sizeof seal_flags / sizeof *seal_flags; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sizeof seal_flags / sizeof *seal_flags; ++i) { 15 3 18
110 15 ▶ if (seal_flags[i].value & seals)
Branch: 1 branch
BranchTrueFalseTotal
if (seal_flags[i].value & seals) 3 12 15
111 3 ▶ mXPUSHp(seal_flags[i].key, seal_flags[i].length);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHp(seal_flags[i].key, seal_flags[i].length); 0 3 3
112 }
↑ Summary blib/lib/Linux/FD/Mem.pm »