Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re^5: XS: EXTEND/mPUSHi by ikegami
in thread XS: EXTEND/mPUSHi by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (4)
    As of 2017-12-16 17:13 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      What programming language do you hate the most?




















      Results (457 votes). Check out past polls.

      Notices?