I am experimenting in OCaml to see how I can read/write a numerical array to/from a memory mapped file.
I think I'll need to use Bigarray but not sure how to write down a Bigarray array to a memory mapped file, and then read it back?
I cannot seem to find an example anywhere. I have checked the source code of Jane St. core but to no avail.
Since it seems like you're interested in using core:
Here's map_file
from their docs (https://ocaml.janestreet.com/ocaml-core/109.58.00/doc/core/#Bigstring)
val map_file : shared:bool -> Unix.file_descr -> int -> t
map_file shared fd n memory-maps n characters of the data associated with descriptor fd to a bigstring. Iff shared is true, all changes to the bigstring will be reflected in the file.
You should accept the answer Jonathan creates however.
I'm making this an answer (I was originally afraid that I missed your point). The OCaml library provides a mechanism for this by default, namely, the map_file
function from the Bigarray.Genarray
module. It does precisely what you want and amounts to mmap
'ing the file under Unix. I'm unsure about the Windows specifics.
This has the advantage of being well-supported and well-tested, as well as not requiring you to import JaneSt's Core into your project if you didn't need it already.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With