Virtual memory mapping seems like the deluxe version of what I think you all are talking about here. Browsing around online for mmap() support, I see
The OCaml library provides a mechanism for this by default, namely, the map_file function from the Bigarray.Genarray module.
… but documents I have on hand show no such function.
[edit]
Oh, I see it’s actually in Unix. So,
let map_string_file fd =
let st = Unix.fstat fd
in let gen = Unix.map_file fd Bigarray.Char
Bigarray.c_layout false [|st.st_size|]
in Bigarray.array1_of_genarray gen
;;
let a = map_string_file (Unix.openfile Sys.argv.(1)
[Unix.O_RDWR] 0o640)
in Printf.printf "%c%c%c%c\n" a.{0} a.{1} a.{2} a.{3}
Prints out the first 4 chars of the mapped file. I’m a little weak on the ocaml type system, this could be messed up. I assume that the array1_of_genarray doesn’t copy anything.