< prev index next >

test/jdk/java/foreign/TestNative.java

Print this page

 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 
 25 /*
 26  * @test
 27  * @requires ((os.arch == "amd64" | os.arch == "x86_64") & sun.arch.data.model == "64") | os.arch == "aarch64"
 28  * @modules jdk.incubator.foreign/jdk.internal.foreign
 29  * @run testng/othervm --enable-native-access=ALL-UNNAMED TestNative
 30  */
 31 
 32 import jdk.incubator.foreign.CLinker;
 33 import jdk.incubator.foreign.MemoryAccess;
 34 import jdk.incubator.foreign.MemoryAddress;
 35 import jdk.incubator.foreign.MemoryLayout;
 36 import jdk.incubator.foreign.MemoryLayout.PathElement;
 37 import jdk.incubator.foreign.MemoryLayouts;
 38 import jdk.incubator.foreign.MemorySegment;
 39 import jdk.incubator.foreign.ResourceScope;
 40 import jdk.incubator.foreign.SequenceLayout;

 41 import org.testng.annotations.DataProvider;
 42 import org.testng.annotations.Test;
 43 
 44 import java.lang.invoke.VarHandle;
 45 import java.nio.Buffer;
 46 import java.nio.ByteBuffer;
 47 import java.nio.ByteOrder;
 48 import java.nio.CharBuffer;
 49 import java.nio.DoubleBuffer;
 50 import java.nio.FloatBuffer;
 51 import java.nio.IntBuffer;
 52 import java.nio.LongBuffer;
 53 import java.nio.ShortBuffer;
 54 import java.util.function.BiConsumer;
 55 import java.util.function.BiFunction;
 56 import java.util.function.Consumer;
 57 import java.util.function.Function;
 58 

 59 import static org.testng.Assert.*;
 60 
 61 public class TestNative {
 62 
 63     static SequenceLayout bytes = MemoryLayout.sequenceLayout(100,
 64             MemoryLayouts.JAVA_BYTE.withOrder(ByteOrder.nativeOrder())
 65     );
 66 
 67     static SequenceLayout chars = MemoryLayout.sequenceLayout(100,
 68             MemoryLayouts.JAVA_CHAR.withOrder(ByteOrder.nativeOrder())
 69     );
 70 
 71     static SequenceLayout shorts = MemoryLayout.sequenceLayout(100,
 72             MemoryLayouts.JAVA_SHORT.withOrder(ByteOrder.nativeOrder())
 73     );
 74 
 75     static SequenceLayout ints = MemoryLayout.sequenceLayout(100,
 76             MemoryLayouts.JAVA_INT.withOrder(ByteOrder.nativeOrder())
 77     );
 78 
 79     static SequenceLayout floats = MemoryLayout.sequenceLayout(100,
 80             MemoryLayouts.JAVA_FLOAT.withOrder(ByteOrder.nativeOrder())
 81     );
 82 
 83     static SequenceLayout longs = MemoryLayout.sequenceLayout(100,
 84             MemoryLayouts.JAVA_LONG.withOrder(ByteOrder.nativeOrder())
 85     );
 86 
 87     static SequenceLayout doubles = MemoryLayout.sequenceLayout(100,
 88             MemoryLayouts.JAVA_DOUBLE.withOrder(ByteOrder.nativeOrder())
 89     );
 90 
 91     static VarHandle byteHandle = bytes.varHandle(byte.class, PathElement.sequenceElement());
 92     static VarHandle charHandle = chars.varHandle(char.class, PathElement.sequenceElement());
 93     static VarHandle shortHandle = shorts.varHandle(short.class, PathElement.sequenceElement());
 94     static VarHandle intHandle = ints.varHandle(int.class, PathElement.sequenceElement());
 95     static VarHandle floatHandle = floats.varHandle(float.class, PathElement.sequenceElement());
 96     static VarHandle longHandle = doubles.varHandle(long.class, PathElement.sequenceElement());
 97     static VarHandle doubleHandle = longs.varHandle(double.class, PathElement.sequenceElement());
 98 
 99     static void initBytes(MemorySegment base, SequenceLayout seq, BiConsumer<MemorySegment, Long> handleSetter) {
100         for (long i = 0; i < seq.elementCount().getAsLong() ; i++) {
101             handleSetter.accept(base, i);
102         }
103     }
104 
105     static <Z extends Buffer> void checkBytes(MemorySegment base, SequenceLayout layout,
106                                               BiFunction<MemorySegment, Long, Object> handleExtractor,
107                                               Function<ByteBuffer, Z> bufferFactory,
108                                               BiFunction<Z, Integer, Object> nativeBufferExtractor,
109                                               BiFunction<Long, Integer, Object> nativeRawExtractor) {
110         long nelems = layout.elementCount().getAsLong();
111         ByteBuffer bb = base.asByteBuffer();
112         Z z = bufferFactory.apply(bb);
113         for (long i = 0 ; i < nelems ; i++) {
114             Object handleValue = handleExtractor.apply(base, i);
115             Object bufferValue = nativeBufferExtractor.apply(z, (int)i);
116             Object rawValue = nativeRawExtractor.apply(base.address().toRawLongValue(), (int)i);
117             if (handleValue instanceof Number) {

127     }
128 
129     public static native byte getByteBuffer(ByteBuffer buf, int index);
130     public static native char getCharBuffer(CharBuffer buf, int index);
131     public static native short getShortBuffer(ShortBuffer buf, int index);
132     public static native int getIntBuffer(IntBuffer buf, int index);
133     public static native float getFloatBuffer(FloatBuffer buf, int index);
134     public static native long getLongBuffer(LongBuffer buf, int index);
135     public static native double getDoubleBuffer(DoubleBuffer buf, int index);
136 
137     public static native byte getByteRaw(long addr, int index);
138     public static native char getCharRaw(long addr, int index);
139     public static native short getShortRaw(long addr, int index);
140     public static native int getIntRaw(long addr, int index);
141     public static native float getFloatRaw(long addr, int index);
142     public static native long getLongRaw(long addr, int index);
143     public static native double getDoubleRaw(long addr, int index);
144 
145     public static native long getCapacity(Buffer buffer);
146 
147     public static MemoryAddress allocate(int size) {
148         return CLinker.allocateMemory(size);
149     }
150 
151     public static void free(MemoryAddress addr) {
152         CLinker.freeMemory(addr);
153     }
154 
155     @Test(dataProvider="nativeAccessOps")
156     public void testNativeAccess(Consumer<MemorySegment> checker, Consumer<MemorySegment> initializer, SequenceLayout seq) {
157         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
158             MemorySegment segment = MemorySegment.allocateNative(seq, scope);
159             initializer.accept(segment);
160             checker.accept(segment);
161         }
162     }
163 
164     @Test(dataProvider="buffers")
165     public void testNativeCapacity(Function<ByteBuffer, Buffer> bufferFunction, int elemSize) {
166         int capacity = (int)doubles.byteSize();
167         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
168             MemorySegment segment = MemorySegment.allocateNative(doubles, scope);
169             ByteBuffer bb = segment.asByteBuffer();
170             Buffer buf = bufferFunction.apply(bb);
171             int expected = capacity / elemSize;
172             assertEquals(buf.capacity(), expected);
173             assertEquals(getCapacity(buf), expected);
174         }
175     }
176 
177     @Test
178     public void testDefaultAccessModes() {
179         MemoryAddress addr = allocate(12);
180         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
181             MemorySegment mallocSegment = addr.asSegment(12, () -> free(addr), scope);

182             assertFalse(mallocSegment.isReadOnly());
183         }
184     }
185 
186     @Test
187     public void testDefaultAccessModesEverthing() {
188         MemorySegment everything = MemorySegment.globalNativeSegment();
189         assertFalse(everything.isReadOnly());
190     }
191 
192     @Test
193     public void testMallocSegment() {
194         MemoryAddress addr = allocate(12);
195         MemorySegment mallocSegment = null;
196         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
197             mallocSegment = addr.asSegment(12, () -> free(addr), scope);

198             assertEquals(mallocSegment.byteSize(), 12);
199             //free here
200         }
201         assertTrue(!mallocSegment.scope().isAlive());
202     }
203 
204     @Test
205     public void testEverythingSegment() {
206         MemoryAddress addr = allocate(4);
207         MemorySegment everything = MemorySegment.globalNativeSegment();
208         MemoryAccess.setIntAtOffset(everything, addr.toRawLongValue(), 42);
209         assertEquals(MemoryAccess.getIntAtOffset(everything, addr.toRawLongValue()), 42);
210         free(addr);
211     }
212 
213     @Test(expectedExceptions = IllegalArgumentException.class)
214     public void testBadResize() {
215         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
216             MemorySegment segment = MemorySegment.allocateNative(4, 1, scope);
217             segment.address().asSegment(0, ResourceScope.globalScope());
218         }
219     }
220 
221     static {
222         System.loadLibrary("NativeAccess");
223     }
224 
225     @DataProvider(name = "nativeAccessOps")
226     public Object[][] nativeAccessOps() {
227         Consumer<MemorySegment> byteInitializer =
228                 (base) -> initBytes(base, bytes, (addr, pos) -> byteHandle.set(addr, pos, (byte)(long)pos));
229         Consumer<MemorySegment> charInitializer =
230                 (base) -> initBytes(base, chars, (addr, pos) -> charHandle.set(addr, pos, (char)(long)pos));
231         Consumer<MemorySegment> shortInitializer =
232                 (base) -> initBytes(base, shorts, (addr, pos) -> shortHandle.set(addr, pos, (short)(long)pos));
233         Consumer<MemorySegment> intInitializer =
234                 (base) -> initBytes(base, ints, (addr, pos) -> intHandle.set(addr, pos, (int)(long)pos));
235         Consumer<MemorySegment> floatInitializer =
236                 (base) -> initBytes(base, floats, (addr, pos) -> floatHandle.set(addr, pos, (float)(long)pos));
237         Consumer<MemorySegment> longInitializer =

 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 
 25 /*
 26  * @test
 27  * @requires ((os.arch == "amd64" | os.arch == "x86_64") & sun.arch.data.model == "64") | os.arch == "aarch64"
 28  * @modules jdk.incubator.foreign/jdk.internal.foreign
 29  * @run testng/othervm --enable-native-access=ALL-UNNAMED TestNative
 30  */
 31 


 32 import jdk.incubator.foreign.MemoryAddress;
 33 import jdk.incubator.foreign.MemoryLayout;
 34 import jdk.incubator.foreign.MemoryLayout.PathElement;

 35 import jdk.incubator.foreign.MemorySegment;
 36 import jdk.incubator.foreign.ResourceScope;
 37 import jdk.incubator.foreign.SequenceLayout;
 38 import jdk.incubator.foreign.ValueLayout;
 39 import org.testng.annotations.DataProvider;
 40 import org.testng.annotations.Test;
 41 
 42 import java.lang.invoke.VarHandle;
 43 import java.nio.Buffer;
 44 import java.nio.ByteBuffer;
 45 import java.nio.ByteOrder;
 46 import java.nio.CharBuffer;
 47 import java.nio.DoubleBuffer;
 48 import java.nio.FloatBuffer;
 49 import java.nio.IntBuffer;
 50 import java.nio.LongBuffer;
 51 import java.nio.ShortBuffer;
 52 import java.util.function.BiConsumer;
 53 import java.util.function.BiFunction;
 54 import java.util.function.Consumer;
 55 import java.util.function.Function;
 56 
 57 import static jdk.incubator.foreign.ValueLayout.JAVA_INT;
 58 import static org.testng.Assert.*;
 59 
 60 public class TestNative extends NativeTestHelper {
 61 
 62     static SequenceLayout bytes = MemoryLayout.sequenceLayout(100,
 63             ValueLayout.JAVA_BYTE.withOrder(ByteOrder.nativeOrder())
 64     );
 65 
 66     static SequenceLayout chars = MemoryLayout.sequenceLayout(100,
 67             ValueLayout.JAVA_CHAR.withOrder(ByteOrder.nativeOrder())
 68     );
 69 
 70     static SequenceLayout shorts = MemoryLayout.sequenceLayout(100,
 71             ValueLayout.JAVA_SHORT.withOrder(ByteOrder.nativeOrder())
 72     );
 73 
 74     static SequenceLayout ints = MemoryLayout.sequenceLayout(100,
 75             JAVA_INT.withOrder(ByteOrder.nativeOrder())
 76     );
 77 
 78     static SequenceLayout floats = MemoryLayout.sequenceLayout(100,
 79             ValueLayout.JAVA_FLOAT.withOrder(ByteOrder.nativeOrder())
 80     );
 81 
 82     static SequenceLayout longs = MemoryLayout.sequenceLayout(100,
 83             ValueLayout.JAVA_LONG.withOrder(ByteOrder.nativeOrder())
 84     );
 85 
 86     static SequenceLayout doubles = MemoryLayout.sequenceLayout(100,
 87             ValueLayout.JAVA_DOUBLE.withOrder(ByteOrder.nativeOrder())
 88     );
 89 
 90     static VarHandle byteHandle = bytes.varHandle(PathElement.sequenceElement());
 91     static VarHandle charHandle = chars.varHandle(PathElement.sequenceElement());
 92     static VarHandle shortHandle = shorts.varHandle(PathElement.sequenceElement());
 93     static VarHandle intHandle = ints.varHandle(PathElement.sequenceElement());
 94     static VarHandle floatHandle = floats.varHandle(PathElement.sequenceElement());
 95     static VarHandle longHandle = longs.varHandle(PathElement.sequenceElement());
 96     static VarHandle doubleHandle = doubles.varHandle(PathElement.sequenceElement());
 97 
 98     static void initBytes(MemorySegment base, SequenceLayout seq, BiConsumer<MemorySegment, Long> handleSetter) {
 99         for (long i = 0; i < seq.elementCount().getAsLong() ; i++) {
100             handleSetter.accept(base, i);
101         }
102     }
103 
104     static <Z extends Buffer> void checkBytes(MemorySegment base, SequenceLayout layout,
105                                               BiFunction<MemorySegment, Long, Object> handleExtractor,
106                                               Function<ByteBuffer, Z> bufferFactory,
107                                               BiFunction<Z, Integer, Object> nativeBufferExtractor,
108                                               BiFunction<Long, Integer, Object> nativeRawExtractor) {
109         long nelems = layout.elementCount().getAsLong();
110         ByteBuffer bb = base.asByteBuffer();
111         Z z = bufferFactory.apply(bb);
112         for (long i = 0 ; i < nelems ; i++) {
113             Object handleValue = handleExtractor.apply(base, i);
114             Object bufferValue = nativeBufferExtractor.apply(z, (int)i);
115             Object rawValue = nativeRawExtractor.apply(base.address().toRawLongValue(), (int)i);
116             if (handleValue instanceof Number) {

126     }
127 
128     public static native byte getByteBuffer(ByteBuffer buf, int index);
129     public static native char getCharBuffer(CharBuffer buf, int index);
130     public static native short getShortBuffer(ShortBuffer buf, int index);
131     public static native int getIntBuffer(IntBuffer buf, int index);
132     public static native float getFloatBuffer(FloatBuffer buf, int index);
133     public static native long getLongBuffer(LongBuffer buf, int index);
134     public static native double getDoubleBuffer(DoubleBuffer buf, int index);
135 
136     public static native byte getByteRaw(long addr, int index);
137     public static native char getCharRaw(long addr, int index);
138     public static native short getShortRaw(long addr, int index);
139     public static native int getIntRaw(long addr, int index);
140     public static native float getFloatRaw(long addr, int index);
141     public static native long getLongRaw(long addr, int index);
142     public static native double getDoubleRaw(long addr, int index);
143 
144     public static native long getCapacity(Buffer buffer);
145 








146     @Test(dataProvider="nativeAccessOps")
147     public void testNativeAccess(Consumer<MemorySegment> checker, Consumer<MemorySegment> initializer, SequenceLayout seq) {
148         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
149             MemorySegment segment = MemorySegment.allocateNative(seq, scope);
150             initializer.accept(segment);
151             checker.accept(segment);
152         }
153     }
154 
155     @Test(dataProvider="buffers")
156     public void testNativeCapacity(Function<ByteBuffer, Buffer> bufferFunction, int elemSize) {
157         int capacity = (int)doubles.byteSize();
158         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
159             MemorySegment segment = MemorySegment.allocateNative(doubles, scope);
160             ByteBuffer bb = segment.asByteBuffer();
161             Buffer buf = bufferFunction.apply(bb);
162             int expected = capacity / elemSize;
163             assertEquals(buf.capacity(), expected);
164             assertEquals(getCapacity(buf), expected);
165         }
166     }
167 
168     @Test
169     public void testDefaultAccessModes() {
170         MemoryAddress addr = allocateMemory(12);
171         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
172             scope.addCloseAction(() -> freeMemory(addr));
173             MemorySegment mallocSegment = MemorySegment.ofAddressNative(addr, 12, scope);
174             assertFalse(mallocSegment.isReadOnly());
175         }
176     }
177 






178     @Test
179     public void testMallocSegment() {
180         MemoryAddress addr = allocateMemory(12);
181         MemorySegment mallocSegment = null;
182         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
183             scope.addCloseAction(() -> freeMemory(addr));
184             mallocSegment = MemorySegment.ofAddressNative(addr, 12, scope);
185             assertEquals(mallocSegment.byteSize(), 12);
186             //free here
187         }
188         assertTrue(!mallocSegment.scope().isAlive());
189     }
190 
191     @Test
192     public void testAddressAccess() {
193         MemoryAddress addr = allocateMemory(4);
194         addr.set(JAVA_INT, 0, 42);
195         assertEquals(addr.get(JAVA_INT, 0), 42);
196         freeMemory(addr);

197     }
198 
199     @Test(expectedExceptions = IllegalArgumentException.class)
200     public void testBadResize() {
201         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
202             MemorySegment segment = MemorySegment.allocateNative(4, 1, scope);
203             MemorySegment.ofAddressNative(segment.address(), 0, ResourceScope.globalScope());
204         }
205     }
206 
207     static {
208         System.loadLibrary("NativeAccess");
209     }
210 
211     @DataProvider(name = "nativeAccessOps")
212     public Object[][] nativeAccessOps() {
213         Consumer<MemorySegment> byteInitializer =
214                 (base) -> initBytes(base, bytes, (addr, pos) -> byteHandle.set(addr, pos, (byte)(long)pos));
215         Consumer<MemorySegment> charInitializer =
216                 (base) -> initBytes(base, chars, (addr, pos) -> charHandle.set(addr, pos, (char)(long)pos));
217         Consumer<MemorySegment> shortInitializer =
218                 (base) -> initBytes(base, shorts, (addr, pos) -> shortHandle.set(addr, pos, (short)(long)pos));
219         Consumer<MemorySegment> intInitializer =
220                 (base) -> initBytes(base, ints, (addr, pos) -> intHandle.set(addr, pos, (int)(long)pos));
221         Consumer<MemorySegment> floatInitializer =
222                 (base) -> initBytes(base, floats, (addr, pos) -> floatHandle.set(addr, pos, (float)(long)pos));
223         Consumer<MemorySegment> longInitializer =
< prev index next >