Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file message.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425(******************************************************************************
* 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.
******************************************************************************)typero=MessageSig.rotyperw=MessageSig.rwmoduleMake(Storage:MessageStorage.S)=structmoduleSegment=structtypestorage_t=Storage.ttype-'capt=Storage.tletallocsize=Storage.alloc(Util.round_up_mult_8size)letrelease=Storage.releaseletlength=Storage.lengthletreadonlys=sletof_storages=sletto_storages=sletget_uint8=Storage.get_uint8letget_uint16=Storage.get_uint16letget_uint32=Storage.get_uint32letget_uint64=Storage.get_uint64letget_int8=Storage.get_int8letget_int16=Storage.get_int16letget_int32=Storage.get_int32letget_int64=Storage.get_int64letset_uint8=Storage.set_uint8letset_uint16=Storage.set_uint16letset_uint32=Storage.set_uint32letset_uint64=Storage.set_uint64letset_int8=Storage.set_int8letset_int16=Storage.set_int16letset_int32=Storage.set_int32letset_int64=Storage.set_int64letblit=Storage.blitletblit_to_bytes=Storage.blit_to_bytesletblit_from_string=Storage.blit_from_stringletzero_out=Storage.zero_outend(* module Segment *)moduleMessage=structtypestorage_t=Storage.ttypestorage_descr_t={segment:storage_t;bytes_consumed:int;}type-'capt={segments:storage_descr_tRes.Array.t;attachments:MessageSig.attachments;}letcreatesize=letsegment=Storage.alloc(Util.round_up_mult_8size)inletsegments=Res.Array.create1{segment;bytes_consumed=0}in{segments;attachments=MessageSig.No_attachments}letreleasem=let()=Res.Array.iter(fundescr->Storage.releasedescr.segment)m.segmentsinRes.Array.clearm.segmentsletnum_segmentsm=Res.Array.lengthm.segmentslettotal_sizem=Res.Array.fold_left(funaccx->acc+x.bytes_consumed)0m.segmentslettotal_alloc_sizem=Res.Array.fold_left(funaccx->acc+(Segment.lengthx.segment))0m.segmentsletget_segmentmi=(Res.Array.getm.segmentsi).segmentletget_segment_descrmi=Res.Array.getm.segmentsiletadd_segmentmnew_seg=letnew_seg_descr={segment=new_seg;bytes_consumed=0}inRes.Array.add_onem.segmentsnew_seg_descr;new_seg_descrletreadonlym=(m:>rot)letof_storagems=letsegments=Res.Array.empty()inlet()=ListLabels.iterms~f:(funx->Res.Array.add_onesegments{segment=x;bytes_consumed=Storage.lengthx})in{segments;attachments=MessageSig.No_attachments}letto_storagem=Res.Array.fold_right(funxacc->x::acc)m.segments[]letwith_messagem~f=matchfmwith|x->releasem;x|exceptionex->releasem;raiseexletwith_attachmentsattachmentsm={mwithattachments}letget_attachmentsm=m.attachmentsend(* module Message *)moduleSlice=structtype-'capsegment_t=Storage.ttype-'capmessage_t='capMessage.ttype'capt={msg:'capmessage_t;segment:'capsegment_t;segment_id:int;start:int;len:int;}letallocmsize=(* Going with a wasteful algorithm for now: allocations only happen on the
last segment, with no attempt to go back and do best-fit on previous
segments. This wastes memory, but unallocated regions will pack
very efficiently. *)letsize=Util.round_up_mult_8sizeinletsegment_id,segment_descr=letlast_seg_id=Message.num_segmentsm-1inletlast_seg_descr=Message.get_segment_descrmlast_seg_idinletbytes_avail=Storage.lengthlast_seg_descr.Message.segment-last_seg_descr.Message.bytes_consumedinifsize<=bytes_availthenlast_seg_id,last_seg_descrelse(* Doubling message size on each allocation, under the assumption
that message sizes will be exponentially distributed. *)letmin_alloc_size=Message.total_alloc_sizeminletnew_seg=Storage.alloc(maxmin_alloc_sizesize)inletnew_seg_descr=Message.add_segmentmnew_seginlast_seg_id+1,new_seg_descrinletslice={msg=m;segment=segment_descr.Message.segment;segment_id;start=segment_descr.Message.bytes_consumed;len=size;}inlet()=Res.Array.setm.segmentssegment_id{segment_descrwithMessage.bytes_consumed=slice.start+slice.len}in(* Allocations should be eight-byte aligned *)let()=assert((slice.startland7)=0)insliceletalloc_in_segmentmsegment_idsize=letsize=Util.round_up_mult_8sizeinletseg_descr=Message.get_segment_descrmsegment_idinletbytes_avail=Storage.lengthseg_descr.Message.segment-seg_descr.Message.bytes_consumedinifsize<=bytes_availthenletslice={msg=m;segment=seg_descr.Message.segment;segment_id;start=seg_descr.Message.bytes_consumed;len=size;}inlet()=Res.Array.setm.segmentssegment_id{seg_descrwithMessage.bytes_consumed=slice.start+slice.len}in(* Allocations should be eight-byte aligned *)let()=assert((slice.startland7)=0)inSomesliceelseNoneletget_segmentslice=slice.segmentletget_endslice=slice.start+slice.lenletreadonlyslice={slicewithmsg=Message.readonlyslice.msg;}letget_uint8slicei=ifi<0||i>slice.len-1theninvalid_arg"Slice.get_uint8"elseletsegment=get_segmentsliceinSegment.get_uint8segment(slice.start+i)letget_uint16slicei=ifi<0||i>slice.len-2theninvalid_arg"Slice.get_uint16"elseletsegment=get_segmentsliceinSegment.get_uint16segment(slice.start+i)letget_uint32slicei=ifi<0||i>slice.len-4theninvalid_arg"Slice.get_uint32"elseletsegment=get_segmentsliceinSegment.get_uint32segment(slice.start+i)letget_uint64slicei=ifi<0||i>slice.len-8theninvalid_arg"Slice.get_uint64"elseletsegment=get_segmentsliceinSegment.get_uint64segment(slice.start+i)letget_int8slicei=ifi<0||i>slice.len-1theninvalid_arg"Slice.get_int8"elseletsegment=get_segmentsliceinSegment.get_int8segment(slice.start+i)letget_int16slicei=ifi<0||i>slice.len-2theninvalid_arg"Slice.get_int16"elseletsegment=get_segmentsliceinSegment.get_int16segment(slice.start+i)letget_int32slicei=ifi<0||i>slice.len-4theninvalid_arg"Slice.get_int32"elseletsegment=get_segmentsliceinSegment.get_int32segment(slice.start+i)letget_int64slicei=ifi<0||i>slice.len-8theninvalid_arg"Slice.get_int64"elseletsegment=get_segmentsliceinSegment.get_int64segment(slice.start+i)letset_uint8sliceiv=ifi<0||i>slice.len-1theninvalid_arg"Slice.set_uint8"elseletsegment=get_segmentsliceinSegment.set_uint8segment(slice.start+i)vletset_uint16sliceiv=ifi<0||i>slice.len-2theninvalid_arg"Slice.set_uint16"elseletsegment=get_segmentsliceinSegment.set_uint16segment(slice.start+i)vletset_uint32sliceiv=ifi<0||i>slice.len-4theninvalid_arg"Slice.set_uint32"elseletsegment=get_segmentsliceinSegment.set_uint32segment(slice.start+i)vletset_uint64sliceiv=ifi<0||i>slice.len-8theninvalid_arg"Slice.set_uint64"elseletsegment=get_segmentsliceinSegment.set_uint64segment(slice.start+i)vletset_int8sliceiv=ifi<0||i>slice.len-1theninvalid_arg"Slice.set_int8"elseletsegment=get_segmentsliceinSegment.set_int8segment(slice.start+i)vletset_int16sliceiv=ifi<0||i>slice.len-2theninvalid_arg"Slice.set_int16"elseletsegment=get_segmentsliceinSegment.set_int16segment(slice.start+i)vletset_int32sliceiv=ifi<0||i>slice.len-4theninvalid_arg"Slice.set_int32"elseletsegment=get_segmentsliceinSegment.set_int32segment(slice.start+i)vletset_int64sliceiv=ifi<0||i>slice.len-8theninvalid_arg"Slice.set_int64"elseletsegment=get_segmentsliceinSegment.set_int64segment(slice.start+i)vletblit~src~src_pos~dst~dst_pos~len=ifsrc_pos<0||src_pos+len>src.len||dst_pos<0||dst_pos+len>dst.lentheninvalid_arg"Slice.blit"elseletsrc_seg=get_segmentsrcinletdst_seg=get_segmentdstinSegment.blit~src:src_seg~src_pos:(src.start+src_pos)~dst:dst_seg~dst_pos:(dst.start+dst_pos)~lenletblit_to_bytes~src~src_pos~dst~dst_pos~len=ifsrc_pos<0||src_pos+len>src.lentheninvalid_arg"Slice.blit_to_bytes"elseSegment.blit_to_bytes~src:(get_segmentsrc)~src_pos:(src.start+src_pos)~dst~dst_pos~lenletblit_from_string~src~src_pos~dst~dst_pos~len=ifdst_pos<0||dst_pos+len>dst.lentheninvalid_arg"Slice.blit_from_string"elseSegment.blit_from_string~src~src_pos~dst:(get_segmentdst)~dst_pos:(dst.start+dst_pos)~lenletzero_outslice~pos~len=ifpos<0||pos+len>slice.lentheninvalid_arg"Slice.zero_out"elseSegment.zero_out~pos:(slice.start+pos)~len(get_segmentslice)end(* module Slice *)moduleStructStorage=struct(** Storage associated with a cap'n proto struct. *)type('cap,'a)t={data:'capSlice.t;(** Storage for struct fields stored by value *)pointers:'capSlice.t;(** Storage for struct fields stored by reference *)}letreadonly(struct_storage:('cap,'a)t):(ro,'a)t={data=Slice.readonlystruct_storage.data;pointers=Slice.readonlystruct_storage.pointers;}letwith_attachments(a:MessageSig.attachments)(struct_storage:('cap,'a)t)=letmsg=Message.with_attachmentsastruct_storage.data.Slice.msgin{data={struct_storage.datawithSlice.msg};pointers={struct_storage.pointerswithSlice.msg};}letget_attachments(struct_storage:('cap,'a)t)=Message.get_attachmentsstruct_storage.data.Slice.msgletv~data~pointers={data;pointers}letcastx=(x:>('cap,'a)t)type'areader_t=(ro,'a)toptiontype'abuilder_t=(rw,'a)tletcast_readerx=(x:>'areader_t)letreader_of_builderx=Some(readonlyx)letmessage_of_builderx=x.data.Slice.msgendmoduleListStorage=struct(** Storage associated with a cap'n proto list. *)type'capt={storage:'capSlice.t;(** Range of bytes used to hold list elements *)storage_type:ListStorageType.t;(** Describes the list packing format *)num_elements:int;(** Number of list elements *)}letreadonly(list_storage:'capt):rot={storage=Slice.readonlylist_storage.storage;storage_type=list_storage.storage_type;num_elements=list_storage.num_elements;}endmoduleObject=structtype('cap,'a)t=|None|Listof'capListStorage.t|Structof('cap,'a)StructStorage.t|CapabilityofUint32.tendend[@@inline]moduleBytesMessage=Make[@inlined](BytesStorage)exceptionInvalid_messageofstringexceptionOut_of_int_range=Util.Out_of_int_rangeletinvalid_msgs=raise(Invalid_messages)letout_of_int_ranges=raise(Out_of_int_ranges)