Nicholas Clark <[EMAIL PROTECTED]> writes:
>On Wed, May 28, 2003 at 02:51:10PM +0700, Oleg Yu. Tokarev wrote:
>> An xs-function has a reference to a hash as its input, like this:
>> my $h = {KEY => 123, VAL => 0};
>> bla::bla( $h );
>> The xs-function updates $h->{VAL} during its execution.
>>
>> Inside itself the xs-function call a C-library function.
>> Its prototype is: int cbla (int key, int *val);
>>
>> The question:
>> Can i get a pointer to int-value saved in the hash?
>> Or in other words: I can get SV saved in the hash. Can i get a pointer
>> to a memory slot in the SV where 'int' saved?
>>
>> '&(SvIV(something))' does not work!
>
>Once you know that SvIV is a macro defined like this in sv.h:
>
>#define SvIV(sv) (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv))
>
>It's hardly surprising, is it? :-)
>
>> May be there is another way to resolve a such problem, is there?
>
>I'm not quite sure of the best way to do this.
>I'm loathe to suggest:
>
>(void) SvIV(something);
>&(SvIVX(something));
>
>as I believe that it's wrong to assume that the side effect of SvIV()
>is that a SV now has a valid IV slot. But I suspect that it will work
>for all the values that you can throw at it from a hash
>(eg it's not possible to give it a literal undef - PL_sv_undef, as an
>accessible SV value)
If I recall correctly references can be silently forced to IV.
The IV slot will be valid but messing with it would be dangerous.
>
>I believe that SvIVX() is the correct way to get at the IV slot, but I think
>that someone else needs to suggest better (more "correct") logic to
>ensure an upgrade sufficient to ensure that an IV slot exists.
Something based on above but with extra checks:
IV *
get_IV(pTHX_ SV *sv)
{
if (SvROK(sv))
Perl_croak(aTHX_ "Cannot convert %_ to IV",sv);
(void) SvIV(sv);
if (SvIOK(sv))
return &(SvIVX(sv));
else
Perl_croak(aTHX_ "Cannot convert %_ to IV",sv);
return NULL;
}
I think that all that is true. But one further thing to consider is
that &SvIVX(something) returns an IV * - which isn't an int * in general.
--
Nick Ing-Simmons
http://www.ni-s.u-net.com/