Skip to content

Commit 29f5b62

Browse files
committed
Move NFIPosixSupport.initUnsafe to PythonUtils
1 parent 0567b14 commit 29f5b62

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NFIPosixSupport.java

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
import static com.oracle.truffle.api.CompilerDirectives.injectBranchProbability;
6262
import static com.oracle.truffle.api.CompilerDirectives.shouldNotReachHere;
6363

64-
import java.lang.reflect.Field;
6564
import java.util.ArrayList;
6665
import java.util.Arrays;
6766
import java.util.List;
@@ -132,24 +131,10 @@ public final class NFIPosixSupport extends PosixSupport {
132131

133132
private static final TruffleLogger LOGGER = PythonLanguage.getLogger(NFIPosixSupport.class);
134133

135-
private static final Unsafe UNSAFE = initUnsafe();
134+
private static final Unsafe UNSAFE = PythonUtils.initUnsafe();
136135

137136
private static final Object CRYPT_LOCK = new Object();
138137

139-
private static Unsafe initUnsafe() {
140-
try {
141-
return Unsafe.getUnsafe();
142-
} catch (SecurityException se) {
143-
try {
144-
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
145-
theUnsafe.setAccessible(true);
146-
return (Unsafe) theUnsafe.get(Unsafe.class);
147-
} catch (Exception e) {
148-
throw new UnsupportedOperationException("Cannot initialize Unsafe for the native POSIX backend", e);
149-
}
150-
}
151-
}
152-
153138
private enum PosixNativeFunction {
154139
get_errno("():sint32"),
155140
set_errno("(sint32):void"),
@@ -1666,7 +1651,7 @@ private String gai_strerror(int errorCode,
16661651
* Provides access to {@code struct addrinfo}.
16671652
*
16681653
* The layout of native {@code struct addrinfo} is as follows:
1669-
*
1654+
*
16701655
* <pre>
16711656
* {@code
16721657
* struct addrinfo {
@@ -1681,7 +1666,7 @@ private String gai_strerror(int errorCode,
16811666
* };
16821667
* }
16831668
* </pre>
1684-
*
1669+
*
16851670
* To avoid multiple NFI calls, we transfer the data in batch using arrays of {@code int}s and
16861671
* {@code long}s - int values are stored in {@code intData}, the {@code ai_canonname} and
16871672
* {@code ai_next} pointers are stored in {@code longData} and the socket address pointed to by

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/util/PythonUtils.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import static com.oracle.graal.python.nodes.SpecialMethodNames.__NEW__;
4545

4646
import java.lang.management.ManagementFactory;
47+
import java.lang.reflect.Field;
4748
import java.nio.ByteBuffer;
4849
import java.nio.ByteOrder;
4950
import java.util.ArrayDeque;
@@ -83,6 +84,8 @@
8384
import com.oracle.truffle.api.profiles.ConditionProfile;
8485
import com.oracle.truffle.api.profiles.ValueProfile;
8586

87+
import sun.misc.Unsafe;
88+
8689
public final class PythonUtils {
8790

8891
public static final PCell[] NO_CLOSURE = new PCell[0];
@@ -718,4 +721,18 @@ private static Object[] createCalltargetKeys(Object[] callTargetCacheKeys, Class
718721
arraycopy(callTargetCacheKeys, 0, keys, 1, callTargetCacheKeys.length);
719722
return keys;
720723
}
724+
725+
public static Unsafe initUnsafe() {
726+
try {
727+
return Unsafe.getUnsafe();
728+
} catch (SecurityException se) {
729+
try {
730+
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
731+
theUnsafe.setAccessible(true);
732+
return (Unsafe) theUnsafe.get(Unsafe.class);
733+
} catch (Exception e) {
734+
throw new UnsupportedOperationException("Cannot initialize Unsafe for the native POSIX backend", e);
735+
}
736+
}
737+
}
721738
}

0 commit comments

Comments
 (0)