| 1 |
|
| 2 |
|
| 3 |
open Base |
| 4 |
open Printf |
| 5 |
open Bigarray |
| 6 |
|
| 7 |
type pos = int [@@deriving sexp_of] |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
exception Buffer_short |
| 12 |
exception No_variant_match |
| 13 |
|
| 14 |
module ReadError = struct |
| 15 |
type t = |
| 16 |
| Neg_int8 |
| 17 |
| Int_code |
| 18 |
| Int_overflow |
| 19 |
| Nat0_code |
| 20 |
| Nat0_overflow |
| 21 |
| Int32_code |
| 22 |
| Int64_code |
| 23 |
| Nativeint_code |
| 24 |
| Unit_code |
| 25 |
| Bool_code |
| 26 |
| Option_code |
| 27 |
| String_too_long |
| 28 |
| Variant_tag |
| 29 |
| Array_too_long |
| 30 |
| List_too_long of |
| 31 |
{ len : int |
| 32 |
; max_len : int |
| 33 |
} |
| 34 |
| Hashtbl_too_long |
| 35 |
| Sum_tag of string |
| 36 |
| Variant of string |
| 37 |
| Poly_rec_bound of string |
| 38 |
| Variant_wrong_type of string |
| 39 |
| Silly_type of string |
| 40 |
| Empty_type of string |
| 41 |
|
| 42 |
let to_string = function |
| 43 |
| Neg_int8 -> "Neg_int8" |
| 44 |
| Int_code -> "Int_code" |
| 45 |
| Int_overflow -> "Int_overflow" |
| 46 |
| Nat0_code -> "Nat0_code" |
| 47 |
| Nat0_overflow -> "Nat0_overflow" |
| 48 |
| Int32_code -> "Int32_code" |
| 49 |
| Int64_code -> "Int64_code" |
| 50 |
| Nativeint_code -> "Nativeint_code" |
| 51 |
| Unit_code -> "Unit_code" |
| 52 |
| Bool_code -> "Bool_code" |
| 53 |
| Option_code -> "Option_code" |
| 54 |
| String_too_long -> "String_too_long" |
| 55 |
| Variant_tag -> "Variant_tag" |
| 56 |
| Array_too_long -> "Array_too_long" |
| 57 |
| List_too_long { len; max_len } -> sprintf "List_too_long / %d (max %d)" len max_len |
| 58 |
| Hashtbl_too_long -> "Hashtbl_too_long" |
| 59 |
| Sum_tag loc -> "Sum_tag / " ^ loc |
| 60 |
| Variant loc -> "Variant / " ^ loc |
| 61 |
| Poly_rec_bound loc -> "Poly_rec_bound / " ^ loc |
| 62 |
| Variant_wrong_type loc -> "Variant_wrong_type / " ^ loc |
| 63 |
| Silly_type loc -> "Silly_type / " ^ loc |
| 64 |
| Empty_type loc -> "Empty_type / " ^ loc |
| 65 |
;; |
| 66 |
|
| 67 |
let sexp_of_t t = Sexp.Atom (to_string t) |
| 68 |
end |
| 69 |
|
| 70 |
exception Read_error of ReadError.t * pos [@@deriving sexp_of] |
| 71 |
exception Poly_rec_write of string |
| 72 |
exception Empty_type of string |
| 73 |
|
| 74 |
let raise_read_error err pos = raise (Read_error (err, pos)) |
| 75 |
|
| 76 |
let raise_variant_wrong_type name pos = |
| 77 |
raise (Read_error (ReadError.Variant_wrong_type name, pos)) |
| 78 |
;; |
| 79 |
|
| 80 |
let raise_concurrent_modification loc = failwith (loc ^ ": concurrent modification") |
| 81 |
let array_bound_error () = invalid_arg "index out of bounds" |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
type pos_ref = pos ref |
| 86 |
type buf = (char, int8_unsigned_elt, c_layout) Array1.t |
| 87 |
|
| 88 |
let create_buf n = Array1.create Bigarray.char c_layout n |
| 89 |
let buf_len buf = Array1.dim buf |
| 90 |
let assert_pos pos = if pos < 0 then array_bound_error () |
| 91 |
let check_pos (buf : buf) pos = if pos >= Array1.dim buf then raise Buffer_short |
| 92 |
|
| 93 |
let safe_get_pos buf pos_ref = |
| 94 |
let pos = !pos_ref in |
| 95 |
check_pos buf pos; |
| 96 |
pos |
| 97 |
;; |
| 98 |
|
| 99 |
let check_next (buf : buf) next = if next > Array1.dim buf then raise Buffer_short |
| 100 |
|
| 101 |
let get_opt_pos ~loc ~var = function |
| 102 |
| Some pos -> |
| 103 |
if pos < 0 then invalid_arg (sprintf "Bin_prot.Common.%s: %s < 0" loc var); |
| 104 |
pos |
| 105 |
| None -> 0 |
| 106 |
;; |
| 107 |
|
| 108 |
external unsafe_blit_buf |
| 109 |
: src_pos:int |
| 110 |
-> src:(buf[@local_opt]) |
| 111 |
-> dst_pos:int |
| 112 |
-> dst:buf |
| 113 |
-> len:int |
| 114 |
-> unit |
| 115 |
= "bin_prot_blit_buf_stub" |
| 116 |
|
| 117 |
let blit_buf ?src_pos ~src ?dst_pos ~dst len = |
| 118 |
let loc = "blit_buf" in |
| 119 |
let src_pos = get_opt_pos ~loc ~var:"src_pos" src_pos in |
| 120 |
let dst_pos = get_opt_pos ~loc ~var:"dst_pos" dst_pos in |
| 121 |
if len < 0 |
| 122 |
then invalid_arg "Bin_prot.Common.blit_buf: len < 0" |
| 123 |
else if len = 0 |
| 124 |
then ( |
| 125 |
if src_pos > Array1.dim src |
| 126 |
then invalid_arg "Bin_prot.Common.blit_buf: src_pos > src_len"; |
| 127 |
if dst_pos > Array1.dim dst |
| 128 |
then invalid_arg "Bin_prot.Common.blit_buf: dst_pos > dst_len") |
| 129 |
else if src_pos + len > Array1.dim src |
| 130 |
then invalid_arg "Bin_prot.Common.blit_buf: src_pos + len > src_len" |
| 131 |
else if dst_pos + len > Array1.dim dst |
| 132 |
then invalid_arg "Bin_prot.Common.blit_buf: dst_pos + len > dst_len" |
| 133 |
else unsafe_blit_buf ~src_pos ~src ~dst_pos ~dst ~len |
| 134 |
;; |
| 135 |
|
| 136 |
external unsafe_blit_string_buf |
| 137 |
: src_pos:int |
| 138 |
-> (string[@local_opt]) |
| 139 |
-> dst_pos:int |
| 140 |
-> buf |
| 141 |
-> len:int |
| 142 |
-> unit |
| 143 |
= "bin_prot_blit_string_buf_stub" |
| 144 |
[@@noalloc] |
| 145 |
|
| 146 |
external unsafe_blit_bytes_buf |
| 147 |
: src_pos:int |
| 148 |
-> (bytes[@local_opt]) |
| 149 |
-> dst_pos:int |
| 150 |
-> buf |
| 151 |
-> len:int |
| 152 |
-> unit |
| 153 |
= "bin_prot_blit_bytes_buf_stub" |
| 154 |
[@@noalloc] |
| 155 |
|
| 156 |
let blit_string_buf ?src_pos str ?dst_pos buf ~len = |
| 157 |
let loc = "blit_string_buf" in |
| 158 |
let src_pos = get_opt_pos ~loc ~var:"src_pos" src_pos in |
| 159 |
let dst_pos = get_opt_pos ~loc ~var:"dst_pos" dst_pos in |
| 160 |
if len < 0 |
| 161 |
then invalid_arg "Bin_prot.Common.blit_string_buf: len < 0" |
| 162 |
else if len = 0 |
| 163 |
then ( |
| 164 |
if src_pos > String.length str |
| 165 |
then invalid_arg "Bin_prot.Common.blit_string_buf: src_pos > str_len"; |
| 166 |
if dst_pos > Array1.dim buf |
| 167 |
then invalid_arg "Bin_prot.Common.blit_string_buf: src_pos > buf") |
| 168 |
else if src_pos + len > String.length str |
| 169 |
then invalid_arg "Bin_prot.Common.blit_string_buf: src_pos + len > str_len" |
| 170 |
else if dst_pos + len > Array1.dim buf |
| 171 |
then invalid_arg "Bin_prot.Common.blit_string_buf: src_pos + len > buf" |
| 172 |
else unsafe_blit_string_buf ~src_pos str ~dst_pos buf ~len |
| 173 |
;; |
| 174 |
|
| 175 |
let blit_bytes_buf ?src_pos str ?dst_pos buf ~len = |
| 176 |
let loc = "blit_bytes_buf" in |
| 177 |
let src_pos = get_opt_pos ~loc ~var:"src_pos" src_pos in |
| 178 |
let dst_pos = get_opt_pos ~loc ~var:"dst_pos" dst_pos in |
| 179 |
if len < 0 |
| 180 |
then invalid_arg "Bin_prot.Common.blit_bytes_buf: len < 0" |
| 181 |
else if len = 0 |
| 182 |
then ( |
| 183 |
if src_pos > Bytes.length str |
| 184 |
then invalid_arg "Bin_prot.Common.blit_bytes_buf: src_pos > str_len"; |
| 185 |
if dst_pos > Array1.dim buf |
| 186 |
then invalid_arg "Bin_prot.Common.blit_bytes_buf: src_pos > buf") |
| 187 |
else if src_pos + len > Bytes.length str |
| 188 |
then invalid_arg "Bin_prot.Common.blit_bytes_buf: src_pos + len > str_len" |
| 189 |
else if dst_pos + len > Array1.dim buf |
| 190 |
then invalid_arg "Bin_prot.Common.blit_bytes_buf: src_pos + len > buf" |
| 191 |
else unsafe_blit_bytes_buf ~src_pos str ~dst_pos buf ~len |
| 192 |
;; |
| 193 |
|
| 194 |
external unsafe_blit_buf_string |
| 195 |
: src_pos:int |
| 196 |
-> buf |
| 197 |
-> dst_pos:int |
| 198 |
-> bytes |
| 199 |
-> len:int |
| 200 |
-> unit |
| 201 |
= "bin_prot_blit_buf_bytes_stub" |
| 202 |
[@@noalloc] |
| 203 |
|
| 204 |
external unsafe_blit_buf_bytes |
| 205 |
: src_pos:int |
| 206 |
-> buf |
| 207 |
-> dst_pos:int |
| 208 |
-> bytes |
| 209 |
-> len:int |
| 210 |
-> unit |
| 211 |
= "bin_prot_blit_buf_bytes_stub" |
| 212 |
[@@noalloc] |
| 213 |
|
| 214 |
let blit_buf_bytes ?src_pos buf ?dst_pos str ~len = |
| 215 |
let loc = "blit_buf_string" in |
| 216 |
let src_pos = get_opt_pos ~loc ~var:"src_pos" src_pos in |
| 217 |
let dst_pos = get_opt_pos ~loc ~var:"dst_pos" dst_pos in |
| 218 |
if len < 0 |
| 219 |
then invalid_arg "Bin_prot.Common.blit_buf_string: len < 0" |
| 220 |
else if len = 0 |
| 221 |
then ( |
| 222 |
if src_pos > Array1.dim buf |
| 223 |
then invalid_arg "Bin_prot.Common.blit_buf_string: src_pos > buf_len"; |
| 224 |
if dst_pos > Bytes.length str |
| 225 |
then invalid_arg "Bin_prot.Common.blit_buf_string: src_pos > str_len") |
| 226 |
else if src_pos + len > Array1.dim buf |
| 227 |
then invalid_arg "Bin_prot.Common.blit_buf_string: src_pos + len > buf_len" |
| 228 |
else if dst_pos + len > Bytes.length str |
| 229 |
then invalid_arg "Bin_prot.Common.blit_buf_string: src_pos + len > str_len" |
| 230 |
else unsafe_blit_buf_bytes ~src_pos buf ~dst_pos str ~len |
| 231 |
;; |
| 232 |
|
| 233 |
let blit_buf_string = blit_buf_bytes |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
let rec copy_htbl_list htbl = function |
| 238 |
| [] -> htbl |
| 239 |
| (k, v) :: rest -> |
| 240 |
Stdlib.Hashtbl.add htbl k v; |
| 241 |
copy_htbl_list htbl rest |
| 242 |
;; |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
type vec32 = (float, float32_elt, fortran_layout) Array1.t |
| 247 |
type vec64 = (float, float64_elt, fortran_layout) Array1.t |
| 248 |
type vec = vec64 |
| 249 |
type mat32 = (float, float32_elt, fortran_layout) Array2.t |
| 250 |
type mat64 = (float, float64_elt, fortran_layout) Array2.t |
| 251 |
type mat = mat64 |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
external unsafe_blit_float_array_buf |
| 256 |
: src_pos:int |
| 257 |
-> (float array[@local_opt]) |
| 258 |
-> dst_pos:int |
| 259 |
-> buf |
| 260 |
-> len:int |
| 261 |
-> unit |
| 262 |
= "bin_prot_blit_float_array_buf_stub" |
| 263 |
[@@noalloc] |
| 264 |
|
| 265 |
external unsafe_blit_buf_float_array |
| 266 |
: src_pos:int |
| 267 |
-> buf |
| 268 |
-> dst_pos:int |
| 269 |
-> float array |
| 270 |
-> len:int |
| 271 |
-> unit |
| 272 |
= "bin_prot_blit_buf_float_array_stub" |
| 273 |
[@@noalloc] |
| 274 |
|
| 275 |
external unsafe_blit_floatarray_buf |
| 276 |
: src_pos:int |
| 277 |
-> (floatarray[@local_opt]) |
| 278 |
-> dst_pos:int |
| 279 |
-> buf |
| 280 |
-> len:int |
| 281 |
-> unit |
| 282 |
= "bin_prot_blit_float_array_buf_stub" |
| 283 |
[@@noalloc] |
| 284 |
|
| 285 |
external unsafe_blit_buf_floatarray |
| 286 |
: src_pos:int |
| 287 |
-> buf |
| 288 |
-> dst_pos:int |
| 289 |
-> floatarray |
| 290 |
-> len:int |
| 291 |
-> unit |
| 292 |
= "bin_prot_blit_buf_float_array_stub" |
| 293 |
[@@noalloc] |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
let ( + ) = ( + ) |