Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^4: How get PV address?

by xiaoyafeng (Deacon)
on Aug 06, 2019 at 02:40 UTC ( [id://11104008]=note: print w/replies, xml ) Need Help??


in reply to Re^3: How get PV address?
in thread How get PV address?

Thanks, I set return type to UV, it works and I can call it back and forth in C:

use strict; use warnings; use Devel::Peek; my $dd = "hello world\n"; Dump $dd; my $ee = cc($dd); printf("the dd string address is %x \n", cc($dd)); dd($ee); use Inline Config => BUILD_NOISY => 1, CLEAN_AFTER_BUILD => 0; use Inline 'C' => <<'CODE'; int dd(UV x){ printf("string is %s \n", (char*)x); return 0; } UV cc(SV* a){ return SvPVX(a); } CODE ###############output########### andy@andyhome:~/one-off$ perl abe.pl SV = PV(0x56386170b050) at 0x56386172f008 REFCNT = 1 FLAGS = (POK,IsCOW,pPOK) PV = 0x5638617addf0 "hello world\n"\0 CUR = 12 LEN = 14 COW_REFCNT = 1 the dd string address is 5638617addf0 string is hello world

I pass it back to perl, But I got little confused: in typemap, both unsigned int and UV refer to T_UV and in OUTPUT it does convert to UV:

int T_IV unsigned T_UV unsigned int T_UV UV T_UV T_UV sv_setuv($arg, (UV)$var);
why seems perl neglect typemap file but use config?
Platform: osname=linux osvers=4.15.0-52-generic archname=x86_64-linux uname='linux andyhome 4.15.0-52-generic #56-ubuntu smp tue jun 4 2 +2:49:08 utc 2019 x86_64 x86_64 x86_64 gnulinux ' config_args='-de -Dprefix=/home/andy/perl5/perlbrew/perls/perl-5.3 +0.0 -Aeval:scriptdir=/home/andy/perl5/perlbrew/perls/perl-5.30.0/bin' hint=recommended useposix=true d_sigaction=define useithreads=undef usemultiplicity=undef use64bitint=define use64bitall=define uselongdouble=undef usemymalloc=n default_inc_excludes_dot=define bincompat5005=undef Compiler: cc='cc' ccflags ='-fwrapv -fno-strict-aliasing -pipe -fstack-protector-str +ong -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64' optimize='-O2' cppflags='-fwrapv -fno-strict-aliasing -pipe -fstack-protector-str +ong -I/usr/local/include' ccversion='' gccversion='7.4.0' gccosandvers='' intsize=4 longsize=8 ptrsize=8 doublesize=8 byteorder=12345678
Please enlighten me.




I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Replies are listed 'Best First'.
Re^5: How get PV address?
by dave_the_m (Monsignor) on Aug 06, 2019 at 07:13 UTC
    You are using a typemap to create an SV to return to perl. That SV is being assigned an integer value. The only types of integer values that SVs can have are IVs and UVs. Hence a plain integer gets upgraded to an IV/UV before being assigned to the SV.

    Dave.

      Hence a plain integer gets upgraded to an IV/UV before being assigned to the SV.

      That's what I thought before, but as above, in typemap, unsigned int also use sv_setnv function. Does it mean sv_setnv function wouldn't upgrade an IV to UV silently if it get a unsigned integer?




      I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

        I don't understand why you are now talking about sv_setnv(): this doesn't appear in the typemap you showed above.

        The standard typemap for outputting integer values will do either sv_setiv($arg, (IV)$var) or sv_setuv($arg, (UV)$var) depending on whether the integer type is signed or not. The IV or UV types are usually defined as being the largest integer type that the compiler supports: they're large enough to hold a pointer, and often larger than a plain int (8 bytes versus 4 bytes).

        Dave.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11104008]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-25 23:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found