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
 Thomas Fischbacher [Thursday 19 January 2006] :
 >
 > 
 > On Thu, 19 Jan 2006, Damien Doligez wrote:
 > 
 > > > StoreField(v,n,(value)(void*)p);
 > > 
 > > This is the only way.  Always use Store_field,
 > > Store_double_field, and Store_double_val.
 > 
 > 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
`----

(And the part that "uses c_array" must not make any (caml) allocation)

-- 
   Olivier