]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/ocaml/ocaml/contrib/otherlibs/threads/marshal.ml
Update
[l4.git] / l4 / pkg / ocaml / ocaml / contrib / otherlibs / threads / marshal.ml
1 (***********************************************************************)
2 (*                                                                     *)
3 (*                           Objective Caml                            *)
4 (*                                                                     *)
5 (*            Xavier Leroy, projet Cristal, INRIA Rocquencourt         *)
6 (*                                                                     *)
7 (*  Copyright 1997 Institut National de Recherche en Informatique et   *)
8 (*  en Automatique.  All rights reserved.  This file is distributed    *)
9 (*  under the terms of the GNU Library General Public License, with    *)
10 (*  the special exception on linking described in file ../../LICENSE.  *)
11 (*                                                                     *)
12 (***********************************************************************)
13
14 (* $Id: marshal.ml 6338 2004-05-27 15:28:05Z doligez $ *)
15
16 type extern_flags =
17     No_sharing
18   | Closures
19
20 external to_string: 'a -> extern_flags list -> string
21     = "caml_output_value_to_string"
22
23 let to_channel chan v flags =
24   output_string chan (to_string v flags)
25
26 external to_buffer_unsafe:
27       string -> int -> int -> 'a -> extern_flags list -> int
28     = "caml_output_value_to_buffer"
29
30 let to_buffer buff ofs len v flags =
31   if ofs < 0 || len < 0 || ofs + len > String.length buff
32   then invalid_arg "Marshal.to_buffer: substring out of bounds"
33   else to_buffer_unsafe buff ofs len v flags
34
35 let to_buffer' ~buf ~pos ~len v ~mode = to_buffer buf pos len v mode
36
37 external from_string_unsafe: string -> int -> 'a
38          = "caml_input_value_from_string"
39 external data_size_unsafe: string -> int -> int = "caml_marshal_data_size"
40
41 let header_size = 20
42 let data_size buff ofs =
43   if ofs < 0 || ofs > String.length buff - header_size
44   then invalid_arg "Marshal.data_size"
45   else data_size_unsafe buff ofs
46 let total_size buff ofs = header_size + data_size buff ofs
47
48 let from_string buff ofs =
49   if ofs < 0 || ofs > String.length buff - header_size
50   then invalid_arg "Marshal.from_size"
51   else begin
52     let len = data_size_unsafe buff ofs in
53     if ofs > String.length buff - (header_size + len)
54     then invalid_arg "Marshal.from_string"
55     else from_string_unsafe buff ofs
56   end  
57
58 let from_channel = Pervasives.input_value