1Weak(3) OCaml library Weak(3)
2
3
4
6 Weak - Arrays of weak pointers and hash tables of weak pointers.
7
9 Module Weak
10
12 Module Weak
13 : sig end
14
15
16 Arrays of weak pointers and hash tables of weak pointers.
17
18
19
20
21
22
23
24
25 === Low-level functions ===
26
27
28 type 'a t
29
30
31 The type of arrays of weak pointers (weak arrays). A weak pointer is a
32 value that the garbage collector may erase whenever the value is not
33 used any more (through normal pointers) by the program. Note that
34 finalisation functions are run after the weak pointers are erased.
35
36 A weak pointer is said to be full if it points to a value, empty if the
37 value was erased by the GC.
38
39 Notes:
40
41 -Integers are not allocated and cannot be stored in weak arrays.
42
43 -Weak arrays cannot be marshaled using Pervasives.output_value nor the
44 functions of the Marshal module.
45
46
47
48
49
50 val create : int -> 'a t
51
52
53 Weak.create n returns a new weak array of length n . All the pointers
54 are initially empty. Raise Invalid_argument if n is negative or
55 greater than Sys.max_array_length -1 .
56
57
58
59
60 val length : 'a t -> int
61
62
63 Weak.length ar returns the length (number of elements) of ar .
64
65
66
67
68 val set : 'a t -> int -> 'a option -> unit
69
70
71 Weak.set ar n (Some el) sets the n th cell of ar to be a (full) pointer
72 to el ; Weak.set ar n None sets the n th cell of ar to empty. Raise
73 Invalid_argument Weak.set if n is not in the range 0 to Weak.length a -
74 1 .
75
76
77
78
79 val get : 'a t -> int -> 'a option
80
81
82 Weak.get ar n returns None if the n th cell of ar is empty, Some x
83 (where x is the value) if it is full. Raise Invalid_argument Weak.get
84 if n is not in the range 0 to Weak.length a - 1 .
85
86
87
88
89 val get_copy : 'a t -> int -> 'a option
90
91
92 Weak.get_copy ar n returns None if the n th cell of ar is empty, Some x
93 (where x is a (shallow) copy of the value) if it is full. In addition
94 to pitfalls with mutable values, the interesting difference with get is
95 that get_copy does not prevent the incremental GC from erasing the
96 value in its current cycle ( get may delay the erasure to the next GC
97 cycle). Raise Invalid_argument Weak.get if n is not in the range 0 to
98 Weak.length a - 1 .
99
100
101
102
103 val check : 'a t -> int -> bool
104
105
106 Weak.check ar n returns true if the n th cell of ar is full, false if
107 it is empty. Note that even if Weak.check ar n returns true , a subse‐
108 quent Weak.get ar n can return None .
109
110
111
112
113 val fill : 'a t -> int -> int -> 'a option -> unit
114
115
116 Weak.fill ar ofs len el sets to el all pointers of ar from ofs to ofs +
117 len - 1 . Raise Invalid_argument Weak.fill if ofs and len do not des‐
118 ignate a valid subarray of a .
119
120
121
122
123 val blit : 'a t -> int -> 'a t -> int -> int -> unit
124
125
126 Weak.blit ar1 off1 ar2 off2 len copies len weak pointers from ar1
127 (starting at off1 ) to ar2 (starting at off2 ). It works correctly
128 even if ar1 and ar2 are the same. Raise Invalid_argument Weak.blit if
129 off1 and len do not designate a valid subarray of ar1 , or if off2 and
130 len do not designate a valid subarray of ar2 .
131
132
133
134
135
136 === Weak hash tables ===
137
138
139 === A weak hash table is a hashed set of values. Each value may magi‐
140 cally disappear from the set when it is not used by the rest of the
141 program any more. This is normally used to share data structures with‐
142 out inducing memory leaks. Weak hash tables are defined on values from
143 a Hashtbl.HashedType module; the equal relation and hash function are
144 taken from that module. We will say that v is an instance of x if equal
145 x v is true. The equal relation must be able to work on a shallow copy
146 of the values and give the same result as with the values themselves.
147 ===
148
149
150 module type S = sig end
151
152
153 The output signature of the functor Weak.Make .
154
155
156
157 module Make : functor (H : Hashtbl.HashedType) -> sig end
158
159
160 Functor building an implementation of the weak hash table structure.
161
162
163
164
165
166
167OCamldoc 2017-03-22 Weak(3)