Skip to content

Commit

Permalink
Hide nativeMemUtils and pointerSize
Browse files Browse the repository at this point in the history
  • Loading branch information
SvyatoslavScherbina committed Sep 26, 2017
1 parent 388aba5 commit d8ed1f4
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 6 additions & 0 deletions Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
3 changes: 1 addition & 2 deletions samples/libcurl/src/main/kotlin/org/konan/libcurl/CUrl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit d8ed1f4

Please sign in to comment.