http://www.perlmonks.org?node_id=927989


in reply to Re^4: XS: EXTEND/mPUSHi
in thread XS: EXTEND/mPUSHi

It doesn't appear to leak any memory

Indeed, there's no leak.

Perl's stack isn't refcounted, so that means you can't decrement the refcount of a scalar you place on the stack even if you don't keep a reference to it.

What you do is place the scalar on a list of variables whose refcount needs to be decremented when they are removed from the stack. Variables on this list are called mortals.

(This way of doing things is a source of many bugs, but that's the box you have to play in.)

The "m" in "mPUSHi" stands for mortal. The created scalar will be made mortal.

mPUSHi(4);
boils down to
*(++SP) = sv_2mortal(newSViv(4));

but that still leaves me with the question of whether I should be resetting the stack to account for the single input parameter

Check the first value being returned. It's always your argument (n).

or whether the EXTEND() takes care for that for me?

As documented EXTEND will make sure there is n spaces available. That's it. It doesn't remove anything already there.

In your case, you end up with at least n+1 spaces on the the stack total, one of which is used for the argument.

Weirdness like the completely unused & redundant PUTBACK; return; sequence generated by the Inline::C wrapper functions

It's actually added by the XS to C converter, xsubpp. Inline::C doesn't put there. In fact, Inline::C specifically doesn't want it, but doesn't have a way of telling xsubpp not to call it.

Inline::C requires that you call PUTBACK (explicitly or via XSRETURN) because it can't do it for you because it doesn't have access to your sub's SP to "put it back".

Real XS doesn't require that you call PUTBACK because xsubpp actually creates the sub, so it has access to the sub's SP.

PS — C doesn't require the use of return; when the return type is void.