diff --git a/Interop/Runtime/src/jvm/kotlin/kotlinx/cinterop/JvmNativeMem.kt b/Interop/Runtime/src/jvm/kotlin/kotlinx/cinterop/JvmNativeMem.kt index 74a1012494e..99280aa5098 100644 --- a/Interop/Runtime/src/jvm/kotlin/kotlinx/cinterop/JvmNativeMem.kt +++ b/Interop/Runtime/src/jvm/kotlin/kotlinx/cinterop/JvmNativeMem.kt @@ -34,9 +34,11 @@ private val dataModel: DataModel = when (System.getProperty("sun.arch.data.model } // Must be only used in interop, contains host pointer size, not target! -val pointerSize: Int = dataModel.pointerSize.toInt() +@PublishedApi +internal val pointerSize: Int = dataModel.pointerSize.toInt() -object nativeMemUtils { +@PublishedApi +internal object nativeMemUtils { fun getByte(mem: NativePointed) = unsafe.getByte(mem.address) fun putByte(mem: NativePointed, value: Byte) = unsafe.putByte(mem.address, value) diff --git a/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt b/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt index 2f849d72468..7b7d193a25a 100644 --- a/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt +++ b/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt @@ -379,3 +379,9 @@ inline fun memScoped(block: MemScope.()->R): R { memScope.clear() } } + +fun COpaquePointer.readBytes(count: Int): ByteArray { + val result = ByteArray(count) + nativeMemUtils.getByteArray(this.reinterpret().pointed, result, count) + return result +} diff --git a/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeMem.kt b/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeMem.kt index 1871a912c7b..409520218d4 100644 --- a/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeMem.kt +++ b/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeMem.kt @@ -2,13 +2,16 @@ package kotlinx.cinterop import konan.internal.Intrinsic -inline val pointerSize: Int +@PublishedApi +internal inline val pointerSize: Int get() = getPointerSize() -@Intrinsic external fun getPointerSize(): Int +@PublishedApi +@Intrinsic internal external fun getPointerSize(): Int // TODO: do not use singleton because it leads to init-check on any access. -object nativeMemUtils { +@PublishedApi +internal object nativeMemUtils { @Intrinsic external fun getByte(mem: NativePointed): Byte @Intrinsic external fun putByte(mem: NativePointed, value: Byte) 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 5f99121e0f6..17892e1b6e8 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 @@ -57,9 +57,7 @@ public fun base64Decode(encoded: String): ByteArray { val errorCode = DecodeBase64(encoded, encoded.length, result, resultSize) if (errorCode != 0) throw Error("Non-zero exit code of DecodeBase64: ${errorCode}") val realSize = resultSize[0] - val bytes = ByteArray(realSize) - nativeMemUtils.getByteArray(result.pointed, bytes, realSize) - return bytes + return result.readBytes(realSize) } } diff --git a/samples/libcurl/src/main/kotlin/org/konan/libcurl/CUrl.kt b/samples/libcurl/src/main/kotlin/org/konan/libcurl/CUrl.kt index 26fd041bcde..6da4b377fd3 100644 --- a/samples/libcurl/src/main/kotlin/org/konan/libcurl/CUrl.kt +++ b/samples/libcurl/src/main/kotlin/org/konan/libcurl/CUrl.kt @@ -31,8 +31,7 @@ class CUrl(val url: String) { } fun CPointer.toKString(length: Int): String { - val bytes = ByteArray(length) - nativeMemUtils.getByteArray(pointed, bytes, length) + val bytes = this.readBytes(length) return kotlin.text.fromUtf8Array(bytes, 0, bytes.size) }