xiaoyafeng has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks

I found that perlcall and perlxs treat mortal variable are different. When you calls perl subroutine from C, you have to save and free them manually, but in XS code, you don't have to do that. like:

#in perlcall, you have to use ENTER,SAVETMPS, FREETMPS, LEAVE to free +mortal variables dSP; ENTER; SAVETMPS; PUSHMARK(SP); EXTEND(SP, 2); PUSHs(sv_2mortal(newSVpv(a, 0))); PUSHs(sv_2mortal(newSViv(b))); PUTBACK; call_pv("LeftString", G_DISCARD); FREETMPS; LEAVE; #in XS, you don't have to do that: void rpcb_gettime(host) char *host PREINIT: time_t timep; bool_t status; PPCODE: status = rpcb_gettime( host, &timep ); EXTEND(SP, 2); PUSHs(sv_2mortal(newSViv(status))); PUSHs(sv_2mortal(newSViv(timep)));
I've searched C file generated by XS, there are still no any code to free mortal variables. So When these mortal variable are freed? Please enlightened me. TIA.




I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Replies are listed 'Best First'.
Re: What does XSUB do on TMPS Stack for us?
by dave_the_m (Monsignor) on Jul 23, 2019 at 06:48 UTC
    Typically, any mortals created during the call to an XS function are freed when the next OP_NEXTSTATE op is called (i.e. at the start of the next statement), or when the current block scope is exited - whichever occurs first.

    Dave.

      Thanks! It's very clear.




      I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction