< prev index next >

test/jdk/java/foreign/TestHeapAlignment.java

Print this page

  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  * @test
 26  * @enablePreview
 27  * @requires jdk.foreign.linker != "UNSUPPORTED"
 28  * @run testng/othervm --enable-native-access=ALL-UNNAMED TestHeapAlignment
 29  */
 30 
 31 import java.lang.foreign.AddressLayout;
 32 import java.lang.foreign.MemoryLayout;
 33 import java.lang.foreign.MemorySegment;
 34 import java.lang.foreign.Arena;
 35 import java.lang.foreign.ValueLayout;
 36 import java.util.ArrayList;
 37 import java.util.List;
 38 import java.util.function.Function;
 39 import org.testng.annotations.DataProvider;
 40 import org.testng.annotations.Test;
 41 
 42 import static org.testng.Assert.fail;
 43 
 44 public class TestHeapAlignment {
 45 
 46     @Test(dataProvider = "layouts")
 47     public void testHeapAlignment(MemorySegment segment, int align, Object val, Object arr, ValueLayout layout, Function<Object, MemorySegment> segmentFactory) {
 48         assertAligned(align, layout, () -> layout.varHandle().get(segment));
 49         assertAligned(align, layout, () -> layout.varHandle().set(segment, val));
 50         MemoryLayout seq = MemoryLayout.sequenceLayout(10, layout);
 51         assertAligned(align, layout, () -> seq.varHandle(MemoryLayout.PathElement.sequenceElement()).get(segment, 0L));
 52         assertAligned(align, layout, () -> seq.varHandle(MemoryLayout.PathElement.sequenceElement()).set(segment, 0L, val));
 53         assertAligned(align, layout, () -> segment.spliterator(layout));
 54         if (arr != null) {
 55             assertAligned(align, layout, () -> MemorySegment.copy(arr, 0, segment, layout, 0, 1));
 56             assertAligned(align, layout, () -> MemorySegment.copy(segment, layout, 0, arr, 0, 1));
 57             assertAligned(align, layout, () -> {
 58                 MemorySegment other = segmentFactory.apply(arr);
 59                 MemorySegment.copy(other, layout, 0, segment, layout, 0, 1);
 60             });
 61             MemorySegment other = segmentFactory.apply(arr);
 62             assertAligned(align, layout, () -> {
 63                 MemorySegment.copy(segment, layout, 0, other, layout, 0, 1);
 64             });
 65             assertAligned(align, layout, () -> {
 66                 MemorySegment.copy(other, layout, 0, segment, layout, 0, 1);
 67             });
 68         }
 69     }
 70 
 71     static void assertAligned(int align, ValueLayout layout, Runnable runnable) {
 72         boolean shouldFail = layout.byteAlignment() > align && align != -1;

  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  * @test


 26  * @run testng/othervm --enable-native-access=ALL-UNNAMED TestHeapAlignment
 27  */
 28 
 29 import java.lang.foreign.AddressLayout;
 30 import java.lang.foreign.MemoryLayout;
 31 import java.lang.foreign.MemorySegment;
 32 import java.lang.foreign.Arena;
 33 import java.lang.foreign.ValueLayout;
 34 import java.util.ArrayList;
 35 import java.util.List;
 36 import java.util.function.Function;
 37 import org.testng.annotations.DataProvider;
 38 import org.testng.annotations.Test;
 39 
 40 import static org.testng.Assert.fail;
 41 
 42 public class TestHeapAlignment {
 43 
 44     @Test(dataProvider = "layouts")
 45     public void testHeapAlignment(MemorySegment segment, int align, Object val, Object arr, ValueLayout layout, Function<Object, MemorySegment> segmentFactory) {
 46         assertAligned(align, layout, () -> layout.varHandle().get(segment, 0L));
 47         assertAligned(align, layout, () -> layout.varHandle().set(segment, 0L, val));
 48         MemoryLayout seq = MemoryLayout.sequenceLayout(10, layout);
 49         assertAligned(align, layout, () -> seq.varHandle(MemoryLayout.PathElement.sequenceElement()).get(segment, 0L, 0L));
 50         assertAligned(align, layout, () -> seq.varHandle(MemoryLayout.PathElement.sequenceElement()).set(segment, 0L, 0L, val));
 51         assertAligned(align, layout, () -> segment.spliterator(layout));
 52         if (arr != null) {
 53             assertAligned(align, layout, () -> MemorySegment.copy(arr, 0, segment, layout, 0, 1));
 54             assertAligned(align, layout, () -> MemorySegment.copy(segment, layout, 0, arr, 0, 1));
 55             assertAligned(align, layout, () -> {
 56                 MemorySegment other = segmentFactory.apply(arr);
 57                 MemorySegment.copy(other, layout, 0, segment, layout, 0, 1);
 58             });
 59             MemorySegment other = segmentFactory.apply(arr);
 60             assertAligned(align, layout, () -> {
 61                 MemorySegment.copy(segment, layout, 0, other, layout, 0, 1);
 62             });
 63             assertAligned(align, layout, () -> {
 64                 MemorySegment.copy(other, layout, 0, segment, layout, 0, 1);
 65             });
 66         }
 67     }
 68 
 69     static void assertAligned(int align, ValueLayout layout, Runnable runnable) {
 70         boolean shouldFail = layout.byteAlignment() > align && align != -1;
< prev index next >