Introduced

nativeMemUtils.getByteArray
and
    nativeMemUtils.putByteArray
for faster memory copy.
This commit is contained in:
Alexander Gorshenev
2016-12-20 17:52:07 +03:00
committed by alexander-gorshenev
parent 823ef031eb
commit e48c5a7215
2 changed files with 16 additions and 2 deletions
@@ -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
}
}
}
@@ -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
}
}