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

Maurizio Cimadamore mcimadamore at openjdk.java.net
Tue Apr 21 20:36:19 UTC 2020


On Tue, 21 Apr 2020 18:39:45 GMT, Henry Jen <henryjen at openjdk.org> 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.
>> 
>> 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.
>
> src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/MemorySegment.java line 494:
> 
>> 493:                                               segment,
>> 494:                                               null);
>> 495:      * }</pre></blockquote>
> 
> In such use case, close() called on either segment will affect the other as well, correct?
> I am assuming in use case like this, the new segment close is preferred to the old segment and developer most likely
> should not keep reference to the old segment?

Yes - calling close will also affect the other segment. We have three options here:
* forget about passing a segment - just accept a Runnable; but this will make it harder e.g. to take an existing mapped
  segment and make it "unconfined" it. Also, still doesn't remove the problem, since you can have a native memory segment
  for an address X, and you can create another unchecked memory segment at same address which calls Unsafe::freeMemory
  when you close - which will again invalidate the segment
* make it so that when you pass a segment as an argument, the input segment is also killed - so that it becomes "not
  alive"; this can be bypassed in the same way described above
* leave it as is

There's no great solution here; I think I like either the current approach, or the first one, where we just take a
`Runnable` and leave it up to the user to define what should happen when you close.

-------------

PR: https://git.openjdk.java.net/panama-foreign/pull/122


More information about the panama-dev mailing list