-----BEGIN PGP SIGNED MESSAGE-----

Moin,

On Saturday 10 January 2004 19:22, Nick Ing-Simmons wrote:
> Tels <[EMAIL PROTECTED]> writes:
> >-----BEGIN PGP SIGNED MESSAGE-----
> valgrid is complaining about XS_Math__BigInt__GMP_DESTROY
> can we see that please?

##############################################################################
# DESTROY() - free memory of a GMP number

void
DESTROY(n)
        mpz_t*  n

  PPCODE:
    mpz_clear(*n);
    free(n);

Sorry, should have included it. Here is the typemap, too:

mpz_t *         MPZ

INPUT
MPZ
        if (sv_derived_from($arg, \"Math::BigInt::GMP\")) {
            IV tmp = SvIV((SV*)SvRV($arg));
            $var = ($type) tmp;
        }
        else
            croak(\"$var is not of type Math::BigInt::GMP\")

OUTPUT
MPZ
        sv_setref_pv($arg, \"Math::BigInt::GMP\", (void*)$var);

> >The routine basically only returns (undef,undef) (but not in my testcase!)
> > or (4404, '+'). The case of ($somenumber, '-') shouldn't happen (and does
> > not in my testcase, anyway).
> >
> >void
> >_modinv(class,x,y)
> >        SV*     class
> >        mpz_t*  x
> >        mpz_t*  y
> >
> >  PREINIT:
> >    int rc, sign;
> >    SV* s;
> >  PPCODE:
> >    rc = mpz_invert(*x, *x, *y);
> >    EXTEND(SP, 2);      /* we return two values */
> >    if (rc == 0)
> >      {
> >      /* inverse doesn't exist, return value undefined */
> >      PUSHs ( &PL_sv_undef );
> >      PUSHs ( &PL_sv_undef );
> >      }
> >    else
> >      {
> >      /* inverse exists, get sign */
> >      sign = mpz_sgn (*x);
> >      /* absolute result */
> >      mpz_abs (*x, *x);
> >      PUSHs(sv_setref_pv(sv_newmortal(), "Math::BigInt::GMP", (void*)x));
>
> That is probably wrong. A pv is a char * and perl is going to do strlen()
> on it and strcpy() it into memory allocated by perl's New().
> My guess is x is already malloc()'ed or C++ new'ed by mpz stuff.
> In such cases it is usual to use sv_setref_iv(...,PTR2IV(x)),
> and then have DESTROY function call the inner library's free() routine.
> If *x is a struct then it may also be apprpriate to put the struct
> as binary data in a PV - but there isn't a handy create-blessed-ref
> function to do that, you need to do it long-hand
> something like:
>
>   SV *sv = sv_bless(new_RV(newSVpv(x,sizeof(*x))),gv_stashpv("...", TRUE));
>
> (Trying to do that in one expression may be too rich for some compilers
> when all macros are expanded.)

The typemap should do something like that, so I should be able just to copy 
it's code, right?

        sv_setref_pv($arg, \"Math::BigInt::GMP\", (void*)$var);

Looks the same to me. (But the typemaps INPUT looks funny to my naive eyes..)

> The other issue you _may_ be having is that *x came in from an SV
> (possibly tempoary). Unless *x is refcounted in mpz library you need
> get library to give you a new one to return from the XSUB to another perl
> SV, or change perl view of API to mimic the library's modify-in-place
> semantics. Otherwise both SVs will call DESTROY and try and free(x).

Thank you for your explanations. This is still a little bit over my head, I'll 
see if I can make heads or tail out of this tomorrow morning.

Best wishes,

Tels

- -- 
 Signed on Sat Jan 10 19:57:55 2004 with key 0x93B84C15.
 Visit my photo gallery at http://bloodgate.com/photos/
 PGP key on http://bloodgate.com/tels.asc or per email.

 "Call me Justin, Justin Case."

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2-rc1-SuSE (GNU/Linux)
Comment: When cryptography is outlawed, bayl bhgynjf jvyy unir cevinpl.

iQEVAwUBQABMDHcLPEOTuEwVAQE1LAf8Cy9YjSjQVXXpTb0xJEl6gVUrS2XqAE2J
Z4OxmhGJyG/xXucLeMM3jcayoCGDiA0A0z/V/aOwUlrXt1k/JCSbgQ/mgsKtpuYr
MS0oGhD+GQ+GUJMBb2ZkVixD6eIG0XiHeYZR4oRp+jypedY3TaanlxxgzixkXbVa
88xGn048fU4IM5XiQ1obO2IQ5cX+bhbRMws/37bI54BkujDs9oOzbWl2n1jUxKRV
PVdjtNYnT3GbwEwHQZZp0eX3X8tNC7f7O68Iw3iF7l03J5zdIJuErFwcijB5dbtr
zjkAaLmbZIEWR50z0PG7SVr30E7X6pDkZXQDKGC/r1Bd4D1OEpG/hQ==
=crrd
-----END PGP SIGNATURE-----

Reply via email to