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.MemoryHandles;
  32 import jdk.incubator.foreign.MemoryLayouts;
  33 import jdk.incubator.foreign.MemorySegment;
  34 import org.testng.annotations.DataProvider;
  35 import org.testng.annotations.Test;
  36 
  37 import java.lang.invoke.VarHandle;
  38 import java.util.ArrayList;
  39 import java.util.Collections;
  40 import java.util.List;
  41 import java.util.function.IntFunction;
  42 import java.util.stream.Collectors;
  43 import java.util.stream.IntStream;
  44 
  45 import static org.testng.Assert.*;
  46 
  47 public class TestMemoryCopy {
  48 
  49     final static VarHandle BYTE_HANDLE = MemoryLayouts.JAVA_BYTE.varHandle(byte.class);
  50 
  51     @Test(dataProvider = "slices")
  52     public void testCopy(SegmentSlice s1, SegmentSlice s2) {
  53         MemoryAddress addr1 = s1.segment.baseAddress();
  54         MemoryAddress addr2 = s2.segment.baseAddress();
  55         int size = Math.min(s1.size(), s2.size());
  56         boolean overlap = SegmentSlice.overlap(s1, s2, size);
  57         //prepare source and target segments
  58         for (int i = 0 ; i < size ; i++) {
  59             BYTE_HANDLE.set(addr1.offset(i), (byte)i);
  60             BYTE_HANDLE.set(addr2.offset(i), (byte)0);
  61         }
  62         try {
  63             MemoryAddress.copy(addr1, addr2, size);
  64             assertFalse(overlap);
  65             //check that copy actually worked
  66             for (int i = 0 ; i < size ; i++) {
  67                 assertEquals((byte)i, BYTE_HANDLE.get(addr2.offset(i)));
  68             }
  69 
  70         } catch (IllegalArgumentException ex) {
  71             assertTrue(overlap);
  72         }
  73     }
  74 
  75     static class SegmentSlice {
  76 
  77         enum Kind {
  78             NATIVE(MemorySegment::allocateNative),
  79             ARRAY(i -> MemorySegment.ofArray(new byte[i]));
  80 
  81             final IntFunction<MemorySegment> segmentFactory;
  82 
  83             Kind(IntFunction<MemorySegment> segmentFactory) {
  84                 this.segmentFactory = segmentFactory;
  85             }
  86 
  87             MemorySegment makeSegment(int elems) {
  88                 return segmentFactory.apply(elems);
  89             }
  90         }
  91 
  92         final Kind kind;
  93         final int first;
  94         final int last;
  95         final MemorySegment segment;
  96 
  97         public SegmentSlice(Kind kind, int first, int last, MemorySegment segment) {
  98             this.kind = kind;
  99             this.first = first;
 100             this.last = last;
 101             this.segment = segment;
 102         }
 103 
 104         List<Integer> range(int size) {
 105             return IntStream.range(first, last + 1)
 106                     .limit(size)
 107                     .boxed()
 108                     .collect(Collectors.toList());
 109         }
 110 
 111         static boolean overlap(SegmentSlice s1, SegmentSlice s2, int size) {
 112             if (s1.kind != s2.kind) {
 113                 return false;
 114             }
 115             return !Collections.disjoint(s1.range(size), s2.range(size));
 116         }
 117 
 118         int size() {
 119             return last - first + 1;
 120         }
 121     }
 122 
 123     @DataProvider(name = "slices")
 124     static Object[][] slices() {
 125         int[] sizes = { 16, 8, 4, 2, 1 };
 126         List<SegmentSlice> slices = new ArrayList<>();
 127         for (SegmentSlice.Kind kind : SegmentSlice.Kind.values()) {
 128             MemorySegment segment = kind.makeSegment(16);
 129             //compute all slices
 130             for (int size : sizes) {
 131                 for (int index = 0 ; index < 16 ; index += size) {
 132                     MemorySegment slice = segment.asSlice(index, size);
 133                     slices.add(new SegmentSlice(kind, index, index + size - 1, slice));
 134                 }
 135             }
 136         }
 137         Object[][] sliceArray = new Object[slices.size() * slices.size()][];
 138         for (int i = 0 ; i < slices.size() ; i++) {
 139             for (int j = 0 ; j < slices.size() ; j++) {
 140                 sliceArray[i * slices.size() + j] = new Object[] { slices.get(i), slices.get(j) };
 141             }
 142         }
 143         return sliceArray;
 144     }
 145 }