in reply to XS efficient copy

try this instead:
ExpensiveGetString(row, col, message, 83); av_push(array, newSVpvn(message, 83));

Replies are listed 'Best First'.
Re^2: XS efficient copy
by creamygoodness (Curate) on Apr 06, 2006 at 20:11 UTC

    That's a copy, which the OP doesn't want.

    I think the problem in the original code is the use of SvPV_nolen instead of SvPVX. If XS is doing the right thing, it's returning a (new) buffer to an empty string, because the SV is still undefined and while its string has been allocated, it hasn't been initialized. This is the usual formula for creating a buffer, then passing the buffer to a C routine:

    message_sv = newSV(83); SvPOK_on(message_sv); SvCUR_set(message_sv, 82); message = SvPVX(message_sv); ExpensiveGetString(row, col, message, 83); av_push(array, message_sv);
    Ordinarily, one would null terminate with *SvEND(message_sv) = "\0", but I'm assuming that the null byte is the 83rd byte, so there's no need.
    --
    Marvin Humphrey
    Rectangular Research ― http://www.rectangular.com
      Thanks, your code works, and helped me figure it out.
      I had two problems.
      SvPOK_on must be called at some point, otherwise, even though it contains data, perl doesn't 'know' that it contains data. Also, I wasn't setting the SvCUR_set, which makes perl think I had an empty string.
      I went through several variations on this theme, but all of them were missing that key piece... which makes perfect sense after reading perlguts a bit more carefully.

        perlguts doesn't cover the writing of strings as fully as it does reading. I went through the same confusion -- earlier versions of one of my XS distros, Sort::External, performed unnecessary copy operations before I figured it out.

        Spelunking sv.h and sv.c in the Perl source can be very helpful.

        --
        Marvin Humphrey
        Rectangular Research ― http://www.rectangular.com

      Actually, SvPV_nolen() is safer than SvPVX(); SvPVX() gives you a pointer to the "PV" (the scalar's string value) even if the scalar doesn't have a string value while SvPV_nolen() will force the scalar to get a string value if it doesn't have one already.

      The problem is that the size of the buffer is specified but the length of the string stored in that buffer is never set. I'd change your XS-code-for-cargo-culting, (:, to:

      int bufsize= 83; SV* svBuf= newSV( bufsize ); char* pBuf= SvPV_nolen( svBuf ); ...( ..., pBuf, bufsize, ... ); SvCUR_set( svBuf, length_of_data_written );

      where "length_of_data_written" might be variable, such as the return value from the function that stuffs characters into pBuf.

      - tye        

        Hi Tye,

        While it's true that SvPV_nolen is safer for reading, SvPVX and SvPV_nolen are equally dangerous with regards to the write operation that TheDauthi wants to deploy. In both cases, it is absolutely necessary to...

        1. allocate space via newSV(STRLEN), SvGROW(SV*, STRLEN), etc.
        2. make the SV* "POK" via SvPOK_on(SV*), so that it knows it contains a string.

        The SvPV_nolen macro first checks the SV's private SVf_POK flag to see whether it contains a string. If it does, then it returns the pointer to the string, via SvPVX(SV*). If it doesn't, it calls sv_2pv_flags, which spits out a Use of uninitialized value warning, upgrades the sv, and returns (char*)"". That's safe to read from, but if you try to write to it... kaboom.

        Here's a demo app...

        #!/usr/bin/perl use strict; use warnings; use Inline C => <<'END_C'; void POKe() { SV *good_sv, *bad_sv; char *good_ptr, *bad_ptr; good_sv = newSV(83); bad_sv = newSV(83); SvPOK_on(good_sv); /* !!!! */ good_ptr = SvPV_nolen(good_sv); Copy("Joy!", good_ptr, 4, char); SvCUR_set(good_sv, 4); fprintf(stderr, "%s\n", SvPVX(good_sv)); bad_ptr = SvPV_nolen(bad_sv); fprintf(stderr, "wait for it...\n"); Copy("DEATH!", bad_ptr, 6, char); fprintf(stderr, "in heaven, everything is fine..."); } END_C POKe();

        ... and here's the output on my system...

        slothbear:~/perltest marvin$ perl sv_poke.plx Joy! Use of uninitialized value in subroutine entry at sv_poke.plx line 33. wait for it... Bus error slothbear:~/perltest marvin$
        --
        Marvin Humphrey
        Rectangular Research ― http://www.rectangular.com