[foreign] Pointer to fixed-size array: is jexctract's mapping to Java correct?

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Wed Feb 27 21:18:02 UTC 2019


On 27/02/2019 19:23, Lev Serebryakov wrote:
> So, semantically speaking, it should be something like (pseudocode, I
> know, that we can not have non-type parameters for Java generics)
> `Array<FixedArray<Double, 2>>`, or, more realistically, but more
> error-prone Array<Array<Double>>, but it will give structure which is
> very inconvenient to work with (it is tedious to allocate array for each
> complex number in this array).

So, let me make sure I understand what you are saying:

1) the API takes a pointer-to-array-of-double

2) In reality this means, I want one or more double pairs (where 
emphasis is on the 'more' side of things)

3) In C, you would just allocate a big slab of memory, big enough to 
hold the data you need, and fill it accordingly (with all the pairs). 
Then you pass the pointer to the API and you rely on C loose pointer vs. 
array semantics

4) The fact that Panama is stricter and it models the signature as 
Pointer<Array<Double>> is correct, but inconvenient, as now, to call the 
function, you have to create many Arrays (one for each pair) - that is, 
it seems less flat that what the C code does - something like:


int N = 200; //number of complex pairs
try (Scope s = Scope.globalScope().fork()) {
    Array<Array<Double>> pairs = 
s.allocateArray(NativeTypes.DOUBLE.array(2), N);
    for (int i = 0 ; i < N ; i++) {
        pairs.set(i, s.allocateArray(NativeTypes.DOUBLE, new double[] { 
d1, d2 }); // replace d1 and d2 with actual data
    }

    ...

    Pointer<Array<Double>> pairs_ptr = pairs.elementPointer();

}


Am I right that your discomfort comes from having to allocate all those 
native arrays? (which will then copied in the destination pair array)

Maurizio



More information about the panama-dev mailing list