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