classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Classpath build process and VM-specific issues


From: Andrew Haley
Subject: Re: Classpath build process and VM-specific issues
Date: Mon, 5 Apr 2004 19:55:41 +0100

Etienne Gagnon writes:
 > Andrew Haley wrote:
 > > Sure.  Instead of putting a native pointer in a long or in a byte[],
 > > you just declare a class with a single field that contains the
 > > pointer.  Everyone who needs a pointer makes a suitably named
 > > subclass, so they'll know what they're pointing to.
 > 
 > How is that more efficient than a byte array?

It's a heck of a lot *better* than a byte array.  Whether it's more
efficient or not depends on the design of your VM.


class SomeRandomLibraryClass
{
   static
   {
     System.loadLibrary("SomeRandomJNILibrary");
   }

   private RandomPointer extends nativePointer;

   private native RandomPointer initNativeData(...);
   private native void someNativeMethod(RandomPointer nativePointer, ...);

   public SomeRandomLibraryClass(...)
   {
     ...
     nativePointer = initNativeData(...);
   }

   public void someMethod(...)
   {
     someNativeMethod(nativePointer, ...)  /**  THE REAL CALL **/
   }
}

In the C code, we'll have:

 > JNIEXPORT void JNICALL
 > Java_somepackage_someNativeMethod
 >    (JNIEnv *env, jobject this, jbyteArray nativePointer, ...)
 > 
 > {
 >    void *ptr;
 >    (*env)->GetByteArrayRegion(env, nativePointer, 0, sizeof(void *), (jbyte 
 > *) &ptr);

Danger, Will Robinson!  This is not legal C!!!!  You can *not* take
the address of a pointer and cast it to a jbyte*.

Try finding a legal way to do this.

Andrew.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]