From e48c5a72156658997da5ae9de02225ec1478675a Mon Sep 17 00:00:00 2001 From: Alexander Gorshenev Date: Tue, 20 Dec 2016 17:52:07 +0300 Subject: [PATCH] Introduced nativeMemUtils.getByteArray and nativeMemUtils.putByteArray for faster memory copy. --- .../jvm/kotlin/kotlinx/cinterop/JvmNativeMem.kt | 14 +++++++++++++- .../kotlin/backend/konan/llvm/HashUtils.kt | 4 +++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Interop/Runtime/src/jvm/kotlin/kotlinx/cinterop/JvmNativeMem.kt b/Interop/Runtime/src/jvm/kotlin/kotlinx/cinterop/JvmNativeMem.kt index a1e4818a35e..ee89f72d0be 100644 --- a/Interop/Runtime/src/jvm/kotlin/kotlinx/cinterop/JvmNativeMem.kt +++ b/Interop/Runtime/src/jvm/kotlin/kotlinx/cinterop/JvmNativeMem.kt @@ -48,6 +48,18 @@ object nativeMemUtils { DataModel._64BIT -> putLong(mem, value) } + fun getByteArray(source: NativePointed, dest: ByteArray, length: Int) { + val clazz = ByteArray::class.java + val baseOffset = unsafe.arrayBaseOffset(clazz).toLong(); + unsafe.copyMemory(null, source.address, dest, baseOffset, length.toLong()) + } + + fun putByteArray(source: ByteArray, dest: NativePointed, length: Int) { + val clazz = ByteArray::class.java + val baseOffset = unsafe.arrayBaseOffset(clazz).toLong(); + unsafe.copyMemory(source, baseOffset, null, dest.address, length.toLong()) + } + internal class NativeAllocated(override val rawPtr: NativePtr) : NativePointed fun alloc(size: Long, align: Int): NativePointed { @@ -67,4 +79,4 @@ object nativeMemUtils { isAccessible = true return@with this.get(null) as Unsafe } -} \ No newline at end of file +} diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/HashUtils.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/HashUtils.kt index 8288d4e2abe..03f8f8e4f87 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/HashUtils.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/HashUtils.kt @@ -41,7 +41,9 @@ public fun base64Decode(encoded: String): ByteArray { val errorCode = DecodeBase64(encoded, encoded.length, result[0].ptr, resultSize[0].ptr) if (errorCode != 0) throw Error("Non-zero exit code of DecodeBase64: ${errorCode}") val realSize = resultSize[0].value!! - return ByteArray(realSize, {i -> result[i].value!!}) + val bytes = ByteArray(realSize) + nativeMemUtils.getByteArray(result[0], bytes, realSize) + return bytes } }