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