From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id QAA27405; Thu, 31 Jan 2002 16:05:42 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f 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 QAA28590 for ; Thu, 31 Jan 2002 16:05:41 +0100 (MET) Received: from mail.cs.net.pl (cyber.cs.net.pl [62.233.142.98]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g0VF5aD10265 for ; Thu, 31 Jan 2002 16:05:40 +0100 (MET) Received: by mail.cs.net.pl (Postfix, from userid 510) id 8414351F; Thu, 31 Jan 2002 16:05:29 +0100 (CET) Date: Thu, 31 Jan 2002 16:05:29 +0100 From: Tomasz Zielonka To: Markus Mottl Cc: CAML list Subject: Re: [Caml-list] Using C values in ocaml code Message-ID: <20020131150529.GA15230@cs.net.pl> References: <20020131101112.GA30020@cs.net.pl> <20020131111251.GA27804@chopin.ai.univie.ac.at> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20020131111251.GA27804@chopin.ai.univie.ac.at> User-Agent: Mutt/1.3.25i Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Thu, Jan 31, 2002 at 12:12:51PM +0100, Markus Mottl wrote: > You can't just cast 'x' to an OCaml-value: if it is an integer, you'll > have to use the "Val_int"-macro. Otherwise the integer might exceed the > range allowed by OCaml and could be interpreted as a pointer by the GC, > possibly resulting in a crash (integers need to be tagged). It's even more fun. Odd ints would be interpreted as ints with halved value, even ints as pointers. from mlvalues.h: #define Is_long(x) (((x) & 1) != 0) #define Long_val(x) ((x) >> 1) #define Int_val(x) ((int) Long_val(x)) > If it is a pointer to something allocated by C, you could return it > directly, but this may cause troubles if you deallocate this region of > memory while the value is still reachable from the OCaml-world (read > section 17.2.3 to learn what can happen). Both - an integer or a pointer. It is not always possible to check whether it is a pointer or an integer not knowing the context. > Therefore you should use a more appropriate encoding. If you don't want > the GC to notify your C-program that the value is not reachable from your > program anymore (i.e. can be finalized/deallocated), then just allocate > a block tagged abstract of size 1, e.g: I will use abstract block then. Thanks, Tom -- .-. Tomasz Zielonka CYBER SERVICE oo| programista http://www.cs.net.pl /`'\ zielony@cs.net.pl (\_;/) tel: [48] (22) 723-06-79 | tel/fax: [48] (22) 723-01-75 ------------------- Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr