From 3f65eeaeab21dfc0454710024549d0d768dc728c Mon Sep 17 00:00:00 2001 From: Alexander Gorshenev Date: Wed, 28 Dec 2016 18:08:32 +0300 Subject: [PATCH] Changed byte array copying to a more efficient mechanism. --- .../src/main/kotlin/kotlinx/cinterop/Utils.kt | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt b/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt index a97d1fa3a70..a8e11cf810e 100644 --- a/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt +++ b/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt @@ -169,10 +169,10 @@ class CString private constructor(override val rawPtr: NativePtr) : CPointed { override fun toString(): String { val array = reinterpret>() - val bytes = ByteArray(this.length()) - bytes.forEachIndexed { i, byte -> - bytes[i] = array[i].value - } + val len = this.length() + val bytes = ByteArray(len) + + nativeMemUtils.getByteArray(array[0], bytes, len) return String(bytes) // TODO: encoding } @@ -188,9 +188,7 @@ fun CString.Companion.fromString(str: String?, placement: NativePlacement): CStr val len = bytes.size val nativeBytes = nativeHeap.allocArray(len + 1) - bytes.forEachIndexed { i, byte -> - nativeBytes[i].value = byte - } + nativeMemUtils.putByteArray(bytes, nativeBytes[0], len) nativeBytes[len].value = 0 return CString.fromArray(nativeBytes) @@ -222,4 +220,4 @@ class MemScope private constructor(private val arena: Arena) : NativePlacement b inline fun memScoped(block: MemScope.()->R): R { @Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE") // TODO: it is a hack return MemScope.use(block) -} \ No newline at end of file +}