jw
10/18/2021, 5:07 PMCValuesRef
to the leading item + item count to a C function? I've done with things like ByteArray where I can call refTo(0)
but I don't see an explanation or example of how to do this with a struct type.mbonnin
10/18/2021, 5:09 PMjw
10/18/2021, 6:19 PMjw
10/18/2021, 6:19 PMjw
10/18/2021, 6:19 PMval args = allocArray<Thing>(4)
arg0.useContents {
args[0].tag = tag
args[0].u.int32 = u.int32
args[0].u.float64 = u.float64
}
// etc.
jw
10/18/2021, 6:20 PMmbonnin
10/18/2021, 6:29 PMmbonnin
10/18/2021, 6:30 PMvoid myfunction(struct thing * input_output_param, int *size)
mbonnin
10/18/2021, 6:32 PMGavin Ray
10/19/2021, 6:29 PMtemplate<typename T>
T *allocArray(int length) {
auto ptr = reinterpret_cast<T *>(malloc(sizeof(T) * length));
if (ptr == nullptr) {
fprintf(stderr, "Failed to allocate array of T (length=%d).\n", length);
return nullptr;
}
return ptr;
}
And then uses it like you see in ParameterChanges.kt
(or other places if you search for allocArray
)Gavin Ray
10/19/2021, 6:29 PMjw
10/20/2021, 4:20 AMinternal inline operator fun <reified T : CVariable> CPointer<T>.set(index: Int, item: CValues<T>) {
val offset = index * sizeOf<T>()
item.place(interpretCPointer(rawValue + offset)!!)
}
internal inline fun <reified T : CVariable> NativePlacement.allocArrayOf(
vararg items: CValues<T>,
): CArrayPointer<T> {
val array = allocArray<T>(items.size)
items.forEachIndexed { index, item ->
array[index] = item
}
return array
}
jw
10/20/2021, 4:20 AMGavin Ray
10/21/2021, 3:37 PM