Anyone got any idea why the following leaks like a sieve:
char *
getline(str)
SV * str
CODE:
{
STRLEN len;
char *cval = SvPV(str, len);
char *ret = (char*)calloc(len+1, sizeof(char));
I32 taken = 0;
if (!SvOK(str)) {
XSRETURN_UNDEF;
}
if (SvTYPE(str) < SVt_PVIV)
sv_upgrade(str,SVt_PVIV);
RETVAL = ret;
while (*cval) {
*ret = *cval;
if (*cval == '\n')
break;
*cval++; *ret++; taken++;
}
if (*cval == '\n') {
*cval++; *ret++; taken++;
}
*ret = 0;
if (!SvOOK(str)) {
SvOOK_on(str);
SvIVX(str) = 0;
}
SvIVX(str) += taken;
SvPVX(str) += taken;
SvLEN(str) -= taken;
SvCUR(str) -= taken;
}
OUTPUT:
RETVAL
It's designed to be a fast replacement for: s/^(.*?)\n//; in case you were wondering, and uses the rather underdocumented OOK hack in perl core (which allows you to quickly truncate off the start of a string by specifying an offset in the IV slot).
Re: XS/guts Expert help needed (Score:1)
Aren't you calloc()ing a char buffer without free()ing it.
John.
--
Re: (Score:2)
Re: (Score:2)
Re: (Score:1)
Re: (Score:2)
Re: (Score:1)
SV *
getline(str)
SV * str
CODE:
{
STRLEN len;
char *cval = SvPV(str, len);
Re: (Score:2)
Re: (Score:1)
CLEANUP section:
CLEANUP:
free (ret);
Custom typemap: in a typemap file:
TYPEMAP
char_own * T_CHAR_OWN
OUTPUT
T_CHAR_OWN
sv_setpv ((SV*)$arg, $var);
g_f
Re: (Score:1)
But you shouldn't use calloc() or other system level memory allocation routines. You should use New() or one of its variants. This is because Perl may be configured to use its own malloc routines and using the system ones at the same time could lead to issues. IOW, it might work fine for you on your box but might go disasterously wrong when built on a Perl with a different configura