From e3bad2a55a43a40161eb674e0ac27ac0615fc4c7 Mon Sep 17 00:00:00 2001 From: nathan Date: Sun, 1 Feb 2015 23:39:48 +0100 Subject: [PATCH] Moved unsafe to FastObjectPool (which is where it should have been) --- Dorkbox-Util/src/dorkbox/util/Sys.java | 36 ++++-------------- .../util/objectPool/FastObjectPool.java | 37 ++++++++++++++++--- 2 files changed, 38 insertions(+), 35 deletions(-) diff --git a/Dorkbox-Util/src/dorkbox/util/Sys.java b/Dorkbox-Util/src/dorkbox/util/Sys.java index 3312d9e..2c45105 100644 --- a/Dorkbox-Util/src/dorkbox/util/Sys.java +++ b/Dorkbox-Util/src/dorkbox/util/Sys.java @@ -30,8 +30,6 @@ import java.net.NetworkInterface; import java.net.SocketException; import java.net.URL; import java.net.UnknownHostException; -import java.security.AccessController; -import java.security.PrivilegedExceptionAction; import java.util.ArrayList; import java.util.Arrays; import java.util.Enumeration; @@ -47,8 +45,6 @@ public class Sys { public static final int javaVersion = getJavaVersion(); public static final boolean isAndroid = getIsAndroid(); - public static final sun.misc.Unsafe unsafe = getUNSAFE(); - public static final int KILOBYTE = 1024; public static final int MEGABYTE = 1024 * KILOBYTE; public static final int GIGABYTE = 1024 * MEGABYTE; @@ -95,29 +91,7 @@ public class Sys { } } - private static sun.misc.Unsafe getUNSAFE() { - try { - final PrivilegedExceptionAction action = new PrivilegedExceptionAction() { - @Override - public sun.misc.Unsafe run() throws Exception { - Class unsafeClass = sun.misc.Unsafe.class; - Field theUnsafe = unsafeClass.getDeclaredField("theUnsafe"); - theUnsafe.setAccessible(true); - Object unsafeObject = theUnsafe.get(null); - if (unsafeClass.isInstance(unsafeObject)) { - return unsafeClass.cast(unsafeObject); - } - throw new NoSuchFieldError("the Unsafe"); - } - }; - - return AccessController.doPrivileged(action); - } - catch (Exception e) { - throw new RuntimeException("Unable to load unsafe", e); - } - } public static final void eraseString(String string) { // You can change the value of the inner char[] using reflection. @@ -137,9 +111,13 @@ public class Sys { valueField.set(string, new char[0]); // replace it. // set count to 0 - Field countField = String.class.getDeclaredField("count"); - countField.setAccessible(true); - countField.set(string, 0); + try { + // newer versions of java don't have this field + Field countField = String.class.getDeclaredField("count"); + countField.setAccessible(true); + countField.set(string, 0); + } catch (Exception ignored) { + } // set hash to 0 Field hashField = String.class.getDeclaredField("hash"); diff --git a/Dorkbox-Util/src/dorkbox/util/objectPool/FastObjectPool.java b/Dorkbox-Util/src/dorkbox/util/objectPool/FastObjectPool.java index fb7d94a..877e709 100644 --- a/Dorkbox-Util/src/dorkbox/util/objectPool/FastObjectPool.java +++ b/Dorkbox-Util/src/dorkbox/util/objectPool/FastObjectPool.java @@ -19,13 +19,16 @@ */ package dorkbox.util.objectPool; +import java.lang.reflect.Field; +import java.security.AccessController; +import java.security.PrivilegedExceptionAction; import java.util.concurrent.locks.ReentrantLock; -import dorkbox.util.Sys; - class FastObjectPool implements ObjectPool { + private final sun.misc.Unsafe unsafe; + private static final boolean FREE = true; private static final boolean USED = false; @@ -43,6 +46,28 @@ class FastObjectPool implements ObjectPool { private ThreadLocal> localValue = new ThreadLocal<>(); FastObjectPool(PoolableObject poolableObject, int size) { + try { + final PrivilegedExceptionAction action = new PrivilegedExceptionAction() { + @Override + public sun.misc.Unsafe run() throws Exception { + Class unsafeClass = sun.misc.Unsafe.class; + Field theUnsafe = unsafeClass.getDeclaredField("theUnsafe"); + theUnsafe.setAccessible(true); + Object unsafeObject = theUnsafe.get(null); + if (unsafeClass.isInstance(unsafeObject)) { + return unsafeClass.cast(unsafeObject); + } + + throw new NoSuchFieldError("the Unsafe"); + } + }; + + this.unsafe = AccessController.doPrivileged(action); + } + catch (Exception e) { + throw new RuntimeException("Unable to load unsafe", e); + } + int newSize = 1; while (newSize < size) { @@ -61,8 +86,8 @@ class FastObjectPool implements ObjectPool { this.mask = size-1; this.releasePointer = size; - this.BASE = Sys.unsafe.arrayBaseOffset(ObjectPoolHolder[].class); - this.INDEXSCALE = Sys.unsafe.arrayIndexScale(ObjectPoolHolder[].class); + this.BASE = this.unsafe.arrayBaseOffset(ObjectPoolHolder[].class); + this.INDEXSCALE = this.unsafe.arrayIndexScale(ObjectPoolHolder[].class); this.ASHIFT = 31 - Integer.numberOfLeadingZeros((int) this.INDEXSCALE); } @@ -78,7 +103,7 @@ class FastObjectPool implements ObjectPool { } } - sun.misc.Unsafe unsafe = Sys.unsafe; + sun.misc.Unsafe unsafe = this.unsafe; while (this.releasePointer != (localTakePointer=this.takePointer)) { int index = localTakePointer & this.mask; @@ -109,7 +134,7 @@ class FastObjectPool implements ObjectPool { long index = ((localValue & this.mask)<