-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxSpotlib.ml
50 lines (40 loc) · 1.02 KB
/
xSpotlib.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
(* This should be ported / already ported to spotlib *)
module Exn = struct
(* lwt does not like ~finally *)
let protect f v ~final = Spotlib.Exn.protect f v ~finally:final
end
module Array = struct
let shuffle ?(random=Random.int) a =
let len = Array.length a - 1 in
for i = 0 to len - 1 do
let d = len - i in
let j = random d + i in
let x = Array.unsafe_get a i in
let y = Array.unsafe_get a j in
Array.unsafe_set a i y;
Array.unsafe_set a j x;
done
end
module Base = struct
let ( !++ ) r =
let v = !r in
incr r;
v
end
module Gc = struct
open Gc
let used_words () =
let c = get () in
set { c with minor_heap_size = 1000; (* 1k words = 8k bytes *) };
compact ();
let s = stat () in
let res = s.live_words in
set c;
res
let with_big_compacts f v =
let used_before = used_words () in
Spotlib.Spot.Exn.protect_with f v
~finally:(fun _ ->
let used_after = used_words () in
used_before, used_after)
end