Merge branch 'handle-stdout'
[SXSI/xpathcomp.git] / src / ocaml.ml
1 open Obj
2
3 module H = Hashtbl.Make(
4   struct
5     type t = Obj.t
6     let equal = (==)
7     let hash o = Hashtbl.hash (magic o : int)
8   end)
9
10 let node_table = (H.create 257 : unit H.t)
11
12 let in_table o = try H.find node_table o; true with Not_found -> false
13
14 let add_in_table o = H.add node_table o ()
15
16 let reset_table () = H.clear node_table
17
18 let size_of_double = size (repr 1.0)
19
20 let count = ref 0
21
22 let rec traverse t =
23   if not (in_table t) then begin
24     add_in_table t;
25     if is_block t then begin
26       let n = size t in
27       let tag = tag t in
28       if tag < no_scan_tag then begin
29         count := !count + 1 + n;
30         for i = 0 to n - 1 do
31           let f = field t i in
32           if is_block f then traverse f
33         done
34       end else if tag = string_tag then
35         count := !count + 1 + n
36       else if tag = double_tag then
37         count := !count + size_of_double
38       else if tag = double_array_tag then
39         count := !count + 1 + size_of_double * n
40       else
41         incr count
42     end
43   end
44
45 let size_w o =
46   reset_table ();
47   count := 0;
48   traverse (repr o);
49   !count
50
51 let size_b o = (size_w o) * (Sys.word_size / 8)
52
53 let size_kb o = (size_b o) / 1024