Hi Phil,
Can't you wrap the array and size in a struct. For example in https://github.com/polyml/polyml/tree/cb1b36caa242fc6ea9f74b015158466efac68d... we have :
--------------------------------- //ForeignTest.c typedef struct _tree { struct _tree *left, *right; int nValue; } *tree;
int SumTree(tree t) { if (t == NULL) return 0; else return t->nValue + SumTree(t->left) + SumTree(t->right); } ---------------------------------
and
--------------------------------- (*ForeignTest.sml*) val sumTree = CInterface.call1 ( CInterface.load_sym mylib "SumTree") TREE INT;
---------------------------------
So you just write a FreeTree function, which is modelled after the FreeIt function in ForeignTest.c :
void FreeIt(void *p) { printf("Freed object at %p\n", p); fflush(stdout); free(p); }
, which then reads nValue and passes it on to the relevant gtk function?
On Mon, Sep 14, 2015 at 10:36 PM, Phil Clayton <phil.clayton at lineone.net> wrote:
I am trying to create an SML binding to a C function that returns an array that the caller must free. Usually, there is a free function that takes just the array (as a pointer) which can be attached as a finalizer with CInterface.setFinal. I have encountered a case [1] where the caller must also pass the size of the array, returned when the array is created, to the free function.
Simplifying the example, we have, for some C type Elem:
Elem *new (..., int *n_elems); /* n_elems is an 'out' parameter */ void free (Elem *elems, int n_elems);
and want an SML function like
val new : ... -> elem vector
Unfortunately, the function given to CInterface.setFinal is called with only one argument, the vol that is being finalized. Therefore this free function cannot be used. Does the current FFI architecture allow a variant of setFinal that passes extra arguments to the finalization function? For example:
val setFinal1 : sym -> 'a Conversion -> vol -> 'a -> unit
This isn't particularly common so is probably not a show-stopper. Another benefit could be enabling use of functions g_slice_alloc and g_slice_free1 that needs the number of bytes to free: https://developer.gnome.org/glib/stable/glib-Memory-Slices.html
Thanks, Phil
- The C function in question is gtk_target_table_new_from_list that
returns an array and its size. The array should be freed with gtk_target_table_free which should be passed the size. See:
https://developer.gnome.org/gtk3/stable/gtk3-Selections.html#gtk-target-tabl...
https://developer.gnome.org/gtk3/stable/gtk3-Selections.html#gtk-target-tabl... _______________________________________________ polyml mailing list polyml at inf.ed.ac.uk http://lists.inf.ed.ac.uk/mailman/listinfo/polyml