From: Richard Jones <rich@annexia.org>
To: micha <micha-1@fantasymail.de>
Cc: OCaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] allocating memory for c-structures
Date: Tue, 26 Dec 2006 18:35:48 +0000 [thread overview]
Message-ID: <20061226183547.GA29273@furbychan.cocan.org> (raw)
In-Reply-To: <459166C7.4080005@fantasymail.de>
On Tue, Dec 26, 2006 at 07:15:35PM +0100, micha wrote:
> Normaly I allocate memory for c-structures with malloc or with "new" for
> c++ objects. Some time ago a read about a library which places external
> structures in strings of the interfacing languages (it was a scheme lib
> I think). So instead of using malloc or new I would allocate an
> ocaml-string and put the c-structure there. So it will be free by the gc.
> That seems o.k. for me, any comments? I'm missing something?
That seems like it'll work for "opaque" C objects, but it's a bit of a
hack. The immediate issues I can think are:
(a) Pointers in the C code which point at the object will not be
"counted" by the GC, and so the object may be collected while there
are still C pointers around. This is easily avoided in OCaml, but
read chapter 18 of the manual carefully.
(b) By storing the object as a string you're telling the GC not to
examine the inside of the object, eg. looking for pointers inside to
other objects. Fine, if you know what you're doing, but OCaml already
has a number of established ways to do this - eg. using Abstract or
Custom blocks - and these standard ways are not just standard, but
offer additional features too. Alternatively you may consider a
non-abstract block and deliberately allow the GC to look inside. C
and OCaml structures are not actually too different.
Actually, while I was writing the above, it struck me that perhaps
you're talking about some sort of marshalling system? OCaml supports
its own marshalling format, and a rich variety of other external forms
of marshalling.
Rich.
--
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Internet Marketing and AdWords courses - http://merjis.com/courses - NEW!
Merjis blog - http://blog.merjis.com - NEW!
next prev parent reply other threads:[~2006-12-26 18:35 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-12-26 18:15 micha
2006-12-26 18:33 ` [Caml-list] " skaller
2006-12-26 18:35 ` Richard Jones [this message]
2006-12-26 18:55 ` micha
2006-12-26 18:32 EL CHAAR Rabih SGAM/AI/SAM
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=20061226183547.GA29273@furbychan.cocan.org \
--to=rich@annexia.org \
--cc=caml-list@inria.fr \
--cc=micha-1@fantasymail.de \
/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