Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: RABIH.ELCHAAR@s...
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);
  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