1 /*
  2  * Copyright (c) 2020, 2022, 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.java.lang.foreign.points.support;
 24 
 25 import java.lang.foreign.*;
 26 
 27 import org.openjdk.bench.java.lang.foreign.CLayouts;
 28 
 29 import java.lang.invoke.MethodHandle;
 30 import java.lang.invoke.VarHandle;
 31 
 32 import static java.lang.foreign.MemoryLayout.PathElement.groupElement;
 33 
 34 public class PanamaPoint extends CLayouts implements AutoCloseable {
 35 
 36     public static final MemoryLayout LAYOUT = MemoryLayout.structLayout(
 37         C_INT.withName("x"),
 38         C_INT.withName("y")
 39     );
 40 
 41     private static final VarHandle VH_x = LAYOUT.varHandle(groupElement("x"));
 42     private static final VarHandle VH_y = LAYOUT.varHandle(groupElement("y"));
 43     private static final MethodHandle MH_distance;
 44     private static final MethodHandle MH_distance_ptrs;
 45 
 46     static {
 47         Linker abi = Linker.nativeLinker();
 48         System.loadLibrary("Point");
 49         SymbolLookup loaderLibs = SymbolLookup.loaderLookup();
 50         MH_distance = abi.downcallHandle(
 51                 loaderLibs.find("distance").get(),
 52                 FunctionDescriptor.of(C_DOUBLE, LAYOUT, LAYOUT)
 53         );
 54         MH_distance_ptrs = abi.downcallHandle(
 55                 loaderLibs.find("distance_ptrs").get(),
 56                 FunctionDescriptor.of(C_DOUBLE, C_POINTER, C_POINTER)
 57         );
 58     }
 59 
 60     Arena arena;
 61     private final MemorySegment segment;
 62 
 63     public PanamaPoint(int x, int y) {
 64         this.arena = Arena.ofConfined();
 65         this.segment = arena.allocate(LAYOUT);
 66         setX(x);
 67         setY(y);
 68     }
 69 
 70     public void setX(int x) {
 71         VH_x.set(segment, 0L, x);
 72     }
 73 
 74     public int getX() {
 75         return (int) VH_x.get(segment, 0L);
 76     }
 77 
 78     public void setY(int y) {
 79         VH_y.set(segment, 0L, y);
 80     }
 81 
 82     public int getY() {
 83         return (int) VH_y.get(segment, 0L);
 84     }
 85 
 86     public double distanceTo(PanamaPoint other) {
 87         try {
 88             return (double) MH_distance.invokeExact(segment, other.segment);
 89         } catch (Throwable throwable) {
 90             throw new InternalError(throwable);
 91         }
 92     }
 93 
 94     public double distanceToPtrs(PanamaPoint other) {
 95         try {
 96             return (double) MH_distance_ptrs.invokeExact(segment, other.segment);
 97         } catch (Throwable throwable) {
 98             throw new InternalError(throwable);
 99         }
100     }
101 
102     @Override
103     public void close() {
104         arena.close();
105     }
106 }