Removed/cached some reflection calls
This commit is contained in:
@@ -91,6 +91,11 @@ object nativeMemUtils {
|
|||||||
|
|
||||||
fun zeroMemory(dest: NativePointed, length: Int): Unit = unsafe.setMemory(dest.address, length.toLong(), 0)
|
fun zeroMemory(dest: NativePointed, length: Int): Unit = unsafe.setMemory(dest.address, length.toLong(), 0)
|
||||||
|
|
||||||
|
@Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE")
|
||||||
|
inline fun<reified T> allocateInstance(): T {
|
||||||
|
return unsafe.allocateInstance(T::class.java) as T
|
||||||
|
}
|
||||||
|
|
||||||
internal class NativeAllocated(rawPtr: NativePtr) : NativePointed(rawPtr)
|
internal class NativeAllocated(rawPtr: NativePtr) : NativePointed(rawPtr)
|
||||||
|
|
||||||
fun alloc(size: Long, align: Int): NativePointed {
|
fun alloc(size: Long, align: Int): NativePointed {
|
||||||
|
|||||||
@@ -16,32 +16,33 @@
|
|||||||
|
|
||||||
package kotlinx.cinterop
|
package kotlinx.cinterop
|
||||||
|
|
||||||
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
import kotlin.reflect.full.companionObjectInstance
|
import kotlin.reflect.full.companionObjectInstance
|
||||||
import kotlin.reflect.full.primaryConstructor
|
|
||||||
|
|
||||||
typealias NativePtr = Long
|
typealias NativePtr = Long
|
||||||
val nativeNullPtr: NativePtr = 0L
|
val nativeNullPtr: NativePtr = 0L
|
||||||
|
|
||||||
// TODO: the functions below should eventually be intrinsified
|
// TODO: the functions below should eventually be intrinsified
|
||||||
|
|
||||||
inline fun <reified T : CVariable> typeOf() = T::class.companionObjectInstance as CVariable.Type
|
private val typeOfCache = ConcurrentHashMap<Class<*>, CVariable.Type>()
|
||||||
|
|
||||||
|
@Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE")
|
||||||
|
inline fun <reified T : CVariable> typeOf() =
|
||||||
|
typeOfCache.computeIfAbsent(T::class.java) { T::class.companionObjectInstance as CVariable.Type }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns interpretation of entity with given pointer, or `null` if it is null.
|
* Returns interpretation of entity with given pointer, or `null` if it is null.
|
||||||
*
|
*
|
||||||
* @param T must not be abstract
|
* @param T must not be abstract
|
||||||
*/
|
*/
|
||||||
|
@Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE")
|
||||||
inline fun <reified T : NativePointed> interpretNullablePointed(ptr: NativePtr): T? {
|
inline fun <reified T : NativePointed> interpretNullablePointed(ptr: NativePtr): T? {
|
||||||
if (ptr == nativeNullPtr) {
|
if (ptr == nativeNullPtr) {
|
||||||
return null
|
return null
|
||||||
} else {
|
} else {
|
||||||
val kClass = T::class
|
val result = nativeMemUtils.allocateInstance<T>()
|
||||||
val primaryConstructor = kClass.primaryConstructor
|
result.rawPtr = ptr
|
||||||
if (primaryConstructor == null) {
|
return result
|
||||||
throw IllegalArgumentException("${kClass.simpleName} doesn't have a constructor")
|
|
||||||
}
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
|
||||||
return (primaryConstructor as (NativePtr) -> T)(ptr)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user