Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Tomasz Zielonka <zielony@cs.net.pl>
To: Markus Mottl <markus@oefai.at>
Cc: CAML list <caml-list@inria.fr>
Subject: Re: [Caml-list] Using C values in ocaml code
Date: Thu, 31 Jan 2002 16:05:29 +0100	[thread overview]
Message-ID: <20020131150529.GA15230@cs.net.pl> (raw)
In-Reply-To: <20020131111251.GA27804@chopin.ai.univie.ac.at>

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


  reply	other threads:[~2002-01-31 15:05 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-01-31 10:11 Tomasz Zielonka
2002-01-31 11:12 ` Markus Mottl
2002-01-31 15:05   ` Tomasz Zielonka [this message]
2002-01-31 15:23     ` Markus Mottl

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=20020131150529.GA15230@cs.net.pl \
    --to=zielony@cs.net.pl \
    --cc=caml-list@inria.fr \
    --cc=markus@oefai.at \
    /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