1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465(*
* 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.
*)typeoffset=|First|Second(** Valid offsets within the shared page *)letto_offset=function|First->1024|Second->2048typet=|Within_shared_pageofoffset|Externalofintletto_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(`Msg(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