Date: Wed, 7 Sep 2005 22:17:36 +0200 From: benedikt-grundmann at web.de To: romildo at uber.com.br Cc: lablgtk at math.nagoya-u.ac.jp Subject: Re: Help in C interface Message-ID: <20050907201736.GA4858 at localhost.localdomain> References: <20050907192141.GA7177 at malaquias> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20050907192141.GA7177 at malaquias> Sender: benedikt-grundmann at web.de Use OCaml's Bigarrays. Let me cite the manual: Large, multi-dimensional, numerical arrays. This module implements multi-dimensional arrays of integers and floating-point numbers, thereafter referred to as ``big arrays''. The implementation allows efficient sharing of large numerical arrays between Caml code and C or Fortran numerical libraries. Cheers, Bene On Wed, Sep 07, 2005 at 04:21:41PM -0300, romildo@uber.com.br wrote: > Hello. > > I am just trying to build an OCaml interface to > the gtkdatabox (http://www.eudoxos.de/gtk/gtkdatabox/) > C library. > > This library has the function: > > gint gtk_databox_data_add (GtkDatabox * box, guint length, gfloat * X, > gfloat * Y, GdkColor color, > guint type, guint size); > > which is used to add two arrays of float values > (X and Y) to the list of data of the GtkDatabox > (these are the data plotted by GtkDatabox). > GtkDatabox will not copy or backup the data. The > user has to make sure that X and Y pointers will > stay valid until the data is removed from the > GtkDatabox or until the user destroys the > GtkDatabox itself. > > I am having difficults in making an interface > for this function in OCaml, because it seems > that it is not possible to construct a C > array in OCaml. So the two arrays would be > constructed in C. Then I do not know how to > control their lifetimes. > > The following attempt is not good because > of the memory leaks: > > CAMLprim value ml_gtk_databox_data_add(value box, > value length, value X, value Y, > value color, value type, value size) > { > const gint n = Int_val(length); > double vetx = malloc(n * sizeof float); // memory leak > double vety = malloc(n * sizeof float); // memory leak > int i; > for (i = 0; i < n; i++) > { > vetx[i] = Float_field(X,i); > vety[i] = Float_field(Y,i); > } > return Val_int(gtk_databox_data_add(GtkDatabox_val(box), > n, vetx, vety, > GdkColor_val(color), > Int_val(type), > Int_val(size))); > } > > Note this function can be made better, but > the memory leak will be there. > > Any clues on how to solve this problem? > > Romildo