From 5acd49215b488c6a4930fafd8513a610a0089160 Mon Sep 17 00:00:00 2001 From: SvyatoslavScherbina Date: Tue, 25 Sep 2018 15:54:46 +0300 Subject: [PATCH] Update INTEROP.md (#2131) --- INTEROP.md | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/INTEROP.md b/INTEROP.md index 51638c4c15d..55fc7733838 100644 --- a/INTEROP.md +++ b/INTEROP.md @@ -236,7 +236,7 @@ All the supported C types have corresponding representations in Kotlin: Kotlin counterpart with the same width. * Pointers and arrays are mapped to `CPointer?`. * Enums can be mapped to either Kotlin enum or integral values, depending on - heuristics and the definition file hints (see "Definition file hints" below). + heuristics and the [definition file hints](#definition-file-hints). * Structs are mapped to types having fields available via the dot notation, i.e. `someStructInstance.field1`. * `typedef` are represented as `typealias`. @@ -383,7 +383,7 @@ used like ```kotlin val fileSize = memScoped { - val statBuf = alloc() + val statBuf = alloc() val error = stat("/", statBuf.ptr) statBuf.st_size } @@ -443,8 +443,8 @@ expecting a C string. There are also some tools available to convert between Kotlin and C strings manually: -* `fun CPointer.toKString(): String` -* `val String.cstr: CValuesRef`. +* `fun CPointer.toKString(): String` +* `val String.cstr: CValuesRef`. To get the pointer, `.cstr` should be allocated in native memory, e.g. @@ -562,8 +562,8 @@ To wrap the reference:
```kotlin -val stablePtr = StableRef.create(kotlinReference) -val voidPtr = stablePtr.value +val stableRef = StableRef.create(kotlinReference) +val voidPtr = stableRef.asCPointer() ```
@@ -575,14 +575,13 @@ To unwrap the reference:
```kotlin -val stablePtr = StableRef.fromValue(voidPtr) -val kotlinReference = stablePtr.get() +val stableRef = voidPtr.asStableRef() +val kotlinReference = stableRef.get() ```
-where `kotlinReference` is the original wrapped reference (however, it's type is -`Any` so it may require casting). +where `kotlinReference` is the original wrapped reference. The created `StableRef` should eventually be manually disposed using the `.dispose()` method to prevent memory leaks: @@ -590,7 +589,7 @@ the `.dispose()` method to prevent memory leaks:
```kotlin -stablePtr.dispose() +stableRef.dispose() ```