caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
To: la@iki.fi
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] NULL as a caml value
Date: Mon, 13 May 2002 16:20:35 +0900	[thread overview]
Message-ID: <20020513162035N.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: <20020512234522.A5687@kruuna.Helsinki.FI>

From: Lauri Alanko <la@iki.fi>

> The docs weren't very clear on this, so I just wanted to check: is
> (value)NULL a legal caml value? Checking from the sources, it seems that the
> GC does leave alone all pointers that don't point to within the caml-managed
> heap. However, the docs state that only "memory allocated by malloc" can be
> turned directly into an abstract caml value. So may I rely on NULL working?
> It would ease dealing with many C interfaces.

Yes, NULL is valid, as are all word-aligned addresses outside of the
Caml heap.
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.
Better to limit direct pointers to structure that are not going to be
free-ed, and use boxed ones otherwise.

Cheers,

Jacques Garrigue
-------------------
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


  reply	other threads:[~2002-05-13  7:21 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 [this message]
2002-05-13 10:05   ` Christopher Quinn
2002-05-13 10:56     ` Jacques Garrigue
2002-05-13 19:47   ` Lauri Alanko
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=20020513162035N.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=la@iki.fi \
    /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;
as well as URLs for NNTP newsgroup(s).