blob: a3ffc6d1d7a8fee4ee90a3ab7c1561b08fa5ee1e (
plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
(***********************************************************************)
(* v * The Coq Proof Assistant / The Coq Development Team *)
(* <O___,, * INRIA-Rocquencourt & LRI-CNRS-Orsay *)
(* \VV/ *************************************************************)
(* // * This file is distributed under the terms of the *)
(* * GNU Lesser General Public License Version 2.1 *)
(***********************************************************************)
(** This module implements an "untyped store", in this particular case
we see it as an extensible record whose fields are left
unspecified. ***)
(** We use a dynamic "name" allocator. But if we needed to serialise
stores, we might want something static to avoid troubles with
plugins order. *)
module type T =
sig
end
module type S =
sig
type t
type 'a field
val empty : t
val set : t -> 'a field -> 'a -> t
val get : t -> 'a field -> 'a option
val remove : t -> 'a field -> t
val merge : t -> t -> t
val field : unit -> 'a field
end
module Make (M : T) : S =
struct
let next =
let count = ref 0 in fun () ->
let n = !count in
incr count;
n
type t = Obj.t option array
(** Objects are accessed through an array access. *)
type 'a field = int
let max_length = 128
(** Ought to be enough for anybody. It has to be smaller that [Max_young_wosize]
in order to fit into the minor heap, the latter being defined to 256 in
OCaml. *)
let uset (obj : t) (i : 'a field) (v : 'a option) =
(** We cast to integers in order not to use the costly [caml_modify]. This
assumes that [obj] lives in the minor heap. *)
let v : int = Obj.magic v in
let obj : int array = Obj.magic obj in
Array.unsafe_set obj i v
let allocate len : t =
(** Returns an array filled with [None]. *)
Obj.magic (Obj.new_block 0 len)
let empty : t = [||]
let set (s : t) (i : 'a field) (v : 'a) : t =
let v = Some v in
let len = Array.length s in
let nlen = if i < len then len else succ i in
let () = assert (0 < nlen && nlen <= max_length) in
let ans = allocate nlen in
(** Important: No more allocation from here. *)
for i = 0 to pred len do
uset ans i (Array.unsafe_get s i)
done;
uset ans i v;
ans
let get (s : t) (i : 'a field) : 'a option =
let len = Array.length s in
if len <= i then None
else Obj.magic (Array.unsafe_get s i)
let remove (s : t) (i : 'a field) =
let len = Array.length s in
let () = assert (0 <= i) in
let ans = allocate len in
(** Important: No more allocation from here. *)
for i = 0 to pred len do
uset ans i (Array.unsafe_get s i)
done;
if i < len then uset ans i None;
ans
let merge (s1 : t) (s2 : t) : t =
let len1 = Array.length s1 in
let len2 = Array.length s2 in
let nlen = if len1 < len2 then len2 else len1 in
let ans = allocate nlen in
(** Important: No more allocation from here. *)
for i = 0 to pred len2 do
uset ans i (Array.unsafe_get s2 i)
done;
for i = 0 to pred len1 do
let v = Array.unsafe_get s1 i in
match v with
| None -> ()
| Some _ -> uset ans i v
done;
ans
let field () = next ()
end
|