|
Module Marshalmodule Marshal:
Marshaling of data structures.
This module provides functions to encode arbitrary data structures as sequences of bytes, which can then be written on a file or sent over a pipe or network connection. The bytes can then be read back later, possibly in another process, and decoded back into a data structure. The format for the byte sequences is compatible across all machines for a given version of Objective Caml.
Warning: marshaling is currently not type-safe. The type
of marshaled data is not transmitted along the value of the data,
making it impossible to check that the data read back possesses the
type expected by the context. In particular, the result type of
the
Marshal.to_channel
and Marshal.from_channel must be opened in binary mode, using e.g.
open_out_bin or open_in_bin ; channels opened in text mode will
cause unmarshaling errors on platforms where text channels behave
differently than binary channels, e.g. Windows.type extern_flags =
The flags to the
Marshal.to_* functions below.val to_channel : Marshal.to_channel chan v flags writes the representation
of v on channel chan . The flags argument is a
possibly empty list of flags that governs the marshaling
behavior with respect to sharing and functional values.
If
If val to_string : Marshal.to_string v flags returns a string containing
the representation of v as a sequence of bytes.
The flags argument has the same meaning as for
Marshal.to_channel .val to_buffer : Marshal.to_buffer buff ofs len v flags marshals the value v ,
storing its byte representation in the string buff ,
starting at character number ofs , and writing at most
len characters. It returns the number of characters
actually written to the string. If the byte representation
of v does not fit in len characters, the exception Failure
is raised.val from_channel : Marshal.from_channel chan reads from channel chan the
byte representation of a structured value, as produced by
one of the Marshal.to_* functions, and reconstructs and
returns the corresponding value.val from_string : Marshal.from_string buff ofs unmarshals a structured value
like Marshal.from_channel does, except that the byte
representation is not read from a channel, but taken from
the string buff , starting at position ofs .val header_size :
The bytes representing a marshaled value are composed of
a fixed-size header and a variable-sized data part,
whose size can be determined from the header.
Marshal.header_size is the size, in characters, of the header.
Marshal.data_size buff ofs is the size, in characters,
of the data part, assuming a valid header is stored in
buff starting at position ofs .
Finally, Marshal.total_size buff ofs is the total size,
in characters, of the marshaled value.
Both Marshal.data_size and Marshal.total_size raise Failure
if buff , ofs does not contain a valid header.
To read the byte representation of a marshaled value into
a string buffer, the program needs to read first
val data_size :
See
Marshal.header_size .val total_size :
See
Marshal.header_size . |