Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/misc/CopyMemory.java
38833 views
/*1* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 656554325* @summary Minimal test for unsafe.copyMemory() and unsafe.setMemory()26* @key randomness27*/2829import java.util.*;30import java.lang.reflect.*;31import java.nio.*;3233import sun.misc.Unsafe;3435import sun.nio.ch.DirectBuffer;3637public class CopyMemory {3839private final static int BUFFER_SIZE = 1024;40private final static int N = 16 * 1024;4142private final static int FILLER = 0x55;43private final static int FILLER2 = 0x33;4445private final static Random random = new Random();4647private static void set(byte[] b, int ofs, int len, int value) {48for (int i = 0; i < len; i++) {49b[ofs + i] = (byte)value;50}51}5253private static void check(byte[] b, int ofs, int len, int value) {54for (int i = 0; i < len; i++) {55int r = b[ofs + i] & 0xff;56if (r != value) {57throw new RuntimeException("mismatch");58}59}60}6162private static void set(Unsafe unsafe, long addr, int ofs, int len, int value) {63for (int i = 0; i < len; i++) {64unsafe.putByte(null, addr + ofs + i, (byte)value);65}66}6768private static void check(Unsafe unsafe, long addr, int ofs, int len, int value) {69for (int i = 0; i < len; i++) {70int r = unsafe.getByte(null, addr + ofs + i) & 0xff;71if (r != value) {72throw new RuntimeException("mismatch");73}74}75}7677private static final List<ByteBuffer> buffers = new ArrayList<ByteBuffer>();7879private static long getMemory(int n) {80ByteBuffer b = ByteBuffer.allocateDirect(n);81if (b instanceof DirectBuffer == false) {82throw new RuntimeException("Not a direct buffer");83}84buffers.add(b); // make sure the buffer does not get GCed85return ((DirectBuffer)b).address();86}8788private static void testSetByteArray(Unsafe unsafe) throws Exception {89System.out.println("Testing setMemory() for byte[]...");90byte[] b = new byte[BUFFER_SIZE];91for (int i = 0; i < N; i++) {92set(b, 0, BUFFER_SIZE, FILLER);93int ofs = random.nextInt(BUFFER_SIZE / 2);94int len = random.nextInt(BUFFER_SIZE / 2);95int val = random.nextInt(256);96unsafe.setMemory(b, Unsafe.ARRAY_BYTE_BASE_OFFSET + ofs, len, (byte)val);97check(b, 0, ofs - 1, FILLER);98check(b, ofs, len, val);99check(b, ofs + len, BUFFER_SIZE - (ofs + len), FILLER);100}101}102103private static void testSetRawMemory(Unsafe unsafe) throws Exception {104System.out.println("Testing setMemory() for raw memory...");105long b = getMemory(BUFFER_SIZE);106for (int i = 0; i < N; i++) {107set(unsafe, b, 0, BUFFER_SIZE, FILLER);108int ofs = random.nextInt(BUFFER_SIZE / 2);109int len = random.nextInt(BUFFER_SIZE / 2);110int val = random.nextInt(256);111unsafe.setMemory(null, b + ofs, len, (byte)val);112check(unsafe, b, 0, ofs - 1, FILLER);113check(unsafe, b, ofs, len, val);114check(unsafe, b, ofs + len, BUFFER_SIZE - (ofs + len), FILLER);115}116}117118private static void testCopyByteArrayToByteArray(Unsafe unsafe) throws Exception {119System.out.println("Testing copyMemory() for byte[] to byte[]...");120byte[] b1 = new byte[BUFFER_SIZE];121byte[] b2 = new byte[BUFFER_SIZE];122for (int i = 0; i < N; i++) {123set(b1, 0, BUFFER_SIZE, FILLER);124set(b2, 0, BUFFER_SIZE, FILLER2);125int ofs = random.nextInt(BUFFER_SIZE / 2);126int len = random.nextInt(BUFFER_SIZE / 2);127int val = random.nextInt(256);128set(b1, ofs, len, val);129int ofs2 = random.nextInt(BUFFER_SIZE / 2);130unsafe.copyMemory(b1, Unsafe.ARRAY_BYTE_BASE_OFFSET + ofs,131b2, Unsafe.ARRAY_BYTE_BASE_OFFSET + ofs2, len);132check(b2, 0, ofs2 - 1, FILLER2);133check(b2, ofs2, len, val);134check(b2, ofs2 + len, BUFFER_SIZE - (ofs2 + len), FILLER2);135}136}137138private static void testCopyByteArrayToRawMemory(Unsafe unsafe) throws Exception {139System.out.println("Testing copyMemory() for byte[] to raw memory...");140byte[] b1 = new byte[BUFFER_SIZE];141long b2 = getMemory(BUFFER_SIZE);142for (int i = 0; i < N; i++) {143set(b1, 0, BUFFER_SIZE, FILLER);144set(unsafe, b2, 0, BUFFER_SIZE, FILLER2);145int ofs = random.nextInt(BUFFER_SIZE / 2);146int len = random.nextInt(BUFFER_SIZE / 2);147int val = random.nextInt(256);148set(b1, ofs, len, val);149int ofs2 = random.nextInt(BUFFER_SIZE / 2);150unsafe.copyMemory(b1, Unsafe.ARRAY_BYTE_BASE_OFFSET + ofs,151null, b2 + ofs2, len);152check(unsafe, b2, 0, ofs2 - 1, FILLER2);153check(unsafe, b2, ofs2, len, val);154check(unsafe, b2, ofs2 + len, BUFFER_SIZE - (ofs2 + len), FILLER2);155}156}157158private static void testCopyRawMemoryToByteArray(Unsafe unsafe) throws Exception {159System.out.println("Testing copyMemory() for raw memory to byte[]...");160long b1 = getMemory(BUFFER_SIZE);161byte[] b2 = new byte[BUFFER_SIZE];162for (int i = 0; i < N; i++) {163set(unsafe, b1, 0, BUFFER_SIZE, FILLER);164set(b2, 0, BUFFER_SIZE, FILLER2);165int ofs = random.nextInt(BUFFER_SIZE / 2);166int len = random.nextInt(BUFFER_SIZE / 2);167int val = random.nextInt(256);168set(unsafe, b1, ofs, len, val);169int ofs2 = random.nextInt(BUFFER_SIZE / 2);170unsafe.copyMemory(null, b1 + ofs,171b2, Unsafe.ARRAY_BYTE_BASE_OFFSET + ofs2, len);172check(b2, 0, ofs2 - 1, FILLER2);173check(b2, ofs2, len, val);174check(b2, ofs2 + len, BUFFER_SIZE - (ofs2 + len), FILLER2);175}176}177178private static void testCopyRawMemoryToRawMemory(Unsafe unsafe) throws Exception {179System.out.println("Testing copyMemory() for raw memory to raw memory...");180long b1 = getMemory(BUFFER_SIZE);181long b2 = getMemory(BUFFER_SIZE);182for (int i = 0; i < N; i++) {183set(unsafe, b1, 0, BUFFER_SIZE, FILLER);184set(unsafe, b2, 0, BUFFER_SIZE, FILLER2);185int ofs = random.nextInt(BUFFER_SIZE / 2);186int len = random.nextInt(BUFFER_SIZE / 2);187int val = random.nextInt(256);188set(unsafe, b1, ofs, len, val);189int ofs2 = random.nextInt(BUFFER_SIZE / 2);190unsafe.copyMemory(null, b1 + ofs,191null, b2 + ofs2, len);192check(unsafe, b2, 0, ofs2 - 1, FILLER2);193check(unsafe, b2, ofs2, len, val);194check(unsafe, b2, ofs2 + len, BUFFER_SIZE - (ofs2 + len), FILLER2);195}196}197198private static Unsafe getUnsafe() throws Exception {199Field f = Unsafe.class.getDeclaredField("theUnsafe");200f.setAccessible(true);201return (Unsafe)f.get(null);202}203204public static void main(String[] args) throws Exception {205Unsafe unsafe = getUnsafe();206207testSetByteArray(unsafe);208testSetRawMemory(unsafe);209testCopyByteArrayToByteArray(unsafe);210testCopyByteArrayToRawMemory(unsafe);211testCopyRawMemoryToByteArray(unsafe);212testCopyRawMemoryToRawMemory(unsafe);213214System.out.println("OK");215}216217}218219220