2019-12-12 22:59:57 +00:00
|
|
|
/*
|
2020-01-13 07:31:23 +00:00
|
|
|
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
|
2019-12-12 22:59:57 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @test
|
2020-12-09 06:42:38 +00:00
|
|
|
* @requires ((os.arch == "amd64" | os.arch == "x86_64") & sun.arch.data.model == "64") | os.arch == "aarch64"
|
2020-12-07 17:25:23 +00:00
|
|
|
* @run testng/othervm -Xmx4G -XX:MaxDirectMemorySize=1M TestSegments
|
2019-12-12 22:59:57 +00:00
|
|
|
*/
|
|
|
|
|
2021-06-02 10:53:06 +00:00
|
|
|
import jdk.incubator.foreign.MemoryAccess;
|
2019-12-12 22:59:57 +00:00
|
|
|
import jdk.incubator.foreign.MemoryLayout;
|
|
|
|
import jdk.incubator.foreign.MemoryLayouts;
|
|
|
|
import jdk.incubator.foreign.MemorySegment;
|
2021-06-02 10:53:06 +00:00
|
|
|
import jdk.incubator.foreign.ResourceScope;
|
2020-05-25 09:54:39 +00:00
|
|
|
import org.testng.annotations.DataProvider;
|
|
|
|
import org.testng.annotations.Test;
|
2019-12-12 22:59:57 +00:00
|
|
|
|
|
|
|
import java.lang.invoke.VarHandle;
|
2020-06-03 15:50:03 +00:00
|
|
|
import java.nio.ByteBuffer;
|
2019-12-12 22:59:57 +00:00
|
|
|
import java.nio.ByteOrder;
|
|
|
|
import java.util.List;
|
2020-05-25 09:54:39 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
2020-12-07 17:25:23 +00:00
|
|
|
import java.util.function.IntFunction;
|
2019-12-12 22:59:57 +00:00
|
|
|
import java.util.function.LongFunction;
|
2020-05-25 09:54:39 +00:00
|
|
|
import java.util.function.Supplier;
|
2021-06-02 10:53:06 +00:00
|
|
|
|
2019-12-12 22:59:57 +00:00
|
|
|
import static org.testng.Assert.*;
|
|
|
|
|
|
|
|
public class TestSegments {
|
|
|
|
|
|
|
|
@Test(dataProvider = "badSizeAndAlignments", expectedExceptions = IllegalArgumentException.class)
|
|
|
|
public void testBadAllocateAlign(long size, long align) {
|
2021-06-02 10:53:06 +00:00
|
|
|
MemorySegment.allocateNative(size, align, ResourceScope.newImplicitScope());
|
2019-12-12 22:59:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(dataProvider = "badLayouts", expectedExceptions = UnsupportedOperationException.class)
|
|
|
|
public void testBadAllocateLayout(MemoryLayout layout) {
|
2021-06-02 10:53:06 +00:00
|
|
|
MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
|
2019-12-12 22:59:57 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 07:31:23 +00:00
|
|
|
@Test(expectedExceptions = { OutOfMemoryError.class,
|
|
|
|
IllegalArgumentException.class })
|
2019-12-12 22:59:57 +00:00
|
|
|
public void testAllocateTooBig() {
|
2021-06-02 10:53:06 +00:00
|
|
|
MemorySegment.allocateNative(Long.MAX_VALUE, ResourceScope.newImplicitScope());
|
2020-11-12 16:37:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expectedExceptions = OutOfMemoryError.class)
|
|
|
|
public void testNativeAllocationTooBig() {
|
2021-06-02 10:53:06 +00:00
|
|
|
MemorySegment segment = MemorySegment.allocateNative(1024 * 1024 * 8 * 2, ResourceScope.newImplicitScope()); // 2M
|
2020-11-12 16:37:23 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 22:59:57 +00:00
|
|
|
@Test
|
|
|
|
public void testNativeSegmentIsZeroed() {
|
2021-06-02 10:53:06 +00:00
|
|
|
VarHandle byteHandle = MemoryLayout.sequenceLayout(MemoryLayouts.JAVA_BYTE)
|
2019-12-12 22:59:57 +00:00
|
|
|
.varHandle(byte.class, MemoryLayout.PathElement.sequenceElement());
|
2021-06-02 10:53:06 +00:00
|
|
|
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
|
|
|
|
MemorySegment segment = MemorySegment.allocateNative(1000, 1, scope);
|
2019-12-12 22:59:57 +00:00
|
|
|
for (long i = 0 ; i < segment.byteSize() ; i++) {
|
2020-11-12 16:37:23 +00:00
|
|
|
assertEquals(0, (byte)byteHandle.get(segment, i));
|
2020-05-25 09:54:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:09:00 +00:00
|
|
|
@Test
|
|
|
|
public void testSlices() {
|
2021-06-02 10:53:06 +00:00
|
|
|
VarHandle byteHandle = MemoryLayout.sequenceLayout(MemoryLayouts.JAVA_BYTE)
|
2020-01-15 11:09:00 +00:00
|
|
|
.varHandle(byte.class, MemoryLayout.PathElement.sequenceElement());
|
2021-06-02 10:53:06 +00:00
|
|
|
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
|
|
|
|
MemorySegment segment = MemorySegment.allocateNative(10, 1, scope);
|
2020-01-15 11:09:00 +00:00
|
|
|
//init
|
|
|
|
for (byte i = 0 ; i < segment.byteSize() ; i++) {
|
2020-11-12 16:37:23 +00:00
|
|
|
byteHandle.set(segment, (long)i, i);
|
2020-01-15 11:09:00 +00:00
|
|
|
}
|
2020-11-12 16:37:23 +00:00
|
|
|
for (int offset = 0 ; offset < 10 ; offset++) {
|
|
|
|
MemorySegment slice = segment.asSlice(offset);
|
|
|
|
for (long i = offset ; i < 10 ; i++) {
|
2020-01-15 11:09:00 +00:00
|
|
|
assertEquals(
|
2020-11-12 16:37:23 +00:00
|
|
|
byteHandle.get(segment, i),
|
|
|
|
byteHandle.get(slice, i - offset)
|
2020-01-15 11:09:00 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 10:53:06 +00:00
|
|
|
@Test(expectedExceptions = IndexOutOfBoundsException.class)
|
|
|
|
public void testSmallSegmentMax() {
|
|
|
|
long offset = (long)Integer.MAX_VALUE + (long)Integer.MAX_VALUE + 2L + 6L; // overflows to 6 when casted to int
|
|
|
|
MemorySegment memorySegment = MemorySegment.allocateNative(10, ResourceScope.newImplicitScope());
|
|
|
|
MemoryAccess.getIntAtOffset(memorySegment, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expectedExceptions = IndexOutOfBoundsException.class)
|
|
|
|
public void testSmallSegmentMin() {
|
|
|
|
long offset = ((long)Integer.MIN_VALUE * 2L) + 6L; // underflows to 6 when casted to int
|
|
|
|
MemorySegment memorySegment = MemorySegment.allocateNative(10, ResourceScope.newImplicitScope());
|
|
|
|
MemoryAccess.getIntAtOffset(memorySegment, offset);
|
|
|
|
}
|
|
|
|
|
2020-05-25 09:54:39 +00:00
|
|
|
@Test(dataProvider = "segmentFactories")
|
|
|
|
public void testAccessModesOfFactories(Supplier<MemorySegment> memorySegmentSupplier) {
|
2021-06-02 10:53:06 +00:00
|
|
|
MemorySegment segment = memorySegmentSupplier.get();
|
|
|
|
assertFalse(segment.isReadOnly());
|
|
|
|
tryClose(segment);
|
2020-05-25 09:54:39 +00:00
|
|
|
}
|
|
|
|
|
2021-06-02 10:53:06 +00:00
|
|
|
static void tryClose(MemorySegment segment) {
|
|
|
|
if (!segment.scope().isImplicit()) {
|
|
|
|
segment.scope().close();
|
2020-05-25 09:54:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 15:50:03 +00:00
|
|
|
@DataProvider(name = "segmentFactories")
|
|
|
|
public Object[][] segmentFactories() {
|
|
|
|
List<Supplier<MemorySegment>> l = List.of(
|
|
|
|
() -> MemorySegment.ofArray(new byte[] { 0x00, 0x01, 0x02, 0x03 }),
|
|
|
|
() -> MemorySegment.ofArray(new char[] {'a', 'b', 'c', 'd' }),
|
|
|
|
() -> MemorySegment.ofArray(new double[] { 1d, 2d, 3d, 4d} ),
|
|
|
|
() -> MemorySegment.ofArray(new float[] { 1.0f, 2.0f, 3.0f, 4.0f }),
|
|
|
|
() -> MemorySegment.ofArray(new int[] { 1, 2, 3, 4 }),
|
|
|
|
() -> MemorySegment.ofArray(new long[] { 1l, 2l, 3l, 4l } ),
|
|
|
|
() -> MemorySegment.ofArray(new short[] { 1, 2, 3, 4 } ),
|
2021-06-02 10:53:06 +00:00
|
|
|
() -> MemorySegment.allocateNative(4, ResourceScope.newImplicitScope()),
|
|
|
|
() -> MemorySegment.allocateNative(4, 8, ResourceScope.newImplicitScope()),
|
|
|
|
() -> MemorySegment.allocateNative(MemoryLayout.valueLayout(32, ByteOrder.nativeOrder()), ResourceScope.newImplicitScope()),
|
|
|
|
() -> MemorySegment.allocateNative(4, ResourceScope.newConfinedScope()),
|
|
|
|
() -> MemorySegment.allocateNative(4, 8, ResourceScope.newConfinedScope()),
|
|
|
|
() -> MemorySegment.allocateNative(MemoryLayout.valueLayout(32, ByteOrder.nativeOrder()), ResourceScope.newConfinedScope())
|
|
|
|
|
2020-06-03 15:50:03 +00:00
|
|
|
);
|
|
|
|
return l.stream().map(s -> new Object[] { s }).toArray(Object[][]::new);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(dataProvider = "segmentFactories")
|
|
|
|
public void testFill(Supplier<MemorySegment> memorySegmentSupplier) {
|
2021-06-02 10:53:06 +00:00
|
|
|
VarHandle byteHandle = MemoryLayout.sequenceLayout(MemoryLayouts.JAVA_BYTE)
|
2020-06-03 15:50:03 +00:00
|
|
|
.varHandle(byte.class, MemoryLayout.PathElement.sequenceElement());
|
|
|
|
|
|
|
|
for (byte value : new byte[] {(byte) 0xFF, (byte) 0x00, (byte) 0x45}) {
|
2021-06-02 10:53:06 +00:00
|
|
|
MemorySegment segment = memorySegmentSupplier.get();
|
|
|
|
segment.fill(value);
|
|
|
|
for (long l = 0; l < segment.byteSize(); l++) {
|
|
|
|
assertEquals((byte) byteHandle.get(segment, l), value);
|
|
|
|
}
|
2020-06-03 15:50:03 +00:00
|
|
|
|
2021-06-02 10:53:06 +00:00
|
|
|
// fill a slice
|
|
|
|
var sliceSegment = segment.asSlice(1, segment.byteSize() - 2).fill((byte) ~value);
|
|
|
|
for (long l = 0; l < sliceSegment.byteSize(); l++) {
|
|
|
|
assertEquals((byte) byteHandle.get(sliceSegment, l), ~value);
|
|
|
|
}
|
|
|
|
// assert enclosing slice
|
|
|
|
assertEquals((byte) byteHandle.get(segment, 0L), value);
|
|
|
|
for (long l = 1; l < segment.byteSize() - 2; l++) {
|
|
|
|
assertEquals((byte) byteHandle.get(segment, l), (byte) ~value);
|
2020-06-03 15:50:03 +00:00
|
|
|
}
|
2021-06-02 10:53:06 +00:00
|
|
|
assertEquals((byte) byteHandle.get(segment, segment.byteSize() - 1L), value);
|
|
|
|
tryClose(segment);
|
2020-06-03 15:50:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 10:53:06 +00:00
|
|
|
@Test(dataProvider = "segmentFactories")
|
2020-06-03 15:50:03 +00:00
|
|
|
public void testFillClosed(Supplier<MemorySegment> memorySegmentSupplier) {
|
|
|
|
MemorySegment segment = memorySegmentSupplier.get();
|
2021-06-02 10:53:06 +00:00
|
|
|
tryClose(segment);
|
|
|
|
if (!segment.scope().isAlive()) {
|
|
|
|
try {
|
|
|
|
segment.fill((byte) 0xFF);
|
|
|
|
fail();
|
|
|
|
} catch (IllegalStateException ex) {
|
|
|
|
assertTrue(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(dataProvider = "segmentFactories")
|
|
|
|
public void testNativeSegments(Supplier<MemorySegment> memorySegmentSupplier) throws Exception {
|
|
|
|
MemorySegment segment = memorySegmentSupplier.get();
|
|
|
|
try {
|
|
|
|
segment.address().toRawLongValue();
|
|
|
|
assertTrue(segment.isNative());
|
|
|
|
assertTrue(segment.address().isNative());
|
|
|
|
} catch (UnsupportedOperationException exception) {
|
|
|
|
assertFalse(segment.isNative());
|
|
|
|
assertFalse(segment.address().isNative());
|
|
|
|
}
|
|
|
|
tryClose(segment);
|
2020-06-03 15:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(dataProvider = "segmentFactories", expectedExceptions = UnsupportedOperationException.class)
|
|
|
|
public void testFillIllegalAccessMode(Supplier<MemorySegment> memorySegmentSupplier) {
|
2021-06-02 10:53:06 +00:00
|
|
|
MemorySegment segment = memorySegmentSupplier.get();
|
|
|
|
segment.asReadOnly().fill((byte) 0xFF);
|
|
|
|
tryClose(segment);
|
2020-06-03 15:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(dataProvider = "segmentFactories")
|
|
|
|
public void testFillThread(Supplier<MemorySegment> memorySegmentSupplier) throws Exception {
|
2021-06-02 10:53:06 +00:00
|
|
|
MemorySegment segment = memorySegmentSupplier.get();
|
|
|
|
AtomicReference<RuntimeException> exception = new AtomicReference<>();
|
|
|
|
Runnable action = () -> {
|
|
|
|
try {
|
|
|
|
segment.fill((byte) 0xBA);
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
exception.set(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Thread thread = new Thread(action);
|
|
|
|
thread.start();
|
|
|
|
thread.join();
|
2020-06-03 15:50:03 +00:00
|
|
|
|
2021-06-02 10:53:06 +00:00
|
|
|
if (segment.scope().ownerThread() != null) {
|
2020-06-03 15:50:03 +00:00
|
|
|
RuntimeException e = exception.get();
|
|
|
|
if (!(e instanceof IllegalStateException)) {
|
|
|
|
throw e;
|
|
|
|
}
|
2021-06-02 10:53:06 +00:00
|
|
|
} else {
|
|
|
|
assertNull(exception.get());
|
2020-06-03 15:50:03 +00:00
|
|
|
}
|
2021-06-02 10:53:06 +00:00
|
|
|
tryClose(segment);
|
2020-06-03 15:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testFillEmpty() {
|
|
|
|
MemorySegment.ofArray(new byte[] { }).fill((byte) 0xFF);
|
|
|
|
MemorySegment.ofArray(new byte[2]).asSlice(0, 0).fill((byte) 0xFF);
|
|
|
|
MemorySegment.ofByteBuffer(ByteBuffer.allocateDirect(0)).fill((byte) 0xFF);
|
|
|
|
}
|
|
|
|
|
2020-12-07 17:25:23 +00:00
|
|
|
@Test(dataProvider = "heapFactories")
|
|
|
|
public void testBigHeapSegments(IntFunction<MemorySegment> heapSegmentFactory, int factor) {
|
|
|
|
int bigSize = (Integer.MAX_VALUE / factor) + 1;
|
|
|
|
MemorySegment segment = heapSegmentFactory.apply(bigSize);
|
|
|
|
assertTrue(segment.byteSize() > 0);
|
|
|
|
}
|
|
|
|
|
2019-12-12 22:59:57 +00:00
|
|
|
@DataProvider(name = "badSizeAndAlignments")
|
|
|
|
public Object[][] sizesAndAlignments() {
|
|
|
|
return new Object[][] {
|
|
|
|
{ -1, 8 },
|
|
|
|
{ 1, 15 },
|
|
|
|
{ 1, -15 }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@DataProvider(name = "badLayouts")
|
|
|
|
public Object[][] layouts() {
|
|
|
|
SizedLayoutFactory[] layoutFactories = SizedLayoutFactory.values();
|
|
|
|
Object[][] values = new Object[layoutFactories.length * 2][2];
|
|
|
|
for (int i = 0; i < layoutFactories.length ; i++) {
|
2021-06-02 10:53:06 +00:00
|
|
|
values[i * 2] = new Object[] { MemoryLayout.structLayout(layoutFactories[i].make(7), MemoryLayout.paddingLayout(9)) }; // good size, bad align
|
2019-12-12 22:59:57 +00:00
|
|
|
values[(i * 2) + 1] = new Object[] { layoutFactories[i].make(15).withBitAlignment(16) }; // bad size, good align
|
|
|
|
}
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum SizedLayoutFactory {
|
2021-06-02 10:53:06 +00:00
|
|
|
VALUE_BE(size -> MemoryLayout.valueLayout(size, ByteOrder.BIG_ENDIAN)),
|
|
|
|
VALUE_LE(size -> MemoryLayout.valueLayout(size, ByteOrder.LITTLE_ENDIAN)),
|
|
|
|
PADDING(MemoryLayout::paddingLayout);
|
2019-12-12 22:59:57 +00:00
|
|
|
|
|
|
|
private final LongFunction<MemoryLayout> factory;
|
|
|
|
|
|
|
|
SizedLayoutFactory(LongFunction<MemoryLayout> factory) {
|
|
|
|
this.factory = factory;
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryLayout make(long size) {
|
|
|
|
return factory.apply(size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 17:25:23 +00:00
|
|
|
@DataProvider(name = "heapFactories")
|
|
|
|
public Object[][] heapFactories() {
|
|
|
|
return new Object[][] {
|
|
|
|
{ (IntFunction<MemorySegment>) size -> MemorySegment.ofArray(new char[size]), 2 },
|
|
|
|
{ (IntFunction<MemorySegment>) size -> MemorySegment.ofArray(new short[size]), 2 },
|
|
|
|
{ (IntFunction<MemorySegment>) size -> MemorySegment.ofArray(new int[size]), 4 },
|
|
|
|
{ (IntFunction<MemorySegment>) size -> MemorySegment.ofArray(new float[size]), 4 },
|
|
|
|
{ (IntFunction<MemorySegment>) size -> MemorySegment.ofArray(new long[size]), 8 },
|
|
|
|
{ (IntFunction<MemorySegment>) size -> MemorySegment.ofArray(new double[size]), 8 }
|
|
|
|
};
|
|
|
|
}
|
2019-12-12 22:59:57 +00:00
|
|
|
}
|