caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] register_global_root, malloc, etc.
@ 2001-07-26  8:48 Chris Hecker
  2001-08-14  8:25 ` Xavier Leroy
  0 siblings, 1 reply; 4+ messages in thread
From: Chris Hecker @ 2001-07-26  8:48 UTC (permalink / raw)
  To: caml-list


Say I've got the following C struct:

struct foo
{
	int something;
	value callback;
};

If I'm writing a module in C and want to have an abstract type that corresponds to that struct (allocated with new/malloc/etc. from C), is the following code correct?

CAMLlocal1(result);
foo *p = new foo;
p->callback = Val_unit;
register_global_root(&(p->callback));
result = alloc_small(1,Abstract_tag);
Field(result,0) = (value)p;
CAMLreturn(result);

Then, later, I can just assign another closure passed to a C function (and CAMLparam'ed) to p->callback without worrying about it, like this:

value set_callback( value fooval, value callback )
{
	CAMLparam2(fooval,callback);
	foo *p = (foo *)Field(fooval,0);
	p->callback = callback;
	CAMLreturn(Val_unit);
}

Is that correct?  Should I use Custom_tag and register all the finalization functions and whatnot for my abstract type, or is Abstract_tag good enough assuming I've got a free_foo function that users of the module are supposed to call to deallocate the abstract type?  free_foo should call remove_global_root before deleting the memory, right?  But I don't need to do anything to explicitly delete the callback or the Abstract_tag block that was passed in since the GC will handle it?

Thanks,
Chris

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


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

* Re: [Caml-list] register_global_root, malloc, etc.
  2001-07-26  8:48 [Caml-list] register_global_root, malloc, etc Chris Hecker
@ 2001-08-14  8:25 ` Xavier Leroy
  2001-08-15  2:26   ` Alexander V. Voinov
  0 siblings, 1 reply; 4+ messages in thread
From: Xavier Leroy @ 2001-08-14  8:25 UTC (permalink / raw)
  To: Chris Hecker; +Cc: caml-list

[I see that your message was left unanswered.  Hope the following late
answer will help.]

> If I'm writing a module in C and want to have an abstract type that
> corresponds to that struct (allocated with new/malloc/etc. from C),
> is the following code correct?
> 
> CAMLlocal1(result);
> foo *p = new foo;
> p->callback = Val_unit;
> register_global_root(&(p->callback));
> result = alloc_small(1,Abstract_tag);
> Field(result,0) = (value)p;
> CAMLreturn(result);
> 
> Then, later, I can just assign another closure passed to a C function (and CAMLparam'ed) to p->callback without worrying about it, like this:
> 
> value set_callback( value fooval, value callback )
> {
> 	CAMLparam2(fooval,callback);
> 	foo *p = (foo *)Field(fooval,0);
> 	p->callback = callback;
> 	CAMLreturn(Val_unit);
> }
> 
> Is that correct?

Yes.

> Should I use Custom_tag and register all the finalization functions
> and whatnot for my abstract type, or is Abstract_tag good enough
> assuming I've got a free_foo function that users of the module are
> supposed to call to deallocate the abstract type?  free_foo should
> call remove_global_root before deleting the memory, right?  But I
> don't need to do anything to explicitly delete the callback or the
> Abstract_tag block that was passed in since the GC will handle it?

You're 100% correct.  The only advantage of Custom_tag over
Abstract_tag is that finalization can be handled by the GC instead of
by the user (via free_foo).  GC-based finalization is safer in the
sense that you're certain that the object cannot be reached again by
Caml code.  With user-managed finalization, there is always the risk
that the program will call free_foo, then still use the "foo" value
afterwards.

- Xavier Leroy
-------------------
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


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

* Re: [Caml-list] register_global_root, malloc, etc.
  2001-08-14  8:25 ` Xavier Leroy
@ 2001-08-15  2:26   ` Alexander V. Voinov
  2001-08-16  0:59     ` Chris Hecker
  0 siblings, 1 reply; 4+ messages in thread
From: Alexander V. Voinov @ 2001-08-15  2:26 UTC (permalink / raw)
  To: Xavier Leroy; +Cc: Chris Hecker, caml-list

Hi All,

Xavier Leroy wrote:
> 
> [I see that your message was left unanswered.  Hope the following late
> answer will help.]
> 
> > If I'm writing a module in C and want to have an abstract type that
> > corresponds to that struct (allocated with new/malloc/etc. from C),
> > is the following code correct?
> >
> > CAMLlocal1(result);
> > foo *p = new foo;
> > p->callback = Val_unit;
> > register_global_root(&(p->callback));
> > result = alloc_small(1,Abstract_tag);
> > Field(result,0) = (value)p;
> > CAMLreturn(result);
> >
> > Then, later, I can just assign another closure passed to a C function (and CAMLparam'ed) to p->callback without worrying about it, like this:
> >
> > value set_callback( value fooval, value callback )
> > {
> >       CAMLparam2(fooval,callback);
> >       foo *p = (foo *)Field(fooval,0);
> >       p->callback = callback;
> >       CAMLreturn(Val_unit);
> > }
> >
> > Is that correct?
> 
> Yes.
> 
> > Should I use Custom_tag and register all the finalization functions
> > and whatnot for my abstract type, or is Abstract_tag good enough
> > assuming I've got a free_foo function that users of the module are
> > supposed to call to deallocate the abstract type?  free_foo should
> > call remove_global_root before deleting the memory, right?  But I
> > don't need to do anything to explicitly delete the callback or the
> > Abstract_tag block that was passed in since the GC will handle it?
> 
> You're 100% correct.  The only advantage of Custom_tag over
> Abstract_tag is that finalization can be handled by the GC instead of
> by the user (via free_foo).  GC-based finalization is safer in the
> sense that you're certain that the object cannot be reached again by
> Caml code.  With user-managed finalization, there is always the risk
> that the program will call free_foo, then still use the "foo" value
> afterwards.

I can't understand who will delete `foo *', allocated via `foo *p = new
foo'? As I understood from the manual, Custom_tag is the only way to
assign free_foo with the resulting Caml object. And I'm surprised by
your statement that after calling free_foo the program can still use
this reference. This sounds as if I bought a train ticket but the
railway station clerk warned me that despite of the fact that I paid
there still is a risk that my seat will be sold to somebody else :-).

Alexander
-------------------
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


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

* Re: [Caml-list] register_global_root, malloc, etc.
  2001-08-15  2:26   ` Alexander V. Voinov
@ 2001-08-16  0:59     ` Chris Hecker
  0 siblings, 0 replies; 4+ messages in thread
From: Chris Hecker @ 2001-08-16  0:59 UTC (permalink / raw)
  To: Alexander V. Voinov, Xavier Leroy; +Cc: caml-list


>I can't understand who will delete `foo *', allocated via `foo *p = new
>foo'?

free_foo unregisters the roots pointing inside foo, and then deletes foo.  The GC will deallocate the abstract object itself.

> As I understood from the manual, Custom_tag is the only way to
>assign free_foo with the resulting Caml object.

Custom means the object gets a finalization call from the gc.  Abstract means there's no finalization.  I should be using Custom so my foo doesn't leak.

Chris


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


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

end of thread, other threads:[~2001-08-16  0:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-07-26  8:48 [Caml-list] register_global_root, malloc, etc Chris Hecker
2001-08-14  8:25 ` Xavier Leroy
2001-08-15  2:26   ` Alexander V. Voinov
2001-08-16  0:59     ` Chris Hecker

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