Interop: add minor improvements

This commit is contained in:
Svyatoslav Scherbina
2016-09-29 17:23:41 +07:00
parent 5dc871ec32
commit 80ed5a2461
2 changed files with 8 additions and 2 deletions
@@ -195,4 +195,8 @@ class Arena : Placement, Closeable {
override fun close() = clear()
}
fun <T : NativeRef> Placement.alloc(type: NativeRef.TypeWithSize<T>): T {
return type.byPtr(this.alloc(type.size))
}
@@ -19,14 +19,16 @@ fun free(ptr: NativePtr?) {
fun free(ref: NativeRef?) = free(ref.getNativePtr())
fun <T : NativeRef> mallocNativeArrayOf(elemType: NativeRef.Type<T>, vararg elements: T?): NativeArray<RefBox<T>> {
val res = malloc(array[elements.size](elemType.ref))
fun <T : NativeRef> Placement.allocNativeArrayOf(elemType: NativeRef.Type<T>, vararg elements: T?): NativeArray<RefBox<T>> {
val res = this.alloc(array[elements.size](elemType.ref))
elements.forEachIndexed { i, element ->
res[i].value = element
}
return res
}
fun <T : NativeRef> mallocNativeArrayOf(elemType: NativeRef.Type<T>, vararg elements: T?) = heap.allocNativeArrayOf(elemType, *elements)
fun CString.Companion.fromString(str: String?): CString? {
if (str == null) {
return null