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 concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 55CF1BB9C for ; Thu, 19 Jan 2006 15:09:00 +0100 (CET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id k0JE8xRE024215 for ; Thu, 19 Jan 2006 15:09:00 +0100 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id PAA06816 for ; Thu, 19 Jan 2006 15:08:59 +0100 (MET) Received: from [128.93.8.130] (macadam.inria.fr [128.93.8.130]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id k0JE8xiN024208 for ; Thu, 19 Jan 2006 15:08:59 +0100 Mime-Version: 1.0 (Apple Message framework v746.2) In-Reply-To: <1137640656.8943.183.camel@rosella> References: <20060119.093955.97297811.garrigue@math.nagoya-u.ac.jp> <1137640656.8943.183.camel@rosella> Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <02FDC6F0-122C-42BA-A2F4-15E2B08248C8@inria.fr> Content-Transfer-Encoding: 7bit From: Damien Doligez Subject: Re: [Caml-list] C interface style question Date: Thu, 19 Jan 2006 15:09:02 +0100 To: caml users X-Mailer: Apple Mail (2.746.2) X-Miltered: at concorde with ID 43CF9D7C.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 43CF9D7B.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; damien:01 damien:01 caml-list:01 mytype:01 mytype:01 explicitely:01 val:01 allocations:01 bool:01 allocations:01 ocaml:01 wrote:01 wrote:01 doligez: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 04:17, skaller wrote: > MyType *p = ... > (MyType*)Field(v,n) = p; > > it isn't valid ISO C for *any* type MyType (not even 'value'). Indeed. My K&R explicitely says: "a cast does not yield an l-value". > You would have to do it like this: > > *(MyType**)(void*)&Field(v,n) = p; // ** You should only do this if you want strange GC bugs and random crashes, because you are walking around the write barrier and that's a bad idea. > However I strongly recommend instead > > StoreField(v,n,(value)(void*)p); This is the only way. Always use Store_field, Store_double_field, and Store_double_val. On Jan 19, 2006, at 13:13, Thomas Fischbacher wrote: > On Thu, 19 Jan 2006, Jacques Garrigue wrote: > >> Registration is required to have the GC properly update the values. >> The GC may be called by any allocation. > > Just by allocations in the local C code? Is this (and will this always > be) a guaranteed property? How about future extensions and > multithreading? Any allocation performed during the execution of your code. Including context switches and signals, if you're doing begin_blocking_section. >> So it is only safe not to register a parameter (or a variable) in any >> of the following 4 cases. >> 1) you know that it can only hold a non-pointer value (int, >> bool, ...) >> (i.e. the GC has nothing to update) >> 2) there are no allocations in your function >> 3) the parameter is not accessed after the first allocation >> 4) for a new variable whose contents is returned, there is no >> allocation between the setting of the variable and return. >> >> [...] > > Can I take this as an official position of the OCaml team on the > behaviour > of the C interface? The official position is: none of these is guaranteed to hold in future releases. You should play it safe and register all your local variables. If you really need all the speed you can get, you might use these tricks in the two functions where your program spends all its time, but I'd still advise against it. -- Damien