diff --git a/Interop/Runtime/src/main/kotlin/kotlin_native/interop/Utils.kt b/Interop/Runtime/src/main/kotlin/kotlin_native/interop/Utils.kt index d1de51c09cf..db5fb71e7d7 100644 --- a/Interop/Runtime/src/main/kotlin/kotlin_native/interop/Utils.kt +++ b/Interop/Runtime/src/main/kotlin/kotlin_native/interop/Utils.kt @@ -48,4 +48,29 @@ fun CString.Companion.fromString(str: String?): CString? { fun NativeArray.asCString() = CString.fromArray(this) fun Int8Box.asCString() = CString.fromArray(NativeArray.byRefToFirstElem(this, Int8Box)) -fun String.toCString() = CString.fromString(this) \ No newline at end of file +fun String.toCString() = CString.fromString(this) + +class MemScope private constructor(private val arena: Arena) : Placement by arena { + val memScope: Placement + get() = this + + companion object { + internal inline fun use(block: MemScope.()->R): R { + val memScope = MemScope(Arena()) + try { + return memScope.block() + } finally { + memScope.arena.clear() + } + } + } +} + +/** + * Runs given [block] providing allocation of memory + * which will be automatically disposed at the end of this scope. + */ +inline fun memScoped(block: MemScope.()->R): R { + @Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE") // TODO: it is a hack + return MemScope.use(block) +} \ No newline at end of file