Merge pull request #4496 from JetBrains/small_backend_optimizations

Small backend optimizations
This commit is contained in:
Igor Chevdar
2020-11-10 10:59:24 +03:00
committed by Stanislav Erokhin
parent 2c898fe72c
commit f8b4db21aa
16 changed files with 234 additions and 82 deletions
@@ -390,11 +390,29 @@ private class CString(val bytes: ByteArray): CValues<ByteVar>() {
}
}
private object EmptyCString: CValues<ByteVar>() {
override val size get() = 1
override val align get() = 1
private val placement =
interpretCPointer<ByteVar>(nativeMemUtils.alloc(1, 1).rawPtr)!!.also {
it[0] = 0.toByte()
}
override fun getPointer(scope: AutofreeScope): CPointer<ByteVar> {
return placement
}
override fun place(placement: CPointer<ByteVar>): CPointer<ByteVar> {
placement[0] = 0.toByte()
return placement
}
}
/**
* @return the value of zero-terminated UTF-8-encoded C string constructed from given [kotlin.String].
*/
public val String.cstr: CValues<ByteVar>
get() = CString(encodeToUtf8(this))
get() = if (isEmpty()) EmptyCString else CString(encodeToUtf8(this))
/**
* @return the value of zero-terminated UTF-8-encoded C string constructed from given [kotlin.String].