From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 2DB7CBB9C for ; Fri, 20 Jan 2006 11:49:27 +0100 (CET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id k0KAnQNI031475 for ; Fri, 20 Jan 2006 11:49:27 +0100 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id LAA24614 for ; Fri, 20 Jan 2006 11:49:26 +0100 (MET) Received: from [128.93.8.130] (macadam.inria.fr [128.93.8.130]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id k0KAnQKS031468 for ; Fri, 20 Jan 2006 11:49:26 +0100 Mime-Version: 1.0 (Apple Message framework v746.2) In-Reply-To: <17359.42955.268490.985894@karryall.dnsalias.org> References: <20060119.093955.97297811.garrigue@math.nagoya-u.ac.jp> <1137640656.8943.183.camel@rosella> <02FDC6F0-122C-42BA-A2F4-15E2B08248C8@inria.fr> <17359.42955.268490.985894@karryall.dnsalias.org> Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <0E70B6E0-7C47-40FD-B5C7-51F497847047@inria.fr> Content-Transfer-Encoding: 7bit From: Damien Doligez Subject: Re: [Caml-list] C interface style question Date: Fri, 20 Jan 2006 11:49:30 +0100 To: caml users X-Mailer: Apple Mail (2.746.2) X-Miltered: at nez-perce with ID 43D0C036.003 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 43D0C036.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; damien:01 damien:01 caml-list:01 andrieu:01 ocaml:01 doubles:01 aligned:01 ocaml's:01 config:01 ifdef:01 ifdef:01 pointer:01 wrote:01 wrote:01 doligez:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 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. Thomas wanted an "out" parameter, and the above code is for an "in" parameter, but it should work if you make the obvious changes. On Jan 19, 2006, at 16:15, Thomas Fischbacher wrote: > Is it (= will it always be) permissible to nest Field / Store_field > macros? If you are not doing some strange things, the Store_field will always be outermost, and some Fields will be nested inside it. In that case, it is safe. Otherwise, you're using C macros with arguments that have side-effects, and your program will never work anyway :-) -- Damien