caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Freeing allocated C-memory by GC (destructors)
@ 2008-01-23 16:46 Oliver Bandel
  2008-01-23 17:18 ` [Caml-list] " RABIH.ELCHAAR
  0 siblings, 1 reply; 2+ messages in thread
From: Oliver Bandel @ 2008-01-23 16:46 UTC (permalink / raw)
  To: caml-list

Hello,

after using some non-permanent allocating C-stuff
together with OCaml, I think about going one step further and
now have allocated memory from C represented as OCaml-variables
(in the concrete example I have married some of my C-stuff that
uses jpeglib to get information from picture-files, and
now think about representing complete picture-data as
OCaml-variables).

So, I want to have the possibility to have malloced/calloced
data avaliable in OCaml, and that the GC calls C-destructor-
functions (using free(3)) when the values are of no use anymore.


How to say the GC, which is the destructor-functions
for a variable, which has been created?

TIA,
   Oliver


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

* RE: [Caml-list] Freeing allocated C-memory by GC (destructors)
  2008-01-23 16:46 Freeing allocated C-memory by GC (destructors) Oliver Bandel
@ 2008-01-23 17:18 ` RABIH.ELCHAAR
  0 siblings, 0 replies; 2+ messages in thread
From: RABIH.ELCHAAR @ 2008-01-23 17:18 UTC (permalink / raw)
  To: oliver, caml-list

Use custom_blocks, passing the corresponding functions (mainly the finalizer)
Here is an example of random generator defined in c and wrapped in ocaml

#define Gen(v) (*((int **) Data_custom_val(v)))

static void finalize_Gen(value v)
{
  int * stream = Gen(v);
  free_Gen(stream);
}

CAMLexport struct custom_operations Gen_ops = {
  "mlfi_Gen_state",
  //custom_finalize_default,
  &finalize_Gen,
  custom_compare_default,
  custom_hash_default,
  custom_serialize_default,
  custom_deserialize_default
};


CAMLprim value Gen_make(value init_seed, value rng_type, value rng_parameter, ...)
{
  CAMLparam5(init_seed,rng_type,rng_parameter,...);
  CAMLlocal1(t);
  int *stream;
  unsigned long int seed; 
  int rngtype;
  int rngparameter;

  seed = Unsigned_long_val(init_seed);

  rngtype = getRngType(rng_type);
  rngparameter = getRngParameter(rng_parameter);

  /* we create here the custom block, with the correct free operator */  
  t = alloc_custom(&Gen_ops, sizeof(int *), 0, 1);
  stream = init_Gen(rngtype,...,seed,rngparameter);
  Gen(t) = stream;

  CAMLreturn(t);
}

-----Message d'origine-----
De : caml-list-bounces@yquem.inria.fr [mailto:caml-list-bounces@yquem.inria.fr] De la part de Oliver Bandel
Envoyé : mercredi 23 janvier 2008 17:46
À : caml-list@inria.fr
Objet : [Caml-list] Freeing allocated C-memory by GC (destructors)

Hello,

after using some non-permanent allocating C-stuff
together with OCaml, I think about going one step further and
now have allocated memory from C represented as OCaml-variables
(in the concrete example I have married some of my C-stuff that
uses jpeglib to get information from picture-files, and
now think about representing complete picture-data as
OCaml-variables).

So, I want to have the possibility to have malloced/calloced
data avaliable in OCaml, and that the GC calls C-destructor-
functions (using free(3)) when the values are of no use anymore.


How to say the GC, which is the destructor-functions
for a variable, which has been created?

TIA,
   Oliver

_______________________________________________
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] 2+ messages in thread

end of thread, other threads:[~2008-01-23 17:18 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-01-23 16:46 Freeing allocated C-memory by GC (destructors) Oliver Bandel
2008-01-23 17:18 ` [Caml-list] " RABIH.ELCHAAR

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