1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
(******************************************************************************
* Core *
* *
* Copyright (C) 2008- Jane Street Holding, LLC *
* Contact: opensource@janestreet.com *
* WWW: http://www.janestreet.com/ocaml *
* *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
* *
******************************************************************************)
INCLUDE "config.mlh"
open Printf
open Unix
open Bigarray
open Common
open Sexplib.Std
module Z : sig
type t = (char, int8_unsigned_elt, c_layout) Array1.t
include Binable.S with type binable = t
end = struct
type bigstring = (char, int8_unsigned_elt, c_layout) Array1.t
include Bin_prot.Std
type t = bigstring with bin_io
type binable = t
end
include Z
exception IOError of int * exn with sexp
external init : unit -> unit = "bigstring_init_stub"
let () =
Callback.register_exception "Bigstring.End_of_file" End_of_file;
Callback.register_exception "Bigstring.IOError" (IOError (0, Exit));
init ()
let create n = Array1.create Bigarray.char c_layout n
let length (bstr : t) = Array1.dim bstr
external is_mmapped : t -> bool = "bigstring_is_mmapped_stub" "noalloc"
let check_args ~loc ~pos ~len (bstr : t) =
if pos < 0 then invalid_arg (loc ^ ": pos < 0");
if len < 0 then invalid_arg (loc ^ ": len < 0");
let bstr_len = length bstr in
if bstr_len < pos + len then
invalid_arg (sprintf "Bigstring.%s: length(bstr) < pos + len" loc)
let get_opt_pos ~loc ~var = function
| None -> 0
| Some pos ->
if pos < 0 then invalid_arg (sprintf "Bigstring.%s: %s < 0" loc var);
pos
let get_opt_len bstr ~pos = function
| Some len -> len
| None -> length bstr - pos
let check_min_len ~loc ~len = function
| None -> 0
| Some min_len ->
if min_len > len then (
let msg = sprintf "%s: min_len (%d) > len (%d)" loc min_len len in
invalid_arg msg);
if min_len < 0 then (
let msg = sprintf "%s: min_len (%d) < 0" loc min_len in
invalid_arg msg);
min_len
let sub_shared ?(pos = 0) ?len (bstr : t) =
let len = get_opt_len bstr ~pos len in
Array1.sub bstr pos len
(* Blitting *)
external unsafe_blit :
src : t -> src_pos : int -> dst : t -> dst_pos : int -> len : int -> unit
= "bigstring_blit_stub"
let blit_common
~loc ~get_src_len ~get_dst_len ~blit ~src ~src_pos ~dst ~dst_pos ~len =
if len < 0 then invalid_argf "%s: len < 0" loc ()
else
let check_pos var total_len pos =
if pos < 0 then invalid_argf "%s: %s < 0" loc var ()
else if pos + len > total_len then
invalid_argf "%s: pos (%d) + len (%d) > total_len (%d)"
loc pos len total_len ()
in
check_pos "src_pos" (get_src_len src) src_pos;
check_pos "dst_pos" (get_dst_len dst) dst_pos;
if len > 0 then blit ~src ~src_pos ~dst ~dst_pos ~len
let blit ~src ~src_pos ~dst ~dst_pos ~len =
blit_common
~loc:"blit"
~get_src_len:length ~get_dst_len:length
~blit:unsafe_blit
~src ~src_pos ~dst ~dst_pos ~len
let sub ?(pos = 0) ?len bstr =
let len = get_opt_len bstr ~pos len in
let dst = create len in
blit ~src:bstr ~src_pos:pos ~dst ~dst_pos:0 ~len;
dst
let get bstr pos = Array1.get bstr pos
external unsafe_blit_string_bigstring :
src : string -> src_pos : int -> dst : t -> dst_pos : int -> len : int -> unit
= "bigstring_blit_string_bigstring_stub" "noalloc"
let blit_string_bigstring ~src ~src_pos ~dst ~dst_pos ~len =
blit_common
~loc:"blit_string_bigstring"
~get_src_len:String.length ~get_dst_len:length
~blit:unsafe_blit_string_bigstring
~src ~src_pos ~dst ~dst_pos ~len
external unsafe_blit_bigstring_string :
src : t -> src_pos : int -> dst : string -> dst_pos : int -> len : int -> unit
= "bigstring_blit_bigstring_string_stub" "noalloc"
let blit_bigstring_string ~src ~src_pos ~dst ~dst_pos ~len =
blit_common
~loc:"blit_bigstring_string"
~get_src_len:length ~get_dst_len:String.length
~blit:unsafe_blit_bigstring_string
~src ~src_pos ~dst ~dst_pos ~len
let of_string ?(pos = 0) ?len src =
let len =
match len with
| Some len -> len
| None -> String.length src - pos
in
let dst = create len in
blit_string_bigstring ~src ~src_pos:pos ~dst ~dst_pos:0 ~len;
dst
let to_string ?(pos = 0) ?len src =
let len = get_opt_len src ~pos len in
check_args ~loc:"to_string" ~pos ~len src;
let dst = String.create len in
blit_bigstring_string ~src ~src_pos:pos ~dst ~dst_pos:0 ~len;
dst
(* Input functions *)
external unsafe_read :
min_len : int -> file_descr -> pos : int -> len : int -> t -> int
= "bigstring_read_stub"
let read ?min_len fd ?(pos = 0) ?len bstr =
let len = get_opt_len bstr ~pos len in
let loc = "read" in
check_args ~loc ~pos ~len bstr;
let min_len = check_min_len ~loc ~len min_len in
unsafe_read ~min_len fd ~pos ~len bstr
let really_read fd ?(pos = 0) ?len bstr =
let len = get_opt_len bstr ~pos len in
ignore (read ~min_len:len fd ~pos ~len bstr)
external unsafe_really_recv :
file_descr -> pos : int -> len : int -> t -> unit
= "bigstring_really_recv_stub"
let really_recv sock ?(pos = 0) ?len bstr =
let len = get_opt_len bstr ~pos len in
check_args ~loc:"really_recv" ~pos ~len bstr;
unsafe_really_recv sock ~pos ~len bstr
external unsafe_recvfrom_assume_fd_is_nonblocking :
file_descr -> pos : int -> len : int -> t -> int * sockaddr
= "bigstring_recvfrom_assume_fd_is_nonblocking_stub"
let recvfrom_assume_fd_is_nonblocking sock ?(pos = 0) ?len bstr =
let len = get_opt_len bstr ~pos len in
check_args ~loc:"recvfrom_assume_fd_is_nonblocking" ~pos ~len bstr;
unsafe_recvfrom_assume_fd_is_nonblocking sock ~pos ~len bstr
external unsafe_read_assume_fd_is_nonblocking :
file_descr -> pos : int -> len : int -> t -> int
= "bigstring_read_assume_fd_is_nonblocking_stub"
let read_assume_fd_is_nonblocking fd ?(pos = 0) ?len bstr =
let len = get_opt_len bstr ~pos len in
check_args ~loc:"read_assume_fd_is_nonblocking" ~pos ~len bstr;
unsafe_read_assume_fd_is_nonblocking fd ~pos ~len bstr
external unsafe_input :
min_len : int -> in_channel -> pos : int -> len : int -> t -> int
= "bigstring_input_stub"
let input ?min_len ic ?(pos = 0) ?len bstr =
let len = get_opt_len bstr ~pos len in
let loc = "input" in
check_args ~loc ~pos ~len bstr;
let min_len = check_min_len ~loc ~len min_len in
unsafe_input ~min_len ic ~pos ~len bstr
let really_input ic ?(pos = 0) ?len bstr =
let len = get_opt_len bstr ~pos len in
check_args ~loc:"really_input" ~pos ~len bstr;
ignore (unsafe_input ~min_len:len ic ~pos ~len bstr)
(* Output functions *)
external unsafe_output :
min_len : int -> out_channel -> pos : int -> len : int -> t -> int
= "bigstring_output_stub"
let output ?min_len oc ?(pos = 0) ?len bstr =
let len = get_opt_len bstr ~pos len in
let loc = "output" in
check_args ~loc ~pos ~len bstr;
let min_len = check_min_len ~loc ~len min_len in
unsafe_output oc ~min_len ~pos ~len bstr
let really_output oc ?(pos = 0) ?len bstr =
let len = get_opt_len bstr ~pos len in
check_args ~loc:"really_output" ~pos ~len bstr;
ignore (unsafe_output oc ~min_len:len ~pos ~len bstr)
external unsafe_really_write :
file_descr -> pos : int -> len : int -> t -> unit
= "bigstring_really_write_stub"
let really_write fd ?(pos = 0) ?len bstr =
let len = get_opt_len bstr ~pos len in
check_args ~loc:"really_write" ~pos ~len bstr;
unsafe_really_write fd ~pos ~len bstr
IFDEF MSG_NOSIGNAL THEN
external unsafe_really_send_no_sigpipe :
file_descr -> pos : int -> len : int -> t -> unit
= "bigstring_really_send_no_sigpipe_stub"
let really_send_no_sigpipe fd ?(pos = 0) ?len bstr =
let len = get_opt_len bstr ~pos len in
check_args ~loc:"really_send_no_sigpipe" ~pos ~len bstr;
unsafe_really_send_no_sigpipe fd ~pos ~len bstr
external unsafe_send_nonblocking_no_sigpipe :
file_descr -> pos : int -> len : int -> t -> int
= "bigstring_send_nonblocking_no_sigpipe_stub"
let unsafe_send_nonblocking_no_sigpipe fd ~pos ~len buf =
let res = unsafe_send_nonblocking_no_sigpipe fd ~pos ~len buf in
if res = -1 then None
else Some res
let send_nonblocking_no_sigpipe fd ?(pos = 0) ?len bstr =
let len = get_opt_len bstr ~pos len in
check_args ~loc:"send_nonblocking_no_sigpipe" ~pos ~len bstr;
unsafe_send_nonblocking_no_sigpipe fd ~pos ~len bstr
external unsafe_sendto_nonblocking_no_sigpipe :
file_descr -> pos : int -> len : int -> t -> sockaddr -> int
= "bigstring_sendto_nonblocking_no_sigpipe_stub"
let unsafe_sendto_nonblocking_no_sigpipe fd ~pos ~len buf sockaddr =
let res = unsafe_sendto_nonblocking_no_sigpipe fd ~pos ~len buf sockaddr in
if res = -1 then None
else Some res
let sendto_nonblocking_no_sigpipe fd ?(pos = 0) ?len bstr sockaddr =
let len = get_opt_len bstr ~pos len in
check_args ~loc:"sendto_nonblocking_no_sigpipe" ~pos ~len bstr;
unsafe_sendto_nonblocking_no_sigpipe fd ~pos ~len bstr sockaddr
ENDIF
external unsafe_write :
file_descr -> pos : int -> len : int -> t -> int = "bigstring_write_stub"
let write fd ?(pos = 0) ?len bstr =
let len = get_opt_len bstr ~pos len in
check_args ~loc:"write" ~pos ~len bstr;
unsafe_write fd ~pos ~len bstr
external unsafe_write_assume_fd_is_nonblocking :
file_descr -> pos : int -> len : int -> t -> int
= "bigstring_write_assume_fd_is_nonblocking_stub"
let write_assume_fd_is_nonblocking fd ?(pos = 0) ?len bstr =
let len = get_opt_len bstr ~pos len in
check_args ~loc:"write_assume_fd_is_nonblocking" ~pos ~len bstr;
unsafe_write_assume_fd_is_nonblocking fd ~pos ~len bstr
external unsafe_writev :
file_descr -> t Core_unix.IOVec.t array -> int -> int
= "bigstring_writev_stub"
let get_iovec_count loc iovecs = function
| None -> Array.length iovecs
| Some count ->
if count < 0 then invalid_arg (loc ^ ": count < 0");
let n_iovecs = Array.length iovecs in
if count > n_iovecs then invalid_arg (loc ^ ": count > n_iovecs");
count
let writev fd ?count iovecs =
let count = get_iovec_count "writev" iovecs count in
unsafe_writev fd iovecs count
external unsafe_writev_assume_fd_is_nonblocking :
file_descr -> t Core_unix.IOVec.t array -> int -> int
= "bigstring_writev_assume_fd_is_nonblocking_stub"
let writev_assume_fd_is_nonblocking fd ?count iovecs =
let count = get_iovec_count "writev_nonblocking" iovecs count in
unsafe_writev_assume_fd_is_nonblocking fd iovecs count
(* Memory mapping *)
let map_file ~shared fd n = Array1.map_file fd Bigarray.char c_layout shared n
IFDEF MSG_NOSIGNAL THEN
(* Input and output, linux only *)
external unsafe_sendmsg_nonblocking_no_sigpipe :
file_descr -> t Core_unix.IOVec.t array -> int -> int
= "bigstring_sendmsg_nonblocking_no_sigpipe_stub"
let unsafe_sendmsg_nonblocking_no_sigpipe fd iovecs count =
let res = unsafe_sendmsg_nonblocking_no_sigpipe fd iovecs count in
if res = -1 then None
else Some res
let sendmsg_nonblocking_no_sigpipe fd ?count iovecs =
let count = get_iovec_count "sendmsg_nonblocking_no_sigpipe" iovecs count in
unsafe_sendmsg_nonblocking_no_sigpipe fd iovecs count
ENDIF
(* Search *)
external unsafe_find : t -> char -> pos:int -> len:int -> int = "bigstring_find"
let find ?(pos = 0) ?len chr bstr =
let len = get_opt_len bstr ~pos len in
check_args ~loc:"find" ~pos ~len bstr;
let res = unsafe_find bstr chr ~pos ~len in
if res < 0 then None else Some res
(* Destruction *)
external unsafe_destroy : t -> unit = "bigstring_destroy_stub"
(* vim: set filetype=ocaml : *)
|