type extern_flags =| No_sharingDon't preserve sharing
| ClosuresSend function closures
| Compat_32Ensure 32-bit compatibility
The flags to the
Marshal.to_*functions below.
val to_channel : Stdlib.out_channel -> 'a -> extern_flags list -> unitMarshal.to_channel chan v flagswrites the representation ofvon channelchan. Theflagsargument is a possibly empty list of flags that governs the marshaling behavior with respect to sharing, functional values, and compatibility between 32- and 64-bit platforms.If
flagsdoes not containMarshal.No_sharing, circularities and sharing inside the valuevare detected and preserved in the sequence of bytes produced. In particular, this guarantees that marshaling always terminates. Sharing between values marshaled by successive calls toMarshal.to_channelis neither detected nor preserved, though. IfflagscontainsMarshal.No_sharing, sharing is ignored. This results in faster marshaling ifvcontains no shared substructures, but may cause slower marshaling and larger byte representations ifvactually contains sharing, or even non-termination ifvcontains cycles.If
flagsdoes not containMarshal.Closures, marshaling fails when it encounters a functional value insidev: only 'pure' data structures, containing neither functions nor objects, can safely be transmitted between different programs. IfflagscontainsMarshal.Closures, functional values will be marshaled as a the position in the code of the program together with the values corresponding to the free variables captured in the closure. In this case, the output of marshaling can only be read back in processes that run exactly the same program, with exactly the same compiled code. (This is checked at un-marshaling time, using an MD5 digest of the code transmitted along with the code position.)The exact definition of which free variables are captured in a closure is not specified and can vary between bytecode and native code (and according to optimization flags). In particular, a function value accessing a global reference may or may not include the reference in its closure. If it does, unmarshaling the corresponding closure will create a new reference, different from the global one.
If
flagscontainsMarshal.Compat_32, marshaling fails when it encounters an integer value outside the range[-2{^30}, 2{^30}-1]of integers that are representable on a 32-bit platform. This ensures that marshaled data generated on a 64-bit platform can be safely read back on a 32-bit platform. Ifflagsdoes not containMarshal.Compat_32, integer values outside the range[-2{^30}, 2{^30}-1]are marshaled, and can be read back on a 64-bit platform, but will cause an error at un-marshaling time when read back on a 32-bit platform. TheMashal.Compat_32flag only matters when marshaling is performed on a 64-bit platform; it has no effect if marshaling is performed on a 32-bit platform.
val to_bytes : 'a -> extern_flags list -> bytesMarshal.to_bytes v flagsreturns a byte sequence containing the representation ofv. Theflagsargument has the same meaning as forMarshal.to_channel.- since
- 4.02.0
val to_string : 'a -> extern_flags list -> stringSame as
to_bytesbut return the result as a string instead of a byte sequence.
val to_buffer : bytes -> int -> int -> 'a -> extern_flags list -> intMarshal.to_buffer buff ofs len v flagsmarshals the valuev, storing its byte representation in the sequencebuff, starting at indexofs, and writing at mostlenbytes. It returns the number of bytes actually written to the sequence. If the byte representation ofvdoes not fit inlencharacters, the exceptionFailureis raised.
val from_channel : Stdlib.in_channel -> 'aMarshal.from_channel chanreads from channelchanthe byte representation of a structured value, as produced by one of theMarshal.to_*functions, and reconstructs and returns the corresponding value.It raises
End_of_fileif the function has already reached the end of file when starting to read from the channel, and raisesFailure "input_value: truncated object"if it reaches the end of file later during the unmarshalling.
val from_bytes : bytes -> int -> 'aMarshal.from_bytes buff ofsunmarshals a structured value likeMarshal.from_channeldoes, except that the byte representation is not read from a channel, but taken from the byte sequencebuff, starting at positionofs. The byte sequence is not mutated.- since
- 4.02.0
val from_string : string -> int -> 'aSame as
from_bytesbut take a string as argument instead of a byte sequence.
val header_size : intThe 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_sizeis the size, in bytes, of the header.Marshal.data_sizebuff ofsis the size, in bytes, of the data part, assuming a valid header is stored inbuffstarting at positionofs. Finally,Marshal.total_sizebuff ofsis the total size, in bytes, of the marshaled value. BothMarshal.data_sizeandMarshal.total_sizeraiseFailureifbuff,ofsdoes not contain a valid header.To read the byte representation of a marshaled value into a byte sequence, the program needs to read first
Marshal.header_sizebytes into the sequence, then determine the length of the remainder of the representation usingMarshal.data_size, make sure the sequence is large enough to hold the remaining data, then read it, and finally callMarshal.from_bytesto unmarshal the value.
val data_size : bytes -> int -> intSee
Marshal.header_size.
val total_size : bytes -> int -> intSee
Marshal.header_size.