[foreign-memaccess] RFR: JDK-8243284: Remove Foreign class

Ty Young youngty1997 at gmail.com
Wed Apr 22 12:30:07 UTC 2020


On 4/21/20 11:36 AM, Maurizio Cimadamore wrote:
> The Foreign class is currently used for 3 methods:
>
> * withSize, which takes an unchecked address and gives it a size
> * asUnconfined, which takes an existing segment and turns it into an unconfined segment
> * asMallocSegment, which takes an unchecked address and converts that into a segment which can be closed, and where
>    closed is mapped into "free"
>
> This patch replaces all these with this method:
>
> static MemorySegment ofNativeUnsafe(MemoryAddress addr, long bytesSize, Thread owner, AutoCloseable cleanup, Object
> attachment)
>
> Which is then moved directly into MemorySegment - so that we can garbage-collect the Foreign class.


That's a lot of arguments. Could simplified overrides be provided too? 
For example:


static MemorySegment ofNativeUnsafe(MemoryAddress addr, long bytesSize, 
AutoCloseable cleanup)


wherein "owner" is currentThread() and attachment is null.


Maybe it'd make sense to provide default AutoClosable implementations? A 
no-op and simple "free" (required by a native libraries often) are 
pretty standard, right?


Also, what does the attachment argument do? The doc says:


"an object that must be kept alive by the returned segment; this can be 
useful when
the returned segment depends on memory which could be released if a 
certain object
is determined to be unreacheable. In most cases this will be set to 
{@code null}."


but besides a ByteBuffer backed MemorySegment I can't think of any. If 
this is the sole reason it exists, maybe <E extends ByteBuffer> would be 
better here since it will enforce better type safety. If not and it's 
intended to be for any object, it might be a good idea to check if the 
object is an instance of AutoClosable(i'm not seeing that currently in 
the source).


>
> I've also made some changes on the JDK property - new name is simply "jdk.foreign" and I now made sure that it cannot
> be bypassed with a simple static initializer.


More information about the panama-dev mailing list