Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Stephan Houben <stephan@pcrm.win.tue.nl>
To: caml-list@inria.fr
Subject: Is this use of Object.magic safe?
Date: Mon, 13 Nov 2000 09:43:48 +0100	[thread overview]
Message-ID: <00111310044001.28230@pcrm> (raw)

Hello list,

I am currently writing a small interpreted language for my app.
The language is dynamically typed, which means that a value is represented
somewhat like this:

type value =
  True | False
| Int of int
| String of string
| Array of value array

Now a well-known optimisation for such an interpreter is to make small
int's not heap-allocated objects but to represent them directly in the pointer.
In fact, O'Caml itself already does this. Moreover, it allows access to
this info via the Obj module.

My idea is to make "value" an abstract type, and have a separate type
allocated_value, which doesn't contain the Int constructor:

type value
 
type allocated_value =
  True | False
| String of string
| Array of value

Then I can use Obj.magic to make constructor and destructor functions
for value:

let value_of_int (i : int) : value =
  Obj.magic i

let value_of_allocated_value (av :allocated_value) : value=
  Obj.magic av
 
let unwrap (int_func : int -> 'a) 
                (aval_func : allocated_value -> 'a)
                (v : value) : 'a =
  if Obj.is_int (Obj.repr v)
  then int_func (Obj.magic v)
  else aval_func (Obj.magic v) 

I have three questions:
1. Is this safe w.r.t. the garbage collector? Or am I going to confuse the GC?
2. Can I safely marshall values?
3. Is it worth the trouble? Perhaps O'Caml already does something smart
   when a constructor of the form `Int of int' is encountered?

Stephan
-- 
ir. Stephan H.M.J. Houben
tel. +31-40-2474358 / +31-40-2743497
e-mail: stephanh@win.tue.nl



             reply	other threads:[~2000-11-14 14:17 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-11-13  8:43 Stephan Houben [this message]
2000-11-14 21:31 ` Anton Moscal
2000-11-14 14:41 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=00111310044001.28230@pcrm \
    --to=stephan@pcrm.win.tue.nl \
    --cc=caml-list@inria.fr \
    --cc=stephanh@win.tue.nl \
    /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