Version française
Home     About     Download     Resources     Contact us    
Browse thread
C interface style question
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Olivier Andrieu <oandrieu@n...>
Subject: Re: [Caml-list] C interface style question
Replying to a somehow old message here ...

 Damien Doligez [Friday 20 January 2006] :
 >
 > On Jan 19, 2006, at 15:52, Olivier Andrieu wrote:
 > 
 > >  Thomas Fischbacher [Thursday 19 January 2006] :
 > >> One more question about this: can I interface a C function in such
 > >> a way that it uses an OCaml float array to store its output data,
 > >> i.e. pass &(Double_field(ml_output,0)) as a double* "output
 > >> parameter"?
 > >
 > > You can only do this on platform that accept doubles aligned on word
 > > boundaries (such as x86). On those platforms, OCaml's config.h
 > > undefines ARCH_ALIGN_DOUBLE. So your code might look like this :
 > >
 > > ,----
 > > |   double *c_array;
 > > | #ifdef ARCH_ALIGN_DOUBLE
 > > |   c_array = /* allocate temporary storage and copy the caml float  
 > > array */
 > > | #else
 > > |   c_array = (double *) ml_array;
 > > | #endif
 > > |
 > > |   /* use c_array */
 > > |
 > > | #ifdef ARCH_ALIGN_DOUBLE
 > > |   free (c_array);
 > > | #endif
 > > `----
 > 
 > This is correct, except for the way you get the pointer.  You should  
 > do it
 > properly with &(Double_field(ml_array,0)).  There is no guarantee that
 > the value ml_array points to the first element of the array.

Err... I don't get it.

in mlvalues.h, I have (with ARCH_ALIGN_DOUBLE undefined) :

  #define Double_val(v) (* (double *)(v))
  #define Double_field(v,i) Double_val((value)((double *)(v) + (i)))

so &(Double_field(ml_array, 0)) looks pretty equivalent to a simple
cast to double*, no ? 

Also how would it happen that ml_array does not point to first element
of the array ?

-- 
   Olivier