2
|
1 |
/*
|
|
2 |
* Copyright 2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/* @test
|
|
25 |
* @bug 6565543
|
|
26 |
* @summary Minimal test for unsafe.copyMemory() and unsafe.setMemory()
|
|
27 |
*/
|
|
28 |
|
|
29 |
import java.util.*;
|
|
30 |
import java.lang.reflect.*;
|
|
31 |
import java.nio.*;
|
|
32 |
|
|
33 |
import sun.misc.Unsafe;
|
|
34 |
|
|
35 |
import sun.nio.ch.DirectBuffer;
|
|
36 |
|
|
37 |
public class CopyMemory {
|
|
38 |
|
|
39 |
private final static int BUFFER_SIZE = 1024;
|
|
40 |
private final static int N = 16 * 1024;
|
|
41 |
|
|
42 |
private final static int FILLER = 0x55;
|
|
43 |
private final static int FILLER2 = 0x33;
|
|
44 |
|
|
45 |
private final static Random random = new Random();
|
|
46 |
|
|
47 |
private static void set(byte[] b, int ofs, int len, int value) {
|
|
48 |
for (int i = 0; i < len; i++) {
|
|
49 |
b[ofs + i] = (byte)value;
|
|
50 |
}
|
|
51 |
}
|
|
52 |
|
|
53 |
private static void check(byte[] b, int ofs, int len, int value) {
|
|
54 |
for (int i = 0; i < len; i++) {
|
|
55 |
int r = b[ofs + i] & 0xff;
|
|
56 |
if (r != value) {
|
|
57 |
throw new RuntimeException("mismatch");
|
|
58 |
}
|
|
59 |
}
|
|
60 |
}
|
|
61 |
|
|
62 |
private static void set(Unsafe unsafe, long addr, int ofs, int len, int value) {
|
|
63 |
for (int i = 0; i < len; i++) {
|
|
64 |
unsafe.putByte(null, addr + ofs + i, (byte)value);
|
|
65 |
}
|
|
66 |
}
|
|
67 |
|
|
68 |
private static void check(Unsafe unsafe, long addr, int ofs, int len, int value) {
|
|
69 |
for (int i = 0; i < len; i++) {
|
|
70 |
int r = unsafe.getByte(null, addr + ofs + i) & 0xff;
|
|
71 |
if (r != value) {
|
|
72 |
throw new RuntimeException("mismatch");
|
|
73 |
}
|
|
74 |
}
|
|
75 |
}
|
|
76 |
|
|
77 |
private static final List<ByteBuffer> buffers = new ArrayList<ByteBuffer>();
|
|
78 |
|
|
79 |
private static long getMemory(int n) {
|
|
80 |
ByteBuffer b = ByteBuffer.allocateDirect(n);
|
|
81 |
if (b instanceof DirectBuffer == false) {
|
|
82 |
throw new RuntimeException("Not a direct buffer");
|
|
83 |
}
|
|
84 |
buffers.add(b); // make sure the buffer does not get GCed
|
|
85 |
return ((DirectBuffer)b).address();
|
|
86 |
}
|
|
87 |
|
|
88 |
private static void testSetByteArray(Unsafe unsafe) throws Exception {
|
|
89 |
System.out.println("Testing setMemory() for byte[]...");
|
|
90 |
byte[] b = new byte[BUFFER_SIZE];
|
|
91 |
for (int i = 0; i < N; i++) {
|
|
92 |
set(b, 0, BUFFER_SIZE, FILLER);
|
|
93 |
int ofs = random.nextInt(BUFFER_SIZE / 2);
|
|
94 |
int len = random.nextInt(BUFFER_SIZE / 2);
|
|
95 |
int val = random.nextInt(256);
|
|
96 |
unsafe.setMemory(b, Unsafe.ARRAY_BYTE_BASE_OFFSET + ofs, len, (byte)val);
|
|
97 |
check(b, 0, ofs - 1, FILLER);
|
|
98 |
check(b, ofs, len, val);
|
|
99 |
check(b, ofs + len, BUFFER_SIZE - (ofs + len), FILLER);
|
|
100 |
}
|
|
101 |
}
|
|
102 |
|
|
103 |
private static void testSetRawMemory(Unsafe unsafe) throws Exception {
|
|
104 |
System.out.println("Testing setMemory() for raw memory...");
|
|
105 |
long b = getMemory(BUFFER_SIZE);
|
|
106 |
for (int i = 0; i < N; i++) {
|
|
107 |
set(unsafe, b, 0, BUFFER_SIZE, FILLER);
|
|
108 |
int ofs = random.nextInt(BUFFER_SIZE / 2);
|
|
109 |
int len = random.nextInt(BUFFER_SIZE / 2);
|
|
110 |
int val = random.nextInt(256);
|
|
111 |
unsafe.setMemory(null, b + ofs, len, (byte)val);
|
|
112 |
check(unsafe, b, 0, ofs - 1, FILLER);
|
|
113 |
check(unsafe, b, ofs, len, val);
|
|
114 |
check(unsafe, b, ofs + len, BUFFER_SIZE - (ofs + len), FILLER);
|
|
115 |
}
|
|
116 |
}
|
|
117 |
|
|
118 |
private static void testCopyByteArrayToByteArray(Unsafe unsafe) throws Exception {
|
|
119 |
System.out.println("Testing copyMemory() for byte[] to byte[]...");
|
|
120 |
byte[] b1 = new byte[BUFFER_SIZE];
|
|
121 |
byte[] b2 = new byte[BUFFER_SIZE];
|
|
122 |
for (int i = 0; i < N; i++) {
|
|
123 |
set(b1, 0, BUFFER_SIZE, FILLER);
|
|
124 |
set(b2, 0, BUFFER_SIZE, FILLER2);
|
|
125 |
int ofs = random.nextInt(BUFFER_SIZE / 2);
|
|
126 |
int len = random.nextInt(BUFFER_SIZE / 2);
|
|
127 |
int val = random.nextInt(256);
|
|
128 |
set(b1, ofs, len, val);
|
|
129 |
int ofs2 = random.nextInt(BUFFER_SIZE / 2);
|
|
130 |
unsafe.copyMemory(b1, Unsafe.ARRAY_BYTE_BASE_OFFSET + ofs,
|
|
131 |
b2, Unsafe.ARRAY_BYTE_BASE_OFFSET + ofs2, len);
|
|
132 |
check(b2, 0, ofs2 - 1, FILLER2);
|
|
133 |
check(b2, ofs2, len, val);
|
|
134 |
check(b2, ofs2 + len, BUFFER_SIZE - (ofs2 + len), FILLER2);
|
|
135 |
}
|
|
136 |
}
|
|
137 |
|
|
138 |
private static void testCopyByteArrayToRawMemory(Unsafe unsafe) throws Exception {
|
|
139 |
System.out.println("Testing copyMemory() for byte[] to raw memory...");
|
|
140 |
byte[] b1 = new byte[BUFFER_SIZE];
|
|
141 |
long b2 = getMemory(BUFFER_SIZE);
|
|
142 |
for (int i = 0; i < N; i++) {
|
|
143 |
set(b1, 0, BUFFER_SIZE, FILLER);
|
|
144 |
set(unsafe, b2, 0, BUFFER_SIZE, FILLER2);
|
|
145 |
int ofs = random.nextInt(BUFFER_SIZE / 2);
|
|
146 |
int len = random.nextInt(BUFFER_SIZE / 2);
|
|
147 |
int val = random.nextInt(256);
|
|
148 |
set(b1, ofs, len, val);
|
|
149 |
int ofs2 = random.nextInt(BUFFER_SIZE / 2);
|
|
150 |
unsafe.copyMemory(b1, Unsafe.ARRAY_BYTE_BASE_OFFSET + ofs,
|
|
151 |
null, b2 + ofs2, len);
|
|
152 |
check(unsafe, b2, 0, ofs2 - 1, FILLER2);
|
|
153 |
check(unsafe, b2, ofs2, len, val);
|
|
154 |
check(unsafe, b2, ofs2 + len, BUFFER_SIZE - (ofs2 + len), FILLER2);
|
|
155 |
}
|
|
156 |
}
|
|
157 |
|
|
158 |
private static void testCopyRawMemoryToByteArray(Unsafe unsafe) throws Exception {
|
|
159 |
System.out.println("Testing copyMemory() for raw memory to byte[]...");
|
|
160 |
long b1 = getMemory(BUFFER_SIZE);
|
|
161 |
byte[] b2 = new byte[BUFFER_SIZE];
|
|
162 |
for (int i = 0; i < N; i++) {
|
|
163 |
set(unsafe, b1, 0, BUFFER_SIZE, FILLER);
|
|
164 |
set(b2, 0, BUFFER_SIZE, FILLER2);
|
|
165 |
int ofs = random.nextInt(BUFFER_SIZE / 2);
|
|
166 |
int len = random.nextInt(BUFFER_SIZE / 2);
|
|
167 |
int val = random.nextInt(256);
|
|
168 |
set(unsafe, b1, ofs, len, val);
|
|
169 |
int ofs2 = random.nextInt(BUFFER_SIZE / 2);
|
|
170 |
unsafe.copyMemory(null, b1 + ofs,
|
|
171 |
b2, Unsafe.ARRAY_BYTE_BASE_OFFSET + ofs2, len);
|
|
172 |
check(b2, 0, ofs2 - 1, FILLER2);
|
|
173 |
check(b2, ofs2, len, val);
|
|
174 |
check(b2, ofs2 + len, BUFFER_SIZE - (ofs2 + len), FILLER2);
|
|
175 |
}
|
|
176 |
}
|
|
177 |
|
|
178 |
private static void testCopyRawMemoryToRawMemory(Unsafe unsafe) throws Exception {
|
|
179 |
System.out.println("Testing copyMemory() for raw memory to raw memory...");
|
|
180 |
long b1 = getMemory(BUFFER_SIZE);
|
|
181 |
long b2 = getMemory(BUFFER_SIZE);
|
|
182 |
for (int i = 0; i < N; i++) {
|
|
183 |
set(unsafe, b1, 0, BUFFER_SIZE, FILLER);
|
|
184 |
set(unsafe, b2, 0, BUFFER_SIZE, FILLER2);
|
|
185 |
int ofs = random.nextInt(BUFFER_SIZE / 2);
|
|
186 |
int len = random.nextInt(BUFFER_SIZE / 2);
|
|
187 |
int val = random.nextInt(256);
|
|
188 |
set(unsafe, b1, ofs, len, val);
|
|
189 |
int ofs2 = random.nextInt(BUFFER_SIZE / 2);
|
|
190 |
unsafe.copyMemory(null, b1 + ofs,
|
|
191 |
null, b2 + ofs2, len);
|
|
192 |
check(unsafe, b2, 0, ofs2 - 1, FILLER2);
|
|
193 |
check(unsafe, b2, ofs2, len, val);
|
|
194 |
check(unsafe, b2, ofs2 + len, BUFFER_SIZE - (ofs2 + len), FILLER2);
|
|
195 |
}
|
|
196 |
}
|
|
197 |
|
|
198 |
private static Unsafe getUnsafe() throws Exception {
|
|
199 |
Field f = Unsafe.class.getDeclaredField("theUnsafe");
|
|
200 |
f.setAccessible(true);
|
|
201 |
return (Unsafe)f.get(null);
|
|
202 |
}
|
|
203 |
|
|
204 |
public static void main(String[] args) throws Exception {
|
|
205 |
Unsafe unsafe = getUnsafe();
|
|
206 |
|
|
207 |
testSetByteArray(unsafe);
|
|
208 |
testSetRawMemory(unsafe);
|
|
209 |
testCopyByteArrayToByteArray(unsafe);
|
|
210 |
testCopyByteArrayToRawMemory(unsafe);
|
|
211 |
testCopyRawMemoryToByteArray(unsafe);
|
|
212 |
testCopyRawMemoryToRawMemory(unsafe);
|
|
213 |
|
|
214 |
System.out.println("OK");
|
|
215 |
}
|
|
216 |
|
|
217 |
}
|