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.CLinker;
 26 import jdk.incubator.foreign.FunctionDescriptor;
 27 import jdk.incubator.foreign.MemoryAddress;
 28 import jdk.incubator.foreign.SymbolLookup;
 29 import jdk.incubator.foreign.MemoryLayout;
 30 import jdk.incubator.foreign.MemorySegment;
 31 import jdk.incubator.foreign.ResourceScope;
 32 import org.openjdk.bench.jdk.incubator.foreign.CLayouts;
 33 
 34 import java.lang.invoke.MethodHandle;
 35 import java.lang.invoke.VarHandle;
 36 
 37 import static java.lang.invoke.MethodType.methodType;
 38 import static jdk.incubator.foreign.MemoryLayout.PathElement.groupElement;
 39 
 40 public class PanamaPoint extends CLayouts 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(groupElement("x"));
 48     private static final VarHandle VH_y = LAYOUT.varHandle(groupElement("y"));
 49     private static final MethodHandle MH_distance;
 50     private static final MethodHandle MH_distance_ptrs;
 51 
 52     static {
 53         CLinker abi = CLinker.systemCLinker();
 54         System.loadLibrary("Point");
 55         SymbolLookup lookup = SymbolLookup.loaderLookup();
 56         MH_distance = abi.downcallHandle(
 57             lookup.lookup("distance").get(),
 58                 FunctionDescriptor.of(C_DOUBLE, LAYOUT, LAYOUT)
 59         );
 60         MH_distance_ptrs = abi.downcallHandle(
 61                 lookup.lookup("distance_ptrs").get(),
 62                 FunctionDescriptor.of(C_DOUBLE, C_POINTER, C_POINTER)
 63         );
 64     }
 65 
 66     private final MemorySegment segment;
 67 
 68     public PanamaPoint(int x, int y) {
 69         this(MemorySegment.allocateNative(LAYOUT, ResourceScope.newConfinedScope()), x, y);
 70     }
 71 
 72     public PanamaPoint(MemorySegment segment, int x, int y) {
 73         this(segment);
 74         setX(x);
 75         setY(y);
 76     }
 77 
 78     public PanamaPoint(MemorySegment segment) {
 79         this.segment = segment;
 80     }
 81 
 82     public void setX(int x) {
 83         VH_x.set(segment, x);
 84     }
 85 
 86     public int getX() {
 87         return (int) VH_x.get(segment);
 88     }
 89 
 90     public void setY(int y) {
 91         VH_y.set(segment, y);
 92     }
 93 
 94     public int getY() {
 95         return (int) VH_y.get(segment);
 96     }
 97 
 98     public double distanceTo(PanamaPoint other) {
 99         try {
100             return (double) MH_distance.invokeExact(segment, other.segment);
101         } catch (Throwable throwable) {
102             throw new InternalError(throwable);
103         }
104     }
105 
106     public double distanceToPtrs(PanamaPoint other) {
107         try {
108             return (double) MH_distance_ptrs.invokeExact(segment.address(), other.segment.address());
109         } catch (Throwable throwable) {
110             throw new InternalError(throwable);
111         }
112     }
113 
114     @Override
115     public void close() {
116         segment.scope().close();
117     }
118 }