Hide nativeMemUtils and pointerSize

This commit is contained in:
Svyatoslav Scherbina
2017-09-15 11:39:16 +03:00
committed by SvyatoslavScherbina
parent 388aba5040
commit d8ed1f4414
5 changed files with 18 additions and 10 deletions
@@ -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)
@@ -379,3 +379,9 @@ inline fun <R> memScoped(block: MemScope.()->R): R {
memScope.clear()
}
}
fun COpaquePointer.readBytes(count: Int): ByteArray {
val result = ByteArray(count)
nativeMemUtils.getByteArray(this.reinterpret<ByteVar>().pointed, result, count)
return result
}
@@ -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)
@@ -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)
}
}
@@ -31,8 +31,7 @@ class CUrl(val url: String) {
}
fun CPointer<ByteVar>.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)
}