Raif S. Naffah wrote:
> hello all,
> 
> the attached patch adds support for GNU MP in BigInteger if/when configured.
> 
> 2006-09-03  Raif S. Naffah  <[EMAIL PROTECTED]>
> 
>       PR Classpath/28664
>       * INSTALL: Added documentation about GNU MP.
>       * configure.ac: Add support for configuring GNU MP.
>       * native/jni/Makefile.am: Include java-math directory if required.
>       * native/jni/java_math/.cvsignore: New file.
>       * native/jni/java_math/Makefile.am: Likewise.
>       * native/jni/java_math/java_math_BigInteger.c: Likewise.
>       * native/jni/java_math: New folder.
>       * include/java_math_BigInteger.h: New file.
>       * java/math/BigInteger.java: Added support for native methods.
>       * gnu/classpath/Configuration.java.in (WANT_NATIVE_BIG_INTEGER): New 
> field.
> 
> this was tested with current CVS head and cacao trunk (updated to revision 
> 5285).  Mauve tests for BigInteger, and crypto classes 
> (gnu.testlet.java.security and gnu.testlet.javax.crypto) all pass with cacao 
> but systematically, almost half of them, fail with jamvm (latest CVS head).
> 
> a couple of more Mauve tests will be added soon, and another couple will be 
> updated.
> 
> Mark, would you be kind as to have a look at this before i commit?
> 
> finally, thanks for Mark and Dalibor for their help and advice.
> 

Some comments:

> +static void
> +_throw (JNIEnv *env, const char *exception, const char *msg)
> +{
> +  jclass _theclass = (*env)->FindClass (env, exception);
> +  TRACE("begin");
> +  if (! _theclass)
> +    {
> +      (*env)->FatalError (env, "exception class not found");
> +    }
> +  (*env)->ThrowNew (env, _theclass, msg);
> +  TRACE("end");
> +}

You should use JCL_ThrowException.

> + * a. Pass NULLs to the mp_set_memory_functions implying that GMP should use
> + *    malloc, realloc and free for memory allocation, and if they fail GMP
> + *    will print a message to the standard error output and terminates the
> + *    program.

Should you use JCL_malloc, etc. there? Also, it's a little odd that
you're using JNI_OnLoad, and aren't just calling some native init method
when the BigInteger class is loaded.

> +     For building, and using a GNU MP based implementation of the
> +     java.math.BigInteger class, you will need, at least version 3.1
> +     of GMP.  http://swox.com/gmp/

That's bad grammar. If you omit the first and third comma it sounds a
little better.


As far as the implementation (and the comments about finalize, etc.
elsewhere in this thread) I wonder if it's possible here to use a direct
ByteBuffer to store your native state (I guess GMP is allocating
structures behind the scenes, and is sticking it into your mpz_ptr; BUT,
BigInteger is immutable, so you presumably know how large it is), then
you pass that ByteBuffer to all native methods, which pull out the
native pointer.

I'm really a fan of this approach, which I used to some success in my
kqueue Selector implementation (which hasn't been committed yet,
unfortunately), and I do encourage others to try it. The benefits of
doing it this way are legion:

  - The GC will take care of freeing the native state for you.
  - You don't need to finalize anything, and don't need to synchronize.
  - Your native methods can be static, and what's more, they can be in
an entirely different class, so other VM implementors won't have to deal
with native methods being in java.math directly.
  - You need exactly one JNI function call (GetDirectBufferAddress) and
no (or possibly little) reflection.
  - You can debug your native state in Javaland, if you feel like it.

It makes your usage of JNI *much* more careful and simple.

Thanks.

Reply via email to