Discussion:
char * argument, undef -> NULL
(too old to reply)
Florian Weimer
2007-09-07 09:43:21 UTC
Permalink
Is there an easy way to get a char * argument which is set to NULL
when the passed scalar is undefined?

Currently, I'm using

typedef char *crypt_gnutls_string;

and the following typemap entries:

crypt_gnutls_string T_GNUTLS_STRING

T_GNUTLS_STRING
$var = SvOK($arg) ? SvPV_nolen($arg) : NULL;

This works, but I wonder if there is a built-in solution.
--
Florian Weimer <***@bfk.de>
BFK edv-consulting GmbH http://www.bfk.de/
Kriegsstraße 100 tel: +49-721-96201-1
D-76133 Karlsruhe fax: +49-721-96201-99
Steve Fink
2007-09-08 04:17:04 UTC
Permalink
I'm not saying that this is necessarily a good idea, but I became
similarly annoyed with some of the built-in typemaps. In my case, it
was AV* and HV*, which leak memory by default unless you do stuff that
is not needed with SV* returns. This is known, and documented, but
unfixable because of backwards compatibility.

I don't need backwards compatibility for my own stuff, and my custom
typemap is only used for my stuff, so I overrode T_AV and T_HV. It
seems to be happy taking the last definition it finds. You could do
the same with T_PV.
Florian Weimer
2007-09-10 09:28:38 UTC
Permalink
Post by Steve Fink
I'm not saying that this is necessarily a good idea, but I became
similarly annoyed with some of the built-in typemaps. In my case, it
was AV* and HV*, which leak memory by default unless you do stuff that
is not needed with SV* returns. This is known, and documented, but
unfixable because of backwards compatibility.
Could you post the snippet? I'd like to use that as well.
Post by Steve Fink
I don't need backwards compatibility for my own stuff, and my custom
typemap is only used for my stuff, so I overrode T_AV and T_HV. It
seems to be happy taking the last definition it finds. You could do
the same with T_PV.
Oh, in other cases, the definedness check is quite convenient.
--
Florian Weimer <***@bfk.de>
BFK edv-consulting GmbH http://www.bfk.de/
Kriegsstraße 100 tel: +49-721-96201-1
D-76133 Karlsruhe fax: +49-721-96201-99
Steve Fink
2007-09-12 20:00:05 UTC
Permalink
Post by Florian Weimer
Post by Steve Fink
I'm not saying that this is necessarily a good idea, but I became
similarly annoyed with some of the built-in typemaps. In my case, it
was AV* and HV*, which leak memory by default unless you do stuff that
is not needed with SV* returns. This is known, and documented, but
unfixable because of backwards compatibility.
Could you post the snippet? I'd like to use that as well.
OUTPUT
T_AVREF
$arg = $var ? Perl_newRV(aTHX_ Perl_sv_2mortal(aTHX_ (SV *)
$var)) : &PL_sv_undef;
T_HVREF
$arg = $var ? Perl_newRV(aTHX_ Perl_sv_2mortal(aTHX_ (SV *)
$var)) : &PL_sv_undef;

The default typemap is just '$arg = newRV((SV*)$var);'. So the
differences are an sv_2mortal and the same sort of NULL -> undef
mapping that you were talking about.

Loading...