1 /* 2 * Copyright (c) 2019, 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 25 /* 26 * @test 27 * @run testng TestMemoryCopy 28 */ 29 30 import jdk.incubator.foreign.MemoryAddress; 31 import jdk.incubator.foreign.MemoryLayouts; 32 import jdk.incubator.foreign.MemorySegment; 33 import org.testng.annotations.DataProvider; 34 import org.testng.annotations.Test; 35 36 import java.lang.invoke.VarHandle; 37 import java.util.ArrayList; 38 import java.util.List; 39 import java.util.function.IntFunction; 40 41 import static org.testng.Assert.*; 42 43 public class TestMemoryCopy { 44 45 final static VarHandle BYTE_HANDLE = MemoryLayouts.JAVA_BYTE.varHandle(byte.class); 46 47 @Test(dataProvider = "slices") 48 public void testCopy(SegmentSlice s1, SegmentSlice s2) { 49 MemoryAddress addr1 = s1.segment.baseAddress(); 50 MemoryAddress addr2 = s2.segment.baseAddress(); 51 int size = Math.min(s1.size(), s2.size()); 52 //prepare source and target segments 53 for (int i = 0 ; i < size ; i++) { 54 BYTE_HANDLE.set(addr2.addOffset(i), (byte)0); 55 } 56 for (int i = 0 ; i < size ; i++) { 57 BYTE_HANDLE.set(addr1.addOffset(i), (byte) i); 58 } 59 //perform copy 60 MemoryAddress.copy(addr1, addr2, size); 61 //check that copy actually worked 62 for (int i = 0 ; i < size ; i++) { 63 assertEquals((byte)i, BYTE_HANDLE.get(addr2.addOffset(i))); 64 } 65 } 66 67 static class SegmentSlice { 68 69 enum Kind { 70 NATIVE(MemorySegment::allocateNative), 71 ARRAY(i -> MemorySegment.ofArray(new byte[i])); 72 73 final IntFunction<MemorySegment> segmentFactory; 74 75 Kind(IntFunction<MemorySegment> segmentFactory) { 76 this.segmentFactory = segmentFactory; 77 } 78 79 MemorySegment makeSegment(int elems) { 80 return segmentFactory.apply(elems); 81 } 82 } 83 84 final Kind kind; 85 final int first; 86 final int last; 87 final MemorySegment segment; 88 89 public SegmentSlice(Kind kind, int first, int last, MemorySegment segment) { 90 this.kind = kind; 91 this.first = first; 92 this.last = last; 93 this.segment = segment; 94 } 95 96 int size() { 97 return last - first + 1; 98 } 99 } 100 101 @DataProvider(name = "slices") 102 static Object[][] slices() { 103 int[] sizes = { 16, 8, 4, 2, 1 }; 104 List<SegmentSlice> slices = new ArrayList<>(); 105 for (SegmentSlice.Kind kind : SegmentSlice.Kind.values()) { 106 MemorySegment segment = kind.makeSegment(16); 107 //compute all slices 108 for (int size : sizes) { 109 for (int index = 0 ; index < 16 ; index += size) { 110 MemorySegment slice = segment.asSlice(index, size); 111 slices.add(new SegmentSlice(kind, index, index + size - 1, slice)); 112 } 113 } 114 } 115 Object[][] sliceArray = new Object[slices.size() * slices.size()][]; 116 for (int i = 0 ; i < slices.size() ; i++) { 117 for (int j = 0 ; j < slices.size() ; j++) { 118 sliceArray[i * slices.size() + j] = new Object[] { slices.get(i), slices.get(j) }; 119 } 120 } 121 return sliceArray; 122 } 123 }