Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] register_global_root, malloc, etc.
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Chris Hecker <checker@d...>
Subject: [Caml-list] register_global_root, malloc, etc.

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?

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

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 )
	foo *p = (foo *)Field(fooval,0);
	p->callback = callback;

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?


Bug reports:  FAQ:
To unsubscribe, mail  Archives: