Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/hotspot/jtreg/compiler/loopopts/FillArrayWithUnsafe.java
64478 views
1
/*
2
* Copyright (c) 2022, Arm Limited. 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
* @test
26
* @bug 8283408
27
* @summary Fill a byte array with Java Unsafe API
28
* @run main/othervm -XX:+OptimizeFill compiler.loopopts.FillArrayWithUnsafe
29
*/
30
31
package compiler.loopopts;
32
33
import java.lang.reflect.Field;
34
35
import sun.misc.Unsafe;
36
37
public class FillArrayWithUnsafe {
38
39
private static Unsafe unsafe;
40
41
public static void main(String[] args) throws Exception {
42
Class klass = Unsafe.class;
43
Field field = klass.getDeclaredField("theUnsafe");
44
field.setAccessible(true);
45
unsafe = (Unsafe) field.get(null);
46
47
byte[] buffer;
48
// Make sure method newByteArray is compiled by C2
49
for (int i = 0; i < 50000; i++) {
50
buffer = newByteArray(100, (byte) 0x80);
51
}
52
}
53
54
public static byte[] newByteArray(int size, byte val) {
55
byte[] arr = new byte[size];
56
int offset = unsafe.arrayBaseOffset(byte[].class);
57
for (int i = offset; i < offset + size; i++) {
58
unsafe.putByte(arr, i, val);
59
}
60
return arr;
61
}
62
}
63
64
65