Nick Ing-Simmons wrote:

>Steve Hay <[EMAIL PROTECTED]> writes:
>  
>
>>I have an XSUB that returns a list of things like this:
>>
>>void
>>foo()
>>   PPCODE:
>>   {
>>       EXTEND(SP, 2);
>>       PUSHs(sv_2mortal(newSViv(42)));
>>       PUSHs(sv_2mortal(newSVpvn("Forty-two", 9)));
>>       XSRETURN(2);
>>   }
>>
>>It works fine as it is, but I though that the following might be a 
>>little easier on the eye:
>>
>>void
>>foo()
>>   PPCODE:
>>   {
>>       EXTEND(SP, 2);
>>       PUSHi(42);
>>       PUSHp("Forty-two", 9);
>>       XSRETURN(2);
>>   }
>>
>>However, when I try to build the latter I get this compiler error:
>>
>>'targ' : undeclared identifier
>>
>>Having hunted around a bit for examples of other things using PUSHi() 
>>and PUSHp(), I found that the constant() XSUB in a typical 
>>"const-xs.inc" file produed by ExtUtils::Constants contains a PREINIT: 
>>section like this:
>>
>>   PREINIT:
>>#ifdef dXSTARG
>>   dXSTARG; /* Faster if we have it.  */
>>#else
>>   dTARGET;
>>#endif
>>
>>and sure enough, if I add that to my XSUB then it builds and works OK 
>>once more.
>>
>>Why is this necessary, 
>>    
>>
>
>PUSHi et. al. are core perl utils and (most) perl ops have a TARG SV
>
I was puzzled by the difference with PUSHs(), that's all.  Are you 
implying that PUSHs() is not a core Perl util, or just that it happens 
to be one that doesn' t have a targ SV?

>>Aside from all that, is the PUSHi()/PUSHp() example above OK?  It 
>>doesn't appear to leak, but I'm always wary of removing calls to 
>>sv_2mortal().  
>>    
>>
>
>It it doesn't leak or give "attempt to free ..." warnings 
>it is probably okay.
>
>(Devel::Leak may help here.)
>
Nice little module.  (Why can't it do dumps unless you have a DEBUGGING 
Perl?  Just removing

#ifndef DEBUGGING
#define sv_dump(sv) PerlIO_printf(PerlIO_stderr(), "\n")
#endif

from Leak.xs lets it produce SV dumps with my "release" build Perl...)

Anyway, Devel::Leak says that there are no leaks.

However, it seems that I've taken my eye off the ball with all these 
compilation problems and concerns over memory leaks:  the 
PUSHi()/PUSHp() version of the XSUB above doesn't actually work properly!

Instead of returning (42, "Forty-two"), it returns ("Forty-two", 
"Forty-two").  In general, if I use PUSHi() or PUSHp() to push N things 
onto the stack and then XSRETURN(N) then I seem to get N copies of the 
last return value that I pushed.

What have I done wrong?  (I tried using CODE: instead of PPCODE:, but to 
no avail.)

- Steve



------------------------------------------------
Radan Computational Ltd.

The information contained in this message and any files transmitted with it are 
confidential and intended for the addressee(s) only.  If you have received this 
message in error or there are any problems, please notify the sender immediately.  The 
unauthorized use, disclosure, copying or alteration of this message is strictly 
forbidden.  Note that any views or opinions presented in this email are solely those 
of the author and do not necessarily represent those of Radan Computational Ltd.  The 
recipient(s) of this message should check it and any attached files for viruses: Radan 
Computational will accept no liability for any damage caused by any virus transmitted 
by this email.

Reply via email to