nativeMain.kotlinx.io.pool.DefaultPool.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlinx-io-linuxx64 Show documentation
Show all versions of kotlinx-io-linuxx64 Show documentation
IO support libraries for Kotlin
package kotlinx.io.pool
actual abstract class DefaultPool actual constructor(actual override final val capacity: Int) : ObjectPool {
private val instances = arrayOfNulls(capacity)
private var size = 0
actual protected abstract fun produceInstance(): T
actual protected open fun disposeInstance(instance: T) {}
actual protected open fun clearInstance(instance: T): T = instance
actual protected open fun validateInstance(instance: T) {}
actual final override fun borrow(): T {
if (size == 0) return produceInstance()
val idx = --size
@Suppress("UNCHECKED_CAST")
val instance = instances[idx] as T
instances[idx] = null
return clearInstance(instance)
}
actual final override fun recycle(instance: T) {
validateInstance(instance)
if (size == capacity) {
disposeInstance(instance)
} else {
instances[size++] = instance
}
}
actual final override fun dispose() {
for (i in 0 until size) {
@Suppress("UNCHECKED_CAST")
val instance = instances[i] as T
instances[i] = null
disposeInstance(instance)
}
size = 0
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy