123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107(*
* Copyright (C) 2015 David Scott <dave.scott@unikernel.com>
*
* 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.
*
*)openLwt.InfixopenMirage_block_lwt_smoduleB=Mirage_blocklet(>>*=)xf=x>>=function|Okq->fq|Errore->Lwt.return@@ErroremoduleFold(Block:S)=structlets~finit(b:Block.t)=Block.get_infob>>=funinfo->letbuffer=Io_page.(to_cstruct(get8))inletsectors=Cstruct.lenbuffer/info.B.sector_sizeinletrecloopaccnext=ifnext>=info.B.size_sectorsthenLwt.return(Okacc)elsebeginletremaining=Int64.subinfo.B.size_sectorsnextinletthis_time=minsectors(Int64.to_intremaining)inletbuf=Cstruct.subbuffer0(info.B.sector_size*this_time)inBlock.readbnext[buf]>>*=fun()->faccInt64.(mulnext(of_intinfo.B.sector_size))buf>>=funacc->loopaccInt64.(addnext(of_intthis_time))endinloopinit0LendmoduleFast_fold(Seekable:SEEKABLE)=structletmapped_s~finit(s:Seekable.t)=Seekable.get_infos>>=funinfo->letbuffer=Io_page.(to_cstruct(get8))inletsectors=Cstruct.lenbuffer/info.B.sector_sizeinletrecloopaccnext=(* next points to the next mapped chunk (or end of device) *)ifnext>=info.B.size_sectorsthenLwt.return(Okacc)elsebeginSeekable.seek_unmappedsnext>>*=funnext_unmapped->(* A chunk of data exists between next and next_unmapped *)letrecinneraccnext=ifnext>=next_unmapped||next>=info.B.size_sectorsthenLwt.return(Ok(acc,next))elsebeginletremaining=Int64.subinfo.B.size_sectorsnextinletmapped=Int64.subnext_unmappednextinletthis_time=min(minsectors(Int64.to_intremaining))(Int64.to_intmapped)inletbuf=Cstruct.subbuffer0(info.B.sector_size*this_time)inSeekable.readsnext[buf]>>*=fun()->faccnextbuf>>=funacc->letnext=Int64.(addnext(of_intthis_time))ininneraccnextendininneraccnext>>*=fun(acc,next)->(* next points to the next unmapped chunk (or end of device) *)Seekable.seek_mappedsnext>>*=funnext->loopaccnextendinSeekable.seek_mappeds0L>>*=funstart->loopinitstartletunmapped_s~finit(s:Seekable.t)=Seekable.get_infos>>=funinfo->letrecloopaccnext=(* next points to the next mapped chunk (or end of device) *)ifnext>=info.B.size_sectorsthenLwt.return@@OkaccelsebeginSeekable.seek_unmappedsnext>>*=funnext_unmapped->Seekable.seek_mappedsnext_unmapped>>*=funnext_mapped->faccnext_unmapped(Int64.subnext_mappednext_unmapped)>>=funacc->loopaccnext_mappedendinloopinit0Lend