Module Weak
: sigend
Arrays of weak pointers and hash sets of weak pointers.
Low-levelfunctionstype!'at
The type of arrays of weak pointers (weak arrays). A weak pointer is a value that the garbage collector
may erase whenever the value is not used any more (through normal pointers) by the program. Note that
finalisation functions are run before the weak pointers are erased, because the finalisation functions
can make values alive again (before 4.03 the finalisation functions were run after).
A weak pointer is said to be full if it points to a value, empty if the value was erased by the GC.
Notes:
-Integers are not allocated and cannot be stored in weak arrays.
-Weak arrays cannot be marshaled using output_value nor the functions of the Marshal module.
valcreate : int->'atWeak.createn returns a new weak array of length n . All the pointers are initially empty.
RaisesInvalid_argument if n is not comprised between zero and Obj.Ephemeron.max_ephe_length (limits
included).
vallength : 'at->intWeak.lengthar returns the length (number of elements) of ar .
valset : 'at->int->'aoption->unitWeak.setarn(Someel) sets the n th cell of ar to be a (full) pointer to el ; Weak.setarnNone sets
the n th cell of ar to empty.
RaisesInvalid_argument if n is not in the range 0 to Weak.lengthar-1 .
valget : 'at->int->'aoptionWeak.getarn returns None if the n th cell of ar is empty, Somex (where x is the value) if it is full.
RaisesInvalid_argument if n is not in the range 0 to Weak.lengthar-1 .
valget_copy : 'at->int->'aoptionWeak.get_copyarn returns None if the n th cell of ar is empty, Somex (where x is a (shallow) copy of
the value) if it is full. In addition to pitfalls with mutable values, the interesting difference with
get is that get_copy does not prevent the incremental GC from erasing the value in its current cycle (
get may delay the erasure to the next GC cycle).
RaisesInvalid_argument if n is not in the range 0 to Weak.lengthar-1 .
If the element is a custom block it is not copied.
valcheck : 'at->int->boolWeak.checkarn returns true if the n th cell of ar is full, false if it is empty. Note that even if
Weak.checkarn returns true , a subsequent Weak.getarn can return None .
RaisesInvalid_argument if n is not in the range 0 to Weak.lengthar-1 .
valfill : 'at->int->int->'aoption->unitWeak.fillarofslenel sets to el all pointers of ar from ofs to ofs+len-1 .
RaisesInvalid_argument if ofs and len do not designate a valid subarray of ar .
valblit : 'at->int->'at->int->int->unitWeak.blitar1off1ar2off2len copies len weak pointers from ar1 (starting at off1 ) to ar2 (starting at
off2 ). It works correctly even if ar1 and ar2 are the same.
RaisesInvalid_argument if off1 and len do not designate a valid subarray of ar1 , or if off2 and len do
not designate a valid subarray of ar2 .
Weakhashsets
A weak hash set is a hashed set of values. Each value may magically disappear from the set when it is
not used by the rest of the program any more. This is normally used to share data structures without
inducing memory leaks. Weak hash sets are defined on values from a Hashtbl.HashedType module; the equal
relation and hash function are taken from that module. We will say that v is an instance of x if equalxv is true .
The equal relation must be able to work on a shallow copy of the values and give the same result as with
the values themselves.
Unsynchronized accesses
Unsynchronized accesses to weak hash sets are a programming error. Unsynchronized accesses to a weak
hash set may lead to an invalid weak hash set state. Thus, concurrent accesses to weak hash sets must be
synchronized (for instance with a Mutex.t ).
moduletypeS=sigend
The output signature of the functor Weak.Make .
moduleMake:(H:Hashtbl.HashedType)->sigend
Functor building an implementation of the weak hash set structure. H.equal can't be the physical
equality, since only shallow copies of the elements in the set are given to it.
OCamldoc 2025-06-12 Weak(3o)