From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 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 4D206BC0B for ; Thu, 25 Jan 2007 17:26:24 +0100 (CET) Received: from smeltpunt.science.ru.nl (smeltpunt.science.ru.nl [131.174.16.145]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l0PGQRnR029800 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL) for ; Thu, 25 Jan 2007 17:26:27 +0100 Received: from tandem.cs.ru.nl [131.174.142.18] (helo=tandem.cs.ru.nl) by smeltpunt.science.ru.nl (8.13.7/5.11) with ESMTP id l0PGQMnZ005429 for ; Thu, 25 Jan 2007 17:26:22 +0100 (MET) Received: from tews by tandem.cs.ru.nl with local (Exim 4.63) (envelope-from ) id 1HA7Qc-0005PN-NQ for caml-list@yquem.inria.fr; Thu, 25 Jan 2007 17:26:22 +0100 From: Hendrik Tews To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Interfacing with C question... References: <002d01c73fdc$6254d430$6a7ba8c0@treble> Date: Thu, 25 Jan 2007 17:26:22 +0100 In-Reply-To: <002d01c73fdc$6254d430$6a7ba8c0@treble> (David Allsopp's message of "Wed, 24 Jan 2007 17:23:36 -0000") Message-ID: User-Agent: Gnus/5.110006 (No Gnus v0.6) Emacs/21.4 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Scanned-By: MIMEDefang 2.56 on 131.174.16.145 X-Miltered: at concorde with ID 45B8DA33.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; hendrik:01 tews:01 tews:01 interfacing:01 pointers:01 allocates:01 val:01 val:01 pointers:01 pointer:01 pointer:01 hendrik:01 heap:01 garbage:01 garbage:01 "David Allsopp" writes: Sorry if this an RADBOTFM case. Rule 2 in Chapter 18 of the manual states that all local variables of type value must be declared using CAMLlocal macros. However, later on when demonstrating caml_callback we get the statements: value* format_result_closure = caml_named_value("format_result"); Note the type! format_result_closure is not of type value, Rule 2 does not apply! 1. Presumably it's OK to cache values returned by caml_named_value without declaring them in a CAMLlocal "call" or by using register_global_root? Yes it is OK. And you cannot use CAMLlocal or register_global_root, because they only deal with values and not pointers to values. The manual guarantees that the value pointed to by the result of caml_named_value doesn't move. Probably caml_named_value allocates a value outside the heap, registers it as a global root and gives you back its address. value result = caml_callback(*format_result_closure, Val_int(n))); return strdup(String_val(result)); then does that work ok without using CAMLlocal1(result); Yes. You should think of values as pointers that point to data that is moved around by the garbage collector. If there is any chance that the garbage collector is called, then you must make sure that it updates your pointer when it moves the data. Hence you have to register the value. If the garbage collector is not called under any circumstances then the data will not move, the pointer doesn't need to get updated and you don't have to register the value. Furthermore, if your are sure Is_long(your_variable) is true under any circumstances, you don't have to register your_variable, because it is not a pointer. Of course all that is strongly discouraged. Bye, Hendrik