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
Freeing allocated C-memory by GC (destructors)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
Subject: RE: [Caml-list] Freeing allocated C-memory by GC (destructors)
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);

CAMLexport struct custom_operations Gen_ops = {

CAMLprim value Gen_make(value init_seed, value rng_type, value rng_parameter, ...)
  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;


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


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

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?


Caml-list mailing list. Subscription management:
Beginner's list:
Bug reports:
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 
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