I have library, that has function with header like...
# kotlin-native
m
I have library, that has function with header like
int func(const char* src...);
that I use with cinterop. I want to pass inside
CPointer<ByteVar>
or
CPointer<UByteVar>
as a parameter (because char* is pointer to char) that a aquired from previous native call. However the function has in kotlin binding header
public fun func(source: kotlin.String?...
accepting kotlin string. How do I do it? Do I need to call
toKString()
? Doesn't
toKString()
involve some allocation? Does it correctly translates the
CPointer<ByteVar>
buffer to string (without some corruption due to encoding for example)? Isn't it useless step as the KString will have to be translated back to C char array later?
I finally found the sourcecode of toKString() and indeed it does allocate stuff https://github.com/JetBrains/kotlin-native/blob/master/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt#L399.
And the generated function does create .cstr (which also allocates memory) and than it takes pointer from it.
Copy code
fun func(source: String?...): Int {
            memScoped {
                return kniBridge25(source?.cstr?.getPointer(memScope).rawValue ...)
            }
        }
Is there any way to disable this behavior? Because its pretty crazy to allocate two times the same memory just to pass the buffer from one native call to another.
m
noStringConversion = func
in .def file
m
Thank you so much