package grpc

  1. Overview
  2. Docs

Source file buffer.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
type t = { mutable contents : bytes; mutable length : int }

let v ?(capacity = 1024) () = { contents = Bytes.create capacity; length = 0 }
let length t = t.length
let capacity t = Bytes.length t.contents
let extend t amount = t.contents <- Bytes.extend t.contents 0 amount

let rec nearest_power_of_2 acc target =
  if acc >= target then acc else nearest_power_of_2 (acc * 2) target

let ensure_size t ~extra =
  let current_capacity = capacity t in
  let needed_capacity = t.length + extra in
  if needed_capacity >= current_capacity then
    extend t
      (nearest_power_of_2 current_capacity needed_capacity - current_capacity)

let copy_from_bigstringaf ~src_off ~src ~dst ~length =
  ensure_size dst ~extra:length;
  Bigstringaf.blit_to_bytes ~src_off src ~dst_off:dst.length dst.contents
    ~len:length;
  dst.length <- dst.length + length

let sub ~start ~length t =
  let contents = Bytes.sub t.contents start length in
  { contents; length }

let to_bytes t = Bytes.sub t.contents 0 t.length
let to_string t = to_bytes t |> Bytes.to_string

let shift_left ~by t =
  Bytes.blit t.contents by t.contents 0 (t.length - by);
  t.length <- t.length - by

let get_u8 ~pos t = Bytes.get_uint8 t.contents pos

let get_u32_be ~pos t =
  let high = Bytes.get_uint16_be t.contents pos in
  let low = Bytes.get_uint16_be t.contents (pos + 2) in
  (high lsl 16) lor low