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

Jorn Vernee jbvernee at xs4all.nl
Wed Feb 27 21:51:30 UTC 2019


Note that you could avoid the allocation and copy by doing:

```
     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++) {
             Array<Double> pair = pairs.get(i);
             pair.set(0, 1D);
             pair.set(0, 2D);
         }
     }
```

'pairs' is just one big slab of memory, and `pairs.get(i)` gets you a 
'view' into part of it.

This would work the same with Structs as well.

Jorn

Maurizio Cimadamore schreef op 2019-02-27 22:18:
> 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