David Matthews wrote:
... I was thinking that this would be an ML function (which might call a C function) but if it's a C function then it could indeed be called during the GC. In that case the get/set functions return/take a "vol" to denote the finaliser and adding it wouldn't be too difficult.
... maybe for *you*. ;)
It was fairly easy to make the requisite changes to foreign.cpp to support a "set_finalize" function (well, at least something I would like to test)... but getting everything else set up properly in ML-land to actually get it *used* is proving elusive for the moment.
I am (for now) goint with a simple
val set_finalize: vol -> sym -> unit
which sets a new
void (*C_finalizer)(void*);
element in the Volatile struct of the vol with the C function pointer from the sym. This value is checked when a NON-owned vol is being collected, and if != NULL, the C function is called with the C_pointer of the vol passed to it.
Note that I am at least for the moment not trying to do a "get_finalize"... I don't have a use for it, I am note sure it really is needed in general - due at least in part to the somewhat transient, or "volatile", lifespan of the FFI linkages. Also, it would be more complicated, since instead of just grabbing the C function pointer from the sym and saving *that* value in the Volatile struct, I would need to save a reference to the sym itself and then dereference that every time I need to invoke a finalizer in the middle of a GC.
But after that, I am a bit adrift - without fully understanding the model used by Poly to add a new primitive *and* have the compiler actually link to my function in foreign.cpp, I am not achieving the desired results.
While I am of course attempting to use some of the existing FFI code as examples of how to do this (the "trees"), I don't fully grasp the underlying idea(s) (the "forest")... and so am adding various definitions of "set_finalize" into a number of extra/CInterface/*.ML files - with "mixed results". :|
Could you outline the *minimum* changes to SML structures and signatures to connect a
CInterface set_finalize vol -> sym -> unit
function to (I think)
static Handle set_finalize (TaskData *taskData, Handle h)
in foreign.cpp (assuming it will go in at the end of the "handlers" array)?
Thanks!
Robert