1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
|
|
5 | #include <fcntl.h> |
|
|
6 | #include <unistd.h> |
|
|
7 | |
|
|
8 | typedef CV *B__CV; |
|
|
9 | |
5 | static OP *(*old_entersub)(pTHX); |
10 | static OP *(*old_entersub)(pTHX); |
6 | |
|
|
7 | static void |
|
|
8 | traverse (OP *op) |
|
|
9 | { |
|
|
10 | } |
|
|
11 | |
|
|
12 | static void |
|
|
13 | analyze (CV *cv) |
|
|
14 | { |
|
|
15 | OP *op = CvSTART (cv); |
|
|
16 | |
|
|
17 | traverse (op); |
|
|
18 | } |
|
|
19 | |
11 | |
20 | // this is, of course, a slower entersub |
12 | // this is, of course, a slower entersub |
21 | static OP * |
13 | static OP * |
22 | faster_entersub (pTHX) |
14 | faster_entersub (pTHX) |
23 | { |
15 | { |
24 | dSP; |
16 | static int in_perl; |
25 | dTOPss; |
|
|
26 | |
17 | |
27 | if (SvTYPE (sv) == SVt_PVGV) |
18 | if (!PL_compcv || in_perl) // only when not compiling, reduces recompiling due to op-address-shift |
28 | sv = GvCV (sv); |
19 | { |
|
|
20 | dSP; |
|
|
21 | dTOPss; |
29 | |
22 | |
30 | if (sv) |
23 | ++in_perl; |
31 | { |
|
|
32 | sv_dump (sv); |
|
|
33 | // only once for now |
|
|
34 | PL_op->op_ppaddr = old_entersub; |
|
|
35 | |
24 | |
36 | // only simple cv calls for now |
25 | if (SvTYPE (sv) == SVt_PVGV) |
37 | if (!PL_perldb && SvTYPE (sv) == SVt_PVCV && !CvXSUB (sv)) |
26 | sv = (SV *)GvCV (sv); |
|
|
27 | |
|
|
28 | if (sv) |
38 | { |
29 | { |
|
|
30 | // only once for now |
|
|
31 | PL_op->op_ppaddr = old_entersub; |
|
|
32 | |
|
|
33 | // only simple cv calls for now |
|
|
34 | if (!PL_perldb && !PL_tainting |
|
|
35 | && SvTYPE (sv) == SVt_PVCV && !CvISXSUB (sv) |
|
|
36 | && CvSTART (sv) // must exist |
|
|
37 | && CvSTART (sv)->op_type != OP_NULL) // shield against compiling an already-compiled op |
|
|
38 | { |
39 | sv = newSViv (PTR2IV (sv)); |
39 | SV *bsv = newSViv (PTR2IV (sv)); |
|
|
40 | |
40 | ENTER; |
41 | ENTER; |
41 | SAVETMPS; |
42 | SAVETMPS; |
42 | PUSHMARK (SP); |
43 | PUSHMARK (SP); |
43 | // emulate B::CV typemap entry we don't have |
44 | // emulate B::CV typemap entry we don't have |
44 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (sv), gv_stashpv ("B::CV", 1)))); |
45 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (bsv), gv_stashpv ("B::CV", 1)))); |
45 | PUTBACK; |
46 | PUTBACK; |
46 | call_pv ("Faster::entersub", G_VOID|G_DISCARD); |
47 | call_pv ("Faster::entersub", G_VOID|G_DISCARD|G_EVAL); |
47 | SPAGAIN; |
48 | SPAGAIN; |
48 | FREETMPS; |
49 | FREETMPS; |
49 | LEAVE; |
50 | LEAVE; |
|
|
51 | } |
50 | } |
52 | } |
|
|
53 | |
|
|
54 | --in_perl; |
51 | } |
55 | } |
52 | |
56 | |
53 | return old_entersub (aTHX); |
57 | return old_entersub (aTHX); |
54 | } |
58 | } |
55 | |
59 | |
56 | MODULE = Faster PACKAGE = Faster |
60 | MODULE = Faster PACKAGE = Faster |
57 | |
61 | |
|
|
62 | PROTOTYPES: ENABLE |
|
|
63 | |
|
|
64 | IV |
|
|
65 | ppaddr (int optype) |
|
|
66 | CODE: |
|
|
67 | RETVAL = optype == OP_ENTERSUB |
|
|
68 | ? (IV)old_entersub |
|
|
69 | : (IV)PL_ppaddr [optype]; |
|
|
70 | OUTPUT: |
|
|
71 | RETVAL |
|
|
72 | |
58 | void |
73 | void |
59 | hook_entersub () |
74 | hook_entersub () |
60 | CODE: |
75 | CODE: |
61 | old_entersub = PL_ppaddr [OP_ENTERSUB]; |
76 | old_entersub = PL_ppaddr [OP_ENTERSUB]; |
62 | PL_ppaddr [OP_ENTERSUB] = faster_entersub; |
77 | PL_ppaddr [OP_ENTERSUB] = faster_entersub; |
63 | |
78 | |
|
|
79 | void |
|
|
80 | patch_cv (B::CV cv, void *ptr) |
|
|
81 | CODE: |
|
|
82 | { |
|
|
83 | OP *op; |
64 | |
84 | |
|
|
85 | if (!ptr) |
|
|
86 | croak ("NULL not allowed as code address for patch_cv"); |
|
|
87 | |
|
|
88 | NewOp (0, op, 1, OP); |
|
|
89 | |
|
|
90 | op->op_sibling = CvSTART (cv); |
|
|
91 | op->op_type = OP_NULL; |
|
|
92 | op->op_ppaddr = ptr; |
|
|
93 | |
|
|
94 | CvSTART (cv) = op; |
|
|
95 | } |
|
|
96 | |
|
|
97 | bool |
|
|
98 | fcntl_lock (int fd) |
|
|
99 | CODE: |
|
|
100 | { |
|
|
101 | struct flock lck; |
|
|
102 | lck.l_type = F_WRLCK; |
|
|
103 | lck.l_whence = SEEK_SET; |
|
|
104 | lck.l_start = 0; |
|
|
105 | lck.l_len = 0; |
|
|
106 | |
|
|
107 | RETVAL = fcntl (fd, F_SETLKW, &lck) == 0; |
|
|
108 | } |
|
|
109 | OUTPUT: |
|
|
110 | RETVAL |
|
|
111 | |
|
|
112 | |
|
|
113 | |