1Stdlib.Marshal(3) OCaml library Stdlib.Marshal(3)
2
3
4
6 Stdlib.Marshal - no description
7
9 Module Stdlib.Marshal
10
12 Module Marshal
13 : (module Stdlib__Marshal)
14
15
16
17
18
19
20
21 type extern_flags =
22 | No_sharing (* Don't preserve sharing
23 *)
24 | Closures (* Send function closures
25 *)
26 | Compat_32 (* Ensure 32-bit compatibility
27 *)
28
29
30 The flags to the Marshal.to_* functions below.
31
32
33
34 val to_channel : out_channel -> 'a -> extern_flags list -> unit
35
36
37 Marshal.to_channel chan v flags writes the representation of v on chan‐
38 nel chan . The flags argument is a possibly empty list of flags that
39 governs the marshaling behavior with respect to sharing, functional
40 values, and compatibility between 32- and 64-bit platforms.
41
42 If flags does not contain Marshal.No_sharing , circularities and shar‐
43 ing inside the value v are detected and preserved in the sequence of
44 bytes produced. In particular, this guarantees that marshaling always
45 terminates. Sharing between values marshaled by successive calls to
46 Marshal.to_channel is neither detected nor preserved, though. If flags
47 contains Marshal.No_sharing , sharing is ignored. This results in
48 faster marshaling if v contains no shared substructures, but may cause
49 slower marshaling and larger byte representations if v actually con‐
50 tains sharing, or even non-termination if v contains cycles.
51
52 If flags does not contain Marshal.Closures , marshaling fails when it
53 encounters a functional value inside v : only 'pure' data structures,
54 containing neither functions nor objects, can safely be transmitted be‐
55 tween different programs. If flags contains Marshal.Closures , func‐
56 tional values will be marshaled as a the position in the code of the
57 program together with the values corresponding to the free variables
58 captured in the closure. In this case, the output of marshaling can
59 only be read back in processes that run exactly the same program, with
60 exactly the same compiled code. (This is checked at un-marshaling time,
61 using an MD5 digest of the code transmitted along with the code posi‐
62 tion.)
63
64 The exact definition of which free variables are captured in a closure
65 is not specified and can vary between bytecode and native code (and ac‐
66 cording to optimization flags). In particular, a function value ac‐
67 cessing a global reference may or may not include the reference in its
68 closure. If it does, unmarshaling the corresponding closure will cre‐
69 ate a new reference, different from the global one.
70
71 If flags contains Marshal.Compat_32 , marshaling fails when it encoun‐
72 ters an integer value outside the range [-2{^30}, 2{^30}-1] of integers
73 that are representable on a 32-bit platform. This ensures that mar‐
74 shaled data generated on a 64-bit platform can be safely read back on a
75 32-bit platform. If flags does not contain Marshal.Compat_32 , integer
76 values outside the range [-2{^30}, 2{^30}-1] are marshaled, and can be
77 read back on a 64-bit platform, but will cause an error at un-marshal‐
78 ing time when read back on a 32-bit platform. The Mashal.Compat_32
79 flag only matters when marshaling is performed on a 64-bit platform; it
80 has no effect if marshaling is performed on a 32-bit platform.
81
82
83 Raises Failure if chan is not in binary mode.
84
85
86
87 val to_bytes : 'a -> extern_flags list -> bytes
88
89
90 Marshal.to_bytes v flags returns a byte sequence containing the repre‐
91 sentation of v . The flags argument has the same meaning as for Mar‐
92 shal.to_channel .
93
94
95 Since 4.02.0
96
97
98
99 val to_string : 'a -> extern_flags list -> string
100
101 Same as to_bytes but return the result as a string instead of a byte
102 sequence.
103
104
105
106 val to_buffer : bytes -> int -> int -> 'a -> extern_flags list -> int
107
108
109 Marshal.to_buffer buff ofs len v flags marshals the value v , storing
110 its byte representation in the sequence buff , starting at index ofs ,
111 and writing at most len bytes. It returns the number of bytes actually
112 written to the sequence. If the byte representation of v does not fit
113 in len characters, the exception Failure is raised.
114
115
116
117 val from_channel : in_channel -> 'a
118
119
120 Marshal.from_channel chan reads from channel chan the byte representa‐
121 tion of a structured value, as produced by one of the Marshal.to_*
122 functions, and reconstructs and returns the corresponding value.
123
124
125 Raises End_of_file if chan is already at the end of the file.
126
127
128 Raises Failure if the end of the file is reached during unmarshalling
129 itself or if chan is not in binary mode.
130
131
132
133 val from_bytes : bytes -> int -> 'a
134
135
136 Marshal.from_bytes buff ofs unmarshals a structured value like Mar‐
137 shal.from_channel does, except that the byte representation is not read
138 from a channel, but taken from the byte sequence buff , starting at po‐
139 sition ofs . The byte sequence is not mutated.
140
141
142 Since 4.02.0
143
144
145
146 val from_string : string -> int -> 'a
147
148 Same as from_bytes but take a string as argument instead of a byte se‐
149 quence.
150
151
152
153 val header_size : int
154
155 The bytes representing a marshaled value are composed of a fixed-size
156 header and a variable-sized data part, whose size can be determined
157 from the header. Marshal.header_size is the size, in bytes, of the
158 header. Marshal.data_size buff ofs is the size, in bytes, of the data
159 part, assuming a valid header is stored in buff starting at position
160 ofs . Finally, Marshal.total_size buff ofs is the total size, in
161 bytes, of the marshaled value. Both Marshal.data_size and Marshal.to‐
162 tal_size raise Failure if buff , ofs does not contain a valid header.
163
164 To read the byte representation of a marshaled value into a byte se‐
165 quence, the program needs to read first Marshal.header_size bytes into
166 the sequence, then determine the length of the remainder of the repre‐
167 sentation using Marshal.data_size , make sure the sequence is large
168 enough to hold the remaining data, then read it, and finally call Mar‐
169 shal.from_bytes to unmarshal the value.
170
171
172
173 val data_size : bytes -> int -> int
174
175 See Marshal.header_size .
176
177
178
179 val total_size : bytes -> int -> int
180
181 See Marshal.header_size .
182
183
184
185
186
187OCamldoc 2023-01-23 Stdlib.Marshal(3)