Debian Codegraph @

src/main/java/io/airlift/slice/JvmUtils.java

raw
1 /*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14 package io.airlift.slice;
15
16 import sun.misc.Unsafe;
17
18 import java.lang.invoke.MethodHandle;
19 import java.lang.invoke.MethodHandles;
20 import java.lang.invoke.MethodType;
21 import java.lang.reflect.Constructor;
22 import java.lang.reflect.Field;
23 import java.nio.ByteBuffer;
24
25 import static sun.misc.Unsafe.ARRAY_BOOLEAN_INDEX_SCALE;
26 import static sun.misc.Unsafe.ARRAY_BYTE_INDEX_SCALE;
27 import static sun.misc.Unsafe.ARRAY_DOUBLE_INDEX_SCALE;
28 import static sun.misc.Unsafe.ARRAY_FLOAT_INDEX_SCALE;
29 import static sun.misc.Unsafe.ARRAY_INT_INDEX_SCALE;
30 import static sun.misc.Unsafe.ARRAY_LONG_INDEX_SCALE;
31 import static sun.misc.Unsafe.ARRAY_SHORT_INDEX_SCALE;
32
33 final class JvmUtils
34 {
35 static final Unsafe unsafe;
36 static final MethodHandle newByteBuffer;
37
38 static {
39 try {
40 // fetch theUnsafe object
41 Field field = Unsafe.class.getDeclaredField("theUnsafe");
42 field.setAccessible(true);
43 unsafe = (Unsafe) field.get(null);
44 if (unsafe == null) {
45 throw new RuntimeException("Unsafe access not available");
46 }
47
48 // verify the stride of arrays matches the width of primitives
49 assertArrayIndexScale("Boolean", ARRAY_BOOLEAN_INDEX_SCALE, 1);
50 assertArrayIndexScale("Byte", ARRAY_BYTE_INDEX_SCALE, 1);
51 assertArrayIndexScale("Short", ARRAY_SHORT_INDEX_SCALE, 2);
52 assertArrayIndexScale("Int", ARRAY_INT_INDEX_SCALE, 4);
53 assertArrayIndexScale("Long", ARRAY_LONG_INDEX_SCALE, 8);
54 assertArrayIndexScale("Float", ARRAY_FLOAT_INDEX_SCALE, 4);
55 assertArrayIndexScale("Double", ARRAY_DOUBLE_INDEX_SCALE, 8);
56
57 // fetch a method handle for the hidden constructor for DirectByteBuffer
58 Class<?> directByteBufferClass = ClassLoader.getSystemClassLoader().loadClass("java.nio.DirectByteBuffer");
59 Constructor<?> constructor = directByteBufferClass.getDeclaredConstructor(long.class, int.class, Object.class);
60 constructor.setAccessible(true);
61 newByteBuffer = MethodHandles.lookup().unreflectConstructor(constructor)
62 .asType(MethodType.methodType(ByteBuffer.class, long.class, int.class, Object.class));
63 }
64 catch (Exception e) {
65 throw new RuntimeException(e);
66 }
67 }
68
69 private static void assertArrayIndexScale(final String name, int actualIndexScale, int expectedIndexScale)
70 {
71 if (actualIndexScale != expectedIndexScale) {
72 throw new IllegalStateException(name + " array index scale must be " + expectedIndexScale + ", but is " + actualIndexScale);
73 }
74 }
75
76 private JvmUtils() {}
77 }