I got it to work by doing this, It feels like I am...
# kotlin-native
n
I got it to work by doing this, It feels like I am still not doing it correctly. Any help would be appreciated.
Copy code
fun main() {
    memScoped {
        val pointer = cValuesOf(0U).getPointer(memScope)
        val result = clGetPlatformIDs(0, null, pointer)
        if (CL_SUCCESS == result) {
            println("Detected OpenCL platforms: ${pointer[0]}")
        } else {
            println("Error calling clGetPlatformIDs. Error code: $result");
        }
    }
}
n
This might be incorrect but should be along the lines of what you want:
Copy code
fun main() {
    memScoped {
        val numPlatformsPtr = alloc<UIntVar>()
        val result = clGetPlatformIDs(num_entries = 0, platforms = null, num_platforms = pointer)
        if (CL_SUCCESS == result) {
            println("Detected OpenCL platforms: ${numPlatformsPtr?.pointed}")
        } else {
            println("Error calling clGetPlatformIDs. Error code: $result");
        }
    }
}
In the code example ☝️ the pointer (numPlatformsPtr) is allocated memory (of type UIntVar) through the
alloc
function. The pointer is passed as is to the
clGetPlatformIDs
function, which initialises the pointer with a value. If the call to ``clGetPlatformIDs`` is successful then the pointer's value is printed via the
pointed
extension property.
👍 1
n
That doesn't appear to work. Type mismatch when calling
clGetPlatformIDs
. The 3rd argument of that method is of type
CValuesRef<cl_uintVar>
. Whereas
alloc<cl_uintVar>
gives a
cl_uintVar
. It seems to work if we use
allocArray<cl_uitVar>
although I am little confused as to why.
Copy code
fun main() {
    memScoped {
        val numPlatformsPtr = allocArray<cl_uintVar>(1)
        val result = clGetPlatformIDs(0, null, numPlatformsPtr)
        if (CL_SUCCESS == result) {
            println("Detected OpenCL platforms: ${numPlatformsPtr[0]}")
        } else {
            println("Error calling clGetPlatformIDs. Error code: $result");
        }
    }
}
m
You did it correctly the first time I am pretty sure. No need to allocate memory on the heap for that (This is no JVM 🙂). How does it feel wrong to you?
n
The original kotlin code doesn't seem equivalent to the C code. I would think it possible to write code that is the same.
m
As long as Kotlin isn't C it will never be identical. Kotlin just doesn't expose the same concept like pointers so you need to make calls to the runtime for that. Besides that theyre identical
👍 1
n
This line (in the code snippet - https://kotlinlang.slack.com/archives/C3SGXARS6/p1577740299112900?thread_ts=1577730476.112600&amp;cid=C3SGXARS6 ) can be fixed by replacing
val result = clGetPlatformIDs(num_entries = 0, platforms = null, num_platforms = numPlatformsPtr)
with
val result = clGetPlatformIDs(num_entries = 0, platforms = null, num_platforms = numPlatformsPtr.ptr
. The numPlatformsPtr should be renamed to numPlatforms to avoid confusion since it is a CPointerVar ( https://kotlinlang.org/api/latest/jvm/stdlib/kotlinx.cinterop/-c-pointer-var.html ), and not a CPointer. Can get a CPointer from CPointerVar via the
ptr
extension property. CValuesRef ( https://kotlinlang.org/api/latest/jvm/stdlib/kotlinx.cinterop/-c-values-ref/index.html ) can take a CPointer by itself ( https://github.com/JetBrains/kotlin-native/blob/master/INTEROP.md#passing-pointers-to-bindings ), "When passing 
CPointer<T>
 as the value of such a parameter, it is passed to the C function as is"
.
👍 1
n
Yep. That has worked a treat. Thanks @napperley