in reply to Perl XS: garbage-collecting my malloc'd buffer
The good of this is that you can see how to write or append to a buffer. And is more Perl based.long wxSocketBase::Read( buf , size , leng = 0 ) SV* buf long size long leng CODE: // Tell that the scalar is string only (not integer, double, utf8. +..) SvPOK_only(buf) ; // Get the original size: int org_size = SvCUR(buf) ; // Grow the scalar to receive the data and return a char* point: char* buffer = SvGROW( buf , size + 1 ) ; // Get the char* point of the end if you want to append data (len +> 0): if ( leng > 0 ) { buffer = SvEND(buf) ;} else { org_size = 0 ;} THIS->Read( buffer , size ) ; // Get the amount of data read in the socket: int nread = THIS->LastCount() ; if ( nread > 0 ) { // null-terminate the buffer, not necessary, but does not hurt buffer[nread] = 0 ; // tell Perl how long the string is SvCUR_set( buf , org_size + nread ) ; } // Return the amount of data read, like Perl read(). RETVAL = nread ; OUTPUT: RETVAL
Note that "SvPOK_only(buf)" is important! Since if your variable is set for integer, like this:
.. you can't use SvGROW, SvCUR and SvCUR_set!my $var = 123 ;
Take a look in the POD perlapi and perlguts too.
Graciliano M. P.
"The creativity is the expression of the liberty".
|
---|