Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Lauri Alanko <la@iki.fi>
To: caml-list@inria.fr
Subject: Re: [Caml-list] NULL as a caml value
Date: Mon, 13 May 2002 22:47:57 +0300	[thread overview]
Message-ID: <20020513224757.A18546@kruuna.Helsinki.FI> (raw)
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

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


  parent reply	other threads:[~2002-05-14  7:41 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-05-12 20:45 Lauri Alanko
2002-05-13  7:20 ` Jacques Garrigue
2002-05-13 10:05   ` Christopher Quinn
2002-05-13 10:56     ` Jacques Garrigue
2002-05-13 19:47   ` Lauri Alanko [this message]
2002-05-13 14:56 Damien Doligez

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20020513224757.A18546@kruuna.Helsinki.FI \
    --to=la@iki.fi \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox