123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257(* This file is part of Lwt, released under the MIT license. See LICENSE.md for
details, or visit https://github.com/ocsigen/lwt/blob/master/LICENSE.md. *)openBigarrayopenLwt.Infixtypet=(char,int8_unsigned_elt,c_layout)Array1.tletcreatesize=Array1.createcharc_layoutsizeletlengthbytes=Array1.dimbytesexternalget:t->int->char="%caml_ba_ref_1"externalset:t->int->char->unit="%caml_ba_set_1"externalunsafe_get:t->int->char="%caml_ba_unsafe_ref_1"externalunsafe_set:t->int->char->unit="%caml_ba_unsafe_set_1"[@@@ocaml.warning"-3"]externalunsafe_fill:t->int->int->char->unit="lwt_unix_fill_bytes""noalloc"[@@@ocaml.warning"+3"]letfillbytesofslench=ifofs<0||len<0||ofs>lengthbytes-lentheninvalid_arg"Lwt_bytes.fill"elseunsafe_fillbytesofslench(* +-----------------------------------------------------------------+
| Blitting |
+-----------------------------------------------------------------+ *)[@@@ocaml.warning"-3"]externalunsafe_blit_from_bytes:Bytes.t->int->t->int->int->unit="lwt_unix_blit_from_bytes""noalloc"externalunsafe_blit_to_bytes:t->int->Bytes.t->int->int->unit="lwt_unix_blit_to_bytes""noalloc"externalunsafe_blit:t->int->t->int->int->unit="lwt_unix_blit""noalloc"[@@@ocaml.warning"+3"]letblit_from_bytessrc_bufsrc_ofsdst_bufdst_ofslen=if(len<0||src_ofs<0||src_ofs>Bytes.lengthsrc_buf-len||dst_ofs<0||dst_ofs>lengthdst_buf-len)theninvalid_arg"Lwt_bytes.blit_from_bytes"elseunsafe_blit_from_bytessrc_bufsrc_ofsdst_bufdst_ofslenletblit_to_bytessrc_bufsrc_ofsdst_bufdst_ofslen=if(len<0||src_ofs<0||src_ofs>lengthsrc_buf-len||dst_ofs<0||dst_ofs>Bytes.lengthdst_buf-len)theninvalid_arg"Lwt_bytes.blit_to_bytes"elseunsafe_blit_to_bytessrc_bufsrc_ofsdst_bufdst_ofslenletblitsrc_bufsrc_ofsdst_bufdst_ofslen=if(len<0||src_ofs<0||src_ofs>lengthsrc_buf-len||dst_ofs<0||dst_ofs>lengthdst_buf-len)theninvalid_arg"Lwt_bytes.blit"elseunsafe_blitsrc_bufsrc_ofsdst_bufdst_ofslenletof_bytesbuf=letlen=Bytes.lengthbufinletbytes=createleninunsafe_blit_from_bytesbuf0bytes0len;bytesletof_stringstr=of_bytes(Bytes.unsafe_of_stringstr)letto_bytesbytes=letlen=lengthbytesinletstr=Bytes.createleninunsafe_blit_to_bytesbytes0str0len;strletto_stringbytes=Bytes.unsafe_to_string(to_bytesbytes)letproxy=Array1.subletextractbufofslen=ifofs<0||len<0||ofs>lengthbuf-lentheninvalid_arg"Lwt_bytes.extract"elsebeginletbuf'=createleninblitbufofsbuf'0len;buf'endletcopybuf=letlen=lengthbufinletbuf'=createleninblitbuf0buf'0len;buf'(* +-----------------------------------------------------------------+
| IOs |
+-----------------------------------------------------------------+ *)openLwt_unixexternalstub_read:Unix.file_descr->t->int->int->int="lwt_unix_bytes_read"externalread_job:Unix.file_descr->t->int->int->intjob="lwt_unix_bytes_read_job"letreadfdbufposlen=ifpos<0||len<0||pos>lengthbuf-lentheninvalid_arg"Lwt_bytes.read"elseblockingfd>>=function|true->wait_readfd>>=fun()->run_job(read_job(unix_file_descrfd)bufposlen)|false->wrap_syscallReadfd(fun()->stub_read(unix_file_descrfd)bufposlen)externalstub_write:Unix.file_descr->t->int->int->int="lwt_unix_bytes_write"externalwrite_job:Unix.file_descr->t->int->int->intjob="lwt_unix_bytes_write_job"letwritefdbufposlen=ifpos<0||len<0||pos>lengthbuf-lentheninvalid_arg"Lwt_bytes.write"elseblockingfd>>=function|true->wait_writefd>>=fun()->run_job(write_job(unix_file_descrfd)bufposlen)|false->wrap_syscallWritefd(fun()->stub_write(unix_file_descrfd)bufposlen)externalstub_recv:Unix.file_descr->t->int->int->Unix.msg_flaglist->int="lwt_unix_bytes_recv"letrecvfdbufposlenflags=ifpos<0||len<0||pos>lengthbuf-lentheninvalid_arg"Lwt_bytes.recv"elsewrap_syscallReadfd(fun()->stub_recv(unix_file_descrfd)bufposlenflags)externalstub_send:Unix.file_descr->t->int->int->Unix.msg_flaglist->int="lwt_unix_bytes_send"letsendfdbufposlenflags=ifpos<0||len<0||pos>lengthbuf-lentheninvalid_arg"Lwt_bytes.send"elsewrap_syscallWritefd(fun()->stub_send(unix_file_descrfd)bufposlenflags)typeio_vector={iov_buffer:t;iov_offset:int;iov_length:int;}letio_vector~buffer~offset~length=({iov_buffer=buffer;iov_offset=offset;iov_length=length;}:io_vector)letcheck_io_vectorsfunc_nameiovs=List.iter(fun(iov:io_vector)->ifiov.iov_offset<0||iov.iov_length<0||iov.iov_offset>lengthiov.iov_buffer-iov.iov_lengththenPrintf.ksprintfinvalid_arg"Lwt_bytes.%s"func_name)iovsexternalstub_recv_msg:Unix.file_descr->int->io_vectorlist->int*Unix.file_descrlist="lwt_unix_bytes_recv_msg"letrecv_msg~socket~io_vectors=check_io_vectors"recv_msg"io_vectors;letn_iovs=List.lengthio_vectorsinwrap_syscallReadsocket(fun()->stub_recv_msg(unix_file_descrsocket)n_iovsio_vectors)externalstub_send_msg:Unix.file_descr->int->io_vectorlist->int->Unix.file_descrlist->int="lwt_unix_bytes_send_msg"letsend_msg~socket~io_vectors~fds=check_io_vectors"send_msg"io_vectors;letn_iovs=List.lengthio_vectorsandn_fds=List.lengthfdsinwrap_syscallWritesocket(fun()->stub_send_msg(unix_file_descrsocket)n_iovsio_vectorsn_fdsfds)externalstub_recvfrom:Unix.file_descr->t->int->int->Unix.msg_flaglist->int*Unix.sockaddr="lwt_unix_bytes_recvfrom"letrecvfromfdbufposlenflags=ifpos<0||len<0||pos>lengthbuf-lentheninvalid_arg"Lwt_bytes.recvfrom"elsewrap_syscallReadfd(fun()->stub_recvfrom(unix_file_descrfd)bufposlenflags)externalstub_sendto:Unix.file_descr->t->int->int->Unix.msg_flaglist->Unix.sockaddr->int="lwt_unix_bytes_sendto_byte""lwt_unix_bytes_sendto"letsendtofdbufposlenflagsaddr=ifpos<0||len<0||pos>lengthbuf-lentheninvalid_arg"Lwt_bytes.sendto"elsewrap_syscallWritefd(fun()->stub_sendto(unix_file_descrfd)bufposlenflagsaddr)(* +-----------------------------------------------------------------+
| Memory mapped files |
+-----------------------------------------------------------------+ *)letmap_file~fd?pos~shared?(size=(-1))()=Mmap.V1.map_filefd?poscharc_layoutshared[|size|]|>Bigarray.array1_of_genarray[@@@ocaml.warning"-3"]externalmapped:t->bool="lwt_unix_mapped""noalloc"[@@@ocaml.warning"+3"]typeadvice=|MADV_NORMAL|MADV_RANDOM|MADV_SEQUENTIAL|MADV_WILLNEED|MADV_DONTNEEDexternalstub_madvise:t->int->int->advice->unit="lwt_unix_madvise"letmadvisebufposlenadvice=ifpos<0||len<0||pos>lengthbuf-lentheninvalid_arg"Lwt_bytes.madvise"elsestub_madvisebufposlenadviceexternalget_page_size:unit->int="lwt_unix_get_page_size"letpage_size=get_page_size()externalstub_mincore:t->int->int->boolarray->unit="lwt_unix_mincore"letmincorebufferoffsetstates=if(offsetmodpage_size<>0||offset<0||lengthbuffer-offset<(Array.lengthstates-1)*page_size+1)theninvalid_arg"Lwt_bytes.mincore"elsestub_mincorebufferoffset(Array.lengthstates*page_size)statesexternalwait_mincore_job:t->int->unitjob="lwt_unix_wait_mincore_job"letwait_mincorebufferoffset=ifoffset<0||offset>=lengthbuffertheninvalid_arg"Lwt_bytes.wait_mincore"elsebeginletstate=[|false|]inmincorebuffer(offset-(offsetmodpage_size))state;ifstate.(0)thenLwt.return_unitelserun_job(wait_mincore_jobbufferoffset)end