[foreign-abi] Use MethodHandle.linkToStatic to call MemoryAddress.ofLong
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Thu Dec 19 18:06:02 UTC 2019
Clever!
Maurizio
On 19/12/2019 18:01, Paul Sandoz wrote:
> Here is a small patch to replace the use of MethodHandle.invoke with the trusted MethodHandle.linkToStatic.
>
> Paul.
>
> diff -r 06dbd52de6a3 src/java.base/share/classes/java/lang/invoke/X-VarHandleMemoryAddressView.java.template
> --- a/src/java.base/share/classes/java/lang/invoke/X-VarHandleMemoryAddressView.java.template Thu Dec 19 11:14:10 2019 +0000
> +++ b/src/java.base/share/classes/java/lang/invoke/X-VarHandleMemoryAddressView.java.template Thu Dec 19 09:56:03 2019 -0800
> @@ -27,7 +27,6 @@
> import jdk.internal.access.foreign.MemoryAddressProxy;
> import jdk.internal.vm.annotation.ForceInline;
>
> -import java.lang.reflect.Field;
> import java.util.Objects;
>
> import static java.lang.invoke.MethodHandleStatics.UNSAFE;
> @@ -45,15 +44,15 @@
> #if[MemoryAddressProxy]
> static final int VM_ALIGN = Long.BYTES - 1;
>
> - static final MethodHandle ADDRESS_FACTORY;
> static final MemoryAddressProxy NULL_ADDRESS;
> + static final MemberName MEMORY_ADDRESS_OF_LONG_MEMBER_NAME;
>
> static {
> try {
> Class<?> addrClass = Class.forName("jdk.incubator.foreign.MemoryAddress");
> NULL_ADDRESS = (MemoryAddressProxy)addrClass.getDeclaredField("NULL").get(null);
> - ADDRESS_FACTORY = MethodHandles.Lookup.IMPL_LOOKUP.findStatic(addrClass, "ofLong", MethodType.methodType(addrClass, long.class));
> - } catch (Throwable ex) {
> + MEMORY_ADDRESS_OF_LONG_MEMBER_NAME = new MemberName(addrClass.getMethod("ofLong", long.class));
> + } catch (Exception ex) {
> throw new ExceptionInInitializerError(ex);
> }
> }
> @@ -98,7 +97,7 @@
> try {
> return value == 0L ?
> NULL_ADDRESS :
> - ($type$)ADDRESS_FACTORY.invoke(value);
> + MethodHandle.linkToStatic(value, MEMORY_ADDRESS_OF_LONG_MEMBER_NAME);
> } catch (Throwable ex) {
> throw new IllegalStateException(ex);
> }
>
More information about the panama-dev
mailing list