36 import jdk.incubator.foreign.MemorySegment;
37 import jdk.incubator.foreign.SequenceLayout;
38 import jdk.internal.misc.Unsafe;
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 org.testng.Assert.*;
58
59 public class TestNative {
60
61 static Unsafe UNSAFE;
62
63 static {
64 UNSAFE = Unsafe.getUnsafe();
65 }
66
67 static SequenceLayout bytes = MemoryLayout.ofSequence(100,
68 MemoryLayouts.JAVA_BYTE.withOrder(ByteOrder.nativeOrder())
69 );
70
71 static SequenceLayout chars = MemoryLayout.ofSequence(100,
72 MemoryLayouts.JAVA_CHAR.withOrder(ByteOrder.nativeOrder())
73 );
74
75 static SequenceLayout shorts = MemoryLayout.ofSequence(100,
76 MemoryLayouts.JAVA_SHORT.withOrder(ByteOrder.nativeOrder())
155 public void testNativeAccess(Consumer<MemoryAddress> checker, Consumer<MemoryAddress> initializer, SequenceLayout seq) {
156 try (MemorySegment segment = MemorySegment.allocateNative(seq)) {
157 MemoryAddress address = segment.baseAddress();
158 initializer.accept(address);
159 checker.accept(address);
160 }
161 }
162
163 @Test(dataProvider="buffers")
164 public void testNativeCapacity(Function<ByteBuffer, Buffer> bufferFunction, int elemSize) {
165 int capacity = (int)doubles.byteSize();
166 try (MemorySegment segment = MemorySegment.allocateNative(doubles)) {
167 ByteBuffer bb = segment.asByteBuffer();
168 Buffer buf = bufferFunction.apply(bb);
169 int expected = capacity / elemSize;
170 assertEquals(buf.capacity(), expected);
171 assertEquals(getCapacity(buf), expected);
172 }
173 }
174
175 @Test
176 public void testMallocSegment() {
177 MemoryAddress addr = MemoryAddress.ofLong(allocate(12));
178 assertNull(addr.segment());
179 MemorySegment mallocSegment = MemorySegment.ofNativeRestricted(addr, 12, null,
180 () -> free(addr.toRawLongValue()), null);
181 assertEquals(mallocSegment.byteSize(), 12);
182 mallocSegment.close(); //free here
183 assertTrue(!mallocSegment.isAlive());
184 }
185
186 @Test(expectedExceptions = IllegalArgumentException.class)
187 public void testBadResize() {
188 try (MemorySegment segment = MemorySegment.allocateNative(4)) {
189 MemorySegment.ofNativeRestricted(segment.baseAddress(), 0, null, null, null);
190 }
191 }
192
193 @Test(expectedExceptions = NullPointerException.class)
194 public void testNullUnsafeSegment() {
|
36 import jdk.incubator.foreign.MemorySegment;
37 import jdk.incubator.foreign.SequenceLayout;
38 import jdk.internal.misc.Unsafe;
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 import static jdk.incubator.foreign.MemorySegment.*;
57 import static org.testng.Assert.*;
58
59 public class TestNative {
60
61 static Unsafe UNSAFE;
62
63 static {
64 UNSAFE = Unsafe.getUnsafe();
65 }
66
67 static SequenceLayout bytes = MemoryLayout.ofSequence(100,
68 MemoryLayouts.JAVA_BYTE.withOrder(ByteOrder.nativeOrder())
69 );
70
71 static SequenceLayout chars = MemoryLayout.ofSequence(100,
72 MemoryLayouts.JAVA_CHAR.withOrder(ByteOrder.nativeOrder())
73 );
74
75 static SequenceLayout shorts = MemoryLayout.ofSequence(100,
76 MemoryLayouts.JAVA_SHORT.withOrder(ByteOrder.nativeOrder())
155 public void testNativeAccess(Consumer<MemoryAddress> checker, Consumer<MemoryAddress> initializer, SequenceLayout seq) {
156 try (MemorySegment segment = MemorySegment.allocateNative(seq)) {
157 MemoryAddress address = segment.baseAddress();
158 initializer.accept(address);
159 checker.accept(address);
160 }
161 }
162
163 @Test(dataProvider="buffers")
164 public void testNativeCapacity(Function<ByteBuffer, Buffer> bufferFunction, int elemSize) {
165 int capacity = (int)doubles.byteSize();
166 try (MemorySegment segment = MemorySegment.allocateNative(doubles)) {
167 ByteBuffer bb = segment.asByteBuffer();
168 Buffer buf = bufferFunction.apply(bb);
169 int expected = capacity / elemSize;
170 assertEquals(buf.capacity(), expected);
171 assertEquals(getCapacity(buf), expected);
172 }
173 }
174
175 static final int ALL_ACCESS_MODES = READ | WRITE | CLOSE | ACQUIRE | HANDOFF;
176
177 @Test
178 public void testDefaultAccessModes() {
179 MemoryAddress addr = MemoryAddress.ofLong(allocate(12));
180 MemorySegment mallocSegment = MemorySegment.ofNativeRestricted(addr, 12, null,
181 () -> free(addr.toRawLongValue()), null);
182 try (MemorySegment segment = mallocSegment) {
183 assertTrue(segment.hasAccessModes(ALL_ACCESS_MODES));
184 assertEquals(segment.accessModes(), ALL_ACCESS_MODES);
185 }
186 }
187
188 @Test
189 public void testMallocSegment() {
190 MemoryAddress addr = MemoryAddress.ofLong(allocate(12));
191 assertNull(addr.segment());
192 MemorySegment mallocSegment = MemorySegment.ofNativeRestricted(addr, 12, null,
193 () -> free(addr.toRawLongValue()), null);
194 assertEquals(mallocSegment.byteSize(), 12);
195 mallocSegment.close(); //free here
196 assertTrue(!mallocSegment.isAlive());
197 }
198
199 @Test(expectedExceptions = IllegalArgumentException.class)
200 public void testBadResize() {
201 try (MemorySegment segment = MemorySegment.allocateNative(4)) {
202 MemorySegment.ofNativeRestricted(segment.baseAddress(), 0, null, null, null);
203 }
204 }
205
206 @Test(expectedExceptions = NullPointerException.class)
207 public void testNullUnsafeSegment() {
|