[foreign] RFR : Move array-like methods from Pointer to Array

Jorn Vernee jbvernee at xs4all.nl
Sat Nov 17 21:46:15 UTC 2018


Hi,

I'd like to contribute a patch that moves some of the array-like methods 
from Pointer to Array.

This patch moves the methods `long bytesSize()` and `Stream<Pointer<X>> 
elements()` from Pointer to Array, and removes the `long elementSize()` 
method (since Array already has `long length()` for that). The rationale 
for doing this is that these methods imply an inherent size associated 
with a Pointer, but this is not really the case, since we can only infer 
the size of memory regions that are allocated from the Java side. Array 
on the other hand has an explicit size associated with it, so I believe 
these methods fit a lot better in the Array interface.

The other thing is that `long bytesSize()` sounds like it returns the 
size of the pointee type, but this is not the case, as it actually 
returns the size of the underlying memory region minus the offset of the 
pointer, which can be much larger. I ran into some cases where 
`pointer.bytesSize()` was being used in error instead of 
`pointer.type().bytesSize()`, and I've made that mistake myself as well. 
So I think this move also removes that pitfall.

This change also actually simplified most use cases for the 
`bytesSize()` method, since they were already using an array I cloud 
simplify `array.elementPointer().bytesSize()` to `array.bytesSize()`. 
`elements()` was not being used at all in the JDK code.

Webrev : 
http://cr.openjdk.java.net/~jvernee/panama/webrevs/arrays/webrev.03/

In this patch I've also:
- Updated the Javadoc of the Pointer class.
- Removed the `length()` method from BoundedMemoryRegion. Since it was 
no longer used, and it was only partially supported, I felt it was 
cleaner to remove it (at least for now).
- Simplified some of the `pointer.type().layout().bitSize() / 8` 
patterns to `pointer.type().bytesSize()`.

Cheers,
Jorn



More information about the panama-dev mailing list