in reply to XS, function writing directly into SV PV pointer, SvGROW doesn't like non PV SVs

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.