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

Lev Serebryakov lev at serebryakov.spb.ru
Wed Feb 27 19:23:53 UTC 2019


On 27.02.2019 20:49, Maurizio Cimadamore wrote:

>> Hmm. It is correct from point of view of type system, but it is
>> incorrect from point of view of memory layout, when it is translated to
>> Java verbatim. "pointer to array" is something strange in plain C (see
>> comp.lang.c FAQ 6.12 about it: difference between array and pointer to
>> it is in type, but not in memory layout, as array is second-class
>> citizen in C). It is not pointer-to-pointer, it is equivalent (from
>> memory layout point of view) to array itself, and Pointer<Array<Double>>
>> looks like double-indirection (if we remember, that Array is fancy name
>> for Pointer in this case), which is not what this native API expected.
> 
> Not sure I follow. A pointer to an array has a very different layout
> from an array.
 From fixed-size array, yes.

> What I think you are saying is, I believe, that if you have a
> Pointer<Array<X>> you can always flatten it as a Pointer<X>; while this
> is technically correct (the underlying layout is correct), when you do
> the flattening you completely lose track of the fact that the memory
> region is not just a sequence of X - but a sequence _of sequence_ of X.
> 
> In your example, I agree that you might want to pass an array of 2048
> double to the function and be happy with it. But, what if you pass an
> array with only _one_ ? Or an array with an _odd_ number of doubles?
  Memory corruption :-) And even worse in this case: memory corruption
will be result if I pass array with 4 doubles, but "3" (which means "3
complex numbers or 6 doubles") as first argument. It is C :-(

> This is allowed if we're just talking about a flattened double* - but,
> semantically speaking, this is NOT what the code says with the typedef.
 Strictly speaking, fftw's API is just plain bad, because it passes
pointer to two-element array, and not array of arrays or, better, array
of structs. What this API try to say, that it takes number of complex
numbers (each complex number is array of two doubles) and two pointers
to array (=pointers to first element of these arrays), each contains
this (first-argument-dependent) number of complex numbers. Nobody do FFT
of 1 number. It is ID function, which is not very useful :-)

 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).

 It will be nice to have ability to add some hints (as C comments) to
jmapper in such non-obvious cases. Including, maybe, relation between
array sizes and other arguments ("poor man dependent types")!

-- 
// Black Lion AKA Lev Serebryakov



More information about the panama-dev mailing list