I'm working on a perl XS module that accesses an underlying library that happens to make use of libgmp. (it's perl bindings for the Nettle cryptographic library)

I'd like to be able to accept Math::BigInt::GMP objects as arguments to some of the functions, and then be able to make use of the underlying mpz_t within the XS code, but i can't see how to do that.

Math::BigInt::GMP version 1.36 has a typemap that claims to be able to extract an mpz_t with:

mpz_from_sv($arg);
But this is declared within GMP.xs itself as:
STATIC mpz_t * mpz_from_sv_nofail (SV *sv) { MAGIC *mg; if (!sv_derived_from(sv, "Math::BigInt::GMP")) croak("not of type Math::BigInt::GMP"); for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) { if (mg->mg_type == PERL_MAGIC_ext #if GMP_HAS_MAGICEXT && mg->mg_virtual == &vtbl_gmp #endif ) { #if GMP_HAS_MAGICEXT return (mpz_t *)mg->mg_ptr; #else return INT2PTR(mpz_t *, SvIV((SV *)mg->mg_ptr)); #endif } } return (mpz_t *)NULL; } STATIC mpz_t * mpz_from_sv (SV *sv) { mpz_t *mpz; if (!(mpz = mpz_from_sv_nofail(sv))) croak("failed to fetch mpz pointer"); return mpz; }
Since these are static (and therefore internal to GMP.so), i'm pretty sure i can't use them directly in my own XS. And given that they have #if blocks based on preprocessor directives, i don't think i can reasonably even cargo-cult the code into my own .xs file and make sure that it works portably :/

Is there some clue i'm missing? Should i give up on direct mpz_t access, and force the communication across the XS boundaries to be laundered through some more primitive type (like an ASCII representation of a hex string)? That would allow me to work with more than just Math::BigInt::GMPs, but it's also much more inefficient than i'd like it to be.

Any suggestions for how i should approach this?


In reply to accessing mpz_t * of Math::BigInt::GMP from XS by dkg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.