Hi,
I wanted to know if it was possible to convert back a cstring to a string.
Specifically, I'm binding to sqlite for user functions and when the user function returns a string, it provides a cstring as result value and a destructor callback called when the string is no longer used by sqlite in order to avoid copies. My idea was to call GC_ref before and set the destructor callback to call GC_unref on the string.
However, the callback is given a void* pointer which I can easily cast to const char* pointer, the cstring but is it possible to get the string back to I can call GC_unref on it?
Thank you.
$myCString
You can do it with address arithmetic, maybe. You need to know the offset (8?), subtract that from the char* pointer and cast it to ptr StringDesc (pointer) and then create a string from it, strings have a word sized length and then a payload pointer.
Or something like that, I'm too lazy to lookup the string layout. :-)
It would be easier with actual function signatures.
The GC_ref / GC_unref might only work for --mm:refc, unsure about --mm:arc
I would just use $myCString and worry about zero-copy if allocation becomes a bottleneck that shows up in profiling. As you are using a DB chances are that IO is the bottleneck.
And even if allocation becomes a bottleneck, maybe you will introduce a cache in front of DB queries and all of that disappear.
From what you're saying, the tradeoff in readability and debuggability don't seem worth it at the moment.