123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128(***************************************************************************)(* bytearray.ml : functions for efficient marshaling to and from bigarrays *)(* *)(* Copyright 1999-2011, Jérôme Vouillon *)(* *)(* 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. A special linking *)(* exception to the GNU Lesser General Public License applies to this *)(* library, see the LICENSE file for more information. *)(***************************************************************************)openBigarraytypet=(char,int8_unsigned_elt,c_layout)Array1.ttypetf=(float,float64_elt,c_layout)Array1.tletlength=Bigarray.Array1.dimletcreatel=Bigarray.Array1.createBigarray.charBigarray.c_layoutlletcreatefl=Bigarray.Array1.createBigarray.float64Bigarray.c_layoutl(*
let unsafe_blit_from_string s i a j l =
for k = 0 to l - 1 do
a.{j + k} <- s.[i + k]
done
let unsafe_blit_to_string a i s j l =
for k = 0 to l - 1 do
s.[j + k] <- a.{i + k}
done
*)externalunsafe_blit_from_string:string->int->t->int->int->unit="ml_blit_string_to_bigarray"[@@noalloc](*
external unsafe_blit_to_bytes : t -> int -> bytes -> int -> int -> unit
= "ml_blit_bigarray_to_string" "noalloc"
*)(*
let unsafe_sub a ofs len =
let s = Bytes.create len in
unsafe_blit_to_bytes a ofs s 0 len;
Bytes.to_string s
*)(*
let to_string a =
let l = length a in
if l > Sys.max_string_length then invalid_arg "Bytearray.to_string" else
unsafe_sub a 0 l
*)letof_strings=letl=String.lengthsinleta=createlinunsafe_blit_from_strings0a0l;aletmmap_of_stringfds=letl=String.lengthsinletba=Bigarray.array1_of_genarray@@Parmap_compat.map_filefdBigarray.charBigarray.c_layouttrue[|l|]inunsafe_blit_from_strings0ba0l;ba(*
let sub a ofs len =
if
ofs < 0 || len < 0 || ofs > length a - len || len > Sys.max_string_length
then
invalid_arg "Bytearray.sub"
else
unsafe_sub a ofs len
*)letrecprefix_recaia'i'l=l=0||(a.{i}=a'.{i'}&&prefix_reca(i+1)a'(i'+1)(l-1))letprefixaa'i=letl=lengthainletl'=lengtha'ini<=l'-l&&prefix_reca0a'illetblit_from_stringsiajl=ifl<0||i<0||i>String.lengths-l||j<0||j>lengtha-ltheninvalid_arg"Bytearray.blit_from_string"elseunsafe_blit_from_stringsiajl(*
let blit_to_bytes a i s j l =
if l < 0 || i < 0 || i > length a - l
|| j < 0 || j > Bytes.length s - l
then invalid_arg "Bytearray.blit_to_bytes"
else unsafe_blit_to_bytes a i s j l
*)externalmarshal:'a->Marshal.extern_flagslist->t="ml_marshal_to_bigarray"externalmarshal_to_buffer:t->int->'a->Marshal.extern_flagslist->int="ml_marshal_to_bigarray_buffer"externalunmarshal:t->int->'a="ml_unmarshal_from_bigarray"externalunsafe_blit_from_floatarray:floatarray->int->tf->int->int->unit="ml_blit_floatarray_to_bigarray"[@@noalloc]externalunsafe_blit_to_floatarray:tf->int->floatarray->int->int->unit="ml_blit_bigarray_to_floatarray"[@@noalloc]letto_floatarrayal=letfa=Array.create_floatlinunsafe_blit_to_floatarraya0fa0l;faletto_this_floatarrayfaal=unsafe_blit_to_floatarraya0fa0l;faletof_floatarrayfa=letl=Array.lengthfainleta=createflinunsafe_blit_from_floatarrayfa0a0l;a