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 JAA09785; Tue, 14 May 2002 09:41:36 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id JAA09780 for caml-list@pauillac.inria.fr; Tue, 14 May 2002 09:41:35 +0200 (MET DST) 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 VAA25630 for ; Mon, 13 May 2002 21:48:01 +0200 (MET DST) Received: from kruuna.Helsinki.FI (kruuna.helsinki.fi [128.214.205.14]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id g4DJm0r10129 for ; Mon, 13 May 2002 21:48:00 +0200 (MET DST) Received: (from lealanko@localhost) by kruuna.Helsinki.FI (8.11.6/8.10.1) id g4DJm0F21568; Mon, 13 May 2002 22:48:00 +0300 (EEST) Date: Mon, 13 May 2002 22:47:57 +0300 From: Lauri Alanko To: caml-list@inria.fr Subject: Re: [Caml-list] NULL as a caml value Message-ID: <20020513224757.A18546@kruuna.Helsinki.FI> References: <20020512234522.A5687@kruuna.Helsinki.FI> <20020513162035N.garrigue@kurims.kyoto-u.ac.jp> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i In-Reply-To: <20020513162035N.garrigue@kurims.kyoto-u.ac.jp>; from garrigue@kurims.kyoto-u.ac.jp on Mon, May 13, 2002 at 04:20:35PM +0900 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Mon, May 13, 2002 at 04:20:35PM +0900, Jacques Garrigue wrote: > Yes, NULL is valid, as are all word-aligned addresses outside of the > Caml heap. This probably ought to be mentioned in the manual... Unless there's a perlish "the implementation is the specification" -attitude with ocaml. > Of course it should be given an abstract type, since there is nothing > you can do with it inside Caml. > > Note however that the statement about "memory allocated by malloc" > being safe is to be taken carefully, since people have already > reported strange bugs with the following scenario: > > * allocate a data structure with malloc > * get a direct pointer to it in caml > * free the structure > * expand the caml heap, unfortunately including the free-ed malloc block > * your pointer is now seen as a pointer inside the caml-heap, > and followed by the GC, directly into a segmentation fault... > > This is a rather improbable scenario, but better know it in advance. The moment you free a structure without being absolutely certain that there are no references to it anywhere in caml world, you are already asking for trouble, so this isn't much of an additional setback. > Better to limit direct pointers to structure that are not going to be > free-ed, and use boxed ones otherwise. Actually, I was going to use abstract-tagged blocks. I noticed, though, that allocating them is a bother, since you have to calculate the size in fields manually: (size + sizeof(value) - 1) / sizeof(value). An allocation function for abstract blocks that takes a size in _chars_ would be a convenient addition to alloc.h. Or even a macro that takes a type t and returns a block of sizeof(t) casted to t*. Also, there seems to be no direct way of getting the actual raw data pointer for an abstract value v. Of course you can do (t*)v, but is that "portable" in the sense of guaranteed by the FFI? I noted that in mlvalues.h there is a macro Bp_val(v) that returns the pointer to data as char*, but this is not mentioned in the manual, so I'm wondering whether this is a part of the FFI or an implementation detail? The Correct way to do this at the moment seems to be (t*)&Field(v, 0), which is not very clear and kind of lengthy. I guess I'm just being a bit pedantic, coming from the Haskell world where the FFI has (soon) a real implementation-independent specification... Lauri Alanko la@iki.fi ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners