Subject: Using JNI in less than simple areas

OK,

Have Tried JNI before then went to several other technologies for doing this, but am back to JNI. Trying to get started on wrapping the Newton Physics Engine(dll and header file only).

The native function is:

NEWTON_API NewtonWorld* NewtonCreate (NewtonAllocMemory malloc, NewtonFreeMemory mfree);

The two parameters are virtual functions as follows:

typedef void* (*NewtonAllocMemory) (int sizeInBytes);

typedefvoid (*NewtonFreeMemory) (void *ptr,int sizeInBytes);

My Java code is:

package jnewton;

publicclass JNewton{

static{

System.loadLibrary("JNewton");

}

/**

* @param args

*/

publicnativeint newtonCreate(Object o1, Object o2);

publicstaticvoid main(String[] args){

JNewton jn =new JNewton();

int l = jn.newtonCreate(null,null);

System.out.println(l);

}

}

The writer of the engine in his tutorials specifies that on this call the two parameter should be null, but may not be on other uses.

The JNI implementation is:

#include"jnewton_JNewton.h"

#include <jni.h>

#include"Newton.h"

/*

* Class: jnewton_JNewton

* Method: newtonCreate

* Signature: (Ljava/lang/Object;Ljava/lang/Object;)J

*/

JNIEXPORT jint JNICALL Java_jnewton_JNewton_newtonCreate

(JNIEnv *env, jobject class, jobject obj1, jobject obj2){

jint result;

NewtonWorld* nw = NewtonCreate(obj1, obj2);

result = (jint)nw;

return(result);

}

This actually compiles and runs. There are however two things which bother me about it. First, the return value is the same every time. Since it is in fact a memory address, am I wrong in assuming that it should vary with each running? Second, when I compile, I get the following warnings:

source/jnewton_JNewton.c: In function `Java_jnewton_JNewton_newtonCreate':

source/jnewton_JNewton.c:13: warning: passing arg 1 of `NewtonCreate' from incompatible pointer type

source/jnewton_JNewton.c:13: warning: passing arg 2 of `NewtonCreate' from incompatible pointer type

I've tried several ways to fix this in a C/JNI compatible way, but only succeed in creating new warnings or errors.

Am using Eclipse and minGW.

Any advice, opinions, or insights are appreciated.

Jim

[3373 byte] By [jjones3566a] at [2007-11-26 18:50:07]
# 1

I would have to see the declaration of newtonCreate to understand more about the formal parameters, but it is highly likely that jobject is not a type match and hence the compiler errors. Any idea what those two parameters are? You mention that they are not supposed to be null in future invocations.

georgekwatsona at 2007-7-9 6:24:08 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2
Just re-read the post and now understand what those parameters are about. passing a pair of null jobject handles will not do in future calls. You need to write a wrapper for malloc and free that you pass the pointers of. This should be done in your C code.
georgekwatsona at 2007-7-9 6:24:08 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3

In his help files for this function he has the following:

Parameters

NewtonAllocMemory mallocFnt - is a pointer to the memory allocator callback function. If this parameter is NULL the standard malloc function is used.

NewtonFreeMemory mfreeFnt - is a pointer to the memory release callback function. If this parameter is NULL the standard free function is used

In the later tutorials he says the following:

The first thing you will notice is that the function NewtonCreate is now passing two arguments. These are the pointers to the memory allocation and de-allocation functions

// Create the newton world

nWorld = NewtonCreate (PhysicsAlloc, PhysicsFree);

Newton will call these functions whenever it needs memory and whenever some piece of memory needs to be freed. The application does not have to worry about memory be aligned; Newton takes care of that internally.

The allocation functions pass the memory size as an argument. The application can use this value to monitor how much memory the physics engine is using. Newton uses the concept of singleton internally, so it is possible that not all memory is de-allocated when the Newton world is destroyed. It is guarantied that all memory is de-allocated when the application terminates.

Apparently, in the initial call the engine specific functions are not available so the normal malloc and free are used or what he says in the help files is true and I can continue to use null if I don't want to monitor memory use.

I went to using Object because even though they are pointers and can be seen as ints, null is not a allowed value for any primitive in Java so I'm stuck with using Object. Once in the JNI c function I might do something with a little more finesse to prevent the warnings, but haven't found that there is much you can do with the jxxxx things.

NOTE: Am I correct in assuming that there should be som variation in the memory address I get back?

I'll write to the engine author and see if I can't get some clarification on how some of this works. Will post back here if your interested. All the info I have at this point was included in the first post. There seems to be a real pausity of example with most having to do with either single function out of a dll or writing both sides. I look at the JNI code associated with e.g. J3D and I wonder how people have figured all that out if there is such a little amount of information.

Thanks,

Jim

jjones3566a at 2007-7-9 6:24:08 > top of Java-index,Java HotSpot Virtual Machine,Specifications...