Interoperability with C
Kotlin/Native follows the general tradition of Kotlin to provide excellent existing platform software interoperability. In the case of a native platform, the most important interoperability target is a C library. So Kotlin/Native comes with a cinterop tool, which can be used to quickly generate everything needed to interact with an external library.
The following workflow is expected when interacting with the native library:
Create a
.def
file describing what to include into bindings.Use the cinterop tool to produce Kotlin bindings.
Run the Kotlin/Native compiler on an application to produce the final executable.
The interoperability tool analyses C headers and produces a "natural" mapping of the types, functions, and constants into the Kotlin world. The generated stubs can be imported into an IDE for the purpose of code completion and navigation.
Interoperability with Swift/Objective-C is provided too and covered in Objective-C interop.
Platform libraries
Note that in many cases there's no need to use custom interoperability library creation mechanisms described below, as for APIs available on the platform standardized bindings called platform libraries could be used. For example, POSIX on Linux/macOS platforms, Win32 on Windows platform, or Apple frameworks on macOS/iOS are available this way.
Simple example
Install libgit2 and prepare stubs for the git library:
Compile the client:
Run the client:
Create bindings for a new library
To create bindings for a new library, first create and configure a definition file.
Bindings
Basic interop types
All the supported C types have corresponding representations in Kotlin:
Signed, unsigned integral, and floating point types are mapped to their Kotlin counterpart with the same width.
Pointers and arrays are mapped to
CPointer<T>?
.Enums can be mapped to either Kotlin enum or integral values, depending on heuristics and the definition file settings.
Structs and unions are mapped to types having fields available via the dot notation, i.e.
someStructInstance.field1
.typedef
are represented astypealias
.
Also, any C type has the Kotlin type representing the lvalue of this type, i.e., the value located in memory rather than a simple immutable self-contained value. Think C++ references, as a similar concept. For structs (and typedef
s to structs) this representation is the main one and has the same name as the struct itself, for Kotlin enums it is named ${type}Var
, for CPointer<T>
it is CPointerVar<T>
, and for most other types it is ${type}Var
.
For types that have both representations, the one with a "lvalue" has a mutable .value
property for accessing the value.
Pointer types
The type argument T
of CPointer<T>
must be one of the "lvalue" types described above, e.g., the C type struct S*
is mapped to CPointer<S>
, int8_t*
is mapped to CPointer<int_8tVar>
, and char**
is mapped to CPointer<CPointerVar<ByteVar>>
.
C null pointer is represented as Kotlin's null
, and the pointer type CPointer<T>
is not nullable, but the CPointer<T>?
is. The values of this type support all the Kotlin operations related to handling null
, e.g. ?:
, ?.
, !!
etc.:
Since the arrays are also mapped to CPointer<T>
, it supports the []
operator for accessing values by index:
The .pointed
property for CPointer<T>
returns the lvalue of type T
, pointed by this pointer. The reverse operation is .ptr
: it takes the lvalue and returns the pointer to it.
void*
is mapped to COpaquePointer
– the special pointer type which is the supertype for any other pointer type. So if the C function takes void*
, then the Kotlin binding accepts any CPointer
.
Casting a pointer (including COpaquePointer
) can be done with .reinterpret<T>
, e.g.:
or
As is with C, these reinterpret casts are unsafe and can potentially lead to subtle memory problems in the application.
Also, there are unsafe casts between CPointer<T>?
and Long
available, provided by the .toLong()
and .toCPointer<T>()
extension methods:
Note that if the type of the result is known from the context, the type argument can be omitted as usual due to the type inference.
Memory allocation
The native memory can be allocated using the NativePlacement
interface, for example:
or
The most "natural" placement is in the object nativeHeap
. It corresponds to allocating native memory with malloc
and provides an additional .free()
operation to free allocated memory:
However, the lifetime of allocated memory is often bound to the lexical scope. It is possible to define such scope with memScoped { ... }
. Inside the braces, the temporary placement is available as an implicit receiver, so it is possible to allocate native memory with alloc
and allocArray
, and the allocated memory will be automatically freed after leaving the scope.
For example, the C function returning values through pointer parameters can be used like:
Pass pointers to bindings
Although C pointers are mapped to the CPointer<T>
type, the C function pointer-typed parameters are mapped to CValuesRef<T>
. When passing CPointer<T>
as the value of such a parameter, it is passed to the C function as is. However, the sequence of values can be passed instead of a pointer. In this case the sequence is passed "by value", i.e., the C function receives the pointer to the temporary copy of that sequence, which is valid only until the function returns.
The CValuesRef<T>
representation of pointer parameters is designed to support C array literals without explicit native memory allocation. To construct the immutable self-contained sequence of C values, the following methods are provided:
${type}Array.toCValues()
, wheretype
is the Kotlin primitive typeArray<CPointer<T>?>.toCValues()
,List<CPointer<T>?>.toCValues()
cValuesOf(vararg elements: ${type})
, wheretype
is a primitive or pointer
For example:
Strings
Unlike other pointers, the parameters of type const char*
are represented as a Kotlin String
. So it is possible to pass any Kotlin string to a binding expecting a C string.
There are also some tools available to convert between Kotlin and C strings manually:
fun CPointer<ByteVar>.toKString(): String
val String.cstr: CValuesRef<ByteVar>
.
To get the pointer, .cstr
should be allocated in native memory, for example:
In all cases, the C string is supposed to be encoded as UTF-8.
To skip automatic conversion and ensure raw pointers are used in the bindings, a noStringConversion
statement in the .def
file could be used:
This way any value of type CPointer<ByteVar>
can be passed as an argument of const char*
type. If a Kotlin string should be passed, code like this could be used:
Scope-local pointers
It is possible to create a scope-stable pointer of C representation of CValues<T>
instance using the CValues<T>.ptr
extension property, available under memScoped { }
. It allows using the APIs which require C pointers with a lifetime bound to a certain MemScope
. For example:
In this example, all values passed to the C API new_menu()
have a lifetime of the innermost memScope
it belongs to. Once the control flow leaves the memScoped
scope the C pointers become invalid.
Pass and receive structs by value
When a C function takes or returns a struct / union T
by value, the corresponding argument type or return type is represented as CValue<T>
.
CValue<T>
is an opaque type, so the structure fields cannot be accessed with the appropriate Kotlin properties. It should be possible, if an API uses structures as handles, but if field access is required, there are the following conversion methods available:
fun T.readValue(): CValue<T>
. Converts (the lvalue)T
to aCValue<T>
. So to construct theCValue<T>
,T
can be allocated, filled, and then converted toCValue<T>
.CValue<T>.useContents(block: T.() -> R): R
. Temporarily places theCValue<T>
to memory, and then runs the passed lambda with this placed valueT
as receiver. So to read a single field, the following code can be used:val fieldValue = structValue.useContents { field }
Callbacks
To convert a Kotlin function to a pointer to a C function, staticCFunction(::kotlinFunction)
can be used. It is also able to provide the lambda instead of a function reference. The function or lambda must not capture any values.
Pass user data to callbacks
Often C APIs allow passing some user data to callbacks. Such data is usually provided by the user when configuring the callback. It is passed to some C function (or written to the struct) as e.g. void*
. However, references to Kotlin objects can't be directly passed to C. So they require wrapping before configuring the callback and then unwrapping in the callback itself, to safely swim from Kotlin to Kotlin through the C world. Such wrapping is possible with StableRef
class.
To wrap the reference:
where the voidPtr
is a COpaquePointer
and can be passed to the C function.
To unwrap the reference:
where kotlinReference
is the original wrapped reference.
The created StableRef
should eventually be manually disposed using the .dispose()
method to prevent memory leaks:
After that it becomes invalid, so voidPtr
can't be unwrapped anymore.
See the samples/libcurl
for more details.
Macros
Every C macro that expands to a constant is represented as a Kotlin property. Other macros are not supported. However, they can be exposed manually by wrapping them with supported declarations. E.g. function-like macro FOO
can be exposed as function foo
by adding the custom declaration to the library:
Portability
Sometimes the C libraries have function parameters or struct fields of a platform-dependent type, e.g. long
or size_t
. Kotlin itself doesn't provide neither implicit integer casts nor C-style integer casts (e.g. (size_t) intValue
), so to make writing portable code in such cases easier, the convert
method is provided:
where each of type1
and type2
must be an integral type, either signed or unsigned.
.convert<${type}>
has the same semantics as one of the .toByte
, .toShort
, .toInt
, .toLong
, .toUByte
, .toUShort
, .toUInt
or .toULong
methods, depending on type
.
The example of using convert
:
Also, the type parameter can be inferred automatically and so may be omitted in some cases.
Object pinning
Kotlin objects could be pinned, i.e. their position in memory is guaranteed to be stable until unpinned, and pointers to such objects inner data could be passed to the C functions. For example
Here we use service function usePinned
, which pins an object, executes block and unpins it on normal and exception paths.
Forward declarations
To import forward declarations, use the cnames
package. For example, to import a cstructName
forward declaration declared in a C library with a library.package
, use a special forward declaration package: import cnames.structs.cstructName
.
Consider two cinterop libraries: one that has a forward declaration of a struct and another with an actual implementation in another package:
To transfer objects between the two libraries, use an explicit as
cast in you Kotlin code: