123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117(******************************************************************************
* capnp-ocaml
*
* Copyright (c) 2013-2014, Paul Pelzl
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
******************************************************************************)typet={offset:int;(** Signed offset in words from end of the pointer to start of struct
data region. *)data_words:int;(** Size of struct data region, in words. *)pointer_words:int;(** Size of struct pointers region, in words. *)}lettag_val_struct=0Lletoffset_shift=2letoffset_mask=Int64.shift_left0x3fffffffLoffset_shiftletoffset_mask_int=0x3ffffffflsloffset_shiftletdata_size_shift=32letdata_size_mask=Int64.shift_left0xffffLdata_size_shiftletdata_size_mask_int=0xfffflsldata_size_shiftletpointers_size_shift=48letdecode(pointer64:Int64.t):t=letpointers_size=letshifted64=Int64.shift_right_logicalpointer64pointers_size_shiftin(* pointers size is left-aligned, no need to mask it *)Int64.to_intshifted64in(* Int64 arithmetic causes unfortunate GC pressure. If we're on a 64-bit
platform, use standard 63-bit ints whenever possible. *)ifSys.word_size=64thenletpointer=Int64.to_intpointer64inletoffset=letv=(pointerlandoffset_mask_int)lsroffset_shiftinUtil.decode_signed30vinletdata_size=(pointerlanddata_size_mask_int)lsrdata_size_shiftin{offset;data_words=data_size;pointer_words=pointers_size;}elseletoffset=letmasked=Int64.logandpointer64offset_maskinletoffset64=Int64.shift_right_logicalmaskedoffset_shiftinletoffset_int=Int64.to_intoffset64inUtil.decode_signed30offset_intinletdata_size=letmasked=Int64.logandpointer64data_size_maskinletsize64=Int64.shift_right_logicalmaskeddata_size_shiftinInt64.to_intsize64in{offset;data_words=data_size;pointer_words=pointers_size;}letencode(storage_descr:t):Int64.t=(* Int64 arithmetic causes unfortunate GC pressure. If we're on a 64-bit
platform, use standard 63-bit ints whenever possible. *)ifSys.word_size=64&&storage_descr.pointer_words<=0x7fffthenletoffset=Util.encode_signed30storage_descr.offsetinletdata_size=storage_descr.data_wordsinletptr_size=storage_descr.pointer_wordsinlettag_val_struct_int=0inInt64.of_int(tag_val_struct_intlor(offsetlsloffset_shift)lor(data_sizelsldata_size_shift)lor(ptr_sizelslpointers_size_shift))elseletoffset64=Int64.of_int(Util.encode_signed30storage_descr.offset)inletdata_size64=Int64.of_intstorage_descr.data_wordsinletptr_size64=Int64.of_intstorage_descr.pointer_wordsintag_val_struct|>Int64.logor(Int64.shift_leftoffset64offset_shift)|>Int64.logor(Int64.shift_leftdata_size64data_size_shift)|>Int64.logor(Int64.shift_leftptr_size64pointers_size_shift)