diff --git a/src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/HeapMemorySegmentImpl.java b/src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/HeapMemorySegmentImpl.java index 46a0a428d16..30e2fc0608b 100644 --- a/src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/HeapMemorySegmentImpl.java +++ b/src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/HeapMemorySegmentImpl.java @@ -98,7 +98,7 @@ public abstract class HeapMemorySegmentImpl extends AbstractMemorySegmentImpl public static MemorySegment fromArray(byte[] arr) { Objects.requireNonNull(arr); - int byteSize = arr.length * Unsafe.ARRAY_BYTE_INDEX_SCALE; + long byteSize = (long)arr.length * Unsafe.ARRAY_BYTE_INDEX_SCALE; MemoryScope scope = MemoryScope.createConfined(null, MemoryScope.DUMMY_CLEANUP_ACTION, null); return new OfByte(Unsafe.ARRAY_BYTE_BASE_OFFSET, arr, byteSize, defaultAccessModes(byteSize), scope); } @@ -122,7 +122,7 @@ public abstract class HeapMemorySegmentImpl extends AbstractMemorySegmentImpl public static MemorySegment fromArray(char[] arr) { Objects.requireNonNull(arr); - int byteSize = arr.length * Unsafe.ARRAY_CHAR_INDEX_SCALE; + long byteSize = (long)arr.length * Unsafe.ARRAY_CHAR_INDEX_SCALE; MemoryScope scope = MemoryScope.createConfined(null, MemoryScope.DUMMY_CLEANUP_ACTION, null); return new OfChar(Unsafe.ARRAY_CHAR_BASE_OFFSET, arr, byteSize, defaultAccessModes(byteSize), scope); } @@ -146,7 +146,7 @@ public abstract class HeapMemorySegmentImpl extends AbstractMemorySegmentImpl public static MemorySegment fromArray(short[] arr) { Objects.requireNonNull(arr); - int byteSize = arr.length * Unsafe.ARRAY_SHORT_INDEX_SCALE; + long byteSize = (long)arr.length * Unsafe.ARRAY_SHORT_INDEX_SCALE; MemoryScope scope = MemoryScope.createConfined(null, MemoryScope.DUMMY_CLEANUP_ACTION, null); return new OfShort(Unsafe.ARRAY_SHORT_BASE_OFFSET, arr, byteSize, defaultAccessModes(byteSize), scope); } @@ -170,7 +170,7 @@ public abstract class HeapMemorySegmentImpl extends AbstractMemorySegmentImpl public static MemorySegment fromArray(int[] arr) { Objects.requireNonNull(arr); - int byteSize = arr.length * Unsafe.ARRAY_INT_INDEX_SCALE; + long byteSize = (long)arr.length * Unsafe.ARRAY_INT_INDEX_SCALE; MemoryScope scope = MemoryScope.createConfined(null, MemoryScope.DUMMY_CLEANUP_ACTION, null); return new OfInt(Unsafe.ARRAY_INT_BASE_OFFSET, arr, byteSize, defaultAccessModes(byteSize), scope); } @@ -194,7 +194,7 @@ public abstract class HeapMemorySegmentImpl extends AbstractMemorySegmentImpl public static MemorySegment fromArray(long[] arr) { Objects.requireNonNull(arr); - int byteSize = arr.length * Unsafe.ARRAY_LONG_INDEX_SCALE; + long byteSize = (long)arr.length * Unsafe.ARRAY_LONG_INDEX_SCALE; MemoryScope scope = MemoryScope.createConfined(null, MemoryScope.DUMMY_CLEANUP_ACTION, null); return new OfLong(Unsafe.ARRAY_LONG_BASE_OFFSET, arr, byteSize, defaultAccessModes(byteSize), scope); } @@ -218,7 +218,7 @@ public abstract class HeapMemorySegmentImpl extends AbstractMemorySegmentImpl public static MemorySegment fromArray(float[] arr) { Objects.requireNonNull(arr); - int byteSize = arr.length * Unsafe.ARRAY_FLOAT_INDEX_SCALE; + long byteSize = (long)arr.length * Unsafe.ARRAY_FLOAT_INDEX_SCALE; MemoryScope scope = MemoryScope.createConfined(null, MemoryScope.DUMMY_CLEANUP_ACTION, null); return new OfFloat(Unsafe.ARRAY_FLOAT_BASE_OFFSET, arr, byteSize, defaultAccessModes(byteSize), scope); } @@ -242,7 +242,7 @@ public abstract class HeapMemorySegmentImpl extends AbstractMemorySegmentImpl public static MemorySegment fromArray(double[] arr) { Objects.requireNonNull(arr); - int byteSize = arr.length * Unsafe.ARRAY_DOUBLE_INDEX_SCALE; + long byteSize = (long)arr.length * Unsafe.ARRAY_DOUBLE_INDEX_SCALE; MemoryScope scope = MemoryScope.createConfined(null, MemoryScope.DUMMY_CLEANUP_ACTION, null); return new OfDouble(Unsafe.ARRAY_DOUBLE_BASE_OFFSET, arr, byteSize, defaultAccessModes(byteSize), scope); } diff --git a/test/jdk/java/foreign/TestSegments.java b/test/jdk/java/foreign/TestSegments.java index 862b55d983f..9568830683b 100644 --- a/test/jdk/java/foreign/TestSegments.java +++ b/test/jdk/java/foreign/TestSegments.java @@ -23,7 +23,7 @@ /* * @test - * @run testng/othervm -XX:MaxDirectMemorySize=1M TestSegments + * @run testng/othervm -Xmx4G -XX:MaxDirectMemorySize=1M TestSegments */ import jdk.incubator.foreign.MappedMemorySegments; @@ -47,6 +47,8 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Function; +import java.util.function.IntFunction; import java.util.function.LongFunction; import java.util.function.Supplier; import java.util.stream.Stream; @@ -277,6 +279,13 @@ public class TestSegments { segment.hasAccessModes((1 << AccessActions.values().length) + 1); } + @Test(dataProvider = "heapFactories") + public void testBigHeapSegments(IntFunction heapSegmentFactory, int factor) { + int bigSize = (Integer.MAX_VALUE / factor) + 1; + MemorySegment segment = heapSegmentFactory.apply(bigSize); + assertTrue(segment.byteSize() > 0); + } + @DataProvider(name = "badSizeAndAlignments") public Object[][] sizesAndAlignments() { return new Object[][] { @@ -445,4 +454,16 @@ public class TestSegments { abstract void run(MemorySegment segment); } + + @DataProvider(name = "heapFactories") + public Object[][] heapFactories() { + return new Object[][] { + { (IntFunction) size -> MemorySegment.ofArray(new char[size]), 2 }, + { (IntFunction) size -> MemorySegment.ofArray(new short[size]), 2 }, + { (IntFunction) size -> MemorySegment.ofArray(new int[size]), 4 }, + { (IntFunction) size -> MemorySegment.ofArray(new float[size]), 4 }, + { (IntFunction) size -> MemorySegment.ofArray(new long[size]), 8 }, + { (IntFunction) size -> MemorySegment.ofArray(new double[size]), 8 } + }; + } }