Debian Codegraph @

testsuite/tests/gc-roots/globrootsprim.c

raw
1 /***********************************************************************/
2 /* */
3 /* OCaml */
4 /* */
5 /* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
6 /* */
7 /* Copyright 2001 Institut National de Recherche en Informatique et */
8 /* en Automatique. All rights reserved. This file is distributed */
9 /* under the terms of the GNU Library General Public License, with */
10 /* the special exception on linking described in file ../LICENSE. */
11 /* */
12 /***********************************************************************/
13
14 /* For testing global root registration */
15
16 #define CAML_INTERNALS
17
18 #include <caml/mlvalues.h>
19 #include <caml/memory.h>
20 #include <caml/alloc.h>
21 #include <caml/gc.h>
22 #include <caml/shared_heap.h>
23 #include <caml/callback.h>
24
25 struct block { value header; value v; };
26
27 #define Block_val(v) ((struct block*) &((value*) v)[-1])
28 #define Val_block(b) ((value) &((b)->v))
29
30 value gb_get(value vblock)
31 {
32 return Block_val(vblock)->v;
33 }
34
35 value gb_classic_register(value v)
36 {
37 struct block * b = caml_stat_alloc(sizeof(struct block));
38 b->header = Make_header(1, 0, NOT_MARKABLE);
39 b->v = v;
40 caml_register_global_root(&(b->v));
41 return Val_block(b);
42 }
43
44 value gb_classic_set(value vblock, value newval)
45 {
46 Block_val(vblock)->v = newval;
47 return Val_unit;
48 }
49
50 value gb_classic_remove(value vblock)
51 {
52 caml_remove_global_root(&(Block_val(vblock)->v));
53 return Val_unit;
54 }
55
56 value gb_generational_register(value v)
57 {
58 struct block * b = caml_stat_alloc(sizeof(struct block));
59 b->header = Make_header(1, 0, NOT_MARKABLE);
60 b->v = v;
61 caml_register_generational_global_root(&(b->v));
62 return Val_block(b);
63 }
64
65 value gb_generational_set(value vblock, value newval)
66 {
67 caml_modify_generational_global_root(&(Block_val(vblock)->v), newval);
68 return Val_unit;
69 }
70
71 value gb_generational_remove(value vblock)
72 {
73 caml_remove_generational_global_root(&(Block_val(vblock)->v));
74 return Val_unit;
75 }
76
77 value root;
78
79 value gb_young2old(value _dummy) {
80 root = caml_alloc_small(1, 0);
81 caml_register_generational_global_root(&root);
82 caml_modify_generational_global_root(&root, caml_alloc_shr(10, String_tag));
83 Field(root, 0) = 0xFFFFFFFF;
84 caml_remove_generational_global_root(&root);
85 root += sizeof(value);
86 return Val_unit;
87 }
88
89 value gb_static2young(value static_value, value full_major) {
90 CAMLparam2 (static_value, full_major);
91 CAMLlocal1(v);
92
93 root = Val_unit;
94 caml_register_generational_global_root(&root);
95
96 /* Write a static value in the root. */
97 caml_modify_generational_global_root(&root, static_value);
98
99 /* Overwrite it with a young value. */
100 v = caml_alloc_small(1, 0);
101 Field(v, 0) = Val_long(0x42);
102 caml_modify_generational_global_root(&root, v);
103
104 /* Promote the young value */
105 caml_callback(full_major, Val_unit);
106
107 /* Fill the minor heap to make sure the old block is overwritten */
108 for (int i = 0; i < 1000000; i++)
109 caml_alloc_small(1, 0);
110
111 v = Field(root, 0);
112 caml_remove_generational_global_root(&root);
113
114 CAMLreturn(v);
115 }