123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869(*
* Copyright (c) 2014 Citrix Systems Inc
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)openSexplib.Stdtypeoffset=|First|Second[@@derivingsexp](** Valid offsets within the shared page *)letto_offset=function|First->1024|Second->2048typet=|Within_shared_pageofoffset|Externalofint[@@derivingsexp]letto_length=function|Within_shared_pageFirst->1024|Within_shared_pageSecond->2048|Externalx->1lsl(x+12)letto_order=function|Within_shared_pageFirst->10|Within_shared_pageSecond->11|Externaln->n+12letof_order=function|10->`Ok(Within_shared_pageFirst)|11->`Ok(Within_shared_pageSecond)|nwhenn>=12->`Ok(External(n-12))|x->`Error(Printf.sprintf"Invalid ring order: %d"x)(* in increasing order of bytes *)letall=[Within_shared_pageFirst;Within_shared_pageSecond;External0;External1;External2;External3;External4;External5;External6;External7;External8]letsuitable_locationsrequested_size=matchList.filter(funbl->to_lengthbl>=requested_size)allwith|[]->External8(* cap at the maximum *)|h::_->hletof_lengthsread_sizewrite_size=matchsuitable_locationsread_size,suitable_locationswrite_sizewith(* avoid clashes for the slots in the original page by giving the
* other buffer a free length upgrade *)|Within_shared_pageFirst,Within_shared_pageFirst->Within_shared_pageFirst,Within_shared_pageSecond|Within_shared_pageSecond,Within_shared_pageSecond->Within_shared_pageFirst,External0|n->n