1 /*
 2  * Copyright (c) 2021, 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 
24 package org.openjdk.bench.java.lang.foreign.pointers;
25 
26 import java.lang.foreign.AddressLayout;
27 import java.lang.foreign.GroupLayout;
28 import java.lang.foreign.MemoryLayout;
29 import java.lang.foreign.ValueLayout;
30 
31 public sealed abstract class NativeType<X> {
32 
33     public abstract MemoryLayout layout();
34 
35     public non-sealed static abstract class OfInt<X> extends NativeType<X> {
36         public abstract ValueLayout.OfInt layout();
37     }
38     public non-sealed static abstract class OfDouble<X> extends NativeType<X> {
39         public abstract ValueLayout.OfDouble layout();
40     }
41 
42     private static final AddressLayout UNSAFE_ADDRESS = ValueLayout.ADDRESS
43             .withTargetLayout(MemoryLayout.sequenceLayout(ValueLayout.JAVA_BYTE));
44 
45     public final static class OfPointer<X> extends NativeType<X> {
46         public AddressLayout layout() {
47             return UNSAFE_ADDRESS;
48         }
49     }
50 
51     public non-sealed static abstract class OfStruct<X> extends NativeType<X> {
52         public abstract GroupLayout layout();
53         public abstract X make(Pointer<X> ptr);
54     }
55 
56     public static final OfInt<Integer> C_INT = new OfInt<>() {
57         @Override
58         public ValueLayout.OfInt layout() {
59             return ValueLayout.JAVA_INT;
60         }
61     };
62 
63     public static final OfDouble<Double> C_DOUBLE = new OfDouble<>() {
64         @Override
65         public ValueLayout.OfDouble layout() {
66             return ValueLayout.JAVA_DOUBLE;
67         }
68     };
69 
70     @SuppressWarnings("unchecked")
71     final static OfPointer C_VOID_PTR = new OfPointer();
72 
73     @SuppressWarnings("unchecked")
74     public static final OfPointer<Pointer<Integer>> C_INT_PTR = NativeType.C_VOID_PTR;
75     @SuppressWarnings("unchecked")
76     public static final OfPointer<Pointer<Double>> C_DOUBLE_PTR = NativeType.C_VOID_PTR;
77 
78 
79 
80     @SuppressWarnings("unchecked")
81     public static <Z> OfPointer<Pointer<Z>> ptr(NativeType<Z> type) {
82         return NativeType.C_VOID_PTR;
83     }
84 }