commonMain.earth.worldwind.util.BasicPool.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of worldwind-jvm Show documentation
Show all versions of worldwind-jvm Show documentation
The WorldWind Kotlin SDK (WWK) includes the library, examples and tutorials for building multiplatform 3D virtual globe applications for Android, Web and Java.
package earth.worldwind.util
import kotlin.math.max
@Suppress("UNCHECKED_CAST")
open class BasicPool: Pool {
companion object {
protected const val MIN_CAPACITY_INCREMENT = 12
}
protected var size = 0
protected var entries = arrayOfNulls(size)
override fun acquire(): T? {
if (size > 0) {
val last = --size
val instance = entries[last]
entries[last] = null
return instance as T?
}
return null
}
override fun release(instance: T?) {
// TODO reduce the pool size when excess entries may not be needed
// TODO use a keep alive time to indicate how long to keep stale instances
if (instance != null) {
val capacity = entries.size
if (capacity == size) {
// increase the pool size by the larger of 50% or the minimum increment
val increment = max(capacity shr 1, MIN_CAPACITY_INCREMENT)
val newEntries = arrayOfNulls(capacity + increment)
entries.copyInto(newEntries, 0, 0, capacity)
entries = newEntries
}
entries[size++] = instance
}
}
}