Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix minor heap stats for bigarrays allocated with custom data pointers. #10788

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,9 @@ OCaml 4.14.0
- #10763, #10764: fix miscompilation of method delegation
(Alain Frisch, review by Vincent Laviron and Jacques Garrigue)

- #10788: fix minor GC memory associated with allocated
bigarrays when passing cutom data pointers
(Romain Beauxis)

OCaml 4.13 maintenance branch
-----------------------------
Expand Down
22 changes: 13 additions & 9 deletions runtime/bigarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ CAMLexport struct custom_operations caml_ba_ops = {
(with [malloc]) by [caml_ba_alloc].
[data] cannot point into the OCaml heap.
[dim] may point into an object in the OCaml heap.

When calling with a custom data pointer, the function may raise
an exception if passed length is inconsistent with the bigarray's
parameters.
*/
CAMLexport value
caml_ba_alloc(int flags, int num_dims, void * data, intnat * dim)
Expand All @@ -98,16 +102,16 @@ caml_ba_alloc(int flags, int num_dims, void * data, intnat * dim)
CAMLassert((flags & CAML_BA_KIND_MASK) <= CAML_BA_CHAR);
for (i = 0; i < num_dims; i++) dimcopy[i] = dim[i];
size = 0;
if (data == NULL) {
num_elts = 1;
for (i = 0; i < num_dims; i++) {
if (caml_umul_overflow(num_elts, dimcopy[i], &num_elts))
caml_raise_out_of_memory();
}
if (caml_umul_overflow(num_elts,
caml_ba_element_size[flags & CAML_BA_KIND_MASK],
&size))
num_elts = 1;
for (i = 0; i < num_dims; i++) {
if (caml_umul_overflow(num_elts, dimcopy[i], &num_elts))
caml_raise_out_of_memory();
}
if (caml_umul_overflow(num_elts,
caml_ba_element_size[flags & CAML_BA_KIND_MASK],
&size))
caml_raise_out_of_memory();
if (data == NULL) {
data = malloc(size);
if (data == NULL && size != 0) caml_raise_out_of_memory();
flags |= CAML_BA_MANAGED;
Expand Down