czuckie
04/18/2019, 7:26 PMCValue
concept and passing structs by reference to C functions?czuckie
04/18/2019, 7:28 PMvoid do_something(struct_t *boop)
I can pass in the result of cValue<struct_t>()
into the function, but any modifications done to the struct are not carried over
If I use memScoped
and pass the ptr
value in, the same happens.
If I dynamically allocated with alloc<struct_t>()
THEN it worksczuckie
04/18/2019, 7:29 PM&structVar
equivalent would be in kotlinczuckie
04/18/2019, 7:34 PMDominaezzz
04/18/2019, 7:56 PMIf I useShould have worked.and pass thememScoped
value in, the same happens.ptr
Dominaezzz
04/18/2019, 7:57 PMDominaezzz
04/18/2019, 7:57 PMalloc<...>
in a memScoped
block.czuckie
04/18/2019, 7:58 PMczuckie
04/18/2019, 7:59 PMclass MyStructWrapper {
private val arena = Arena()
private val myStruct = arena.alloc<my_struct_t>().ptr
fun doSomething() = c_api_do_something(myStruct)
}
czuckie
04/18/2019, 7:59 PMczuckie
04/18/2019, 7:59 PMDominaezzz
04/18/2019, 8:00 PMDominaezzz
04/18/2019, 8:01 PMmemScoped {
val structVar = alloc<struct_t>()
doSomething(structVar.ptr)
// Do stuff with structVar
}
// structVar is freed.
czuckie
04/18/2019, 8:04 PMczuckie
04/18/2019, 8:05 PMDominaezzz
04/18/2019, 8:05 PMDominaezzz
04/18/2019, 8:06 PMczuckie
04/18/2019, 8:06 PMDominaezzz
04/18/2019, 8:08 PMnativeHeap
.Dominaezzz
04/18/2019, 8:08 PMczuckie
04/18/2019, 8:09 PMczuckie
04/18/2019, 8:09 PMDominaezzz
04/18/2019, 8:10 PMDominaezzz
04/18/2019, 8:10 PMnativeHeap
is the way to go. Just don't forget to call free when you're done with the class. Have a Closeable
interface or something.Dominaezzz
04/18/2019, 8:11 PMval struct = nativeHeap.alloc<struct_t>
Dominaezzz
04/18/2019, 8:11 PMnativeHeap.free(struct)
when you're done.czuckie
04/18/2019, 8:11 PMczuckie
04/18/2019, 8:12 PMcairo_matrix_t
. All my unit tests were generating the correct output per-each sample, except the image pattern oneczuckie
04/18/2019, 8:15 PMCairoMatrix
class, but I couldn't figure out how get things working without dynamic allocation which is doing my nut in! But it's only a play thing for me right now, so I'll persevere... I haven't even gotten to the lemon soaked razors yet 😂Dominaezzz
04/18/2019, 8:16 PMDominaezzz
04/18/2019, 8:17 PMCairoSurface
class?czuckie
04/18/2019, 8:19 PMDominaezzz
04/18/2019, 8:19 PMczuckie
04/18/2019, 8:20 PMczuckie
04/18/2019, 8:20 PMDominaezzz
04/18/2019, 8:21 PMDominaezzz
04/18/2019, 8:22 PMczuckie
04/18/2019, 8:25 PMDominaezzz
04/18/2019, 8:26 PMsvyatoslav.scherbina
04/19/2019, 8:31 AMCValue
for pointer parameter creates a temporary copy of it, because CValue
is immutable.
Taking .ptr
on CValue
does the same for the same reason.
I’ve wrapped the cairo_matrix_t variable in aDoes anything prevent you from havingclass, but I couldn’t figure out how get things working without dynamic allocationCairoMatrix
val value: CValue<cairo_matrix_t>
in your CairoMatrix
class?Dominaezzz
04/19/2019, 8:33 AMCValue<... >
is immutable.svyatoslav.scherbina
04/19/2019, 8:57 AMvar
) could be used.Dominaezzz
04/19/2019, 9:31 AMnativeHeap
underneath and free the memory when garbage collected?svyatoslav.scherbina
04/19/2019, 9:33 AMDominaezzz
04/19/2019, 9:35 AMnativeHeap.alloc<...>
every time the value needs changing (or reading) when a single allocation would do.svyatoslav.scherbina
04/19/2019, 9:38 AMCValue
is stored in the heap. You can’t reuse the same memory for a new value.