-
Notifications
You must be signed in to change notification settings - Fork 816
/
UnsafeUtil.java
151 lines (137 loc) · 6.61 KB
/
UnsafeUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* Copyright (c) 2008-2023, Nathan Sweet
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.
* - Neither the name of Esoteric Software nor the names of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
package com.esotericsoftware.kryo.unsafe;
import static com.esotericsoftware.minlog.Log.*;
import com.esotericsoftware.kryo.KryoException;
import com.esotericsoftware.kryo.util.Util;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import sun.misc.Unsafe;
import sun.nio.ch.DirectBuffer;
/** Utility methods for using {@link sun.misc.Unsafe}.
* <p>
* Not available on all JVMs. {@link Util#unsafe} can be checked before using this class.
* @author Roman Levenstein <romixlev@gmail.com> */
@SuppressWarnings("restriction")
public class UnsafeUtil {
/** The sun.misc.Unsafe instance, or null if Unsafe is unavailable. */
public static final Unsafe unsafe;
public static final long byteArrayBaseOffset;
public static final long floatArrayBaseOffset;
public static final long doubleArrayBaseOffset;
public static final long intArrayBaseOffset;
public static final long longArrayBaseOffset;
public static final long shortArrayBaseOffset;
public static final long charArrayBaseOffset;
public static final long booleanArrayBaseOffset;
static {
Unsafe tempUnsafe = null;
long tempByteArrayBaseOffset = 0;
long tempFloatArrayBaseOffset = 0;
long tempDoubleArrayBaseOffset = 0;
long tempIntArrayBaseOffset = 0;
long tempLongArrayBaseOffset = 0;
long tempShortArrayBaseOffset = 0;
long tempCharArrayBaseOffset = 0;
long tempBooleanArrayBaseOffset = 0;
try {
if (!Util.isAndroid) {
Field field = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
tempUnsafe = (sun.misc.Unsafe)field.get(null);
tempByteArrayBaseOffset = tempUnsafe.arrayBaseOffset(byte[].class);
tempCharArrayBaseOffset = tempUnsafe.arrayBaseOffset(char[].class);
tempShortArrayBaseOffset = tempUnsafe.arrayBaseOffset(short[].class);
tempIntArrayBaseOffset = tempUnsafe.arrayBaseOffset(int[].class);
tempFloatArrayBaseOffset = tempUnsafe.arrayBaseOffset(float[].class);
tempLongArrayBaseOffset = tempUnsafe.arrayBaseOffset(long[].class);
tempDoubleArrayBaseOffset = tempUnsafe.arrayBaseOffset(double[].class);
tempBooleanArrayBaseOffset = tempUnsafe.arrayBaseOffset(boolean[].class);
} else {
if (DEBUG) debug("kryo", "Unsafe is not available on Android.");
}
} catch (Exception ex) {
if (DEBUG) debug("kryo", "Unsafe is not available.", ex);
}
byteArrayBaseOffset = tempByteArrayBaseOffset;
charArrayBaseOffset = tempCharArrayBaseOffset;
shortArrayBaseOffset = tempShortArrayBaseOffset;
intArrayBaseOffset = tempIntArrayBaseOffset;
floatArrayBaseOffset = tempFloatArrayBaseOffset;
longArrayBaseOffset = tempLongArrayBaseOffset;
doubleArrayBaseOffset = tempDoubleArrayBaseOffset;
booleanArrayBaseOffset = tempBooleanArrayBaseOffset;
unsafe = tempUnsafe;
}
// Use a static inner class to defer initialization of direct buffer methods until first use
private static final class DirectBuffers {
// Constructor to be used for creation of ByteBuffers that use pre-allocated memory regions.
private static Constructor<? extends ByteBuffer> directByteBufferConstructor;
static {
ByteBuffer buffer = ByteBuffer.allocateDirect(1);
try {
directByteBufferConstructor = buffer.getClass().getDeclaredConstructor(long.class, int.class);
directByteBufferConstructor.setAccessible(true);
} catch (Exception ex) {
if (DEBUG) debug("kryo", "No direct ByteBuffer constructor is available.", ex);
directByteBufferConstructor = null;
}
}
private static Method cleanerMethod, cleanMethod;
static {
try {
cleanerMethod = DirectBuffer.class.getMethod("cleaner");
cleanerMethod.setAccessible(true);
cleanMethod = cleanerMethod.getReturnType().getMethod("clean");
} catch (Exception ex) {
if (DEBUG) debug("kryo", "No direct ByteBuffer clean method is available.", ex);
cleanerMethod = null;
}
}
}
/** Create a ByteBuffer that uses the specified off-heap memory address instead of allocating a new one.
* @param address Address of the memory region to be used for a ByteBuffer.
* @param size Size in bytes of the memory region.
* @throws UnsupportedOperationException if creating a ByteBuffer this way is not available. */
public static ByteBuffer newDirectBuffer (long address, int size) {
if (!isNewDirectBufferAvailable())
throw new UnsupportedOperationException("No direct ByteBuffer constructor is available.");
try {
return DirectBuffers.directByteBufferConstructor.newInstance(address, size);
} catch (Exception ex) {
throw new KryoException("Error creating a ByteBuffer at address: " + address, ex);
}
}
/** Returns true if {@link #newDirectBuffer(long, int)} can be called. */
public static boolean isNewDirectBufferAvailable () {
return DirectBuffers.directByteBufferConstructor != null;
}
/** Release a direct buffer immediately rather than waiting for GC. */
public static void dispose (ByteBuffer buffer) {
if (!(buffer instanceof DirectBuffer)) return;
if (DirectBuffers.cleanerMethod != null) {
try {
DirectBuffers.cleanMethod.invoke(DirectBuffers.cleanerMethod.invoke(buffer));
} catch (Throwable ignored) {
}
}
}
}