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


1. 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-table-new-from-list - https://developer.gnome.org/gtk3/stable/gtk3-Selections.html#gtk-target-table-free
_______________________________________________
polyml mailing list
polyml@inf.ed.ac.uk
http://lists.inf.ed.ac.uk/mailman/listinfo/polyml

Reply via email to