1 /*
  2  * Copyright (c) 2020, 2024, 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.findOrThrow("distance"),
 52                 FunctionDescriptor.of(C_DOUBLE, LAYOUT, LAYOUT)
 53         );
 54         MH_distance_ptrs = abi.downcallHandle(
 55                 loaderLibs.findOrThrow("distance_ptrs"),
 56                 FunctionDescriptor.of(C_DOUBLE, C_POINTER, C_POINTER)
 57         );
 58     }
 59 
 60     Arena arena;
 61     private final MemorySegment segment;
 62 
 63     @SuppressWarnings("initialization")
 64     public PanamaPoint(int x, int y) {
 65         this.arena = Arena.ofConfined();
 66         this.segment = arena.allocate(LAYOUT);
 67         setX(x);
 68         setY(y);
 69     }
 70 
 71     public void setX(int x) {
 72         VH_x.set(segment, 0L, x);
 73     }
 74 
 75     public int getX() {
 76         return (int) VH_x.get(segment, 0L);
 77     }
 78 
 79     public void setY(int y) {
 80         VH_y.set(segment, 0L, y);
 81     }
 82 
 83     public int getY() {
 84         return (int) VH_y.get(segment, 0L);
 85     }
 86 
 87     public double distanceTo(PanamaPoint other) {
 88         try {
 89             return (double) MH_distance.invokeExact(segment, other.segment);
 90         } catch (Throwable throwable) {
 91             throw new InternalError(throwable);
 92         }
 93     }
 94 
 95     public double distanceToPtrs(PanamaPoint other) {
 96         try {
 97             return (double) MH_distance_ptrs.invokeExact(segment, other.segment);
 98         } catch (Throwable throwable) {
 99             throw new InternalError(throwable);
100         }
101     }
102 
103     @Override
104     public void close() {
105         arena.close();
106     }
107 }