summaryrefslogtreecommitdiffstats
path: root/otherlibs/bigarray
diff options
context:
space:
mode:
Diffstat (limited to 'otherlibs/bigarray')
-rw-r--r--otherlibs/bigarray/bigarray.ml8
-rw-r--r--otherlibs/bigarray/bigarray.mli58
-rw-r--r--otherlibs/bigarray/bigarray_stubs.c16
-rw-r--r--otherlibs/bigarray/mmap_unix.c21
4 files changed, 84 insertions, 19 deletions
diff --git a/otherlibs/bigarray/bigarray.ml b/otherlibs/bigarray/bigarray.ml
index 1d3dbcf97..b9f22b182 100644
--- a/otherlibs/bigarray/bigarray.ml
+++ b/otherlibs/bigarray/bigarray.ml
@@ -99,6 +99,8 @@ module Genarray = struct
= "caml_ba_map_file_bytecode" "caml_ba_map_file"
let map_file fd ?(pos = 0L) kind layout shared dims =
map_internal fd kind layout shared dims pos
+ external release: ('a, 'b, 'c) t -> unit
+ = "caml_ba_release"
end
module Array1 = struct
@@ -122,6 +124,8 @@ module Array1 = struct
ba
let map_file fd ?pos kind layout shared dim =
Genarray.map_file fd ?pos kind layout shared [|dim|]
+ external release: ('a, 'b, 'c) t -> unit
+ = "caml_ba_release"
end
module Array2 = struct
@@ -161,6 +165,8 @@ module Array2 = struct
ba
let map_file fd ?pos kind layout shared dim1 dim2 =
Genarray.map_file fd ?pos kind layout shared [|dim1;dim2|]
+ external release: ('a, 'b, 'c) t -> unit
+ = "caml_ba_release"
end
module Array3 = struct
@@ -210,6 +216,8 @@ module Array3 = struct
ba
let map_file fd ?pos kind layout shared dim1 dim2 dim3 =
Genarray.map_file fd ?pos kind layout shared [|dim1;dim2;dim3|]
+ external release: ('a, 'b, 'c) t -> unit
+ = "caml_ba_release"
end
external genarray_of_array1: ('a, 'b, 'c) Array1.t -> ('a, 'b, 'c) Genarray.t
diff --git a/otherlibs/bigarray/bigarray.mli b/otherlibs/bigarray/bigarray.mli
index 8b260bf79..73c27b575 100644
--- a/otherlibs/bigarray/bigarray.mli
+++ b/otherlibs/bigarray/bigarray.mli
@@ -426,7 +426,27 @@ module Genarray :
or a SIGBUS signal may be raised. This happens, for instance, if the
file is shrinked. *)
- end
+ val release: ('a, 'b, 'c) t -> unit
+ (** Release the resources associated with the given big array,
+ then set all of its dimensions to 0, causing subsequent accesses
+ to the big array to fail. This releasing of resources is performed
+ automatically by the garbage collector when the big array is no longer
+ referenced by the program. However, memory behavior of the program
+ can be improved by releasing the resources explicitly via
+ [Genarray.release] as soon as the big array is no longer useful.
+
+ If the big array was created with [Genarray.create], the memory
+ space occupied by its data is freed. If the big array was
+ created with [Genarray.map_file], updates performed on the array
+ are flushed to the file (if the mapping is shared), then the
+ mapping is removed, freeing the corresponding virtual memory
+ space. If several views on the big array data were created
+ using [Genarray.sub_*] or [Genarray.slice_*], data release occurs
+ when the last not-yet-released view is released. Multiple calls
+ to [Genarray.release] on the same big array are safe: the second
+ and subsequent calls have no effect. *)
+
+end
(** {6 One-dimensional arrays} *)
@@ -496,16 +516,20 @@ module Array1 : sig
(** Memory mapping of a file as a one-dimensional big array.
See {!Bigarray.Genarray.map_file} for more details. *)
+ val release: ('a, 'b, 'c) t -> unit
+ (** Explicit release of the resources associated with the big array.
+ See {!Bigarray.Genarray.release} for more details. *)
+
external unsafe_get: ('a, 'b, 'c) t -> int -> 'a = "%caml_ba_unsafe_ref_1"
(** Like {!Bigarray.Array1.get}, but bounds checking is not always performed.
Use with caution and only when the program logic guarantees that
- the access is within bounds. *)
+ the access is within bounds and the big array has not been released. *)
external unsafe_set: ('a, 'b, 'c) t -> int -> 'a -> unit
= "%caml_ba_unsafe_set_1"
(** Like {!Bigarray.Array1.set}, but bounds checking is not always performed.
Use with caution and only when the program logic guarantees that
- the access is within bounds. *)
+ the access is within bounds and the big array has not been released. *)
end
@@ -601,15 +625,21 @@ module Array2 :
(** Memory mapping of a file as a two-dimensional big array.
See {!Bigarray.Genarray.map_file} for more details. *)
+ val release: ('a, 'b, 'c) t -> unit
+ (** Explicit release of the resources associated with the big array.
+ See {!Bigarray.Genarray.release} for more details. *)
+
external unsafe_get: ('a, 'b, 'c) t -> int -> int -> 'a
= "%caml_ba_unsafe_ref_2"
- (** Like {!Bigarray.Array2.get}, but bounds checking is not always
- performed. *)
+ (** Like {!Bigarray.Array2.get}, but bounds checking is not always performed.
+ Use with caution and only when the program logic guarantees that
+ the access is within bounds and the big array has not been released. *)
external unsafe_set: ('a, 'b, 'c) t -> int -> int -> 'a -> unit
= "%caml_ba_unsafe_set_2"
- (** Like {!Bigarray.Array2.set}, but bounds checking is not always
- performed. *)
+ (** Like {!Bigarray.Array2.set}, but bounds checking is not always performed.
+ Use with caution and only when the program logic guarantees that
+ the access is within bounds and the big array has not been released. *)
end
@@ -729,15 +759,21 @@ module Array3 :
(** Memory mapping of a file as a three-dimensional big array.
See {!Bigarray.Genarray.map_file} for more details. *)
+ val release: ('a, 'b, 'c) t -> unit
+ (** Explicit release of the resources associated with the big array.
+ See {!Bigarray.Genarray.release} for more details. *)
+
external unsafe_get: ('a, 'b, 'c) t -> int -> int -> int -> 'a
= "%caml_ba_unsafe_ref_3"
- (** Like {!Bigarray.Array3.get}, but bounds checking is not always
- performed. *)
+ (** Like {!Bigarray.Array3.get}, but bounds checking is not always performed.
+ Use with caution and only when the program logic guarantees that
+ the access is within bounds and the big array has not been released. *)
external unsafe_set: ('a, 'b, 'c) t -> int -> int -> int -> 'a -> unit
= "%caml_ba_unsafe_set_3"
- (** Like {!Bigarray.Array3.set}, but bounds checking is not always
- performed. *)
+ (** Like {!Bigarray.Array3.set}, but bounds checking is not always performed.
+ Use with caution and only when the program logic guarantees that
+ the access is within bounds and the big array has not been released. *)
end
diff --git a/otherlibs/bigarray/bigarray_stubs.c b/otherlibs/bigarray/bigarray_stubs.c
index c66ccbcc3..4021b74ae 100644
--- a/otherlibs/bigarray/bigarray_stubs.c
+++ b/otherlibs/bigarray/bigarray_stubs.c
@@ -496,18 +496,19 @@ CAMLprim value caml_ba_layout(value vb)
return Val_int(Caml_ba_array_val(vb)->flags & CAML_BA_LAYOUT_MASK);
}
-/* Finalization of a big array */
+/* Finalization / release of a big array */
static void caml_ba_finalize(value v)
{
struct caml_ba_array * b = Caml_ba_array_val(v);
+ intnat i;
switch (b->flags & CAML_BA_MANAGED_MASK) {
case CAML_BA_EXTERNAL:
break;
case CAML_BA_MANAGED:
if (b->proxy == NULL) {
- free(b->data);
+ free(b->data); /* no op if b->data = NULL */
} else {
if (-- b->proxy->refcount == 0) {
free(b->proxy->data);
@@ -526,6 +527,17 @@ static void caml_ba_finalize(value v)
}
break;
}
+ /* Make sure that subsequent accesses to the bigarray fail (empty bounds)
+ and that subsequent calls to caml_ba_finalize do nothing. */
+ for (i = 0; i < b->num_dims; i++) b->dim[i] = 0;
+ b->data = NULL;
+ b->proxy = NULL;
+}
+
+CAMLprim value caml_ba_release(value v)
+{
+ caml_ba_finalize(v);
+ return Val_unit;
}
/* Comparison of two big arrays */
diff --git a/otherlibs/bigarray/mmap_unix.c b/otherlibs/bigarray/mmap_unix.c
index f10835443..30294cc4b 100644
--- a/otherlibs/bigarray/mmap_unix.c
+++ b/otherlibs/bigarray/mmap_unix.c
@@ -152,11 +152,14 @@ CAMLprim value caml_ba_map_file(value vfd, value vkind, value vlayout,
}
/* Determine offset so that the mapping starts at the given file pos */
page = getpagesize();
- delta = (uintnat) (startpos % page);
+ delta = (uintnat) startpos % page;
/* Do the mmap */
shared = Bool_val(vshared) ? MAP_SHARED : MAP_PRIVATE;
- addr = mmap(NULL, array_size + delta, PROT_READ | PROT_WRITE,
- shared, fd, startpos - delta);
+ if (array_size > 0)
+ addr = mmap(NULL, array_size + delta, PROT_READ | PROT_WRITE,
+ shared, fd, startpos - delta);
+ else
+ addr = NULL; /* PR#5463 - mmap fails on empty region */
caml_leave_blocking_section();
if (addr == (void *) MAP_FAILED) caml_sys_error(NO_ARG);
addr = (void *) ((uintnat) addr + delta);
@@ -166,8 +169,8 @@ CAMLprim value caml_ba_map_file(value vfd, value vkind, value vlayout,
#else
-value caml_ba_map_file(value vfd, value vkind, value vlayout,
- value vshared, value vdim, value vpos)
+CAMLprim value caml_ba_map_file(value vfd, value vkind, value vlayout,
+ value vshared, value vdim, value vpos)
{
caml_invalid_argument("Bigarray.map_file: not supported");
return Val_unit;
@@ -186,6 +189,12 @@ void caml_ba_unmap_file(void * addr, uintnat len)
#if defined(HAS_MMAP)
uintnat page = getpagesize();
uintnat delta = (uintnat) addr % page;
- munmap((void *)((uintnat)addr - delta), len + delta);
+ if (len == 0) return; /* PR#5463 */
+ addr = (void *)((uintnat)addr - delta);
+ len = len + delta;
+#if defined(_POSIX_SYNCHRONIZED_IO)
+ msync(addr, len, MS_ASYNC); /* PR#3571 */
+#endif
+ munmap(addr, len);
#endif
}