Hi Artella,
Thanks - I wondered about that but it is quite a bit of effort by
comparison because I am creating a framework for automatically
generating bindings rather than writing some bindings by hand. When
calling a C function that takes the array pointer, the struct pointer
would be passed from ML, so it would be necessary to wrap the C function
to dereference the struct pointer to get the array pointer. I would
want to do that dereferencing on the ML side (by manipulating vols) to
avoid having to generate C function wrappers. That's not too much
effort. Also, as you say, a free function would be needed but that
would need to be generated I don't really want to extend the framework
to do that - currently I don't generate any C code.
In MLton, creating a finalizable value from the pointer and size is
simple. Roughly as follows:
fun fromNewPtr p n =
let
val array = Finalizable.new p
in
Finalizable.addFinalizer (t, fn p => free_ (p, n));
array
end
where free_ is the C free function. If Poly/ML can use the same or
similar code, that would be much easier!
Regards,
Phil
15/09/15 15:27, Artella Coding wrote:
Hi Phil,
Can't you wrap the array and size in a struct. For example in
https://github.com/polyml/polyml/tree/cb1b36caa242fc6ea9f74b015158466efac68d66/mlsource/extra/CInterface/Examples
we have :
---------------------------------
//ForeignTest.c
typedef struct _tree {
struct _tree *left, *right;
intnValue;
} *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 <[email protected]
<mailto:[email protected]>> 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
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
[email protected] <mailto:[email protected]>
http://lists.inf.ed.ac.uk/mailman/listinfo/polyml
_______________________________________________
polyml mailing list
[email protected]
http://lists.inf.ed.ac.uk/mailman/listinfo/polyml