Thanks for the corrections. My memory was rusty and I didn't double check it. Mea culpa.

Having double checked now, I'd still do things differently than you did but I realized more of the reasons for my reluctance to using SvPVX() and SvPOK_on() and those had to do with dealing with scalars passed in rather than one freshly created right there. So I now agree that your suggestion is safe (for this particular case) (and mine was fatally flawed, of course).

I'd personally still avoid using SvPVX() and SvPOK_on() as sticking with techniques that work in both cases makes sense to me. But, I see I used SvPV_force() and getting the "use of undefined value" warning was actually a desired feature. If I was creating a new scalar, then I'd sv_setpvn(sv,"",0) before doing SvPV_force() and thus not get the warning when it wasn't appropriate (in part because these two steps would likely be in separate macros "of my own design"). But that has a trivial amount of extra overhead that some might dislike. I'd also use SvPOK_only() but do that and the SvCUR_set() only after the call to fill the allocated buffer had succeeded.

So I guess I'd do something like this in 3 steps: 1) Allocate a scalar containing an empty string (preferably with the desired size of buffer), 2) Prepare the scalar to have a large enough buffer and extract a pointer to it (using a macro that doesn't assume a pristine SV), 3) (after the buffer has been "filled") Mark the scalar as containing the string of the proper length. I find that layer of abstraction makes the XS subroutine easier to understand and each of my three macros easier to understand in isolation. In fact, I spent some concerted study on each of my macros, carefully verifying implementation details of the "sv" macros that they use to make sure my macros were correct and safe. And then I hapilly forgot most of the niggling details of all of those "sv" macros which I'd never faithfully remember anyway.

Thanks again and sorry for the misinformation.

- tye        


In reply to Re^5: XS efficient copy (SvCUR_set) by tye
in thread XS efficient copy by TheDauthi

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, details, 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, summary, 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.