123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232(* 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. *)openBigarraytypet=(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_from_string:string->int->t->int->int->unit="lwt_unix_blit_from_string""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_stringsrc_bufsrc_ofsdst_bufdst_ofslen=if(len<0||src_ofs<0||src_ofs>String.lengthsrc_buf-len||dst_ofs<0||dst_ofs>lengthdst_buf-len)theninvalid_arg"Lwt_bytes.blit_from_string"elseunsafe_blit_from_stringsrc_bufsrc_ofsdst_bufdst_ofslenletblit_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_unixletread=Lwt_unix.read_bigarray"Lwt_bytes.read"[@ocaml.warning"-3"]letwrite=Lwt_unix.write_bigarray"Lwt_bytes.write"[@ocaml.warning"-3"]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)letconvert_io_vectorsold_io_vectors=letio_vectors=IO_vectors.create()inold_io_vectors|>List.iter(fun({iov_buffer;iov_offset;iov_length}:io_vector)->IO_vectors.append_bigarrayio_vectorsiov_bufferiov_offsetiov_length);io_vectorsletrecv_msg~socket~io_vectors=Lwt_unix.recv_msg~socket~io_vectors:(convert_io_vectorsio_vectors)letsend_msg~socket~io_vectors~fds=Lwt_unix.send_msg~socket~io_vectors:(convert_io_vectorsio_vectors)~fdsexternalstub_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))()=Unix.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_DONTNEED|MADV_MERGEABLE|MADV_UNMERGEABLE|MADV_HUGEPAGE|MADV_NOHUGEPAGEexternalstub_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