[foreign] RFR 8214893: downcall/upcall support should support custom LayoutTypes

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Wed Dec 5 15:15:56 UTC 2018


Hi,
this is a patch that makes Universal/DIrectInvoker more flexible so as 
to allow user-defined layout types. Right now, this is not very 
important, since the only way to poke at the invokers is through the 
binder. But with the recent effort to expose the invoker machinery 
directly (SystemABI) this becomes more important. As a way of testing, I 
wrote the following program:

import java.lang.invoke.*;
import java.foreign.*;
import java.foreign.layout.*;
import java.foreign.memory.*;

class TestCustomType {
    public static void main(String[] args) throws Throwable {
       var sym = Libraries.getDefaultLibrary().lookup("getpid");
       var getpid = Libraries.downcallHandle(sym, NativeTypes.INT32);
       var getpidx100 = Libraries.downcallHandle(sym, new IntType());
       System.out.println("pid = " + (int)getpid.invoke());
       System.out.println("pidX100 = " + (int)getpidx100.invoke());
    }
}


class IntType implements LayoutType<Integer> {
    static MethodHandle getter;

    static {
        try {
            getter = 
java.lang.invoke.MethodHandles.lookup().findStatic(IntType.class, 
"getterImpl", MethodType.methodType(int.class, Pointer.class));
        } catch (Throwable t) { throw new IllegalStateException(t); }
    }

    public LayoutType<Array<Integer>> array(long size) { throw new 
UnsupportedOperationException(); }
    public LayoutType<Array<Integer>> array() { throw new 
UnsupportedOperationException(); }
    public LayoutType<Pointer<Integer>> pointer() { throw new 
UnsupportedOperationException(); }


    private static int getterImpl(Pointer<?> p) {
        return p.cast(NativeTypes.INT32).get() * 100;
    }

    public MethodHandle getter() { return getter; }
    public MethodHandle setter() { throw new 
UnsupportedOperationException(); }
    public Layout layout() { return NativeTypes.INT32.layout(); }
}


As you can see, to be able to test I had to add a method in Libraries 
which is essentially the same as the proposed SystemABI::downcallHandle. 
This program calls getpid() with two different super-imposed layout 
types: one is the standard NativeTypes::INT32 - the other is a custom 
one, which, on extraction multiplies the value by 100. Of course this is 
silly, but it is an example of how custom marshalling/unmarshalling can 
be built on top of our API. This, coupled with SystemABI will 
effectively give framework developers a way to define their own 
marshalled types.

 From an operational perspective, it makes sense to push this *afer* the 
SystemABI work, as this patch is otherwise not testable. It also depend 
on other changes done in SystemABI - such as adding a carrier() method 
to LayoutType.

Finally, I made sure that performances are not affected when running 
with binder; that is, if the invokers detect that one of the 'internal' 
layout type is being used, some shortcuts will be applied that will get 
rid of some of extra pointer read/write (this is especially true in the 
direct invoker).

Webrev:

http://cr.openjdk.java.net/~mcimadamore/panama/8214893/

Cheers
Maurizio



More information about the panama-dev mailing list