From: Anton Berezin Date: Fri, 17 Jul 1998 11:49:30 +0000 (+0200) Subject: perlcall.pod SAVETMPS/FREETMPS bracket X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9b6570b4f9243e4e8b953fb4650b22aab639eabc;p=p5sagit%2Fp5-mst-13.2.git perlcall.pod SAVETMPS/FREETMPS bracket Message-Id: <199807170949.LAA18099@lion.plab.ku.dk> p4raw-id: //depot/perl@1558 --- diff --git a/pod/perlcall.pod b/pod/perlcall.pod index 37916ae..ac60007 100644 --- a/pod/perlcall.pod +++ b/pod/perlcall.pod @@ -526,12 +526,18 @@ The C function required to call I would look like this. { dSP ; + ENTER ; + SAVETMPS ; + PUSHMARK(SP) ; XPUSHs(sv_2mortal(newSVpv(a, 0))); XPUSHs(sv_2mortal(newSViv(b))); PUTBACK ; perl_call_pv("LeftString", G_DISCARD); + + FREETMPS ; + LEAVE ; } Here are a few notes on the C function I. @@ -598,6 +604,36 @@ on how the XPUSH macros work. =item 6. +Because we created temporary values (by means of sv_2mortal() calls) +we will have to tidy up the Perl stack and dispose of mortal SVs. + +This is the purpose of + + ENTER ; + SAVETMPS ; + +at the start of the function, and + + FREETMPS ; + LEAVE ; + +at the end. The C/C pair creates a boundary for any +temporaries we create. This means that the temporaries we get rid of +will be limited to those which were created after these calls. + +The C/C pair will get rid of any values returned by +the Perl subroutine (see next example), plus it will also dump the +mortal SVs we have created. Having C/C at the +beginning of the code makes sure that no other mortals are destroyed. + +Think of these macros as working a bit like using C<{> and C<}> in Perl +to limit the scope of local variables. + +See the section I for details of +an alternative to using these macros. + +=item 7. + Finally, I can now be called via the I function. @@ -660,40 +696,8 @@ The only flag specified this time was G_SCALAR. That means the C<@_> array will be created and that the value returned by I will still exist after the call to I. - - =item 2. -Because we are interested in what is returned from I we cannot -specify G_DISCARD. This means that we will have to tidy up the Perl -stack and dispose of any temporary values ourselves. This is the -purpose of - - ENTER ; - SAVETMPS ; - -at the start of the function, and - - FREETMPS ; - LEAVE ; - -at the end. The C/C pair creates a boundary for any -temporaries we create. This means that the temporaries we get rid of -will be limited to those which were created after these calls. - -The C/C pair will get rid of any values returned by -the Perl subroutine, plus it will also dump the mortal SVs we have -created. Having C/C at the beginning of the code -makes sure that no other mortals are destroyed. - -Think of these macros as working a bit like using C<{> and C<}> in Perl -to limit the scope of local variables. - -See the section I for details of -an alternative to using these macros. - -=item 3. - The purpose of the macro C is to refresh the local copy of the stack pointer. This is necessary because it is possible that the memory allocated to the Perl stack has been reallocated whilst in the @@ -703,7 +707,7 @@ If you are making use of the Perl stack pointer in your code you must always refresh the local copy using SPAGAIN whenever you make use of the I functions or any other Perl internal function. -=item 4. +=item 3. Although only a single value was expected to be returned from I, it is still good practice to check the return code from I @@ -715,7 +719,7 @@ didn't check for that possibility and take appropriate action the Perl stack would end up in an inconsistent state. That is something you I don't want to happen ever. -=item 5. +=item 4. The C macro is used here to pop the return value from the stack. In this case we wanted an integer, so C was used. @@ -730,7 +734,7 @@ they return. POPi integer POPl long -=item 6. +=item 5. The final C is used to leave the Perl stack in a consistent state before exiting the function. This is necessary because when we