package biniou

  1. Overview
  2. Docs
Binary data format designed for speed, safety, ease of use and backward compatibility as protocols evolve

Install

dune-project
 Dependency

Authors

Maintainers

Sources

biniou-1.2.2.tbz
sha256=8bf3ff17cd0ecb2d6b6d1d94cb08ef089d44caef96e9bae6be6839d428fa318f
sha512=7d03b3759a3a2e1c77713aa1b8375a1f1917f49d14fe5e3cb01d5e53a12e6385b7a3b0f4827f3be71182c31c416d780e1f9ef011dc205cb8f9b0ab2d8fc23cfd

doc/src/biniou/bi_share.ml.html

Source file bi_share.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
type type_id = int

let dummy_type_id = 0

let create_type_id =
  let n = ref dummy_type_id in
  fun () ->
    incr n;
    if !n < 0 then
      failwith "Bi_share.Rd_poly.create_type_id: \
                exhausted available type_id's"
    else
      !n

module Wr =
struct
  module H = Hashtbl.Make (
    struct
      type t = Obj.t * type_id
      let equal (x1, t1) (x2, t2) = x1 == x2 && t1 == t2
      let hash = Hashtbl.hash
    end
  )

  type tbl = int H.t

  let create = H.create
  let clear tbl =
    if H.length tbl > 0 then
      H.clear tbl

  let put tbl k pos =
    try
      let pos0 = H.find tbl (Obj.magic k) in
      pos - pos0
    with Not_found ->
      H.add tbl (Obj.magic k) pos;
      0
end

module Rd =
struct
  type tbl = ((int * type_id), Obj.t) Hashtbl.t

  let create n = Hashtbl.create n
  let clear = Hashtbl.clear

  let put tbl pos x =
    Hashtbl.add tbl pos x

  let get tbl pos =
    try Hashtbl.find tbl pos
    with Not_found ->
      Bi_util.error "Corrupted data (invalid reference)"
end