Loading core/java/android/util/Pools.java +12 −32 Original line number Original line Diff line number Diff line Loading @@ -69,24 +69,16 @@ public final class Pools { /* do nothing - hiding constructor */ /* do nothing - hiding constructor */ } } private static class PoolableHolder<T> { T mPoolable; PoolableHolder<T> mNext; } /** /** * Simple (non-synchronized) pool of objects. * Simple (non-synchronized) pool of objects. * * * @param <T> The pooled type. * @param <T> The pooled type. */ */ public static class SimplePool<T> implements Pool<T> { public static class SimplePool<T> implements Pool<T> { private final int mMaxPoolSize; private final Object[] mPool; private int mPoolSize; private int mPoolSize; private PoolableHolder<T> mEmptyHolders; private PoolableHolder<T> mPool; /** /** * Creates a new instance. * Creates a new instance. * * Loading @@ -98,20 +90,18 @@ public final class Pools { if (maxPoolSize <= 0) { if (maxPoolSize <= 0) { throw new IllegalArgumentException("The max pool size must be > 0"); throw new IllegalArgumentException("The max pool size must be > 0"); } } mMaxPoolSize = maxPoolSize; mPool = new Object[maxPoolSize]; } } @Override @Override @SuppressWarnings("unchecked") public T acquire() { public T acquire() { if (mPool != null) { if (mPoolSize > 0) { PoolableHolder<T> holder = mPool; final int lastPooledIndex = mPoolSize - 1; mPool = holder.mNext; T instance = (T) mPool[lastPooledIndex]; T poolable = holder.mPoolable; mPool[lastPooledIndex] = null; holder.mPoolable = null; holder.mNext = mEmptyHolders; mEmptyHolders = holder; mPoolSize--; mPoolSize--; return poolable; return instance; } } return null; return null; } } Loading @@ -121,16 +111,8 @@ public final class Pools { if (isInPool(instance)) { if (isInPool(instance)) { throw new IllegalStateException("Already in the pool!"); throw new IllegalStateException("Already in the pool!"); } } if (mPoolSize < mMaxPoolSize) { if (mPoolSize < mPool.length) { PoolableHolder<T> holder = mEmptyHolders; mPool[mPoolSize] = instance; if (holder == null) { holder = new PoolableHolder<T>(); } else { mEmptyHolders = holder.mNext; } holder.mPoolable = instance; holder.mNext = mPool; mPool = holder; mPoolSize++; mPoolSize++; return true; return true; } } Loading @@ -138,12 +120,10 @@ public final class Pools { } } private boolean isInPool(T instance) { private boolean isInPool(T instance) { PoolableHolder<T> current = mPool; for (int i = 0; i < mPoolSize; i++) { while (current != null) { if (mPool[i] == instance) { if (current.mPoolable == instance) { return true; return true; } } current = current.mNext; } } return false; return false; } } Loading Loading
core/java/android/util/Pools.java +12 −32 Original line number Original line Diff line number Diff line Loading @@ -69,24 +69,16 @@ public final class Pools { /* do nothing - hiding constructor */ /* do nothing - hiding constructor */ } } private static class PoolableHolder<T> { T mPoolable; PoolableHolder<T> mNext; } /** /** * Simple (non-synchronized) pool of objects. * Simple (non-synchronized) pool of objects. * * * @param <T> The pooled type. * @param <T> The pooled type. */ */ public static class SimplePool<T> implements Pool<T> { public static class SimplePool<T> implements Pool<T> { private final int mMaxPoolSize; private final Object[] mPool; private int mPoolSize; private int mPoolSize; private PoolableHolder<T> mEmptyHolders; private PoolableHolder<T> mPool; /** /** * Creates a new instance. * Creates a new instance. * * Loading @@ -98,20 +90,18 @@ public final class Pools { if (maxPoolSize <= 0) { if (maxPoolSize <= 0) { throw new IllegalArgumentException("The max pool size must be > 0"); throw new IllegalArgumentException("The max pool size must be > 0"); } } mMaxPoolSize = maxPoolSize; mPool = new Object[maxPoolSize]; } } @Override @Override @SuppressWarnings("unchecked") public T acquire() { public T acquire() { if (mPool != null) { if (mPoolSize > 0) { PoolableHolder<T> holder = mPool; final int lastPooledIndex = mPoolSize - 1; mPool = holder.mNext; T instance = (T) mPool[lastPooledIndex]; T poolable = holder.mPoolable; mPool[lastPooledIndex] = null; holder.mPoolable = null; holder.mNext = mEmptyHolders; mEmptyHolders = holder; mPoolSize--; mPoolSize--; return poolable; return instance; } } return null; return null; } } Loading @@ -121,16 +111,8 @@ public final class Pools { if (isInPool(instance)) { if (isInPool(instance)) { throw new IllegalStateException("Already in the pool!"); throw new IllegalStateException("Already in the pool!"); } } if (mPoolSize < mMaxPoolSize) { if (mPoolSize < mPool.length) { PoolableHolder<T> holder = mEmptyHolders; mPool[mPoolSize] = instance; if (holder == null) { holder = new PoolableHolder<T>(); } else { mEmptyHolders = holder.mNext; } holder.mPoolable = instance; holder.mNext = mPool; mPool = holder; mPoolSize++; mPoolSize++; return true; return true; } } Loading @@ -138,12 +120,10 @@ public final class Pools { } } private boolean isInPool(T instance) { private boolean isInPool(T instance) { PoolableHolder<T> current = mPool; for (int i = 0; i < mPoolSize; i++) { while (current != null) { if (mPool[i] == instance) { if (current.mPoolable == instance) { return true; return true; } } current = current.mNext; } } return false; return false; } } Loading