This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 490
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #327 from xxjy/master
Memory thrashing problem.
- Loading branch information
Showing
3 changed files
with
124 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
library/src/main/java/com/opensource/svgaplayer/utils/Pools.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.opensource.svgaplayer.utils | ||
|
||
/** | ||
* Helper class for creating pools of objects. An example use looks like this: | ||
* <pre> | ||
* public class MyPooledClass { | ||
* | ||
* private static final SynchronizedPool<MyPooledClass> sPool = | ||
* new SynchronizedPool<MyPooledClass>(10); | ||
* | ||
* public static MyPooledClass obtain() { | ||
* MyPooledClass instance = sPool.acquire(); | ||
* return (instance != null) ? instance : new MyPooledClass(); | ||
* } | ||
* | ||
* public void recycle() { | ||
* // Clear state if needed. | ||
* sPool.release(this); | ||
* } | ||
* | ||
* . . . | ||
* } | ||
* </pre> | ||
* | ||
*/ | ||
class Pools private constructor() { | ||
|
||
/** | ||
* Interface for managing a pool of objects. | ||
* | ||
* @param <T> The pooled type. | ||
*/ | ||
interface Pool<T> { | ||
/** | ||
* @return An instance from the pool if such, null otherwise. | ||
*/ | ||
fun acquire(): T? | ||
|
||
/** | ||
* Release an instance to the pool. | ||
* | ||
* @param instance The instance to release. | ||
* @return Whether the instance was put in the pool. | ||
* | ||
* @throws IllegalStateException If the instance is already in the pool. | ||
*/ | ||
fun release(instance: T): Boolean | ||
} | ||
|
||
/** | ||
* Simple (non-synchronized) pool of objects. | ||
* | ||
* @param maxPoolSize The max pool size. | ||
* | ||
* @throws IllegalArgumentException If the max pool size is less than zero. | ||
* | ||
* @param <T> The pooled type. | ||
*/ | ||
open class SimplePool<T>(maxPoolSize: Int) : Pool<T> { | ||
private val mPool: Array<Any?> | ||
private var mPoolSize = 0 | ||
|
||
init { | ||
require(maxPoolSize > 0) { "The max pool size must be > 0" } | ||
mPool = arrayOfNulls(maxPoolSize) | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
override fun acquire(): T? { | ||
if (mPoolSize > 0) { | ||
val lastPooledIndex = mPoolSize - 1 | ||
val instance = mPool[lastPooledIndex] as T? | ||
mPool[lastPooledIndex] = null | ||
mPoolSize-- | ||
return instance | ||
} | ||
return null | ||
} | ||
|
||
override fun release(instance: T): Boolean { | ||
check(!isInPool(instance)) { "Already in the pool!" } | ||
if (mPoolSize < mPool.size) { | ||
mPool[mPoolSize] = instance | ||
mPoolSize++ | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
private fun isInPool(instance: T): Boolean { | ||
for (i in 0 until mPoolSize) { | ||
if (mPool[i] === instance) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
} | ||
|
||
|
||
} |