caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] java: short [] [] -> JNI -> int array array
@ 2002-11-28 19:02 Metalscan
  2002-11-29 12:19 ` Metalscan
  0 siblings, 1 reply; 2+ messages in thread
From: Metalscan @ 2002-11-28 19:02 UTC (permalink / raw)
  To: caml-list

Hello,

I have a problem wich I couldn't solve after one long day of tests.

I've written a module to process some datas in ocaml. The program wich
will use it is in java (I do not have the choice).

Java must pass the datas to ocaml and call my functions. I turned my
module into a dynamic link library (linux ; I'll have to to this on Windowd NT).

I use Jni to call my native (Java) functions, which are registered
callbacks (ocalm) in a C function.

The transfer of "simple" datas from java to C works ok with int, float,
short []. For the latter I passed bigs arrays (about 10^6, i think).
I'll still have to give back the results to the Java world...


My problem is to pass a multidimensionnal array. When they are too big,
there is a problem during my conversion function. I suspect a misunderstanding of
the GC...

Does anybody have an example of such a creation ? I must precise that,
of course, I don't know the number of rows at compilation time, and that
the size of each of them is not garanted to be the same.

Thanks in advance for your time and any suggestion

Matthieu Dubuget
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] java: short [] [] -> JNI -> int array array
  2002-11-28 19:02 [Caml-list] java: short [] [] -> JNI -> int array array Metalscan
@ 2002-11-29 12:19 ` Metalscan
  0 siblings, 0 replies; 2+ messages in thread
From: Metalscan @ 2002-11-29 12:19 UTC (permalink / raw)
  To: caml-list

Lastly I have a working solution.

I'm still not sure of it's correctness...

If some guru have the time to points me any amelioration, I would
appreciate (both for pure C part, and manipulation of values).

I apologize if this is of topic.

Thanks in advance,
Matthieu Dubuget

value short_p_to_val(char const  * i)
{
    value v;  
    short k = * i ;

    v= Val_int((short) *((short *) i) );
    return v ;
}

value f(char const * l)
{
  CAMLparam0() ;
  CAMLlocal1(v);

  v=alloc_array(&short_p_to_val,(char const * * ) l) ;

  CAMLreturn(v);
}

value java_short_array_array_to_ocaml_int_array_array
    (JNIEnv * env, jobject obj , jobjectArray jcscanA) {
 
    CAMLparam0 ();
    CAMLlocal1(v);

    //One element (line) of the jcscanA array
    jobjectArray ligne ;

    //bodies for each lines of the jcscanA pointer
    jshort * * ligne_body = NULL ;
    
    int i, j;  

    // The C array in wich I'll put the values of jcscanA
    // Must do that because we need a null ended array for alloc_array
    short * * * cscan ;

    jsize lignes,colonnes;

    // Allocation de cscan et ligne_body
    lignes = (*env) -> GetArrayLength(env,jcscanA) ;
    cscan = malloc((lignes+1) * sizeof(short * *));
    ligne_body = malloc (lignes * sizeof(jshort *));

    // Again : cscan is NULL ended
    cscan[lignes]=NULL;

    // Remplissage avec les valeurs de jcscanA
    for (i=0;i<lignes;i++){
	
	ligne = (*env)->GetObjectArrayElement(env,jcscanA,i);
	colonnes = (*env) -> GetArrayLength(env,ligne);
	ligne_body[i] = (*env)->GetShortArrayElements(env,ligne,0);
	cscan[i] = malloc( (colonnes+1) * sizeof(short *) );
	cscan[i][colonnes] = NULL ;
	for(j=0;j<colonnes;j++)
	    cscan[i][j] =  &(ligne_body[i][j]) ;
    }
    
    v = alloc_array(&f, (char const * *) cscan) ;

    for(i=0;i<lignes;i++){
	(*env)->ReleaseShortArrayElements(env,ligne,ligne_body[i],0);
	free(cscan[i]);    
    }
    free(ligne_body);
    free(cscan);

    CAMLreturn (v) ;
};
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-11-29 17:41 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-11-28 19:02 [Caml-list] java: short [] [] -> JNI -> int array array Metalscan
2002-11-29 12:19 ` Metalscan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).