123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141(******************************************************************************
* 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.
******************************************************************************)typelanding_pad_t=|NormalPointer|TaggedFarPointertypet={landing_pad:landing_pad_t;(** Type of landing pad which this far pointer points to. *)offset:int;(** Unsigned offset in words from start of the segment to start of
the landing pad. *)segment_id:int(** Segment ID where the landing pad is located. *)}lettag_val_far=0x2Lletlanding_pad_type_shift=2letlanding_pad_type_mask=Int64.shift_left1Llanding_pad_type_shiftletlanding_pad_type_mask_int=1lsllanding_pad_type_shiftletoffset_shift=3letoffset_mask=Int64.shift_left0x1fffffffLoffset_shiftletoffset_mask_int=0x1ffffffflsloffset_shiftletsegment_shift=32letsegment_mask=Int64.shift_left0xffffffffLsegment_shiftletdecode(pointer64:Int64.t):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=64thenletsegment_id=letid64=Int64.shift_right_logicalpointer64segment_shiftinInt64.to_intid64inletpointer=Int64.to_intpointer64inletlanding_pad=if(pointerlandlanding_pad_type_mask_int)=0thenNormalPointerelseTaggedFarPointerinletoffset=(pointerlandoffset_mask_int)lsroffset_shiftin{landing_pad;offset;segment_id;}elseletsegment_id=letmax64=Int64.of_intmax_intin(* Segment ID is left-aligned, no need to mask it *)letid64=Int64.shift_right_logicalpointer64segment_shiftinifInt64.compareid64max64>0thenMessage.invalid_msg"far pointer contains segment ID larger than OCaml max_int"elseInt64.to_intid64inletlanding_pad=letmasked=Int64.logandpointer64landing_pad_type_maskinifInt64.comparemaskedInt64.zero=0thenNormalPointerelseTaggedFarPointerinletoffset=letmasked=Int64.logandpointer64offset_maskinletoffset64=Int64.shift_right_logicalmaskedoffset_shiftinInt64.to_intoffset64in{landing_pad;offset;segment_id;}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.segment_id<=0x7fffffffthenlettp=matchstorage_descr.landing_padwith|NormalPointer->0|TaggedFarPointer->1inlettag_val_far_int=2inInt64.of_int(tag_val_far_intlor(tplsllanding_pad_type_shift)lor(storage_descr.offsetlsloffset_shift)lor(storage_descr.segment_idlslsegment_shift))elselettype64=matchstorage_descr.landing_padwith|NormalPointer->Int64.zero|TaggedFarPointer->Int64.oneinletoffset64=Int64.of_intstorage_descr.offsetinletsegment64=Int64.of_intstorage_descr.segment_idintag_val_far|>Int64.logor(Int64.shift_lefttype64landing_pad_type_shift)|>Int64.logor(Int64.shift_leftoffset64offset_shift)|>Int64.logor(Int64.shift_leftsegment64segment_shift)