File: qsort.ml

package info (click to toggle)
ocaml 5.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 44,372 kB
  • sloc: ml: 370,196; ansic: 52,820; sh: 27,396; asm: 5,462; makefile: 3,679; python: 974; awk: 278; javascript: 273; perl: 59; fortran: 21; cs: 9
file content (48 lines) | stat: -rw-r--r-- 1,323 bytes parent folder | download | duplicates (3)
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
(* TEST
 frame_pointers;
 modules = "qsort_.c";
*)

external with_frame : (unit -> 'a) -> 'a = "with_frame"
external check_frames : unit -> unit = "check_frames"

external in_callback : (unit -> 'a) -> 'a = "in_callback"
external in_callback_stk :
  int -> int -> int -> int -> int ->
  int -> int -> int -> int -> int ->
  (unit -> 'a) -> 'a = "in_callback_stk_byte" "in_callback_stk"

external sort2 : ('a -> 'a -> int) -> 'a -> 'a -> 'a * 'a = "sort2"

let rec recurse n =
  if n = 0 then 0 else 1 + recurse (n-1)

let f a b =
  check_frames ();
  let cmp_str a b =
    Printf.printf "Comparing %s <=> %s\n" a b;
    let n = recurse 10000 in (* force stack realloc *)
    assert (n = 10000);
    (* check_frames not expected to work here:
       we're inside a call to qsort that may not have frame pointers *)
    Gc.minor ();
    String.compare a b
  in
  let a, b = sort2 cmp_str a b in
  check_frames ();
  Printf.printf "Sorted: %s <= %s\n" a b

let in_finaliser f =
  let finalised = ref false in
  Gc.finalise_last (fun () -> finalised := true; f ()) (ref 42);
  Gc.minor ();
  assert (!finalised)

let () =
  in_callback @@ fun () ->
  with_frame @@ fun () ->
  in_finaliser @@ fun () ->
  in_callback @@ fun () ->
  in_callback_stk 10 10 10 10 10 10 10 10 10 10 (fun () ->
    f "foo" "bar";
    f "bar" "foo")