Why asUnbounded()/isUnbounded() was removed in JDK21?
Cheng Jin
jincheng at ca.ibm.com
Wed May 3 17:16:27 UTC 2023
Hi Maurizio,
Many thanks for your quick response with detailed clarification.
There is another question that confused me is: why the pointer segment should be granted with Long.MAX_VALUE given users should know the actual size of the segment. My understanding is they should convert a zero-sized segment (pointer) to a segment with the whatever size it is supposed to be for safety as explained in terms of zero-length memory segments at https://download.java.net/java/early_access/jdk21/docs/api/java.base/java/lang/foreign/MemorySegment.html#wrapping-addresses
Best Regards
Cheng Jin
From: Maurizio Cimadamore <maurizio.cimadamore at oracle.com>
Sent: May 3, 2023 9:47 AM
To: Cheng Jin <jincheng at ca.ibm.com>; panama-dev at openjdk.org
Subject: [EXTERNAL] Re: Why asUnbounded()/isUnbounded() was removed in JDK21?
Hi, the functionality is there - it has just been renamed to make it more general. In 20, one could only create an address layout that was _unbounded_ e. g. that pointed to a region of memory of unbounded size. In 21, AddressLayout is a new
ZjQcmQRYFpfptBannerStart
This Message Is From an External Sender
This message came from outside your organization.
ZjQcmQRYFpfptBannerEnd
Hi,
the functionality is there - it has just been renamed to make it more general.
In 20, one could only create an address layout that was _unbounded_ e.g. that pointed to a region of memory of unbounded size.
In 21, AddressLayout is a new toplevel class. An address layout has a _target layout_ [1] which is used to describe the layout of the pointed region of memory.
This means that you can still get a behavior similar to `asUnbounded`, by doing:
```java
ADDRESS.withTargetLayout(MemoryLayout.sequenceLayout(JAVA_BYTE))
```
The above will create a sequence layout of unbound size (so, the size of that layout would be Long.MAX_VALUE), and use that layout as the target for the address layout. Which means that when this address is passed to MemorySegment::get(AddressLayout), or to the Linker, you will get back a MemorySegment whose size is Long.MAX_VALUE (as in 21).
But, if you know the layout of the pointed region of memory statically, you can also use a sharper target layout - e.g.
```java
ADDRESS.withTargetLayout(JAVA_INT)
```
The latter will create an address layout whose target layout is _exactly_ one int layout. This means that when you pass this address layout to MemorySegment::get(AddressLayout),or to the Linker, you will get back a MemorySegment whose size is precisely 4 bytes. This is especially useful for upcalls (as the qsort example in JEP 442 demonstrates).
Hope this helps.
Maurizio
[1] - https://cr.openjdk.org/~pminborg/panama/21/v2/javadoc/api/java.base/java/lang/foreign/AddressLayout.html#withTargetLayout(java.lang.foreign.MemoryLayout)<https://cr.openjdk.org/~pminborg/panama/21/v2/javadoc/api/java.base/java/lang/foreign/AddressLayout.html#withTargetLayout(java.lang.foreign.MemoryLayout)>
On 03/05/2023 14:34, Cheng Jin wrote:
Hi there,
I am wondering why asUnbounded()/isUnbounded() (existing in JDK20) related code was removed from java.base/share/classes/java/lang/foreign/ValueLayout.java in JDK21.
Is there any reason for deleting them all (or moving them somewhere else which I ignored) for the unbounded segment (pointer)?
Best Regards
Cheng Jin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20230503/1e31b158/attachment-0001.htm>
More information about the panama-dev
mailing list