caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Raj Bandyopadhyay <rajb@rice.edu>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Disabling the OCaml garbage collector
Date: Tue, 4 Dec 2007 14:14:30 -0600	[thread overview]
Message-ID: <3DA0E7CA-2511-4282-89D1-8EB42876EC18@rice.edu> (raw)
In-Reply-To: <474DC8DA.8070806@functionality.de>

Dear all,

As a followup to this discussion, I have been trying to understand  
the OCaml/C interface better. Here's a very small program that I've  
been trying to get to work. It's a mutually recursive function, one  
part in C and the other in OCaml.

(* (* Implement this in C *)
let factC g n = if n=0 then [] else ("C",n)::(g (n-1));;
*)

external factC: (int ->(string * int) list) -> int -> ((string * int)  
list)= "caml_factC"
let factO g n = if n=0 then [] else ("OCaml",n)::(g (n-1));;
let rec fact n = factO (factC fact) n;;

fact 12000;;


The C function corresponding to factC is quite short, however the  
program crashes for values of n > about 11,000. I have tried  
inserting the code to register global roots (currently commented  
out), however, that makes no difference to the point of crash. I was  
just wondering if there is some really obvious step that I am  
missing, or using the wrong allocation function in Caml or something  
like that. Any suggestions would be welcome. I apologize in advice  
for inflicting code on you all, but I am out of ideas right now :(

/*
let factC g n =
         if n=0 then
                 []
         else
                 ("C",n)::(g (n-1))
*/
value caml_factC(value g,value n){
         CAMLparam2(g,n);
         CAMLlocal3(e,l,new_l);

         //C value for n
         int locn = Int_val(n);

         //2 cases
         if (locn <= 0){
                 CAMLreturn(Val_int(0)); //empty list
         }
         else {
                 e = alloc_tuple(2);  //allocate a new list element  
("C",n)
                 Store_field(e,0,copy_string("C"));
                 Store_field(e,1,n);

                 //callback the closure g
                 if(Tag_val(g) == Closure_tag) {
			//PROGRAM CRASHES HERE for large n and never returns from callback
                         l = callback(g,Val_int(locn-1));
                 } else {
                         exit(1);
                 }
                 //cons this tuple to the list obtained by callback
                 new_l = alloc(2,0); //structured list value, tag is 0
                 Store_field(new_l,0,e);
                 Store_field(new_l,1,l);

                 //now we should register e,l and new_l with the GC
		//The program crashes at the same point (n>11000) regdless of  
whether the following
		//code is commented out or not.
                 /*
                 Vstore *roots = (Vstore *)caml_stat_alloc(sizeof 
(Vstore));
                 roots->v1 = e;
                 roots->v2 = l;
                 roots->v3 = new_l;
                 caml_register_global_root(&roots->v1);
                 caml_register_global_root(&roots->v2);
                 caml_register_global_root(&roots->v3);
                 CAMLreturn(roots->v3); //newly constructed list
                 */


                 CAMLreturn(new_l); //newly constructed list
         }
}


Thanks!
Raj


  reply	other threads:[~2007-12-04 20:14 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-11-27 16:28 Raj Bandyopadhyay
2007-11-27 16:41 ` [Caml-list] " Basile STARYNKEVITCH
2007-11-28 18:05   ` Raj
2007-11-28 18:15     ` Alain Frisch
2007-11-28 18:31       ` Thomas Fischbacher
2007-11-28 19:30         ` Raj
2007-11-28 18:25     ` Thomas Fischbacher
2007-11-28 18:32     ` Xavier Leroy
2007-11-28 19:33       ` Raj
2007-11-28 20:00         ` Thomas Fischbacher
2007-12-04 20:14           ` Raj Bandyopadhyay [this message]
2007-12-05  4:07             ` Jon Harrop
2007-11-29  8:54     ` Frédéric van der Plancke
2007-11-27 17:05 ` [Caml-list] Python and Caml (was: Disabling the OCaml garbage collector) Thomas Fischbacher
2011-08-04 13:10   ` [Caml-list] Python and Caml Stéphane Glondu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3DA0E7CA-2511-4282-89D1-8EB42876EC18@rice.edu \
    --to=rajb@rice.edu \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).