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

patcat88 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to avoid copying the buffer redundantly by doing a sv_setpvn every time. Is this the correct way to have a function write directly into the string pointer in the SV? I did look at the source code for sv_setpvn to write this, but I'm not sure if this is the correct way to do it or not, especially with the null character on the end of the buffer beyond the end of the CUR length. Is buffer ST(1) also? My rough testing says this code works, but are there any edge cases where it wouldn't (magic, undef, SVIV instead of SVPV, reference SV, etc)?

I did find out that even though Perl_sv_grow can handle an undef SV/non PV SV and will upgrade it for you, the macro SvGROW (((XPV*) (mySvPtr)->sv_any)->xpv_len < (length) ? Perl_sv_grow(my_perl,mySvPtr,length) : ((mySvPtr)->sv_u.svu_pv)) on Perl 5.10.0 can't handle a SV without a PV part (access violation, null pointer dereference), so I had to add the SvUPGRADE manually, even though its sorta redundant. Hypothetically I thought of replacing the SvGROW with the function call version, or is that forbidden in XS since the functions behind macros aren't frozen in perl? Also I see a performance reduction if the macro is replaced with the function.

int ReadStream(handle, buffer, bufferSize, bytesRead) unsigned long handle char * buffer = NO_INIT unsigned long bufferSize unsigned long bytesRead = NO_INIT CODE: SvUPGRADE((SV*)ST(1), SVt_PV); buffer = SvGROW((SV*)ST(1), bufferSize+1); RETVAL = ReadStream(handle, buffer, bufferSize, &bytesRead); buffer[bytesRead] = '\0'; SvCUR_set((SV*)ST(1), bytesRead); (void)SvPOK_only_UTF8((SV*)ST(1)); SvTAINT(ST(1)); SvSETMAGIC(ST(1)); OUTPUT: RETVAL bytesRead


Also in the source code for sv_setpvn I saw "SV_CHECK_THINKFIRST_COW_DROP(sv);" ? Do I need it? What is it? This is a made up Win32 function, but its irrelavent. I'm more interested in bugs in my code with the SV interaction.

Replies are listed 'Best First'.
Re: XS, function writing directly into SV PV pointer, SvGROW doesn't like non PV SVs
by ikegami (Patriarch) on Oct 29, 2010 at 07:09 UTC

    Hypothetically I thought of replacing the SvGROW with the function call version, or is that forbidden in XS since the functions behind macros aren't frozen in perl?

    sv_grow is part of the public API. You may use it.

    SvGROW expects a scalar with a PV, whereas sv_grow takes any scalar and creates the PV if necessary. The later is more useful here, so I'm going to assume you switch to sv_grow from here on.

    Is buffer ST(1) also?

    I'm not sure of all the implications of the way you are doing — It doesn't seem right to access the Perl stack when you tell Perl how to handle arguments — so I'm just gonna point out the clean and simple way of doing it: take a scalar for parameter instead of a char*. You don't need any kind of implicit conversion if you already do everything explicitly.

    ReadStream(handle, buffer, bufferSize, bytesRead) unsigned long handle SV* sv_buffer unsigned long bufferSize unsigned long bytesRead = NO_INIT PREINIT: char* buffer; CODE: buffer = sv_grow(sv_buffer, bufferSize+1); ... OUTPUT: RETVAL bytesRead

    but are there any edge cases where it wouldn't (magic, undef, SVIV instead of SVPV, reference SV, etc)?

    You handle set magic. If you wanted to improve your function by having it append to any existing value, you'd have to process get magic to grab the value to which to append.

    I don't know what happens if someone passes a read-only scalar for the buffer. This would include the undef (ReadStream(..., undef, ...)).

    As for undefined scalars in general (ReadSream(..., my $buf, ...)), there's nothing special about those. They're just scalars with their OK flags off.

    You handle any existing SV type since sv_grow upgrades.

    You handle any existing values since sv_grow clears references and you clear anything simpler using SvPOK.

    Also in the source code for sv_setpvn I saw "SV_CHECK_THINKFIRST_COW_DROP(sv);" ? Do I need it? What is it?

    From the comment, sounds like an optimisation for a situation that shouldn't apply to you.

    By the way, make sure that ReadStream doesn't expect bytesRead to be initialised.