changes to the foreign preview API

Ty Young youngty1997 at gmail.com
Wed Feb 2 17:04:47 UTC 2022


+1


This change, like others before it, makes it harder to create higher 
level APIs or make it more awkward to use them. Can you please stop 
making life hell for anyone who wants to write or use a higher level API 
using Panama?


Unless I'm missing something, the same basic idea could be achieved by 
making sliced segments have the global scope instead of inheriting the 
parent's scope. Unless I'm missing something, please do that instead.


On 2/1/22 7:30 PM, Michael Zucchi wrote:
> On 1/2/22 23:18, Maurizio Cimadamore wrote:
>> The other change we'd like to make is to remove the scope() accessors 
>> from all the resources (MemorySegment, NativeSymbol, VaList). This 
>> comes from a desire to make the API more principled: only the owner 
>> of a scope/session should have the "right" to close it. In fact, we 
>> have spent significant API estate (e.g. ResourceScope::whileAlive) 
>> just from preventing random clients to shut down scopes. We now 
>> believe a better approach would be to simply make the scope 
>> associated e.g. with a memory segment inaccessible. In other words, a 
>> resource scope/memory session becomes a _capability_: it is up to the 
>> owner of the scope/session to decide who to share that capability 
>> with. Once a client has a scope, it can close that scope, but also 
>> affect the scope by registering cleanup actions, or allocating on 
>> that scope. Conversely, if a client only has a segment, there's no 
>> way for that client to affect the owning scope/session in a meaninful 
>> (and possibly, harmful) way
>
> There's some cases where this will be a bit inconvenient.
>
> A simple example would be something like:
>
> struct a {
>  int c;
> };
>
> struct b {
>   struct a *a;
> };
>
> Where 'a'  is created/managed by 'b'.  The java side might be:
>
>  class b {
>   MemorySegment segment;
>
>   b(MemorySegment seg) {
>     segment=seg;
>   }
>    a getA() {
>       MemoryAddress addr = varhandle$a.get(segment);
>       return new a(MemorySegment.ofAddress(addr, a.layout.byteSize(), 
> segment.scope());
>    }
> }
>
> You don't really want to have to track and pass the scope to the 
> accessor each time.  The alternative is to add a scope field to 'b', 
> but then it also needs to be passed to every constructor of such a class.
>
>  Michael
>


More information about the panama-dev mailing list