changed the API of CCBigstring.map_file

This commit is contained in:
Simon Cruanes 2014-12-18 19:25:34 +01:00
parent a63d095722
commit 88ceaa0d99
2 changed files with 15 additions and 10 deletions

View file

@ -207,13 +207,17 @@ let to_gen_slice a i len =
let map_file_descr ?pos ?(shared=false) fd len = let map_file_descr ?pos ?(shared=false) fd len =
B.map_file fd ?pos Bigarray.char Bigarray.c_layout shared len B.map_file fd ?pos Bigarray.char Bigarray.c_layout shared len
let with_map_file ?pos ?(mode=0o644) ?(flags=[Unix.O_RDONLY]) ?shared name len f = let with_map_file ?pos ?len ?(mode=0o644) ?(flags=[Open_rdonly]) ?shared name f =
let fd = Unix.openfile name flags mode in let ic = open_in_gen flags mode name in
let a = map_file_descr ?pos ?shared fd len in let len = match len with
| None -> in_channel_length ic
| Some n -> n
in
let a = map_file_descr ?pos ?shared (Unix.descr_of_in_channel ic) len in
try try
let x = f a in let x = f a in
Unix.close fd; close_in ic;
x x
with e -> with e ->
Unix.close fd; close_in ic;
raise e raise e

View file

@ -107,11 +107,12 @@ val to_gen_slice : t -> int -> int -> char gen
(** {2 Memory-map} *) (** {2 Memory-map} *)
val with_map_file : val with_map_file :
?pos:int64 -> ?mode:int -> ?flags:Unix.open_flag list -> ?shared:bool -> ?pos:int64 -> ?len:int -> ?mode:int -> ?flags:open_flag list -> ?shared:bool ->
string -> int -> (t -> 'a) -> 'a string -> (t -> 'a) -> 'a
(** [with_map_file name len f] maps the file into memory, opening it, and (** [with_map_file name f] maps the file into memory, opening it, and
call [f] with a slice [pos.... pos+len] of the bytes of the file. When call [f] with a slice [pos.... pos+len] of the bytes of the file
[f] returns, the file is closed. where [len] is the length of the file if not provided.
When [f] returns, the file is closed.
@param pos offset in the file (default 0) @param pos offset in the file (default 0)
@param shared if true, modifications are shared between processes that @param shared if true, modifications are shared between processes that
have mapped this file (requires the filedescr to be open in write mode). have mapped this file (requires the filedescr to be open in write mode).