Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

why SvUV() would crop to 32 bits?

by ptizoom (Beadle)
on Jan 03, 2015 at 15:10 UTC ( #1112069=perlquestion: print w/replies, xml ) Need Help??
ptizoom has asked for the wisdom of the Perl Monks concerning the following question:

I run perl on a 64bit machine. I try callbacks of death, and pass some pointer to functions from (C to) perl back to C (using swig).... but the swig SvIV()/SvUV()/SvRV() seems to force it back to 32 bits. how annoying. are there any tricks I miss?
in perl sub add_idle_callback($$) { printf("\nadd_idle_callback x%x , x%x \n", @_); &idle_callback_run(@_); # calls }
in .swg %typemap(in) void (* cb)(void *) { $1 = INT2PTR($1_ltype, SvUV($input)); printf(":: x%x to x%x\n", (unsigned long long)SvUV($input), $1); if(SvGMAGICAL($input)) mg_get($input); $1 = DPTR2FPTR($1_ltype, SvUV($input)); printf("::DPTR2FPTR x%x to x%x\n", SvIV($input), $1); }
log ... add_idle_callback x7ffb339f8040 , x0 <==== here in perl the pointer + to function is 64bits ... <=========== but then swig- SvUV() or SvIV() cast it to 32 bits :: x339f8040 to x339f8040 ::DPTR2FPTR x339f8040 to x339f8040 ....

Replies are listed 'Best First'.
Re: why SvUV() would crop to 32 bits?
by Anonymous Monk on Jan 03, 2015 at 16:13 UTC

    Check your C printf format. The %x corresponds to unsigned int, whereas long version %lx is probably indicated.

      Or even better, perl provides the UVxf macro, which will be the correct format to print a UV; i.e.
      printf(":: x%"UVxf" to ..."


        yes on. the macro woz working all the way, but just the format cropping let me down. (although I must have missed it with gdb.)


        printf(":: x%" UVxf " to ...", SvUV($input), $1);
        does the job.

        and here with a code example showing a few ugly ways to cast back pointers to all sorts...:

        %typemap(in) void (* cb)(void *) , void *data , void (cb)(void *){ //if SvGETMAGIC($input) {}; $1 = INT2PTR($1_ltype, SvUV($input)); printf(":: x%" UVxf " to x%lx\n", SvUV($input), $1); { int ret = SWIG_ConvertFunctionPtr($input, (void**)(& $1), 0); printf(":: to x%lx\n", $1); } if(SvGMAGICAL($input)) mg_get($input); $1 = DPTR2FPTR($1_ltype, SvUV($input)); printf("::DPTR2FPTR x%lx to x%lx\n", SvIV($input), $1); }
        bot::add_idle_callback x7fc3ff9bc040 , x0 :: x7fc3ff9bc040 to x7fc3ff9bc040 :: to x7fc3ff9bc040 ::DPTR2FPTR x7fc3ff9bc040 to x7fc3ff9bc040 :: x0 to x0 :: to x0 ::DPTR2FPTR x0 to x0 ::in c add_idle_callback xff9bc040 , x0

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1112069]
Approved by philipbailey
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2018-12-14 22:49 GMT
Find Nodes?
    Voting Booth?
    How many stories does it take before you've heard them all?

    Results (69 votes). Check out past polls.