http://www.perlmonks.org?node_id=961074

I havent ever seen a definition of a SV struct that allows you to easily see in a C debugger what the flags/type of an SV is. So I wrote this up.
#define PERL_NO_GET_CONTEXT #include "EXTERN.h" #include "perl.h" #include "XSUB.h" struct pMG; typedef struct pMG pMG; struct pMG { XPVMG* sv_any; U32 sv_refcnt; union { U32 sv_flags; struct { unsigned long type : 8; unsigned long f_IOK : 1; unsigned long f_NOK : 1; unsigned long f_POK : 1; unsigned long f_ROK : 1; unsigned long p_IOK: 1; unsigned long p_NOK: 1; unsigned long p_POK : 1; unsigned long p_SCREAM_phv_CLONEABLE_phv_CLONEABLE_prv_PCS +_IMPORTED : 1; unsigned long s_PADSTALE_SVpad_STATE : 1; unsigned long s_PADTMP_SVpad_TYPED : 1; unsigned long s_PADMY_SVpad_OUR : 1; unsigned long s_TEMP : 1; unsigned long s_OBJECT : 1; unsigned long s_GMG : 1; unsigned long s_SMG : 1; unsigned long s_RMG : 1; unsigned long f_FAKE_SVphv_REHASH : 1; unsigned long f_OOK : 1; unsigned long f_BREAK: 1; unsigned long f_READONLY : 1; unsigned long f_AMAGIC : 1; unsigned long f_UTF8_SVphv_SHAREKEYS : 1; unsigned long pav_REAL_phv_LAZYDEL_pbm_VALID_repl_EVAL : 1 +; unsigned long f_IVisUV_pav_REIFY_phv_HASKFLAGS_pfm_COMPILE +D_pbm_TAIL_prv_WEAKREF : 1; } flagsraw; }; union { IV svu_iv; UV svu_uv; pMG* svu_rv; char* svu_pv; pMG** svu_array; HE** svu_hash; GP* svu_gp; } sv_u; }; #define SV pMG
Works on Visual C. Will generate millions of type warnings. Put right after the perl header includes. Makes debugging very easy. The SVANY is a MG struct. LVs are longer. SV type can't be an enum because enums must be 32 bits on MSVC (bad MS!!!).

Replies are listed 'Best First'.
Re: Looking at a SV in C
by BrowserUk (Patriarch) on Mar 22, 2012 at 19:00 UTC

    Cool & useful.

    One question: any particular reason for:

    struct pMG; typedef struct pMG pMG; struct pMG { .... };

    Rather than

    struct pMG { .... }; typedef struct pMG pMG;

    Or just:

    typedef struct pMG { .... } pMG;

    Or even just:

    typedef struct { .... } pMG;

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    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.

    The start of some sanity?

      The struct is self referential. The RV in the union is another pMG *. pMG = private Magic. An MG SVANY is encountered more often than a LV so thats why I based this off a MG rather than LV. Feel free to expand it. Make sure you've read Perl Illguts before and sv.h before interpreting it. Perl loves to use unions.
        The struct is self referential.

        The primary purpose of the tag name on structs is to allow for self referential definitions.

        This compiles clean with MSVC:

        typedef struct pMG { XPVMG *sv_any; U32 sv_refcnt; union { U32 sv_flags; struct { unsigned long type : 8; unsigned long f_IOK : 1; unsigned long f_NOK : 1; unsigned long f_POK : 1; unsigned long f_ROK : 1; unsigned long p_IOK: 1; unsigned long p_NOK: 1; unsigned long p_POK : 1; unsigned long p_SCREAM_phv_CLONEABLE_phv_CLONEABLE_prv_PCS +_IMPORTED : 1; unsigned long s_PADSTALE_SVpad_STATE : 1; unsigned long s_PADTMP_SVpad_TYPED : 1; unsigned long s_PADMY_SVpad_OUR : 1; unsigned long s_TEMP : 1; unsigned long s_OBJECT : 1; unsigned long s_GMG : 1; unsigned long s_SMG : 1; unsigned long s_RMG : 1; unsigned long f_FAKE_SVphv_REHASH : 1; unsigned long f_OOK : 1; unsigned long f_BREAK: 1; unsigned long f_READONLY : 1; unsigned long f_AMAGIC : 1; unsigned long f_UTF8_SVphv_SHAREKEYS : 1; unsigned long pav_REAL_phv_LAZYDEL_pbm_VALID_repl_EVAL : 1 +; unsigned long f_IVisUV_pav_REIFY_phv_HASKFLAGS_pfm_COMPILE +D_pbm_TAIL_prv_WEAKREF : 1; } flagsraw; }; union { IV svu_iv; UV svu_uv; struct pMG *svu_rv; char *svu_pv; struct pMG **svu_array; HE **svu_hash; GP *svu_gp; } sv_u; };

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        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.

        The start of some sanity?