File: alloc_async_stubs.c

package info (click to toggle)
ocaml 5.3.0-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 43,124 kB
  • sloc: ml: 355,439; ansic: 51,636; sh: 25,098; asm: 5,413; makefile: 3,673; python: 919; javascript: 273; awk: 253; perl: 59; fortran: 21; cs: 9
file content (68 lines) | stat: -rw-r--r-- 1,603 bytes parent folder | download | duplicates (2)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <stdlib.h>
#include <caml/alloc.h>
#include <caml/memory.h>
#define CAML_INTERNALS
#include <caml/gc_ctrl.h>


void print_status(const char *str, int n)
{
  printf("%s: %d\n", str, n);
  fflush(stdout);
}

value print_status_caml(value str, value n)
{
  print_status(String_val(str), Int_val(n));
  return Val_unit;
}

const char* strs[] = { "foo", "bar", 0 };
value stub(value ref)
{
  CAMLparam1(ref);
  CAMLlocal2(x, y);
  char* s; intnat coll_before;

  print_status("C, before", Int_val(Field(ref, 0)));

  /* First, do enough major allocations to do a full major collection cycle */
  coll_before = caml_stat_major_collections;
  while (caml_stat_major_collections <= coll_before+1) {
    caml_alloc(10000, 0);
  }

  /* Now, call lots of allocation functions */

  /* Small allocations */
  caml_alloc(10, 0);
  x = caml_alloc_small(2, 0);
  Field(x, 0) = Val_unit;
  Field(x, 1) = Val_unit;
  caml_alloc_tuple(3);
  caml_alloc_float_array(10);
  caml_alloc_string(42);
  caml_alloc_initialized_string(10, "abcdeabcde");
  caml_copy_string("asoidjfa");
  caml_copy_string_array(strs);
  caml_copy_double(42.0);
  caml_copy_int32(100);
  caml_copy_int64(100);
  caml_alloc_array(caml_copy_string, strs);
  caml_alloc_sprintf("[%d]", 42);

  /* Large allocations */
  caml_alloc(1000, 0);
  caml_alloc_shr(1000, String_tag);
  caml_alloc_tuple(1000);
  caml_alloc_float_array(1000);
  caml_alloc_string(10000);
  s = calloc(10000, 1);
  caml_alloc_initialized_string(10000, s);
  free(s);


  print_status("C, after", Int_val(Field(ref, 0)));
  CAMLreturn (Val_unit);
}