Memory mapping of a file as a Bigarray. map_file fd kind layout shared dims
returns a Bigarray of kind kind
, layout layout
, and dimensions as specified in dims
. The data contained in this Bigarray are the contents of the file referred to by the file descriptor fd
(as opened previously with openfile
, for example). The optional pos
parameter is the byte offset in the file of the data being mapped; it defaults to 0 (map from the beginning of the file).
If shared
is true
, all modifications performed on the array are reflected in the file. This requires that fd
be opened with write permissions. If shared
is false
, modifications performed on the array are done in memory only, using copy-on-write of the modified pages; the underlying file is not affected.
map_file
is much more efficient than reading the whole file in a Bigarray, modifying that Bigarray, and writing it afterwards.
To adjust automatically the dimensions of the Bigarray to the actual size of the file, the major dimension (that is, the first dimension for an array with C layout, and the last dimension for an array with Fortran layout) can be given as -1
. map_file
then determines the major dimension from the size of the file. The file must contain an integral number of sub-arrays as determined by the non-major dimensions, otherwise Failure
is raised.
If all dimensions of the Bigarray are given, the file size is matched against the size of the Bigarray. If the file is larger than the Bigarray, only the initial portion of the file is mapped to the Bigarray. If the file is smaller than the big array, the file is automatically grown to the size of the Bigarray. This requires write permissions on fd
.
Array accesses are bounds-checked, but the bounds are determined by the initial call to map_file
. Therefore, you should make sure no other process modifies the mapped file while you're accessing it, or a SIGBUS signal may be raised. This happens, for instance, if the file is shrunk.
Invalid_argument
or Failure
may be raised in cases where argument validation fails.