Skip to content

Commit 179a3f3

Browse files
committed
Use a different size function
1 parent f817738 commit 179a3f3

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

runtime/obj.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ CAMLprim value caml_obj_tag(value arg)
5454
return Val_int (obj_tag(arg));
5555
}
5656

57+
CAMLprim value caml_obj_size(value arg)
58+
{
59+
return Val_long(Is_block(arg) ? Wosize_val(arg) : 0);
60+
}
61+
5762
CAMLprim value caml_obj_raw_field(value arg, value pos)
5863
{
5964
/* Represent field contents as a native integer */

runtime4/obj.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ CAMLprim value caml_obj_tag(value arg)
4848
return Val_int (obj_tag(arg));
4949
}
5050

51+
CAMLprim value caml_obj_size(value arg)
52+
{
53+
return Val_long(Is_block(arg) ? Wosize_val(arg) : 0);
54+
}
55+
5156
CAMLprim value caml_obj_set_tag (value arg, value new_tag)
5257
{
5358
Tag_val (arg) = Int_val (new_tag);

stdlib/obj.mli

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ external magic : 'a -> 'b = "%obj_magic"
3131
val is_block : t -> bool
3232
external is_int : t -> bool = "%obj_is_int"
3333
external tag : t -> int = "caml_obj_tag" [@@noalloc]
34+
35+
(* CR mshinwell: Work out a proper way of fixing this. *)
36+
(** Note that [size] may give wrong results for arrays of non-value layouts.
37+
For such situations one can bind [caml_obj_size] instead. *)
3438
val size : t -> int
39+
3540
val reachable_words : t -> int
3641
(**
3742
Computes the total size (in words, including the headers) of all

testsuite/tests/typing-layouts-arrays/array_element_size_in_bytes.ml

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
external custom_block_padding : unit -> int = "custom_block_padding_byte" "custom_block_padding_native"
2020
let custom_block_padding = custom_block_padding ()
2121

22+
(* We can't use [Obj.size] because that uses [Parraylength], which will be
23+
specialized even though [Sys.opaque_identity] is present, thus giving a
24+
"proper" array length computation rather than what we want. *)
25+
external obj_size : _ -> int = "%obj_size" [@@noalloc]
26+
2227
(* We only compile for 64 bits. *)
2328
let bytes_per_word = 8
2429

@@ -36,7 +41,7 @@ let check_value ~init ~element_size =
3641
we don't have layout polymorphism. *)
3742
let check_one n =
3843
let x = makearray_dynamic n (fun _ -> init) in
39-
assert ((element_size * n / bytes_per_word) = (Obj.size (Obj.repr x)))
44+
assert ((element_size * n / bytes_per_word) = (obj_size (Obj.repr x)))
4045
in
4146
List.iter check_one array_sizes_to_check
4247

@@ -59,7 +64,7 @@ let _ = check_value ~init:42l ~element_size:int_array_element_size
5964
let check_floatu ~init ~element_size =
6065
let check_one n =
6166
let x = makearray_dynamic n (fun _ -> init) in
62-
assert ((element_size * n / bytes_per_word) = (Obj.size (Obj.repr x)))
67+
assert ((element_size * n / bytes_per_word) = (obj_size (Obj.repr x)))
6368
in
6469
List.iter check_one array_sizes_to_check
6570

@@ -72,7 +77,7 @@ let check_int64u ~(init : int64#) ~element_size =
7277
let check_one n =
7378
let x = makearray_dynamic n (fun _ -> init) in
7479
assert ((custom_block_padding + (element_size * n / bytes_per_word))
75-
= (Obj.size (Obj.repr x)))
80+
= (obj_size (Obj.repr x)))
7681
in
7782
List.iter check_one array_sizes_to_check
7883

@@ -85,7 +90,7 @@ let check_float32u ~(init : float32#) ~element_size =
8590
let check_one n =
8691
let x = makearray_dynamic n (fun _ -> init) in
8792
assert ((custom_block_padding + (element_size * n / bytes_per_word))
88-
= (Obj.size (Obj.repr x)))
93+
= (obj_size (Obj.repr x)))
8994
in
9095
List.iter check_one array_sizes_to_check
9196

@@ -98,7 +103,7 @@ let check_int32u ~(init : int32#) ~element_size =
98103
let check_one n =
99104
let x = makearray_dynamic n (fun _ -> init) in
100105
assert ((custom_block_padding + (element_size * n / bytes_per_word))
101-
= (Obj.size (Obj.repr x)))
106+
= (obj_size (Obj.repr x)))
102107
in
103108
List.iter check_one array_sizes_to_check
104109

@@ -111,7 +116,7 @@ let check_scannable_product1 ~(init : #(int * string * int * float array))
111116
~element_size =
112117
let check_one n =
113118
let x = makearray_dynamic n (fun _ -> init) in
114-
assert ((element_size * n / bytes_per_word) = (Obj.size (Obj.repr x)))
119+
assert ((element_size * n / bytes_per_word) = (obj_size (Obj.repr x)))
115120
in
116121
List.iter check_one array_sizes_to_check
117122

@@ -128,7 +133,7 @@ let check_scannable_product2 ~(init : #(int * t_scan * string * t_scan))
128133
~element_size =
129134
let check_one n =
130135
let x = makearray_dynamic n (fun _ -> init) in
131-
assert ((element_size * n / bytes_per_word) = (Obj.size (Obj.repr x)))
136+
assert ((element_size * n / bytes_per_word) = (obj_size (Obj.repr x)))
132137
in
133138
List.iter check_one array_sizes_to_check
134139

@@ -149,7 +154,7 @@ let check_ignorable_product1 ~(init : #(int * float32# * int * int64#))
149154
~element_size =
150155
let check_one n =
151156
let x = makearray_dynamic n (fun _ -> init) in
152-
assert ((element_size * n / bytes_per_word) = (Obj.size (Obj.repr x)))
157+
assert ((element_size * n / bytes_per_word) = (obj_size (Obj.repr x)))
153158
in
154159
List.iter check_one array_sizes_to_check
155160

@@ -166,7 +171,7 @@ let check_ignorable_product2 ~(init : #(int * t_ignore * bool * t_ignore))
166171
~element_size =
167172
let check_one n =
168173
let x = makearray_dynamic n (fun _ -> init) in
169-
assert ((element_size * n / bytes_per_word) = (Obj.size (Obj.repr x)))
174+
assert ((element_size * n / bytes_per_word) = (obj_size (Obj.repr x)))
170175
in
171176
List.iter check_one array_sizes_to_check
172177

@@ -186,7 +191,7 @@ let _ = check_ignorable_product2 ~init:(mk_el ())
186191
let check_float32u_pair ~(init : #(float32# * float32#)) ~element_size =
187192
let check_one n =
188193
let x = makearray_dynamic n (fun _ -> init) in
189-
assert ((element_size * n / bytes_per_word) = (Obj.size (Obj.repr x)))
194+
assert ((element_size * n / bytes_per_word) = (obj_size (Obj.repr x)))
190195
in
191196
List.iter check_one array_sizes_to_check
192197

@@ -200,7 +205,7 @@ let _ = check_float32u_pair ~init:#(#1.0s, #42.1s)
200205
let check_int32u_pair ~(init : #(int32# * int32#)) ~element_size =
201206
let check_one n =
202207
let x = makearray_dynamic n (fun _ -> init) in
203-
assert ((element_size * n / bytes_per_word) = (Obj.size (Obj.repr x)))
208+
assert ((element_size * n / bytes_per_word) = (obj_size (Obj.repr x)))
204209
in
205210
List.iter check_one array_sizes_to_check
206211

0 commit comments

Comments
 (0)