[Cuis-dev] [FFI] Passing a GC collected record to C Code
Philip Bernhart
philip.bernhart at posteo.de
Sun Jun 23 11:51:29 PDT 2019
Hi,
how do I pass a struct, which is supposed to be
fully lying in the memory space of the VM to some
external C code which then copies data into that
struct and does nothing with it afterwards?
The current approach which I took was:
1. To ExternalStructure subclass: #Record
2. Define fields as: #((buffer 'char *') (length 'ulong'))
3. Define custom builder methods, which make basically
addr := ExternalAddress gcallocate: size.
record := Record new.
record buffer: (ExternalData fromHandle: addr type: ExternalType
char).
record length: size.
^record.
4. Define external function calls like:
myfoo
<cdecl: ulong 'store_something_into_record' (Record *)>
self exetrnalCallFailed.
Looks that basic structure good enough to cause NO memory leaks?
Or double frees? Because the whole point of this exercise was to
ensure that this piece of memory is garbage collected by the VM
and the C code only does with some plugin glue code copying / filling
into that buffer.
Maybe I find the time to put that info into some documentation, as the
FFI is tedious to use than it needs to be.
Thanks,
Philip
More information about the Cuis-dev
mailing list