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

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Wed Feb 27 15:33:38 UTC 2019


On 27/02/2019 15:18, Lev Serebryakov wrote:
>    I'm playing with same libfftw native library and latest (I'm swear! It
> is 44!) EA build.
>
>   If we strip all preprocessor stuff (which is used to generate 3
> versions of library) from fftw3.h, we get:
>
> ```
> typedef double fftw_complex[2];
>
> fftw_plan_dft_1d(int n, fftw_complex *in, fftw_complex *out, ...);
> ```
>
> So, second and third parameters for `fftw_plan_dft_1d()` is pointer to
> array of size 2. In C it is effectively a... pointer! As any pointer to
> array, it collapses to single pointer-to-type and not
> pointer-to-pointer-to-type.
>
>   But jextract maps it to `Pointer<Array<Double>>`. Is it correct?
> Shouldn't it be `Pointer<Double>` nor `Array<Double>`?

Jextract seems correct here-  you have a double[2], which is translated 
as Array<Double> and that a pointer to that, which is 
Pointer<Array<Double>>.

This all seems correct - of course in C you might not reason about 
things this way, but that's what the type info says - `out*` in your 
example expects a pointer to a region of memory that contains two 
contiguous doubles, so Pointer<Array<Double>> seems correct, semantically.

>
>   It is not clear to me, why do I need another layer of indirection in
> Java and how to properly get such pointer-to-array when I have
> `Array<Double>` at hands, to be sure, that it will be correctly mapped.

Yep this is an issue - I've seen Jorn raising it as well (Jorn, correct 
me if wrong) - what we miss is a 'ptr' method in Array that gives you a 
pointer to the array (the same way we have it for structs).

We considered it briefly [1], then decided against it because of naming 
issues (there's already a method in Array called elementPointer), but we 
should do something about it.

In the meantime, a workaround is

Array<X> arr = ...

Pointer<Array<X>> ptrArr = 
arr.elementPointer().cast(arr.elementType().array(2));

This gives you what you want.

Maurizio

[1] - 
https://mail.openjdk.java.net/pipermail/panama-dev/2019-February/004319.html

>


More information about the panama-dev mailing list