caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* RE: [Caml-list] allocating memory for c-structures
@ 2006-12-26 18:32 EL CHAAR Rabih   SGAM/AI/SAM
  0 siblings, 0 replies; 4+ messages in thread
From: EL CHAAR Rabih   SGAM/AI/SAM @ 2006-12-26 18:32 UTC (permalink / raw)
  To: micha; +Cc: caml-list

The issue about having c structures allocated in ocaml heap is about:
1) having a floating pointer that could be displaced by the gc during its cycles (if it is emedded into ocaml heap)
2) having it reside in the c heap, and not be impacted by caml gc cycles (this can be justified if the c structure is heavy in memory, like a c array, ...): this is done by creating caml values with a custom or abstract tag.
3) memory freeing could always be done to a custom_tag value through the finalization function passed during the creation.

Hope this helps,
Rabih


-----Message d'origine-----
De : caml-list-bounces@yquem.inria.fr [mailto:caml-list-bounces@yquem.inria.fr] De la part de micha
Envoyé : mardi 26 décembre 2006 19:16
À : OCaml Mailing List
Objet : [Caml-list] allocating memory for c-structures

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?

cheers
 Michael

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
Ce message et toutes les pieces jointes (ci-apres le "message") sont confidentiels et etablis a l'intention exclusive de ses destinataires. 
Toute utilisation ou diffusion non autorisee est interdite. 
Tout message electronique est susceptible d'alteration. 
Societe Generale Asset Management et ses filiales declinent toute responsabilite au titre de ce message s'il a ete altere, deforme ou falsifie. 
  
Decouvrez l'offre et les services de Societe Generale Asset Management sur le site www.sgam.fr 
  
                                ******** 
  
This message and any attachments (the "message") are confidential and intended solely for the addressees. 
Any unauthorised use or dissemination is prohibited. 
E-mails are susceptible to alteration. 
Neither Societe Generale Asset Management nor any of its subsidiaries or affiliates shall be liable for the message if altered, changed or falsified. 
 
Find out more about Societe Generale Asset Management's proposal on www.sgam.com


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] allocating memory for c-structures
  2006-12-26 18:35 ` Richard Jones
@ 2006-12-26 18:55   ` micha
  0 siblings, 0 replies; 4+ messages in thread
From: micha @ 2006-12-26 18:55 UTC (permalink / raw)
  To: caml-list

thanks for all the answers.

Richard Jones schrieb:
>
> 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.
>   
that's true; for linked data structures it would not work (except all
would be allocated this way)


> 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.
ah no, I just thought that it would be another way to handle external
memory.
What I didn't realize was, that the gc moves pointers around...

cheers
Michael



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] allocating memory for c-structures
  2006-12-26 18:15 micha
  2006-12-26 18:33 ` [Caml-list] " skaller
@ 2006-12-26 18:35 ` Richard Jones
  2006-12-26 18:55   ` micha
  1 sibling, 1 reply; 4+ messages in thread
From: Richard Jones @ 2006-12-26 18:35 UTC (permalink / raw)
  To: micha; +Cc: OCaml Mailing List

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!


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] allocating memory for c-structures
  2006-12-26 18:15 micha
@ 2006-12-26 18:33 ` skaller
  2006-12-26 18:35 ` Richard Jones
  1 sibling, 0 replies; 4+ messages in thread
From: skaller @ 2006-12-26 18:33 UTC (permalink / raw)
  To: micha; +Cc: OCaml Mailing List

On Tue, 2006-12-26 at 19:15 +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?

I don't believe Ocaml guarantees the contents of a string
will remain in a fixed location .. it might move the storage
to a new address .. so pointers into the structure might
dangle.

-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2006-12-26 18:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-12-26 18:32 [Caml-list] allocating memory for c-structures EL CHAAR Rabih   SGAM/AI/SAM
  -- strict thread matches above, loose matches on Subject: below --
2006-12-26 18:15 micha
2006-12-26 18:33 ` [Caml-list] " skaller
2006-12-26 18:35 ` Richard Jones
2006-12-26 18:55   ` micha

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).