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 8C5EDBB9C for ; Thu, 19 Jan 2006 04:17:43 +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 k0J3HgUR001741 for ; Thu, 19 Jan 2006 04:17:43 +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 EAA30294 for ; Thu, 19 Jan 2006 04:17:42 +0100 (MET) Received: from ash25e.internode.on.net (ash25e.internode.on.net [203.16.214.182]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id k0J3He0Y012279 for ; Thu, 19 Jan 2006 04:17:41 +0100 Received: from rosella (ppp21-250.lns2.syd7.internode.on.net [59.167.21.250]) by ash25e.internode.on.net (8.13.5/8.13.5) with ESMTP id k0J3HajA065695; Thu, 19 Jan 2006 13:47:37 +1030 (CST) (envelope-from skaller@users.sourceforge.net) Subject: Re: [Caml-list] C interface style question From: skaller To: Jacques Garrigue Cc: Thomas.Fischbacher@Physik.Uni-Muenchen.DE, caml-list@inria.fr In-Reply-To: <20060119.093955.97297811.garrigue@math.nagoya-u.ac.jp> References: <20060119.093955.97297811.garrigue@math.nagoya-u.ac.jp> Content-Type: text/plain Date: Thu, 19 Jan 2006 14:17:36 +1100 Message-Id: <1137640656.8943.183.camel@rosella> Mime-Version: 1.0 X-Mailer: Evolution 2.4.1 Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 43CF04D6.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 43CF04D4.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 ocaml:01 pointers:01 bool:01 bool:01 allocations:01 mistaken:01 abstraction:01 ocaml:01 hendrik:01 tews:01 tews:01 3.09:01 mytype:01 mytype: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 Thu, 2006-01-19 at 09:39 +0900, Jacques Garrigue wrote: > From: Thomas Fischbacher > > > value-type parameters to C functions exported to OCaml should be > > registered with CAMLparamX(...). Does this hold in general, or is it > > considered acceptable/appropriate to just ignore this for "immediate" > > values that do not hold pointers, but, say, int, bool etc. values? > > Registration is required to have the GC properly update the values. > The GC may be called by any allocation. > 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. > > (1) and (2) are relatively easy to see, but (3) and (4) are a bit > trickier (particularly with side-effecting expressions), so > it is not a bad idea to register more parameters than strictly > necessary. Unless I'm mistaken, 'registration' with these macros is never required: these macros are simply a high level abstraction layer providing convenience and relative safety. The Ocaml manual explains all this fairly well IMHO, the low level interface is well documented, Hendrik Tews version is cool: http://wwwtcs.inf.tu-dresden.de/~tews/htmlman-3.09/manual032.html See 18.5.2 -- IMHO the low level interface, whilst requiring more work to apply, is actually easier to understand. Just one 'BTW': I have seen some code using Field() macro with incorrect C. You must NOT do this: MyType *p = ... (MyType*)Field(v,n) = p; it isn't valid ISO C for *any* type MyType (not even 'value'). You would have to do it like this: *(MyType**)(void*)&Field(v,n) = p; // ** However I strongly recommend instead StoreField(v,n,(value)(void*)p); The incorrect usage was not detected by older versions of gcc. Gcc 4.x does detect this error. The workaround (**) is the ONLY correct way to cast an lvalue in ISO C. This problem arises in some larger codes where a macro has been used to do the type conversion .. and it appeared to work but was in fact invalid C. For example: #define MyThing(v) (MyType*)Field(v,2) is not a good idea, since MyThing(v) = ... is invalid ISO C but may not even produce a warning. You would have to instead say: #define MyThing(v) (*(MyType**)(void*)&Field(v,2)) but it seems better to rewrite your code so values and lvalues are distinguished by usage (eg 'get' and 'set' macros). [The intention of the macros is usually to abstract away the field number] -- John Skaller Felix, successor to C++: http://felix.sf.net