From 002e37a46f516bc2b595ee46a99b0ededb1b82df Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 6 Mar 2024 12:02:50 -0500 Subject: [PATCH] remove IO channels (better use iostream); make Heap private --- src/unix/IO_in.ml | 154 ------------------------------- src/unix/IO_out.ml | 119 ------------------------ src/unix/dune | 2 +- src/unix/{heap.ml => heap_.ml} | 0 src/unix/{heap.mli => heap_.mli} | 0 src/unix/timer.ml | 2 +- 6 files changed, 2 insertions(+), 275 deletions(-) delete mode 100644 src/unix/IO_in.ml delete mode 100644 src/unix/IO_out.ml rename src/unix/{heap.ml => heap_.ml} (100%) rename src/unix/{heap.mli => heap_.mli} (100%) diff --git a/src/unix/IO_in.ml b/src/unix/IO_in.ml deleted file mode 100644 index 09c607de..00000000 --- a/src/unix/IO_in.ml +++ /dev/null @@ -1,154 +0,0 @@ -open Common_ - -class type t = - object - method input : bytes -> int -> int -> int - (** Read into the slice. Returns [0] only if the - stream is closed. *) - - method close : unit -> unit - (** Close the input. Must be idempotent. *) - end - -let create ?(close = ignore) ~input () : t = - object - method close = close - method input = input - end - -let empty : t = - object - method close () = () - method input _ _ _ = 0 - end - -let of_bytes ?(off = 0) ?len (b : bytes) : t = - (* i: current position in [b] *) - let i = ref off in - - let len = - match len with - | Some n -> - if n > Bytes.length b - off then invalid_arg "Iostream.In.of_bytes"; - n - | None -> Bytes.length b - off - in - let end_ = off + len in - - object - method input b_out i_out len_out = - let n = min (end_ - !i) len_out in - Bytes.blit b !i b_out i_out n; - i := !i + n; - n - - method close () = i := end_ - end - -let of_string ?off ?len s : t = of_bytes ?off ?len (Bytes.unsafe_of_string s) - -(** Read into the given slice. - @return the number of bytes read, [0] means end of input. *) -let[@inline] input (self : #t) buf i len = self#input buf i len - -(** Close the channel. *) -let[@inline] close self : unit = self#close () - -let rec really_input (self : #t) buf i len = - if len > 0 then ( - let n = input self buf i len in - if n = 0 then raise End_of_file; - (really_input [@tailrec]) self buf (i + n) (len - n) - ) - -let really_input_string self n : string = - let buf = Bytes.create n in - really_input self buf 0 n; - Bytes.unsafe_to_string buf - -let copy_into ?(buf = Bytes.create _default_buf_size) (ic : #t) (oc : IO_out.t) - : unit = - let continue = ref true in - while !continue do - let len = input ic buf 0 (Bytes.length buf) in - if len = 0 then - continue := false - else - IO_out.output oc buf 0 len - done - -let concat (l0 : t list) : t = - let l = ref l0 in - let rec input b i len : int = - match !l with - | [] -> 0 - | ic :: tl -> - let n = ic#input b i len in - if n > 0 then - n - else ( - l := tl; - input b i len - ) - in - let close () = List.iter close l0 in - create ~close ~input () - -let input_all ?(buf = Bytes.create 128) (self : #t) : string = - let buf = ref buf in - let i = ref 0 in - - let[@inline] full_ () = !i = Bytes.length !buf in - - let grow_ () = - let old_size = Bytes.length !buf in - let new_size = min Sys.max_string_length (old_size + (old_size / 4) + 10) in - if old_size = new_size then - failwith "input_all: maximum input size exceeded"; - let new_buf = Bytes.extend !buf 0 (new_size - old_size) in - buf := new_buf - in - - let rec loop () = - if full_ () then grow_ (); - let available = Bytes.length !buf - !i in - let n = input self !buf !i available in - if n > 0 then ( - i := !i + n; - (loop [@tailrec]) () - ) - in - loop (); - - if full_ () then - Bytes.unsafe_to_string !buf - else - Bytes.sub_string !buf 0 !i - -let of_unix_fd ?(close_noerr = false) ?(buf = Bytes.create _default_buf_size) - (fd : Unix.file_descr) : t = - let buf_len = ref 0 in - let buf_off = ref 0 in - - let refill () = - buf_off := 0; - buf_len := IO_unix.read fd buf 0 (Bytes.length buf) - in - - object - method input b i len : int = - if !buf_len = 0 then refill (); - let n = min len !buf_len in - if n > 0 then ( - Bytes.blit buf !buf_off b i n; - buf_off := !buf_off + n; - buf_len := !buf_len - n - ); - n - - method close () = - if close_noerr then ( - try Unix.close fd with _ -> () - ) else - Unix.close fd - end diff --git a/src/unix/IO_out.ml b/src/unix/IO_out.ml deleted file mode 100644 index ba772345..00000000 --- a/src/unix/IO_out.ml +++ /dev/null @@ -1,119 +0,0 @@ -open Common_ - -class type t = - object - method output_char : char -> unit - method output : bytes -> int -> int -> unit - method flush : unit -> unit - method close : unit -> unit - end - -let create ?(flush = ignore) ?(close = ignore) ~output_char ~output () : t = - object - method flush () = flush () - method close () = close () - method output_char c = output_char c - method output bs i len = output bs i len - end - -let dummy : t = - object - method flush () = () - method close () = () - method output_char _ = () - method output _ _ _ = () - end - -let of_unix_fd ?(close_noerr = false) ?(buf = Bytes.create _default_buf_size) fd - : t = - let buf_off = ref 0 in - - let[@inline] is_full () = !buf_off = Bytes.length buf in - - let flush () = - if !buf_off > 0 then ( - IO_unix.write fd buf 0 !buf_off; - buf_off := 0 - ) - in - - object - method output_char c = - if is_full () then flush (); - Bytes.set buf !buf_off c; - incr buf_off - - method output bs i len : unit = - let i = ref i in - let len = ref len in - - while !len > 0 do - (* make space *) - if is_full () then flush (); - - let n = min !len (Bytes.length buf - !buf_off) in - Bytes.blit bs !i buf !buf_off n; - buf_off := !buf_off + n; - i := !i + n; - len := !len - n - done; - (* if full, write eagerly *) - if is_full () then flush () - - method close () = - if close_noerr then ( - try - flush (); - Unix.close fd - with _ -> () - ) else ( - flush (); - Unix.close fd - ) - - method flush = flush - end - -let of_buffer (buf : Buffer.t) : t = - object - method close () = () - method flush () = () - method output_char c = Buffer.add_char buf c - method output bs i len = Buffer.add_subbytes buf bs i len - end - -(** Output the buffer slice into this channel *) -let[@inline] output_char (self : #t) c : unit = self#output_char c - -(** Output the buffer slice into this channel *) -let[@inline] output (self : #t) buf i len : unit = self#output buf i len - -let[@inline] output_string (self : #t) (str : string) : unit = - self#output (Bytes.unsafe_of_string str) 0 (String.length str) - -let output_line (self : #t) (str : string) : unit = - output_string self str; - output_char self '\n' - -(** Close the channel. *) -let[@inline] close self : unit = self#close () - -(** Flush (ie. force write) any buffered bytes. *) -let[@inline] flush self : unit = self#flush () - -let output_int self i = - let s = string_of_int i in - output_string self s - -let output_lines self seq = Seq.iter (output_line self) seq - -let tee (l : t list) : t = - match l with - | [] -> dummy - | [ oc ] -> oc - | _ -> - let output bs i len = List.iter (fun oc -> output oc bs i len) l in - let output_char c = List.iter (fun oc -> output_char oc c) l in - let close () = List.iter close l in - let flush () = List.iter flush l in - create ~flush ~close ~output ~output_char () diff --git a/src/unix/dune b/src/unix/dune index c3eaa7cd..f47487e3 100644 --- a/src/unix/dune +++ b/src/unix/dune @@ -4,7 +4,7 @@ (public_name moonpool.unix) (optional) (synopsis "Simple Unix-based event loop for moonpool") - (private_modules common_) + (private_modules common_ heap_) (libraries moonpool moonpool.fib unix (select time.ml from (mtime mtime.os.clock -> time.mtime.ml) diff --git a/src/unix/heap.ml b/src/unix/heap_.ml similarity index 100% rename from src/unix/heap.ml rename to src/unix/heap_.ml diff --git a/src/unix/heap.mli b/src/unix/heap_.mli similarity index 100% rename from src/unix/heap.mli rename to src/unix/heap_.mli diff --git a/src/unix/timer.ml b/src/unix/timer.ml index 87eb9fd9..342dfc8a 100644 --- a/src/unix/timer.ml +++ b/src/unix/timer.ml @@ -13,7 +13,7 @@ type task = { kind: kind; } -module Task_heap = Heap.Make (struct +module Task_heap = Heap_.Make (struct type t = task let[@inline] leq t1 t2 = t1.deadline <= t2.deadline