Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | import java.lang.reflect.Field; |
| 18 | import sun.misc.Unsafe; |
| 19 | |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 20 | public class Main { |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 21 | private static void check(int actual, int expected, String msg) { |
| 22 | if (actual != expected) { |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 23 | System.out.println(msg + " : " + actual + " != " + expected); |
Brian Carlstrom | da4442e | 2014-10-14 15:39:01 -0700 | [diff] [blame] | 24 | System.exit(1); |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 25 | } |
| 26 | } |
| 27 | |
Vladimir Marko | 99f391e | 2014-04-03 12:56:06 +0100 | [diff] [blame] | 28 | private static void check(long actual, long expected, String msg) { |
| 29 | if (actual != expected) { |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 30 | System.out.println(msg + " : " + actual + " != " + expected); |
Brian Carlstrom | da4442e | 2014-10-14 15:39:01 -0700 | [diff] [blame] | 31 | System.exit(1); |
Vladimir Marko | 99f391e | 2014-04-03 12:56:06 +0100 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | |
Vladimir Marko | 104883b | 2018-11-09 17:12:23 +0000 | [diff] [blame] | 35 | private static void check(float actual, float expected, String msg) { |
| 36 | if (actual != expected) { |
| 37 | System.out.println(msg + " : " + actual + " != " + expected); |
| 38 | System.exit(1); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | private static void check(double actual, double expected, String msg) { |
| 43 | if (actual != expected) { |
| 44 | System.out.println(msg + " : " + actual + " != " + expected); |
| 45 | System.exit(1); |
| 46 | } |
| 47 | } |
| 48 | |
Roland Levillain | ca1476f | 2015-06-16 18:09:26 +0100 | [diff] [blame] | 49 | private static void check(Object actual, Object expected, String msg) { |
| 50 | if (actual != expected) { |
| 51 | System.out.println(msg + " : " + actual + " != " + expected); |
| 52 | System.exit(1); |
| 53 | } |
| 54 | } |
| 55 | |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 56 | private static Unsafe getUnsafe() throws NoSuchFieldException, IllegalAccessException { |
Aart Bik | 0e54c01 | 2016-03-04 12:08:31 -0800 | [diff] [blame] | 57 | Class<?> unsafeClass = Unsafe.class; |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 58 | Field f = unsafeClass.getDeclaredField("theUnsafe"); |
| 59 | f.setAccessible(true); |
| 60 | return (Unsafe) f.get(null); |
| 61 | } |
| 62 | |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 63 | public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException { |
Mathieu Chartier | 031768a | 2015-08-27 10:25:02 -0700 | [diff] [blame] | 64 | System.loadLibrary(args[0]); |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 65 | Unsafe unsafe = getUnsafe(); |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 66 | |
| 67 | testArrayBaseOffset(unsafe); |
| 68 | testArrayIndexScale(unsafe); |
| 69 | testGetAndPutAndCAS(unsafe); |
| 70 | testGetAndPutVolatile(unsafe); |
Vladimir Marko | 104883b | 2018-11-09 17:12:23 +0000 | [diff] [blame] | 71 | testCopyMemoryPrimitiveArrays(unsafe); |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | private static void testArrayBaseOffset(Unsafe unsafe) { |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 75 | check(unsafe.arrayBaseOffset(boolean[].class), vmArrayBaseOffset(boolean[].class), |
| 76 | "Unsafe.arrayBaseOffset(boolean[])"); |
| 77 | check(unsafe.arrayBaseOffset(byte[].class), vmArrayBaseOffset(byte[].class), |
| 78 | "Unsafe.arrayBaseOffset(byte[])"); |
| 79 | check(unsafe.arrayBaseOffset(char[].class), vmArrayBaseOffset(char[].class), |
| 80 | "Unsafe.arrayBaseOffset(char[])"); |
| 81 | check(unsafe.arrayBaseOffset(double[].class), vmArrayBaseOffset(double[].class), |
| 82 | "Unsafe.arrayBaseOffset(double[])"); |
| 83 | check(unsafe.arrayBaseOffset(float[].class), vmArrayBaseOffset(float[].class), |
| 84 | "Unsafe.arrayBaseOffset(float[])"); |
| 85 | check(unsafe.arrayBaseOffset(int[].class), vmArrayBaseOffset(int[].class), |
| 86 | "Unsafe.arrayBaseOffset(int[])"); |
| 87 | check(unsafe.arrayBaseOffset(long[].class), vmArrayBaseOffset(long[].class), |
| 88 | "Unsafe.arrayBaseOffset(long[])"); |
| 89 | check(unsafe.arrayBaseOffset(Object[].class), vmArrayBaseOffset(Object[].class), |
| 90 | "Unsafe.arrayBaseOffset(Object[])"); |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 91 | } |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 92 | |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 93 | private static void testArrayIndexScale(Unsafe unsafe) { |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 94 | check(unsafe.arrayIndexScale(boolean[].class), vmArrayIndexScale(boolean[].class), |
| 95 | "Unsafe.arrayIndexScale(boolean[])"); |
| 96 | check(unsafe.arrayIndexScale(byte[].class), vmArrayIndexScale(byte[].class), |
| 97 | "Unsafe.arrayIndexScale(byte[])"); |
| 98 | check(unsafe.arrayIndexScale(char[].class), vmArrayIndexScale(char[].class), |
| 99 | "Unsafe.arrayIndexScale(char[])"); |
| 100 | check(unsafe.arrayIndexScale(double[].class), vmArrayIndexScale(double[].class), |
| 101 | "Unsafe.arrayIndexScale(double[])"); |
| 102 | check(unsafe.arrayIndexScale(float[].class), vmArrayIndexScale(float[].class), |
| 103 | "Unsafe.arrayIndexScale(float[])"); |
| 104 | check(unsafe.arrayIndexScale(int[].class), vmArrayIndexScale(int[].class), |
| 105 | "Unsafe.arrayIndexScale(int[])"); |
| 106 | check(unsafe.arrayIndexScale(long[].class), vmArrayIndexScale(long[].class), |
| 107 | "Unsafe.arrayIndexScale(long[])"); |
| 108 | check(unsafe.arrayIndexScale(Object[].class), vmArrayIndexScale(Object[].class), |
| 109 | "Unsafe.arrayIndexScale(Object[])"); |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 110 | } |
Vladimir Marko | 99f391e | 2014-04-03 12:56:06 +0100 | [diff] [blame] | 111 | |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 112 | private static void testGetAndPutAndCAS(Unsafe unsafe) throws NoSuchFieldException { |
Vladimir Marko | 99f391e | 2014-04-03 12:56:06 +0100 | [diff] [blame] | 113 | TestClass t = new TestClass(); |
Roland Levillain | ca1476f | 2015-06-16 18:09:26 +0100 | [diff] [blame] | 114 | |
Vladimir Marko | 99f391e | 2014-04-03 12:56:06 +0100 | [diff] [blame] | 115 | int intValue = 12345678; |
| 116 | Field intField = TestClass.class.getDeclaredField("intVar"); |
| 117 | long intOffset = unsafe.objectFieldOffset(intField); |
| 118 | check(unsafe.getInt(t, intOffset), 0, "Unsafe.getInt(Object, long) - initial"); |
| 119 | unsafe.putInt(t, intOffset, intValue); |
| 120 | check(t.intVar, intValue, "Unsafe.putInt(Object, long, int)"); |
| 121 | check(unsafe.getInt(t, intOffset), intValue, "Unsafe.getInt(Object, long)"); |
Roland Levillain | ca1476f | 2015-06-16 18:09:26 +0100 | [diff] [blame] | 122 | |
| 123 | long longValue = 1234567887654321L; |
Vladimir Marko | 99f391e | 2014-04-03 12:56:06 +0100 | [diff] [blame] | 124 | Field longField = TestClass.class.getDeclaredField("longVar"); |
| 125 | long longOffset = unsafe.objectFieldOffset(longField); |
Vladimir Marko | 99f391e | 2014-04-03 12:56:06 +0100 | [diff] [blame] | 126 | check(unsafe.getLong(t, longOffset), 0, "Unsafe.getLong(Object, long) - initial"); |
| 127 | unsafe.putLong(t, longOffset, longValue); |
| 128 | check(t.longVar, longValue, "Unsafe.putLong(Object, long, long)"); |
| 129 | check(unsafe.getLong(t, longOffset), longValue, "Unsafe.getLong(Object, long)"); |
Andreas Gampe | 2bcf9bf | 2015-01-29 09:56:07 -0800 | [diff] [blame] | 130 | |
Roland Levillain | ca1476f | 2015-06-16 18:09:26 +0100 | [diff] [blame] | 131 | Object objectValue = new Object(); |
| 132 | Field objectField = TestClass.class.getDeclaredField("objectVar"); |
| 133 | long objectOffset = unsafe.objectFieldOffset(objectField); |
| 134 | check(unsafe.getObject(t, objectOffset), null, "Unsafe.getObject(Object, long) - initial"); |
| 135 | unsafe.putObject(t, objectOffset, objectValue); |
| 136 | check(t.objectVar, objectValue, "Unsafe.putObject(Object, long, Object)"); |
| 137 | check(unsafe.getObject(t, objectOffset), objectValue, "Unsafe.getObject(Object, long)"); |
| 138 | |
Andreas Gampe | 2bcf9bf | 2015-01-29 09:56:07 -0800 | [diff] [blame] | 139 | if (unsafe.compareAndSwapInt(t, intOffset, 0, 1)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 140 | System.out.println("Unexpectedly succeeding compareAndSwapInt(t, intOffset, 0, 1)"); |
Andreas Gampe | 2bcf9bf | 2015-01-29 09:56:07 -0800 | [diff] [blame] | 141 | } |
| 142 | if (!unsafe.compareAndSwapInt(t, intOffset, intValue, 0)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 143 | System.out.println( |
| 144 | "Unexpectedly not succeeding compareAndSwapInt(t, intOffset, intValue, 0)"); |
Andreas Gampe | 2bcf9bf | 2015-01-29 09:56:07 -0800 | [diff] [blame] | 145 | } |
| 146 | if (!unsafe.compareAndSwapInt(t, intOffset, 0, 1)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 147 | System.out.println("Unexpectedly not succeeding compareAndSwapInt(t, intOffset, 0, 1)"); |
| 148 | } |
| 149 | // Exercise sun.misc.Unsafe.compareAndSwapInt using the same |
| 150 | // integer (1) for the `expectedValue` and `newValue` arguments. |
| 151 | if (!unsafe.compareAndSwapInt(t, intOffset, 1, 1)) { |
| 152 | System.out.println("Unexpectedly not succeeding compareAndSwapInt(t, intOffset, 1, 1)"); |
Andreas Gampe | 2bcf9bf | 2015-01-29 09:56:07 -0800 | [diff] [blame] | 153 | } |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 154 | |
| 155 | if (unsafe.compareAndSwapLong(t, longOffset, 0, 1)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 156 | System.out.println("Unexpectedly succeeding compareAndSwapLong(t, longOffset, 0, 1)"); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 157 | } |
| 158 | if (!unsafe.compareAndSwapLong(t, longOffset, longValue, 0)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 159 | System.out.println( |
| 160 | "Unexpectedly not succeeding compareAndSwapLong(t, longOffset, longValue, 0)"); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 161 | } |
| 162 | if (!unsafe.compareAndSwapLong(t, longOffset, 0, 1)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 163 | System.out.println("Unexpectedly not succeeding compareAndSwapLong(t, longOffset, 0, 1)"); |
| 164 | } |
| 165 | // Exercise sun.misc.Unsafe.compareAndSwapLong using the same |
| 166 | // integer (1) for the `expectedValue` and `newValue` arguments. |
| 167 | if (!unsafe.compareAndSwapLong(t, longOffset, 1, 1)) { |
| 168 | System.out.println("Unexpectedly not succeeding compareAndSwapLong(t, longOffset, 1, 1)"); |
Mark Mendell | 58d25fd | 2015-04-03 14:52:31 -0400 | [diff] [blame] | 169 | } |
Roland Levillain | b550c2e | 2015-06-26 10:44:53 +0100 | [diff] [blame] | 170 | |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 171 | // We do not use `null` as argument to sun.misc.Unsafe.compareAndSwapObject |
| 172 | // in those tests, as this value is not affected by heap poisoning |
| 173 | // (which uses address negation to poison and unpoison heap object |
| 174 | // references). This way, when heap poisoning is enabled, we can |
| 175 | // better exercise its implementation within that method. |
| 176 | if (unsafe.compareAndSwapObject(t, objectOffset, new Object(), new Object())) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 177 | System.out.println("Unexpectedly succeeding " + |
| 178 | "compareAndSwapObject(t, objectOffset, new Object(), new Object())"); |
Roland Levillain | b550c2e | 2015-06-26 10:44:53 +0100 | [diff] [blame] | 179 | } |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 180 | Object objectValue2 = new Object(); |
| 181 | if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue, objectValue2)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 182 | System.out.println("Unexpectedly not succeeding " + |
| 183 | "compareAndSwapObject(t, objectOffset, objectValue, objectValue2)"); |
Roland Levillain | b550c2e | 2015-06-26 10:44:53 +0100 | [diff] [blame] | 184 | } |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 185 | Object objectValue3 = new Object(); |
| 186 | if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue2, objectValue3)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 187 | System.out.println("Unexpectedly not succeeding " + |
| 188 | "compareAndSwapObject(t, objectOffset, objectValue2, objectValue3)"); |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 189 | } |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 190 | // Exercise sun.misc.Unsafe.compareAndSwapObject using the same |
| 191 | // object (`objectValue3`) for the `expectedValue` and `newValue` arguments. |
| 192 | if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue3, objectValue3)) { |
| 193 | System.out.println("Unexpectedly not succeeding " + |
| 194 | "compareAndSwapObject(t, objectOffset, objectValue3, objectValue3)"); |
| 195 | } |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 196 | // Exercise sun.misc.Unsafe.compareAndSwapObject using the same |
| 197 | // object (`t`) for the `obj` and `newValue` arguments. |
| 198 | if (!unsafe.compareAndSwapObject(t, objectOffset, objectValue3, t)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 199 | System.out.println( |
| 200 | "Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, objectValue3, t)"); |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 201 | } |
| 202 | // Exercise sun.misc.Unsafe.compareAndSwapObject using the same |
| 203 | // object (`t`) for the `obj`, `expectedValue` and `newValue` arguments. |
| 204 | if (!unsafe.compareAndSwapObject(t, objectOffset, t, t)) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 205 | System.out.println("Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, t, t)"); |
Roland Levillain | b488b78 | 2015-10-22 11:38:49 +0100 | [diff] [blame] | 206 | } |
| 207 | // Exercise sun.misc.Unsafe.compareAndSwapObject using the same |
| 208 | // object (`t`) for the `obj` and `expectedValue` arguments. |
| 209 | if (!unsafe.compareAndSwapObject(t, objectOffset, t, new Object())) { |
Roland Levillain | 2e50ecb | 2016-01-27 14:08:33 +0000 | [diff] [blame] | 210 | System.out.println( |
| 211 | "Unexpectedly not succeeding compareAndSwapObject(t, objectOffset, t, new Object())"); |
Roland Levillain | b550c2e | 2015-06-26 10:44:53 +0100 | [diff] [blame] | 212 | } |
Vladimir Marko | 99f391e | 2014-04-03 12:56:06 +0100 | [diff] [blame] | 213 | } |
| 214 | |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 215 | private static void testGetAndPutVolatile(Unsafe unsafe) throws NoSuchFieldException { |
| 216 | TestVolatileClass tv = new TestVolatileClass(); |
| 217 | |
| 218 | int intValue = 12345678; |
| 219 | Field volatileIntField = TestVolatileClass.class.getDeclaredField("volatileIntVar"); |
| 220 | long volatileIntOffset = unsafe.objectFieldOffset(volatileIntField); |
| 221 | check(unsafe.getIntVolatile(tv, volatileIntOffset), |
| 222 | 0, |
| 223 | "Unsafe.getIntVolatile(Object, long) - initial"); |
| 224 | unsafe.putIntVolatile(tv, volatileIntOffset, intValue); |
| 225 | check(tv.volatileIntVar, intValue, "Unsafe.putIntVolatile(Object, long, int)"); |
| 226 | check(unsafe.getIntVolatile(tv, volatileIntOffset), |
| 227 | intValue, |
| 228 | "Unsafe.getIntVolatile(Object, long)"); |
| 229 | |
| 230 | long longValue = 1234567887654321L; |
| 231 | Field volatileLongField = TestVolatileClass.class.getDeclaredField("volatileLongVar"); |
| 232 | long volatileLongOffset = unsafe.objectFieldOffset(volatileLongField); |
| 233 | check(unsafe.getLongVolatile(tv, volatileLongOffset), |
| 234 | 0, |
| 235 | "Unsafe.getLongVolatile(Object, long) - initial"); |
| 236 | unsafe.putLongVolatile(tv, volatileLongOffset, longValue); |
| 237 | check(tv.volatileLongVar, longValue, "Unsafe.putLongVolatile(Object, long, long)"); |
| 238 | check(unsafe.getLongVolatile(tv, volatileLongOffset), |
| 239 | longValue, |
| 240 | "Unsafe.getLongVolatile(Object, long)"); |
| 241 | |
| 242 | Object objectValue = new Object(); |
| 243 | Field volatileObjectField = TestVolatileClass.class.getDeclaredField("volatileObjectVar"); |
| 244 | long volatileObjectOffset = unsafe.objectFieldOffset(volatileObjectField); |
| 245 | check(unsafe.getObjectVolatile(tv, volatileObjectOffset), |
| 246 | null, |
| 247 | "Unsafe.getObjectVolatile(Object, long) - initial"); |
| 248 | unsafe.putObjectVolatile(tv, volatileObjectOffset, objectValue); |
| 249 | check(tv.volatileObjectVar, objectValue, "Unsafe.putObjectVolatile(Object, long, Object)"); |
| 250 | check(unsafe.getObjectVolatile(tv, volatileObjectOffset), |
| 251 | objectValue, |
| 252 | "Unsafe.getObjectVolatile(Object, long)"); |
| 253 | } |
| 254 | |
Vladimir Marko | 104883b | 2018-11-09 17:12:23 +0000 | [diff] [blame] | 255 | // Regression test for "copyMemory" operations hitting a DCHECK() for float/double arrays. |
| 256 | private static void testCopyMemoryPrimitiveArrays(Unsafe unsafe) { |
| 257 | int size = 4 * 1024; |
| 258 | long memory = unsafeTestMalloc(size); |
| 259 | |
| 260 | int floatSize = 4; |
| 261 | float[] inputFloats = new float[size / floatSize]; |
| 262 | for (int i = 0; i != inputFloats.length; ++i) { |
| 263 | inputFloats[i] = ((float)i) + 0.5f; |
| 264 | } |
| 265 | float[] outputFloats = new float[size / floatSize]; |
| 266 | unsafe.copyMemoryFromPrimitiveArray(inputFloats, 0, memory, size); |
| 267 | unsafe.copyMemoryToPrimitiveArray(memory, outputFloats, 0, size); |
| 268 | for (int i = 0; i != inputFloats.length; ++i) { |
| 269 | check(inputFloats[i], outputFloats[i], "unsafe.copyMemory/float"); |
| 270 | } |
| 271 | |
| 272 | int doubleSize = 8; |
| 273 | double[] inputDoubles = new double[size / doubleSize]; |
| 274 | for (int i = 0; i != inputDoubles.length; ++i) { |
| 275 | inputDoubles[i] = ((double)i) + 0.5; |
| 276 | } |
| 277 | double[] outputDoubles = new double[size / doubleSize]; |
| 278 | unsafe.copyMemoryFromPrimitiveArray(inputDoubles, 0, memory, size); |
| 279 | unsafe.copyMemoryToPrimitiveArray(memory, outputDoubles, 0, size); |
| 280 | for (int i = 0; i != inputDoubles.length; ++i) { |
| 281 | check(inputDoubles[i], outputDoubles[i], "unsafe.copyMemory/double"); |
| 282 | } |
| 283 | |
| 284 | unsafeTestFree(memory); |
| 285 | } |
| 286 | |
Vladimir Marko | 99f391e | 2014-04-03 12:56:06 +0100 | [diff] [blame] | 287 | private static class TestClass { |
| 288 | public int intVar = 0; |
| 289 | public long longVar = 0; |
Roland Levillain | ca1476f | 2015-06-16 18:09:26 +0100 | [diff] [blame] | 290 | public Object objectVar = null; |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 291 | } |
| 292 | |
Roland Levillain | 3d31242 | 2016-06-23 13:53:42 +0100 | [diff] [blame] | 293 | private static class TestVolatileClass { |
| 294 | public volatile int volatileIntVar = 0; |
| 295 | public volatile long volatileLongVar = 0; |
| 296 | public volatile Object volatileObjectVar = null; |
| 297 | } |
| 298 | |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 299 | private static native int vmArrayBaseOffset(Class<?> clazz); |
| 300 | private static native int vmArrayIndexScale(Class<?> clazz); |
Vladimir Marko | 104883b | 2018-11-09 17:12:23 +0000 | [diff] [blame] | 301 | private static native long unsafeTestMalloc(long size); |
| 302 | private static native void unsafeTestFree(long memory); |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 303 | } |