|Perl: the Markov chain saw|
XS, pointer scope, strings, allocation responsibility, sv_setpv, SvPV, pack's pointerby patcat88 (Hermit)
|on Oct 24, 2010 at 09:32 UTC||Need Help??|
patcat88 has asked for the
wisdom of the Perl Monks concerning the following question:
In XS, when using sv_setpv or sv_setpvn, what happens to the char pointer? will perl create a SV around the pointer/string, and then free it eventually through normal GC? or will perl copy the contents/string that the pointer points to into its own new allocated memory range that makes up the SV and then its my responsibility to free the pointer manually or through the C block ending?
Similar question for SvPV, I get a pointer from perl, who is supposed to free this pointer? will perl GC/free it when the scalar goes out of scope? is this the pointer of the actual data inside the SV or a newly allocated memory range with the bytes copied from the SV to the new memory range? if I change the data at SvPV's pointer, will the SV be updated in perl land automatically?
What is the SVPVX pointer?
How is SvPV_set different from sv_setpv?
Does perl every "defragment" its SVs which would invalidated any pointers collected from SvPV or SvPVX?
How stable are pack()'s pointers? When does perl free or kill them? What happens if the scalar string in the scalar that pack gave a pointer to shrinks or expands, from Perl land, will the pointer from pack be still valid/the same?
Perl offers Newx and Safefree. How safe to interchange these 2 with native C library free and malloc? Can a pointer made with 1 be freed with the other or vice versa? More specifically on ActivePerl. Are there any cases when you want to use native C library memory management instead of perl memory management or vice versa?