Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

SvUV vs SvIV for pointers in SVs, typemap

by patcat88 (Deacon)
on Feb 20, 2011 at 19:53 UTC ( #889223=perlquestion: print w/ replies, xml ) Need Help??
patcat88 has asked for the wisdom of the Perl Monks concerning the following question:

This is probably a naive question. I've been looking at [lib/ExtUtils/typemap#l147 in perl.git] and have been wondering, shouldn't pointers be SvUV not SvIV? or it makes no difference since the bit layout in memory is the same, and the SVf_IVisUV is only relevant when printing the SvIV as ASCII?

Maybe this question should be reasked as, whats the difference between how perl handles SvIVs and SvUVs and side effects of one verse the other.

Comment on SvUV vs SvIV for pointers in SVs, typemap
Re: SvUV vs SvIV for pointers in SVs, typemap
by ikegami (Pope) on Feb 20, 2011 at 20:32 UTC

    There's no such thing as a SvUV. A ref can be in practically any type of SV (at least SvIV, SvNV, SvPV, SvPVIV, SvPVNV).

    >perl -MDevel::Peek -e"my $x = {}; Dump($x);" SV = IV(0x3269b8) at 0x3269c0 REFCNT = 1 FLAGS = (PADMY,ROK) ... >perl -MDevel::Peek -e"my $x = 'abc'; $x = {}; Dump($x);" SV = PV(0x33abe0) at 0x2a69d0 REFCNT = 1 FLAGS = (PADMY,ROK) ... >perl -MDevel::Peek -e"my $x = 'abc'; $x = 123; $x = {}; Dump($x);" SV = PVIV(0x28da78) at 0x2a6a60 REFCNT = 1 FLAGS = (PADMY,ROK) ...

    A reference is indicated by the ROK flag. The pointers of references are neither IV nor UV. The pointers are returned as SV* as they should. The actual pointer is located in the same location as an IV's number or UV's number, so ROK && IOK is impossible.

    illguts

    Update: It appears possible to have an SV that is both ROK and IOK. It would have to be hand-crafted; Perl never constructs one. No idea if it's safe to construct one.

      I got sidetracked in the parent post. The OP asks about C pointers stored in an SV as numbers, not as references.

      The answer is quite simple: It doesn't matter whether it's stored as an IV or as an UV since the value is casted to the correct pointer type before anything ever sees it as a number.

      By the way, that code will work whether it's stored as an IV or as an UV.

Re: SvUV vs SvIV for pointers in SVs, typemap
by BrowserUk (Pope) on Feb 20, 2011 at 20:34 UTC
    shouldn't pointers be SvUV not SvIV?

    On 32-bit OSs they certainly should be. Whilst out-of-the-box, 32-bit OSs only provide user space programs with access to the lower 2GB of ram; most if not all of them can push that upto 3GB with the appropriate build or start-up options.

    And at that point using signed math on pointers could render incorrect results.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      If you cast the pointer to some int type, pointer arithmetic will be wrong whether it's signed or not.

      use Inline C => <<'__EOI__'; int test() { SV* sv = &PL_sv_undef; printf("%p\n", sv+2); printf("%p\n", ((IV)sv)+2); printf("%p\n", ((UV)sv)+2); } __EOI__ test();
      0x746310 0x7462e2 0x7462e2
        If you cast the pointer to some int type,

        And if you clean your windscreen with a hammer, you'll not like the results either.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2014-12-28 19:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (182 votes), past polls