1 /*
  2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 package org.openjdk.bench.jdk.incubator.foreign.points.support;
 24 
 25 import jdk.incubator.foreign.FunctionDescriptor;
 26 import jdk.incubator.foreign.SymbolLookup;
 27 import jdk.incubator.foreign.MemoryAddress;
 28 import jdk.incubator.foreign.MemoryLayout;
 29 import jdk.incubator.foreign.MemorySegment;
 30 import jdk.incubator.foreign.CLinker;
 31 import jdk.incubator.foreign.ResourceScope;
 32 
 33 import java.lang.invoke.MethodHandle;
 34 import java.lang.invoke.VarHandle;
 35 
 36 import static java.lang.invoke.MethodType.methodType;
 37 import static jdk.incubator.foreign.MemoryLayout.PathElement.groupElement;
 38 import static jdk.incubator.foreign.CLinker.*;
 39 
 40 public class PanamaPoint implements AutoCloseable {
 41 
 42     public static final MemoryLayout LAYOUT = MemoryLayout.structLayout(
 43         C_INT.withName("x"),
 44         C_INT.withName("y")
 45     );
 46 
 47     private static final VarHandle VH_x = LAYOUT.varHandle(int.class, groupElement("x"));
 48     private static final VarHandle VH_y = LAYOUT.varHandle(int.class, groupElement("y"));
 49     private static final MethodHandle MH_distance;
 50     private static final MethodHandle MH_distance_ptrs;
 51 
 52     static {
 53         CLinker abi = CLinker.getInstance();
 54         System.loadLibrary("Point");
 55         SymbolLookup lookup = SymbolLookup.loaderLookup();
 56         MH_distance = abi.downcallHandle(
 57             lookup.lookup("distance").get(),
 58             methodType(double.class, MemorySegment.class, MemorySegment.class),
 59             FunctionDescriptor.of(C_DOUBLE, LAYOUT, LAYOUT)
 60         );
 61         MH_distance_ptrs = abi.downcallHandle(
 62             lookup.lookup("distance_ptrs").get(),
 63             methodType(double.class, MemoryAddress.class, MemoryAddress.class),
 64             FunctionDescriptor.of(C_DOUBLE, C_POINTER, C_POINTER)
 65         );
 66     }
 67 
 68     private final MemorySegment segment;
 69 
 70     public PanamaPoint(int x, int y) {
 71         this(MemorySegment.allocateNative(LAYOUT, ResourceScope.newConfinedScope()), x, y);
 72     }
 73 
 74     public PanamaPoint(MemorySegment segment, int x, int y) {
 75         this(segment);
 76         setX(x);
 77         setY(y);
 78     }
 79 
 80     public PanamaPoint(MemorySegment segment) {
 81         this.segment = segment;
 82     }
 83 
 84     public void setX(int x) {
 85         VH_x.set(segment, x);
 86     }
 87 
 88     public int getX() {
 89         return (int) VH_x.get(segment);
 90     }
 91 
 92     public void setY(int y) {
 93         VH_y.set(segment, y);
 94     }
 95 
 96     public int getY() {
 97         return (int) VH_y.get(segment);
 98     }
 99 
100     public double distanceTo(PanamaPoint other) {
101         try {
102             return (double) MH_distance.invokeExact(segment, other.segment);
103         } catch (Throwable throwable) {
104             throw new InternalError(throwable);
105         }
106     }
107 
108     public double distanceToPtrs(PanamaPoint other) {
109         try {
110             return (double) MH_distance_ptrs.invokeExact(segment.address(), other.segment.address());
111         } catch (Throwable throwable) {
112             throw new InternalError(throwable);
113         }
114     }
115 
116     @Override
117     public void close() {
118         segment.scope().close();
119     }
120 }