changes to the foreign preview API

Michael Zucchi notzed at gmail.com
Wed Feb 2 01:30:28 UTC 2022


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