Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing and reading to a memory mapped file in OCaml

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.

like image 489
user2991422 Avatar asked Jan 11 '23 13:01

user2991422


2 Answers

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.

like image 102
rgrinberg Avatar answered Feb 11 '23 12:02

rgrinberg


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.

like image 29
Jonathan Protzenko Avatar answered Feb 11 '23 11:02

Jonathan Protzenko