package xenstore

  1. Overview
  2. Docs

Source file symbol.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
(*
 * Copyright (C) Citrix Systems Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; version 2.1 only. with the special
 * exception on linking described in file LICENSE.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *)

type t = int
type 'a record = { data : 'a; mutable garbage : bool }

let int_string_tbl : (int, string record) Hashtbl.t = Hashtbl.create 1024
let string_int_tbl : (string, int) Hashtbl.t = Hashtbl.create 1024
let created_counter = ref 0
let used_counter = ref 0
let count = ref 0

let rec fresh () =
  if Hashtbl.mem int_string_tbl !count then (
    incr count;
    fresh ())
  else !count

let new_record v = { data = v; garbage = false }

let of_string name =
  if Hashtbl.mem string_int_tbl name then (
    incr used_counter;
    Hashtbl.find string_int_tbl name)
  else
    let i = fresh () in
    incr created_counter;
    Hashtbl.add string_int_tbl name i;
    Hashtbl.add int_string_tbl i (new_record name);
    i

let to_string i = (Hashtbl.find int_string_tbl i).data

let mark_all_as_unused () =
  Hashtbl.iter (fun _ v -> v.garbage <- true) int_string_tbl

let mark_as_used symb =
  let record1 = Hashtbl.find int_string_tbl symb in
  record1.garbage <- false

let garbage () =
  let records =
    Hashtbl.fold
      (fun symb record accu ->
        if record.garbage then (symb, record.data) :: accu else accu)
      int_string_tbl []
  in
  let remove (int, string) =
    Hashtbl.remove int_string_tbl int;
    Hashtbl.remove string_int_tbl string
  in
  created_counter := 0;
  used_counter := 0;
  List.iter remove records

let stats () = Hashtbl.length string_int_tbl
let created () = !created_counter
let used () = !used_counter
OCaml

Innovation. Community. Security.