ocaml-trace/src/fuchsia/buf.ml
Simon Cruanes 190f70d7c9
feat fuchsia: full revamp of the library, modularized
- separate exporter, writer, subscriber
- use the subscriber span tbl to keep track of context
- use a `Buf_chain.t` to keep multiple buffers in use,
  and keep a set of ready buffers
- batch write the ready buffers and then recycle them
2025-05-07 15:33:34 -04:00

43 lines
1.2 KiB
OCaml

open Util
type t = {
buf: bytes;
mutable offset: int;
}
let empty : t = { buf = Bytes.empty; offset = 0 }
let create (n : int) : t =
(* multiple of 8-bytes size *)
let buf = Bytes.create (round_to_word n) in
{ buf; offset = 0 }
let[@inline] clear self = self.offset <- 0
let[@inline] available self = Bytes.length self.buf - self.offset
let[@inline] size self = self.offset
let[@inline] is_empty self = self.offset = 0
(* see below: we assume little endian *)
let () = assert (not Sys.big_endian)
let[@inline] add_i64 (self : t) (i : int64) : unit =
(* NOTE: we use LE, most systems are this way, even though fuchsia
says we should use the system's native endianess *)
Bytes.set_int64_le self.buf self.offset i;
self.offset <- self.offset + 8
let[@inline] add_string (self : t) (s : string) : unit =
let len = String.length s in
let missing = missing_to_round len in
(* bound check *)
Bytes.blit_string s 0 self.buf self.offset len;
self.offset <- self.offset + len;
(* add 0-padding *)
if missing != 0 then (
Bytes.unsafe_fill self.buf self.offset missing '\x00';
self.offset <- self.offset + missing
)
let to_string (self : t) : string = Bytes.sub_string self.buf 0 self.offset